Source-Changes-HG archive

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

[src/trunk]: src/bin/sh Extract the variable name validity test from setname(...



details:   https://anonhg.NetBSD.org/src/rev/378abd9bbe2f
branches:  trunk
changeset: 827426:378abd9bbe2f
user:      kre <kre%NetBSD.org@localhost>
date:      Sat Oct 28 03:59:11 2017 +0000

description:
Extract the variable name validity test from setname() into a
function of its own.  It will soon be needed from another source.

diffstat:

 bin/sh/var.c |  57 ++++++++++++++++++++++++++++++++++++++++-----------------
 bin/sh/var.h |   3 ++-
 2 files changed, 42 insertions(+), 18 deletions(-)

diffs (103 lines):

diff -r a2806f5ef970 -r 378abd9bbe2f bin/sh/var.c
--- a/bin/sh/var.c      Sat Oct 28 03:47:24 2017 +0000
+++ b/bin/sh/var.c      Sat Oct 28 03:59:11 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.67 2017/08/31 05:09:38 kre Exp $     */
+/*     $NetBSD: var.c,v 1.68 2017/10/28 03:59:11 kre Exp $     */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)var.c      8.3 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: var.c,v 1.67 2017/08/31 05:09:38 kre Exp $");
+__RCSID("$NetBSD: var.c,v 1.68 2017/10/28 03:59:11 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -331,6 +331,42 @@
 }
 
 /*
+ * Validate a string as a valid variable name
+ * nb: not parameter - special params and such are "invalid" here.
+ * Name terminated by either \0 or the term param (usually '=' or '\0').
+ *
+ * If not NULL, the length of the (intended) name is returned via len
+ */
+
+int
+validname(const char *name, int term, int *len)
+{
+       const char *p = name;
+       int ok = 1;
+
+       if (p == NULL || *p == '\0' || *p == term) {
+               if (len != NULL)
+                       *len = 0;
+               return 0;
+       }
+
+       if (!is_name(*p))
+               ok = 0;
+       p++;
+       for (;;) {
+               if (*p == '\0' || *p == term)
+                       break;
+               if (!is_in_name(*p))
+                       ok = 0;
+               p++;
+       }
+       if (len != NULL)
+               *len = p - name;
+
+       return ok;
+}
+
+/*
  * Safe version of setvar, returns 1 on success 0 on failure.
  */
 
@@ -370,23 +406,10 @@
        int len;
        int namelen;
        char *nameeq;
-       int isbad;
 
-       isbad = 0;
        p = name;
-       if (! is_name(*p))
-               isbad = 1;
-       p++;
-       for (;;) {
-               if (! is_in_name(*p)) {
-                       if (*p == '\0' || *p == '=')
-                               break;
-                       isbad = 1;
-               }
-               p++;
-       }
-       namelen = p - name;
-       if (isbad)
+
+       if (!validname(p, '=', &namelen))
                error("%.*s: bad variable name", namelen, name);
        len = namelen + 2;              /* 2 is space for '=' and '\0' */
        if (val == NULL) {
diff -r a2806f5ef970 -r 378abd9bbe2f bin/sh/var.h
--- a/bin/sh/var.h      Sat Oct 28 03:47:24 2017 +0000
+++ b/bin/sh/var.h      Sat Oct 28 03:59:11 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.h,v 1.35 2017/06/30 23:05:45 kre Exp $     */
+/*     $NetBSD: var.h,v 1.36 2017/10/28 03:59:11 kre Exp $     */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -143,5 +143,6 @@
 void choose_ps1(void);
 int setvarsafe(const char *, const char *, int);
 void print_quoted(const char *);
+int validname(const char *, int, int *);
 
 #endif



Home | Main Index | Thread Index | Old Index