From e2357d5e6e8ab454003a6041e7a1c990e8e7d89d Mon Sep 17 00:00:00 2001 From: Roy Marples Date: Wed, 19 Feb 2020 11:15:49 +0000 Subject: [PATCH] options: Define parse_nstring and use it Makes the code easier to read. While here, use sizeof rather than size constants to make it more clear of the length of the property we're copying to. --- src/if-options.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/if-options.c b/src/if-options.c index 4fc70610..b3026491 100644 --- a/src/if-options.c +++ b/src/if-options.c @@ -287,6 +287,7 @@ add_environ(char ***array, const char *value, int uniq) #define PARSE_STRING_NULL 1 #define PARSE_HWADDR 2 #define parse_string(a, b, c) parse_str((a), (b), (c), PARSE_STRING) +#define parse_nstring(a, b, c) parse_str((a), (b), (c), PARSE_STRING_NULL) #define parse_hwaddr(a, b, c) parse_str((a), (b), (c), PARSE_HWADDR) static ssize_t parse_str(char *sbuf, size_t slen, const char *str, int flags) @@ -723,7 +724,7 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, ARG_REQUIRED; if (ifo->script != default_script) free(ifo->script); - s = parse_str(NULL, 0, arg, PARSE_STRING_NULL); + s = parse_nstring(NULL, 0, arg); if (s == 0) { ifo->script = NULL; break; @@ -734,7 +735,7 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, logerr(__func__); return -1; } - s = parse_str(ifo->script, dl, arg, PARSE_STRING_NULL); + s = parse_nstring(ifo->script, dl, arg); if (s == -1 || ifo->script[0] == '\0' || strcmp(ifo->script, "/dev/null") == 0) @@ -755,7 +756,7 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, ifo->options |= DHCPCD_HOSTNAME; break; } - s = parse_string(ifo->hostname, HOSTNAME_MAX_LEN, arg); + s = parse_nstring(ifo->hostname, sizeof(ifo->hostname), arg); if (s == -1) { logerr("%s: hostname", __func__); return -1; @@ -764,7 +765,6 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, logerrx("hostname cannot begin with ."); return -1; } - ifo->hostname[s] = '\0'; if (ifo->hostname[0] == '\0') ifo->options &= ~DHCPCD_HOSTNAME; else @@ -882,9 +882,9 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, } break; case 'u': - s = USERCLASS_MAX_LEN - ifo->userclass[0] - 1; + dl = sizeof(ifo->userclass) - ifo->userclass[0] - 1; s = parse_string((char *)ifo->userclass + - ifo->userclass[0] + 2, (size_t)s, arg); + ifo->userclass[0] + 2, dl, arg); if (s == -1) { logerr("userclass"); return -1; -- 2.47.3