summaryrefslogtreecommitdiff
path: root/sbin
diff options
context:
space:
mode:
authorKenneth R Westerback <krw@cvs.openbsd.org>2018-07-22 21:32:05 +0000
committerKenneth R Westerback <krw@cvs.openbsd.org>2018-07-22 21:32:05 +0000
commit9ca8cb0a669109357766c425d569d5f96d7f7030 (patch)
tree82f095bd8aa3dfd0f5efb50908da6279a0329396 /sbin
parent29a92d40320509cd7da34e999e0273219c7c58fb (diff)
No need to re-implement realloc(). Just use it
the way the man page says. ok tb@
Diffstat (limited to 'sbin')
-rw-r--r--sbin/dhclient/options.c46
1 files changed, 15 insertions, 31 deletions
diff --git a/sbin/dhclient/options.c b/sbin/dhclient/options.c
index 25d4e6e96a1..197685fe909 100644
--- a/sbin/dhclient/options.c
+++ b/sbin/dhclient/options.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: options.c,v 1.112 2018/07/22 08:59:56 krw Exp $ */
+/* $OpenBSD: options.c,v 1.113 2018/07/22 21:32:04 krw Exp $ */
/* DHCP options parsing and reassembly. */
@@ -406,7 +406,7 @@ parse_option_buffer(struct option_data *options, unsigned char *buffer,
{
unsigned char *s, *t, *end;
char *name, *fmt;
- int len, code;
+ int code, len, newlen;
s = buffer;
end = s + length;
@@ -458,36 +458,20 @@ parse_option_buffer(struct option_data *options, unsigned char *buffer,
}
/*
- * If we haven't seen this option before, just make
- * space for it and copy it there.
+ * Concatenate new data + NUL to existing option data.
+ *
+ * Note that the NUL is *not* counted in the len field!
*/
- if (options[code].data == NULL) {
- t = calloc(1, len + 1);
- if (t == NULL)
- fatal("option %s", name);
- /*
- * Copy and NUL-terminate the option (in case
- * it's an ASCII string).
- */
- memcpy(t, &s[2], len);
- t[len] = 0;
- options[code].len = len;
- options[code].data = t;
- } else {
- /*
- * If it's a repeat, concatenate it to whatever
- * we last saw.
- */
- t = calloc(1, len + options[code].len + 1);
- if (t == NULL)
- fatal("option %s concat", name);
- memcpy(t, options[code].data, options[code].len);
- memcpy(t + options[code].len, &s[2], len);
- options[code].len += len;
- t[options[code].len] = 0;
- free(options[code].data);
- options[code].data = t;
- }
+ newlen = options[code].len + len;
+ if ((t = realloc(options[code].data, newlen + 1)) == NULL)
+ fatal("option %s", name);
+
+ memcpy(t + options[code].len, &s[2], len);
+ t[newlen] = 0;
+
+ options[code].len = newlen;
+ options[code].data = t;
+
s += s[1] + 2;
}