]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
options: Define parse_nstring and use it
authorRoy Marples <roy@marples.name>
Wed, 19 Feb 2020 11:15:49 +0000 (11:15 +0000)
committerRoy Marples <roy@marples.name>
Wed, 19 Feb 2020 11:15:49 +0000 (11:15 +0000)
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

index 4fc706109fbd20a0e0295d80aab3a433d4526cf0..b30264912af1c608fdf0c149e8db6063cb50ac63 100644 (file)
@@ -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;