Module Name:    src
Committed By:   kre
Date:           Sat Feb  9 03:35:55 UTC 2019

Modified Files:
        src/bin/sh: eval.c histedit.c input.c jobs.c memalloc.c redir.c var.c

Log Message:
INTON / INTOFF audit and cleanup.

No visible differences expected - there is a remote chance that
some internal lossage may no longer occur in interactive shells
that receive SIGINT (untrapped) at inopportune times, but you would
have had to have been very unlucky to have ever suffered from that.


To generate a diff of this commit:
cvs rdiff -u -r1.171 -r1.172 src/bin/sh/eval.c
cvs rdiff -u -r1.53 -r1.54 src/bin/sh/histedit.c
cvs rdiff -u -r1.69 -r1.70 src/bin/sh/input.c
cvs rdiff -u -r1.103 -r1.104 src/bin/sh/jobs.c
cvs rdiff -u -r1.32 -r1.33 src/bin/sh/memalloc.c
cvs rdiff -u -r1.62 -r1.63 src/bin/sh/redir.c
cvs rdiff -u -r1.75 -r1.76 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/eval.c
diff -u src/bin/sh/eval.c:1.171 src/bin/sh/eval.c:1.172
--- src/bin/sh/eval.c:1.171	Mon Feb  4 11:16:41 2019
+++ src/bin/sh/eval.c	Sat Feb  9 03:35:55 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: eval.c,v 1.171 2019/02/04 11:16:41 kre Exp $	*/
+/*	$NetBSD: eval.c,v 1.172 2019/02/09 03:35:55 kre Exp $	*/
 
 /*-
  * Copyright (c) 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)eval.c	8.9 (Berkeley) 6/8/95";
 #else
-__RCSID("$NetBSD: eval.c,v 1.171 2019/02/04 11:16:41 kre Exp $");
+__RCSID("$NetBSD: eval.c,v 1.172 2019/02/09 03:35:55 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -545,19 +545,19 @@ evalsubshell(union node *n, int flags)
 		outxc('\n');
 		flushout(outx);
 	}
+	INTOFF;
 	if ((!backgnd && flags & EV_EXIT && !have_traps()) ||
 	    forkshell(jp = makejob(n, 1), n, backgnd?FORK_BG:FORK_FG) == 0) {
-		INTON;
 		if (backgnd)
 			flags &=~ EV_TESTED;
 		redirect(n->nredir.redirect, REDIR_KEEP);
-		evaltree(n->nredir.n, flags | EV_EXIT);   /* never returns */
-	} else if (!backgnd) {
-		INTOFF;
-		exitstatus = waitforjob(jp);
 		INTON;
-	} else
+		evaltree(n->nredir.n, flags | EV_EXIT);   /* never returns */
+	} else if (backgnd)
 		exitstatus = 0;
+	else
+		exitstatus = waitforjob(jp);
+	INTON;
 
 	if (!backgnd && xflag && n->nredir.redirect) {
 		outxstr(expandstr(ps4val(), line_number));
@@ -714,11 +714,9 @@ evalpipe(union node *n)
 		close(pip[1]);
 	}
 	if (n->npipe.backgnd == 0) {
-		INTOFF;
 		exitstatus = waitforjob(jp);
 		CTRACE(DBG_EVAL, ("evalpipe:  job done exit status %d\n",
 		    exitstatus));
-		INTON;
 	} else
 		exitstatus = 0;
 	INTON;

Index: src/bin/sh/histedit.c
diff -u src/bin/sh/histedit.c:1.53 src/bin/sh/histedit.c:1.54
--- src/bin/sh/histedit.c:1.53	Fri Jul 13 22:43:44 2018
+++ src/bin/sh/histedit.c	Sat Feb  9 03:35:55 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: histedit.c,v 1.53 2018/07/13 22:43:44 kre Exp $	*/
+/*	$NetBSD: histedit.c,v 1.54 2019/02/09 03:35:55 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.53 2018/07/13 22:43:44 kre Exp $");
+__RCSID("$NetBSD: histedit.c,v 1.54 2019/02/09 03:35:55 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -153,6 +153,7 @@ bad:
 			INTON;
 		}
 		if (el) {
+			INTOFF;
 			if (Vflag)
 				el_set(el, EL_EDITOR, "vi");
 			else if (Eflag)
@@ -160,6 +161,7 @@ bad:
 			el_set(el, EL_BIND, "^I", 
 			    tabcomplete ? "rl-complete" : "ed-insert", NULL);
 			el_source(el, lookupvar("EDITRC"));
+			INTON;
 		}
 	} else {
 		INTOFF;
@@ -190,17 +192,21 @@ set_prompt_lit(const char *lit_ch)
 
 	mbtowc(&wc, NULL, 1);		/* state init */
 
