Module Name:    src
Committed By:   kre
Date:           Fri Jul 13 22:43:44 UTC 2018

Modified Files:
        src/bin/sh: histedit.c mystring.c options.c parser.c var.c

Log Message:
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.52 -r1.53 src/bin/sh/histedit.c src/bin/sh/options.c
cvs rdiff -u -r1.17 -r1.18 src/bin/sh/mystring.c
cvs rdiff -u -r1.146 -r1.147 src/bin/sh/parser.c
cvs rdiff -u -r1.69 -r1.70 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.52 src/bin/sh/histedit.c:1.53
--- src/bin/sh/histedit.c:1.52	Wed Jun 28 13:46:06 2017
+++ src/bin/sh/histedit.c	Fri Jul 13 22:43:44 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: histedit.c,v 1.52 2017/06/28 13:46:06 kre Exp $	*/
+/*	$NetBSD: histedit.c,v 1.53 2018/07/13 22:43:44 kre 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.52 2017/06/28 13:46:06 kre Exp $");
+__RCSID("$NetBSD: histedit.c,v 1.53 2018/07/13 22:43:44 kre 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/options.c
diff -u src/bin/sh/options.c:1.52 src/bin/sh/options.c:1.53
--- src/bin/sh/options.c:1.52	Tue Nov 21 03:42:39 2017
+++ src/bin/sh/options.c	Fri Jul 13 22:43:44 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: options.c,v 1.52 2017/11/21 03:42:39 kre Exp $	*/
+/*	$NetBSD: options.c,v 1.53 2018/07/13 22:43:44 kre 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.52 2017/11/21 03:42:39 kre Exp $");
+__RCSID("$NetBSD: options.c,v 1.53 2018/07/13 22:43:44 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -60,6 +60,7 @@ __RCSID("$NetBSD: options.c,v 1.52 2017/
 #include "memalloc.h"
 #include "error.h"
 #include "mystring.h"
+#include "syntax.h"
 #ifndef SMALL
 #include "myhistedit.h"
 #endif
@@ -456,7 +457,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/mystring.c
diff -u src/bin/sh/mystring.c:1.17 src/bin/sh/mystring.c:1.18
--- src/bin/sh/mystring.c:1.17	Sun Apr 28 17:01:28 2013
+++ src/bin/sh/mystring.c	Fri Jul 13 22:43:44 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: mystring.c,v 1.17 2013/04/28 17:01:28 dholland Exp $	*/
+/*	$NetBSD: mystring.c,v 1.18 2018/07/13 22:43:44 kre 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.18 2018/07/13 22:43:44 kre 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/parser.c
diff -u src/bin/sh/parser.c:1.146 src/bin/sh/parser.c:1.147
--- src/bin/sh/parser.c:1.146	Sat Apr 21 21:32:14 2018
+++ src/bin/sh/parser.c	Fri Jul 13 22:43:44 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: parser.c,v 1.146 2018/04/21 21:32:14 kre Exp $	*/
+/*	$NetBSD: parser.c,v 1.147 2018/07/13 22:43:44 kre 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.146 2018/04/21 21:32:14 kre Exp $");
+__RCSID("$NetBSD: parser.c,v 1.147 2018/07/13 22:43:44 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -1506,7 +1506,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.69 src/bin/sh/var.c:1.70
--- src/bin/sh/var.c:1.69	Sun Nov 19 03:23:01 2017
+++ src/bin/sh/var.c	Fri Jul 13 22:43:44 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.69 2017/11/19 03:23:01 kre Exp $	*/
+/*	$NetBSD: var.c,v 1.70 2018/07/13 22:43:44 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.69 2017/11/19 03:23:01 kre Exp $");
+__RCSID("$NetBSD: var.c,v 1.70 2018/07/13 22:43:44 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -50,6 +50,7 @@ __RCSID("$NetBSD: var.c,v 1.69 2017/11/1
 #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
@@ -1427,7 +1431,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