changeset 4247:22a5ac71f5c8 draft

dhcp6: fix a potential string termination error on status messages
author Roy Marples <roy@marples.name>
date Wed, 28 Mar 2018 00:22:01 +0100
parents 687273d5849d
children 2844dbb214b5
files src/dhcp6.c
diffstat 1 files changed, 6 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/dhcp6.c	Tue Mar 27 23:06:30 2018 +0100
+++ b/src/dhcp6.c	Wed Mar 28 00:22:01 2018 +0100
@@ -1847,6 +1847,7 @@
 {
 	uint8_t *opt;
 	uint16_t opt_len, code;
+	size_t mlen;
 	void * (*f)(void *, size_t, uint16_t, uint16_t *), *farg;
 	char buf[32], *sbuf;
 	const char *status;
@@ -1872,8 +1873,8 @@
 
 	/* Anything after the code is a message. */
 	opt += sizeof(code);
-	opt_len = (uint16_t)(opt_len - sizeof(code));
-	if (opt_len == 0) {
+	mlen = opt_len - sizeof(code);
+	if (mlen == 0) {
 		sbuf = NULL;
 		if (code < sizeof(dhcp6_statuses) / sizeof(char *))
 			status = dhcp6_statuses[code];
@@ -1882,12 +1883,12 @@
 			status = buf;
 		}
 	} else {
-		if ((sbuf = malloc((size_t)opt_len + 1)) == NULL) {
+		if ((sbuf = malloc(mlen + 1)) == NULL) {
 			logerr(__func__);
 			return -1;
 		}
-		memcpy(sbuf, opt, opt_len);
-		sbuf[len] = '\0';
+		memcpy(sbuf, opt, mlen);
+		sbuf[mlen] = '\0';
 		status = sbuf;
 	}