+	INTOFF;
 	if (mbtowc(&wc, lit_ch, strlen(lit_ch)) <= 0)
 		el_set(el, EL_PROMPT, getprompt);
 	else
 		el_set(el, EL_PROMPT_ESC, getprompt, (int)wc);
+	INTON;
 }
 
 void
 set_editrc(const char *fname)
 {
+	INTOFF;
 	if (iflag && editing && el)
 		el_source(el, fname);
+	INTON;
 }
 
 void
@@ -213,19 +219,23 @@ sethistsize(const char *hs)
 		if (hs == NULL || *hs == '\0' || *hs == '-' ||
 		   (histsize = number(hs)) < 0)
 			histsize = 100;
+		INTOFF;
 		history(hist, &he, H_SETSIZE, histsize);
 		history(hist, &he, H_SETUNIQUE, 1);
+		INTON;
 	}
 }
 
 void
 setterm(const char *term)
 {
+	INTOFF;
 	if (el != NULL && term != NULL)
 		if (el_set(el, EL_TERMINAL, term) != 0) {
 			outfmt(out2, "sh: Can't set terminal type %s\n", term);
 			outfmt(out2, "sh: Using dumb terminal settings.\n");
 		}
+	INTON;
 }
 
 int
@@ -236,11 +246,14 @@ inputrc(int argc, char **argv)
 		return 1;
 	}
 	if (el != NULL) {
+		INTOFF;
 		if (el_source(el, argv[1])) {
+			INTON;
 			out2str("inputrc: failed\n");
 			return 1;
-		} else
-			return 0;
+		}
+		INTON;
+		return 0;
 	} else {
 		out2str("sh: inputrc ignored, not editing\n");
 		return 1;
@@ -468,9 +481,10 @@ histcmd(volatile int argc, char ** volat
 		editcmd = stalloc(cmdlen);
 		snprintf(editcmd, cmdlen, "%s %s", editor, editfile);
 		evalstring(editcmd, 0);	/* XXX - should use no JC command */
-		INTON;
+		stunalloc(editcmd);
 		readcmdfile(editfile);	/* XXX - should read back - quick tst */
 		unlink(editfile);
+		INTON;
 	}
 
 	if (lflg == 0 && active > 0)

Index: src/bin/sh/input.c
diff -u src/bin/sh/input.c:1.69 src/bin/sh/input.c:1.70
--- src/bin/sh/input.c:1.69	Wed Jan 16 07:14:17 2019
+++ src/bin/sh/input.c	Sat Feb  9 03:35:55 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: input.c,v 1.69 2019/01/16 07:14:17 kre Exp $	*/
+/*	$NetBSD: input.c,v 1.70 2019/02/09 03:35:55 kre Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)input.c	8.3 (Berkeley) 6/9/95";
 #else
-__RCSID("$NetBSD: input.c,v 1.69 2019/01/16 07:14:17 kre Exp $");
+__RCSID("$NetBSD: input.c,v 1.70 2019/02/09 03:35:55 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -532,6 +532,7 @@ setinputfd(int fd, int push)
 {
 	VTRACE(DBG_INPUT, ("setinputfd(%d, %spush)\n", fd, push?"":"no"));
 
+	INTOFF;
 	register_sh_fd(fd, input_fd_swap);
 	(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
 	if (push)
@@ -543,6 +544,7 @@ setinputfd(int fd, int push)
 		parsefile->buf = ckmalloc(BUFSIZ);
 	parselleft = parsenleft = 0;
 	plinno = 1;
+	INTON;
 
 	CTRACE(DBG_INPUT, ("setinputfd(%d, %spush) done; plinno=1\n", fd,
 	    push ? "" : "no"));
@@ -563,11 +565,11 @@ setinputstring(char *string, int push, i
 	parsenextc = string;
 	parselleft = parsenleft = strlen(string);
 	plinno = line1;
+	INTON;
 
 	CTRACE(DBG_INPUT,
 	    ("setinputstring(\"%.20s%s\" (%d), %spush, @ %d)\n", string,
 	    (parsenleft > 20 ? "..." : ""), parsenleft, push?"":"no", line1));
-	INTON;
 }
 
 

Index: src/bin/sh/jobs.c
diff -u src/bin/sh/jobs.c:1.103 src/bin/sh/jobs.c:1.104
--- src/bin/sh/jobs.c:1.103	Mon Dec  3 02:38:30 2018
+++ src/bin/sh/jobs.c	Sat Feb  9 03:35:55 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: jobs.c,v 1.103 2018/12/03 02:38:30 kre Exp $	*/
+/*	$NetBSD: jobs.c,v 1.104 2019/02/09 03:35:55 kre Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)jobs.c	8.5 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: jobs.c,v 1.103 2018/12/03 02:38:30 kre Exp $");
+__RCSID("$NetBSD: jobs.c,v 1.104 2019/02/09 03:35:55 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -277,8 +277,8 @@ do_fgcmd(const char *arg_ptr)
 		error("Cannot set tty process group (%s) at %d",
 		    strerror(errno), __LINE__);
 	}
-	restartjob(jp);
 	INTOFF;
+	restartjob(jp);
 	status = waitforjob(jp);
 	INTON;
 	return status;
@@ -1110,7 +1110,6 @@ forkshell(struct job *jp, union node *n,
 	case -1:
 		serrno = errno;
 		VTRACE(DBG_JOBS, ("Fork failed, errno=%d\n", serrno));
-		INTON;
 		error("Cannot fork (%s)", strerror(serrno));
 		break;
 	case 0:

Index: src/bin/sh/memalloc.c
diff -u src/bin/sh/memalloc.c:1.32 src/bin/sh/memalloc.c:1.33
--- src/bin/sh/memalloc.c:1.32	Wed Aug 22 20:08:54 2018
+++ src/bin/sh/memalloc.c	Sat Feb  9 03:35:55 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: memalloc.c,v 1.32 2018/08/22 20:08:54 kre Exp $	*/
+/*	$NetBSD: memalloc.c,v 1.33 2019/02/09 03:35:55 kre Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)memalloc.c	8.3 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: memalloc.c,v 1.32 2018/08/22 20:08:54 kre Exp $");
+__RCSID("$NetBSD: memalloc.c,v 1.33 2019/02/09 03:35:55 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -141,9 +141,11 @@ stalloc(int nbytes)
 		stackp = sp;
 		INTON;
 	}
+	INTOFF;
 	p = stacknxt;
 	stacknxt += nbytes;
 	stacknleft -= nbytes;
+	INTON;
 	return p;
 }
 
@@ -176,8 +178,10 @@ setstackmark(struct stackmark *mark)
 void
 popstackmark(struct stackmark *mark)
 {
+	INTOFF;
 	markp = mark->marknext;		/* delete mark from the list */
 	rststackmark(mark);		/* and reset stack */
+	INTON;
 }
 
 /* reset the shell stack to its state recorded in the stack mark */
