Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/seq Fix valid_format() to be more careful about allo...



details:   https://anonhg.NetBSD.org/src/rev/9710f6f3573b
branches:  trunk
changeset: 755197:9710f6f3573b
user:      dholland <dholland%NetBSD.org@localhost>
date:      Thu May 27 08:30:35 2010 +0000

description:
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.

diffstat:

 usr.bin/seq/seq.1 |   7 +++-
 usr.bin/seq/seq.c |  73 +++++++++++++++++++++++++++++++++---------------------
 2 files changed, 50 insertions(+), 30 deletions(-)

diffs (126 lines):

diff -r e081fb1a17b8 -r 9710f6f3573b usr.bin/seq/seq.1
--- a/usr.bin/seq/seq.1 Thu May 27 07:27:35 2010 +0000
+++ b/usr.bin/seq/seq.1 Thu May 27 08:30:35 2010 +0000
@@ -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 ,
diff -r e081fb1a17b8 -r 9710f6f3573b usr.bin/seq/seq.c
--- a/usr.bin/seq/seq.c Thu May 27 07:27:35 2010 +0000
+++ b/usr.bin/seq/seq.c Thu May 27 08:30:35 2010 +0000
@@ -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;
+               }
+               fmt++;
+
+               /* allow %% but not things like %10% */
+               if (*fmt == '%') {
+                       fmt++;
+                       continue;
                }
-               /* scan a conversion */
-               if (*fmt != '\0') {
-                       do {
-                               fmt++;
+
+               /* flags */
+               while (*fmt != '\0' && strchr("#0- +'", *fmt)) {
+                       fmt++;
+               }
+
+               /* field width */
+               while (*fmt != '\0' && strchr("0123456789", *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;
+               /* precision */
+               if (*fmt == '.') {
+                       fmt++;
+                       while (*fmt != '\0' && strchr("0123456789", *fmt)) {
+                               fmt++;
+                       }
+               }
 
-                               /* oops! bad conversion format! */
-                               return (0);
-                       } while (*fmt != '\0');
+               /* 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;
                }
        }
 



Home | Main Index | Thread Index | Old Index