- uint32_t n_padding = ntohl(config->n_padding);
-
- if (n_padding <= (DNS_CONFIG_BUF_MAX - dataLen)) {
- size_t len;
-
- len = dataLen + n_padding;
- buf = malloc(len);
- bcopy((void *)dataRef, buf, dataLen);
- bzero(&buf[dataLen], n_padding);
- }
+ size_t configLen;
+ uint32_t n_attribute = ntohl(config->n_attribute);
+ uint32_t n_padding = ntohl(config->n_padding);
+
+ /*
+ * Check that the size of the configuration header plus the size of the
+ * attribute data matches the size of the configuration buffer.
+ *
+ * If the sizes are different, something that should NEVER happen, CRASH!
+ */
+ configLen = sizeof(_dns_config_buf_t) + n_attribute;
+ assert(configLen == dataLen);
+
+ /*
+ * Check that the size of the requested padding would not result in our
+ * allocating a configuration + padding buffer larger than our maximum size.
+ *
+ * If the requested padding size is too large, something that should NEVER
+ * happen, CRASH!
+ */
+ assert(n_padding <= (DNS_CONFIG_BUF_MAX - dataLen));
+
+ /*
+ * Check that the actual size of the configuration data and any requested
+ * padding will be less than the maximum possible size of the in-memory
+ * configuration buffer.
+ *
+ * If the length needed is too large, something that should NEVER happen, CRASH!
+ */
+ bufLen = dataLen + n_padding;
+ assert(bufLen <= DNS_CONFIG_BUF_MAX);
+
+ // allocate a buffer large enough to hold both the configuration
+ // data and the padding.
+ buf = malloc(bufLen);
+ bcopy((void *)dataRef, buf, dataLen);
+ bzero(&buf[dataLen], n_padding);