@@ -260,8 +264,10 @@ void
 grabstackblock(int len)
 {
 	len = SHELL_ALIGN(len);
+	INTOFF;
 	stacknxt += len;
 	stacknleft -= len;
+	INTON;
 }
 
 /*

Index: src/bin/sh/redir.c
diff -u src/bin/sh/redir.c:1.62 src/bin/sh/redir.c:1.63
--- src/bin/sh/redir.c:1.62	Mon Nov 26 20:03:39 2018
+++ src/bin/sh/redir.c	Sat Feb  9 03:35:55 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: redir.c,v 1.62 2018/11/26 20:03:39 kamil Exp $	*/
+/*	$NetBSD: redir.c,v 1.63 2019/02/09 03:35:55 kre Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)redir.c	8.2 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: redir.c,v 1.62 2018/11/26 20:03:39 kamil Exp $");
+__RCSID("$NetBSD: redir.c,v 1.63 2019/02/09 03:35:55 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -243,7 +243,6 @@ redirect(union node *redir, int flags)
 					/* FALLTHRU */
 				default:
 					i = errno;
-					INTON;    /* XXX not needed here ? */
 					error("%d: %s", fd, strerror(i));
 					/* NOTREACHED */
 				}

Index: src/bin/sh/var.c
diff -u src/bin/sh/var.c:1.75 src/bin/sh/var.c:1.76
--- src/bin/sh/var.c:1.75	Mon Jan 21 13:27:29 2019
+++ src/bin/sh/var.c	Sat Feb  9 03:35:55 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.75 2019/01/21 13:27:29 kre Exp $	*/
+/*	$NetBSD: var.c,v 1.76 2019/02/09 03:35:55 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.75 2019/01/21 13:27:29 kre Exp $");
+__RCSID("$NetBSD: var.c,v 1.76 2019/02/09 03:35:55 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -1342,16 +1342,17 @@ make_space(struct space_reserved *m, int
 		return 1;
 
 	bytes = SHELL_ALIGN(bytes);
+	INTOFF;
 	/* not ckrealloc() - we want failure, not error() here */
 	p = realloc(m->b, bytes);
