From: Yu Watanabe Date: Sat, 1 Oct 2022 06:48:13 +0000 (+0900) Subject: sd-dhcp6-client: use GREEDY_REALLOC() X-Git-Tag: v252-rc2~68^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=60cbf2ea8228e3d2b7dd06fe6ad25dbf09339fef;p=thirdparty%2Fsystemd.git sd-dhcp6-client: use GREEDY_REALLOC() And merge 'total' and 'offset' -> 'n' --- diff --git a/src/libsystemd-network/dhcp6-option.c b/src/libsystemd-network/dhcp6-option.c index f257c56681a..f0a6b53a429 100644 --- a/src/libsystemd-network/dhcp6-option.c +++ b/src/libsystemd-network/dhcp6-option.c @@ -396,7 +396,7 @@ int dhcp6_option_append_fqdn(uint8_t **buf, size_t *buflen, const char *fqdn) { int dhcp6_option_append_user_class(uint8_t **buf, size_t *buflen, char * const *user_class) { _cleanup_free_ uint8_t *p = NULL; - size_t total = 0, offset = 0; + size_t n = 0; assert(buf); assert(*buf); @@ -407,24 +407,19 @@ int dhcp6_option_append_user_class(uint8_t **buf, size_t *buflen, char * const * STRV_FOREACH(s, user_class) { size_t len = strlen(*s); - uint8_t *q; - if (len > 0xffff || len == 0) + if (len > UINT16_MAX || len == 0) return -EINVAL; - q = realloc(p, total + len + 2); - if (!q) - return -ENOMEM; - - p = q; - unaligned_write_be16(&p[offset], len); - memcpy(&p[offset + 2], *s, len); + if (!GREEDY_REALLOC(p, n + len + 2)) + return -ENOMEM; - offset += 2 + len; - total += 2 + len; + unaligned_write_be16(p + n, len); + memcpy(p + n + 2, *s, len); + n += len + 2; } - return dhcp6_option_append(buf, buflen, SD_DHCP6_OPTION_USER_CLASS, total, p); + return dhcp6_option_append(buf, buflen, SD_DHCP6_OPTION_USER_CLASS, n, p); } int dhcp6_option_append_vendor_class(uint8_t **buf, size_t *buflen, char * const *vendor_class) {