From 2478cc98bc46747b4f341949a08cc39431309b15 Mon Sep 17 00:00:00 2001 From: khorben Date: Wed, 13 May 2020 21:44:30 +0000 Subject: [PATCH] Fix and improve parsing of configuration files XXX pull-up to netbsd-9 --- sbin/umbctl/umbctl.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/sbin/umbctl/umbctl.c b/sbin/umbctl/umbctl.c index f99fbc34f043..b749e6efee70 100644 --- a/sbin/umbctl/umbctl.c +++ b/sbin/umbctl/umbctl.c @@ -1,4 +1,4 @@ -/* $NetBSD: umbctl.c,v 1.3 2020/03/22 07:45:02 khorben Exp $ */ +/* $NetBSD: umbctl.c,v 1.4 2020/05/13 21:44:30 khorben Exp $ */ /* * Copyright (c) 2018 Pierre Pronchery * @@ -179,12 +179,15 @@ static int _umbctl(char const * ifname, int verbose, int argc, char * argv[]) /* umbctl_file */ static int _umbctl_file(char const * ifname, char const * filename, int verbose) { + int ret = 0; int fd; struct ifreq ifr; struct umb_info umbi; struct umb_parameter umbp; FILE * fp; char buf[512]; + size_t len; + int i; int eof; char * tokens[3] = { buf, NULL, NULL }; char * p; @@ -197,18 +200,32 @@ static int _umbctl_file(char const * ifname, char const * filename, int verbose) if(buf[0] == '#') continue; buf[sizeof(buf) - 1] = '\0'; - if((p = strstr(buf, "=")) != NULL) + if((len = strlen(buf)) > 0) + { + if(buf[len - 1] != '\n') + { + ret = _error(2, "%s: %s", filename, + "Line too long"); + while((i = fgetc(fp)) != EOF && i != '\n'); + continue; + } + else + buf[len - 1] = '\0'; + } + if((p = strchr(buf, '=')) != NULL) { tokens[1] = p + 1; *p = '\0'; } else tokens[1] = NULL; - if(_umbctl_set(ifname, &umbp, (p != NULL) ? 2 : 1, tokens) != 0) - break; + ret |= _umbctl_set(ifname, &umbp, (p != NULL) ? 2 : 1, tokens) + ? 2 : 0; } eof = feof(fp); if(fclose(fp) != 0 || !eof) return _error(2, "%s: %s", filename, strerror(errno)); + if(ret != 0) + return ret; if((fd = _umbctl_socket()) < 0) return 2; memset(&ifr, 0, sizeof(ifr));