-	if (p == NULL)	/* what we had should still be there */
-		return 0;
-
-	m->b = p;
-	m->len = bytes;
-	m->b[bytes - 1] = '\0';
+	if (p != NULL) {
+		m->b = p;
+		m->len = bytes;
+		m->b[bytes - 1] = '\0';
+	}
+	INTON;
 
-	return 1;
+	return p != NULL;
 }
 #endif
 
@@ -1428,8 +1429,8 @@ get_tod(struct var *vp)
 
 	if (tz != NULL) {
 		if (tzs.b == NULL || strcmp(tzs.b, tz) != 0) {
+			INTOFF;
 			if (make_space(&tzs, strlen(tz) + 1)) {
-				INTOFF;
 				strcpy(tzs.b, tz);
 				if (last_zone)
 					tzfree(last_zone);
@@ -1450,8 +1451,10 @@ get_tod(struct var *vp)
 		if (tmp == NULL) {
 			if (buf.len >= vp->name_len+2+(int)(sizeof t_err - 1)) {
 				strcpy(buf.b + vp->name_len + 1, t_err);
-				if (zone && zone != last_zone)
+				if (zone && zone != last_zone) {
 					tzfree(zone);
+					INTON;
+				}
 				return buf.b;
 			}
 			len = vp->name_len + 4 + sizeof t_err - 1;
@@ -1459,16 +1462,20 @@ get_tod(struct var *vp)
 		}
 		if (strftime_z(zone, buf.b + vp->name_len + 1,
 		     buf.len - vp->name_len - 2, fmt, tmp)) {
-			if (zone && zone != last_zone)
+			if (zone && zone != last_zone) {
 				tzfree(zone);
+				INTON;
+			}
 			return buf.b;
 		}
 		if (len >= 4096)	/* Let's be reasonable */
 			break;
 		len <<= 1;
 	}
-	if (zone && zone != last_zone)
+	if (zone && zone != last_zone) {
 		tzfree(zone);
+		INTON;
+	}
 	return vp->text;
 }
 
@@ -1509,9 +1516,11 @@ get_euser(struct var *vp)
 		return vp->text;
 
 	if (make_space(&buf, vp->name_len + 2 + strlen(pw->pw_name))) {
+		INTOFF;
 		lastuid = euid;
 		snprintf(buf.b, buf.len, "%.*s=%s", vp->name_len, vp->text,
 		    pw->pw_name);
+		INTON;
 		return buf.b;
 	}
 
@@ -1543,6 +1552,7 @@ get_random(struct var *vp)
 			 * initialisation (without pre-seeding),
 			 * or explictly requesting a truly random seed.
 			 */
+			INTOFF;
 			fd = open("/dev/urandom", 0);
 			if (fd == -1) {
 				out2str("RANDOM initialisation failed\n");
@@ -1555,6 +1565,7 @@ get_random(struct var *vp)
 				} while (n != sizeof random_val);
 				close(fd);
 			}
+			INTON;
 		} else
 			/* good enough for today */
 			random_val = strtoimax(vp->text+vp->name_len+1,NULL,0);
@@ -1574,10 +1585,12 @@ get_random(struct var *vp)
 	snprintf(buf.b, buf.len, "%.*s=%jd", vp->name_len, vp->text,
 	    random_val);
 
+	INTOFF;
 	if (buf.b != vp->text && (vp->flags & (VTEXTFIXED|VSTACK)) == 0)
 		free(vp->text);
 	vp->flags |= VTEXTFIXED;
 	vp->text = buf.b;
+	INTON;
 
 	return vp->text;
 #undef random

Reply via email to