Module Name:    src
Committed By:   martin
Date:           Sat Aug 25 14:45:37 UTC 2018

Modified Files:
        src/bin/sh [netbsd-8]: histedit.c mystring.c options.c parser.c var.c

Log Message:
Pull up following revision(s) (requested by kre in ticket #988):

        bin/sh/parser.c: revision 1.147
        bin/sh/var.c: revision 1.70
        bin/sh/mystring.c: revision 1.18
        bin/sh/options.c: revision 1.53
        bin/sh/histedit.c: revision 1.53

Remove atoi()

Mostly use number() (no longer implemented using atoi()) when an
unsigned integer is required, but use strtoXXX() when a conversion
is wanted, without the possibility or error (like setting OPTIND
and RANDOM).   Always init OPTIND to 1 when sh starts (overriding
anything in environ.)


To generate a diff of this commit:
cvs rdiff -u -r1.48.8.1 -r1.48.8.2 src/bin/sh/histedit.c
cvs rdiff -u -r1.17 -r1.17.22.1 src/bin/sh/mystring.c
cvs rdiff -u -r1.49 -r1.49.2.1 src/bin/sh/options.c
cvs rdiff -u -r1.132.2.4 -r1.132.2.5 src/bin/sh/parser.c
cvs rdiff -u -r1.55.2.2 -r1.55.2.3 src/bin/sh/var.c

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

Modified files:

Index: src/bin/sh/histedit.c
diff -u src/bin/sh/histedit.c:1.48.8.1 src/bin/sh/histedit.c:1.48.8.2
--- src/bin/sh/histedit.c:1.48.8.1	Sun Jul 23 14:58:14 2017
+++ src/bin/sh/histedit.c	Sat Aug 25 14:45:37 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: histedit.c,v 1.48.8.1 2017/07/23 14:58:14 snj Exp $	*/
+/*	$NetBSD: histedit.c,v 1.48.8.2 2018/08/25 14:45:37 martin Exp $	*/
 
 /*-
  * Copyright (c) 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)histedit.c	8.2 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: histedit.c,v 1.48.8.1 2017/07/23 14:58:14 snj Exp $");
+__RCSID("$NetBSD: histedit.c,v 1.48.8.2 2018/08/25 14:45:37 martin Exp $");
 #endif
 #endif /* not lint */
 
@@ -210,8 +210,8 @@ sethistsize(const char *hs)
 	HistEvent he;
 
 	if (hist != NULL) {
-		if (hs == NULL || *hs == '\0' ||
-		   (histsize = atoi(hs)) < 0)
+		if (hs == NULL || *hs == '\0' || *hs == '-' ||
+		   (histsize = number(hs)) < 0)
 			histsize = 100;
 		history(hist, &he, H_SETSIZE, histsize);
 		history(hist, &he, H_SETUNIQUE, 1);
@@ -529,7 +529,7 @@ str_to_event(const char *str, int last)
 		s++;
 	}
 	if (is_number(s)) {
-		i = atoi(s);
+		i = number(s);
 		if (relative) {
 			while (retval != -1 && i--) {
 				retval = history(hist, &he, H_NEXT);

Index: src/bin/sh/mystring.c
diff -u src/bin/sh/mystring.c:1.17 src/bin/sh/mystring.c:1.17.22.1
--- src/bin/sh/mystring.c:1.17	Sun Apr 28 17:01:28 2013
+++ src/bin/sh/mystring.c	Sat Aug 25 14:45:37 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: mystring.c,v 1.17 2013/04/28 17:01:28 dholland Exp $	*/
+/*	$NetBSD: mystring.c,v 1.17.22.1 2018/08/25 14:45:37 martin Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)mystring.c	8.2 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: mystring.c,v 1.17 2013/04/28 17:01:28 dholland Exp $");
+__RCSID("$NetBSD: mystring.c,v 1.17.22.1 2018/08/25 14:45:37 martin Exp $");
 #endif
 #endif /* not lint */
 
@@ -51,6 +51,8 @@ __RCSID("$NetBSD: mystring.c,v 1.17 2013
  *	is_number(s)		Return true if s is a string of digits.
  */
 
+#include <inttypes.h>
+#include <limits.h>
 #include <stdlib.h>
 #include "shell.h"
 #include "syntax.h"
@@ -110,10 +112,15 @@ prefix(const char *pfx, const char *stri
 int
 number(const char *s)
 {
+	char *ep = NULL;
+	intmax_t n;
 
-	if (! is_number(s))
-		error("Illegal number: %s", s);
-	return atoi(s);
+	if (!is_digit(*s) || ((n = strtoimax(s, &ep, 10)), 
+	    (ep == NULL || ep == s || *ep != '\0')))
+		error("Illegal number: '%s'", s);
+	if (n < INT_MIN || n > INT_MAX)
+		error("Number out of range: %s", s);
+	return (int)n;
 }
 
 

Index: src/bin/sh/options.c
diff -u src/bin/sh/options.c:1.49 src/bin/sh/options.c:1.49.2.1
--- src/bin/sh/options.c:1.49	Mon May 29 14:03:23 2017
+++ src/bin/sh/options.c	Sat Aug 25 14:45:37 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: options.c,v 1.49 2017/05/29 14:03:23 kre Exp $	*/
+/*	$NetBSD: options.c,v 1.49.2.1 2018/08/25 14:45:37 martin Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)options.c	8.2 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: options.c,v 1.49 2017/05/29 14:03:23 kre Exp $");
+__RCSID("$NetBSD: options.c,v 1.49.2.1 2018/08/25 14:45:37 martin Exp $");
 #endif
 #endif /* not lint */
 
@@ -60,6 +60,7 @@ __RCSID("$NetBSD: options.c,v 1.49 2017/
 #include "memalloc.h"
 #include "error.h"
 #include "mystring.h"
+#include "syntax.h"
 #ifndef SMALL
 #include "myhistedit.h"
 #endif
@@ -444,7 +445,12 @@ setcmd(int argc, char **argv)
 void
 getoptsreset(const char *value)
 {
-	if (number(value) == 1) {
+	/*
+	 * This is just to detect the case where OPTIND=1
+	 * is executed.   Any other string assigned to OPTIND
+	 * is OK, but is not a reset.   No errors, so cannot use number()
+	 */
+	if (is_digit(*value) && strtol(value, NULL, 10) == 1) {
 		shellparam.optnext = NULL;
 		shellparam.reset = 1;
 	}

Index: src/bin/sh/parser.c
diff -u src/bin/sh/parser.c:1.132.2.4 src/bin/sh/parser.c:1.132.2.5
--- src/bin/sh/parser.c:1.132.2.4	Sun May  6 09:32:57 2018
+++ src/bin/sh/parser.c	Sat Aug 25 14:45:37 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: parser.c,v 1.132.2.4 2018/05/06 09:32:57 martin Exp $	*/
+/*	$NetBSD: parser.c,v 1.132.2.5 2018/08/25 14:45:37 martin Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)parser.c	8.7 (Berkeley) 5/16/95";
 #else
-__RCSID("$NetBSD: parser.c,v 1.132.2.4 2018/05/06 09:32:57 martin Exp $");
+__RCSID("$NetBSD: parser.c,v 1.132.2.5 2018/08/25 14:45:37 martin Exp $");
 #endif
 #endif /* not lint */
 
@@ -1528,7 +1528,7 @@ parseredir(const char *out,  int c)
 	union node *np;
 	int fd;
 
-	fd = (*out == '\0') ? -1 : atoi(out);
+	fd = (*out == '\0') ? -1 : number(out);
 
 	np = stalloc(sizeof(struct nfile));
 	if (c == '>') {

Index: src/bin/sh/var.c
diff -u src/bin/sh/var.c:1.55.2.2 src/bin/sh/var.c:1.55.2.3
--- src/bin/sh/var.c:1.55.2.2	Thu Aug 31 11:43:44 2017
+++ src/bin/sh/var.c	Sat Aug 25 14:45:37 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.55.2.2 2017/08/31 11:43:44 martin Exp $	*/
+/*	$NetBSD: var.c,v 1.55.2.3 2018/08/25 14:45:37 martin 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.55.2.2 2017/08/31 11:43:44 martin Exp $");
+__RCSID("$NetBSD: var.c,v 1.55.2.3 2018/08/25 14:45:37 martin Exp $");
 #endif
 #endif /* not lint */
 
@@ -50,6 +50,7 @@ __RCSID("$NetBSD: var.c,v 1.55.2.2 2017/
 #include <time.h>
 #include <pwd.h>
 #include <fcntl.h>
+#include <inttypes.h>
 
 /*
  * Shell variables.
@@ -230,14 +231,17 @@ INIT {
 	 *
 	 * PPID is readonly
 	 * Always default IFS
+	 * POSIX: "Whenever the shell is invoked, OPTIND shall
+	 *         be initialized to 1."
 	 * PSc indicates the root/non-root status of this shell.
-	 * NETBSD_SHELL is a constant (readonly), and is never exported
 	 * START_TIME belongs only to this shell.
+	 * NETBSD_SHELL is a constant (readonly), and is never exported
 	 * LINENO is simply magic...
 	 */
 	snprintf(buf, sizeof(buf), "%d", (int)getppid());
 	setvar("PPID", buf, VREADONLY);
 	setvar("IFS", ifs_default, VTEXTFIXED);
+	setvar("OPTIND", "1", VTEXTFIXED);
 	setvar("PSc", (geteuid() == 0 ? "#" : "$"), VTEXTFIXED);
 
 #ifndef SMALL
@@ -1398,7 +1402,7 @@ get_random(struct var *vp)
 			}
 		} else
 			/* good enough for today */
-			random_val = atoi(vp->text + vp->name_len + 1);
+			random_val = strtoimax(vp->text+vp->name_len+1,NULL,0);
 
 		srandom((long)random_val);
 	}

Reply via email to