Module Name:    src
Committed By:   dholland
Date:           Thu May 27 08:30:35 UTC 2010

Modified Files:
        src/usr.bin/seq: seq.1 seq.c

Log Message:
Fix valid_format() to be more careful about allowing only valid printf
formats.

Also, accept %a and %A, which are new since this logic was last updated,
and also allow %F even though it's not functionally different from %f.
Document these additions and bump date of man page.

Fixes PR 43355.


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/usr.bin/seq/seq.1
cvs rdiff -u -r1.5 -r1.6 src/usr.bin/seq/seq.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/seq/seq.1
diff -u src/usr.bin/seq/seq.1:1.6 src/usr.bin/seq/seq.1:1.7
--- src/usr.bin/seq/seq.1:1.6	Wed Nov 26 15:03:47 2008
+++ src/usr.bin/seq/seq.1	Thu May 27 08:30:35 2010
@@ -1,4 +1,4 @@
-.\"	$NetBSD: seq.1,v 1.6 2008/11/26 15:03:47 ginsbach Exp $
+.\"	$NetBSD: seq.1,v 1.7 2010/05/27 08:30:35 dholland Exp $
 .\"
 .\" Copyright (c) 2005 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -28,7 +28,7 @@
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
 .\"
-.Dd January 17, 2005
+.Dd May 27, 2010
 .Dt SEQ 1
 .Os
 .Sh NAME
@@ -78,8 +78,11 @@
 .Ar format
 to print each number.
 Only the
+.Cm A ,
+.Cm a ,
 .Cm E ,
 .Cm e ,
+.Cm F ,
 .Cm f ,
 .Cm G ,
 .Cm g ,

Index: src/usr.bin/seq/seq.c
diff -u src/usr.bin/seq/seq.c:1.5 src/usr.bin/seq/seq.c:1.6
--- src/usr.bin/seq/seq.c:1.5	Mon Jul 21 14:19:26 2008
+++ src/usr.bin/seq/seq.c	Thu May 27 08:30:35 2010
@@ -31,7 +31,7 @@
 #ifndef lint
 __COPYRIGHT("@(#) Copyright (c) 2005\
  The NetBSD Foundation, Inc.  All rights reserved.");
-__RCSID("$NetBSD: seq.c,v 1.5 2008/07/21 14:19:26 lukem Exp $");
+__RCSID("$NetBSD: seq.c,v 1.6 2010/05/27 08:30:35 dholland Exp $");
 #endif /* not lint */
 
 #include <ctype.h>
@@ -228,39 +228,56 @@
 int
 valid_format(const char *fmt)
 {
-	int conversions = 0;
+	unsigned conversions = 0;
 
 	while (*fmt != '\0') {
 		/* scan for conversions */
-		if (*fmt != '\0' && *fmt != '%') {
-			do {
-				fmt++;
-			} while (*fmt != '\0' && *fmt != '%');
+		if (*fmt != '%') {
+			fmt++;
+			continue;
 		}
-		/* scan a conversion */
-		if (*fmt != '\0') {
-			do {
-				fmt++;
+		fmt++;
 
-				/* ok %% */
-				if (*fmt == '%') {
-					fmt++;
-					break;
-				}
-				/* valid conversions */
-				if (strchr("eEfgG", *fmt) &&
-				    conversions++ < 1) {
-					fmt++;
-					break;
-				}
-				/* flags, width and precsision */
-				if (isdigit((unsigned char)*fmt) ||
-				    strchr("+- 0#.", *fmt))
-					continue;
+		/* allow %% but not things like %10% */
+		if (*fmt == '%') {
+			fmt++;
+			continue;
+		}
 
-				/* oops! bad conversion format! */
-				return (0);
-			} while (*fmt != '\0');
+		/* flags */
+		while (*fmt != '\0' && strchr("#0- +'", *fmt)) {
+			fmt++;
+		}
+
+		/* field width */
+		while (*fmt != '\0' && strchr("0123456789", *fmt)) {
+			fmt++;
+		}
+
+		/* precision */
+		if (*fmt == '.') {
+			fmt++;
+			while (*fmt != '\0' && strchr("0123456789", *fmt)) {
+				fmt++;
+			}
+		}
+
+		/* conversion */
+		switch (*fmt) {
+		    case 'A':
+		    case 'a':
+		    case 'E':
+		    case 'e':
+		    case 'F':
+		    case 'f':
+		    case 'G':
+		    case 'g':
+			/* floating point formats are accepted */
+			conversions++;
+			break;
+		    default:
+			/* anything else is not */
+			return 0;
 		}
 	}
 

Reply via email to