[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

brconfig parsing question/issues



The brconfig "rulefile" command skips the last line in a file if it
doesn't contain a newline.  Is it supposed to be that way?

The line number is also initialized to 1 then incremented at the
beginning of each loop iteration, which will make it refer to the line
after the one it is actually parsing.

And there is some off-by-one problem in the parsing that is overwriting
`xbuf' (on my system), sending garbage output on error.  As far as
error reporting goes, a line read from the file is being hacked on by
strtok(), so the offending line can only be printed out if it is
duplicated.

This patch appears to remedy these situations.  I changed the error
reporting to print the first word on the line, as I figured that
would be more helpful for the user in locating the line to fix as
opposed to the nineth word...

Suggestions?

Index: brconfig.c
===================================================================
RCS file: /cvs/src/sbin/brconfig/brconfig.c,v
retrieving revision 1.30
diff -u -r1.30 brconfig.c
--- brconfig.c	8 Mar 2004 17:23:33 -0000	1.30
+++ brconfig.c	30 Apr 2004 18:19:20 -0000
@@ -1299,7 +1303,7 @@
 {
 	FILE *f;
 	char *str, *argv[MAXRULEWORDS], buf[1024], xbuf[1024];
-	int ln = 1, argc = 0, err = 0, xerr;
+	int ln = 0, argc = 0, err = 0, xerr;
 
 	f = fopen(fname, "r");
 	if (f == NULL) {
@@ -1307,10 +1311,7 @@
 		return (EX_IOERR);
 	}
 
-	while (1) {
-		fgets(buf, sizeof(buf), f);
-		if (feof(f))
-			break;
+	while (fgets(buf, sizeof(buf), f) != NULL) {
 		ln++;
 		if (buf[0] == '#' || buf[0] == '\n')
 			continue;
@@ -1319,17 +1320,18 @@
 		str = strtok(buf, "\n\t\r ");
 		strlcpy(xbuf, buf, sizeof(xbuf));
 		while (str != NULL) {
-			argv[argc++] = str;
-			if (argc > MAXRULEWORDS) {
-				fprintf(stderr, "invalid rule: %d: %s\n",
-				    ln, xbuf);
+			if (argc >= MAXRULEWORDS)
 				break;
-			}
+			argv[argc++] = str;
 			str = strtok(NULL, "\n\t\r ");
 		}
 
-		if (argc > MAXRULEWORDS)
+		/* Rule is too long if there's more.  */
+		if (argc >= MAXRULEWORDS && str != NULL) {
+			fprintf(stderr, "invalid rule: %d: %s ...\n",
+				ln, buf);
 			continue;
+		}
 
 		xerr = bridge_rule(s, brdg, argc, argv, ln);
 		if (xerr)



Visit your host, monkey.org