Module Name:    src
Committed By:   kre
Date:           Tue Oct 26 10:07:20 UTC 2021

Modified Files:
        src/bin/sh: main.c memalloc.c memalloc.h

Log Message:
Use a type-correct end marker for strstrcat() rather than NULL, as
for a function with unknown number & types of args, the compiler isn't
able to automatically convert to the correct type.   Issue pointed out
in off list e-mail by Rolland Illig ... Thanks.

The first arg (pointer to where to put length of result) is of a known
type, so doesn't have the same issue - we can keep using NULL for that
one when the length isn't needed.

Also, make sure to return a correctly null terminated null string in
the (absurd) case that there are no non-null args to strstrcat() (though
there are much better ways to generate "" on the stack).  Since there is
currently just one call in the code, and it has real string args, this
isn't an issue for now, but who knows, some day.

NFCI - if there is any real change, then it is a change that is required.

XXX pullup -9 (together with the previous changes)


To generate a diff of this commit:
cvs rdiff -u -r1.87 -r1.88 src/bin/sh/main.c
cvs rdiff -u -r1.34 -r1.35 src/bin/sh/memalloc.c
cvs rdiff -u -r1.19 -r1.20 src/bin/sh/memalloc.h

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/main.c
diff -u src/bin/sh/main.c:1.87 src/bin/sh/main.c:1.88
--- src/bin/sh/main.c:1.87	Tue Oct 26 00:05:38 2021
+++ src/bin/sh/main.c	Tue Oct 26 10:07:20 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.87 2021/10/26 00:05:38 kre Exp $	*/
+/*	$NetBSD: main.c,v 1.88 2021/10/26 10:07:20 kre Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993
@@ -42,7 +42,7 @@ __COPYRIGHT("@(#) Copyright (c) 1991, 19
 #if 0
 static char sccsid[] = "@(#)main.c	8.7 (Berkeley) 7/19/95";
 #else
-__RCSID("$NetBSD: main.c,v 1.87 2021/10/26 00:05:38 kre Exp $");
+__RCSID("$NetBSD: main.c,v 1.88 2021/10/26 10:07:20 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -241,7 +241,7 @@ main(int argc, char **argv)
 			home = lookupvar("HOME");
 			if (home == NULL)
 				home = nullstr;
-			profile = ststrcat(NULL, home, "/.profile", NULL);
+			profile = ststrcat(NULL, home, "/.profile", STSTRC_END);
 			read_profile(profile);
 			stunalloc(profile);
 		}

Index: src/bin/sh/memalloc.c
diff -u src/bin/sh/memalloc.c:1.34 src/bin/sh/memalloc.c:1.35
--- src/bin/sh/memalloc.c:1.34	Tue Oct 26 00:05:38 2021
+++ src/bin/sh/memalloc.c	Tue Oct 26 10:07:20 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: memalloc.c,v 1.34 2021/10/26 00:05:38 kre Exp $	*/
+/*	$NetBSD: memalloc.c,v 1.35 2021/10/26 10:07:20 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.34 2021/10/26 00:05:38 kre Exp $");
+__RCSID("$NetBSD: memalloc.c,v 1.35 2021/10/26 10:07:20 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -350,6 +350,7 @@ ungrabstackstr(char *s, char *p)
  * Remaining args are pointers to strings - sufficient space to hold
  * the concat of the strings is allocated on the stack, the strings
  * are copied into that space, and a pointer to its start is retured.
+ * The arg list is terminated with STSTRC_END.
  *
  * Use stunalloc(string) (in proper sequence) to release the string
  */
@@ -365,7 +366,7 @@ ststrcat(size_t *lp, ...)
 	n = 0;
 	va_start(ap, lp);
 	arg = va_arg(ap, const char *);
-	while (arg != NULL) {
+	while (arg != STSTRC_END) {
 		len = strlen(arg);
 		if (n < sizeof(alen)/sizeof(alen[0]))
 			alen[n++] = len;
@@ -380,12 +381,13 @@ ststrcat(size_t *lp, ...)
 	if (tlen >= INT_MAX)
 		error("ststrcat() over length botch");
 	str = (char *)stalloc((int)tlen + 1);	/* 1 for \0 */
+	str[tlen] = '\0';	/* in case of no args  */
 
 	n = 0;
 	nxt = str;
 	va_start(ap, lp);
 	arg = va_arg(ap, const char *);
-	while (arg != NULL) {
+	while (arg != STSTRC_END) {
 		if (n < sizeof(alen)/sizeof(alen[0]))
 			len = alen[n++];
 		else

Index: src/bin/sh/memalloc.h
diff -u src/bin/sh/memalloc.h:1.19 src/bin/sh/memalloc.h:1.20
--- src/bin/sh/memalloc.h:1.19	Tue Oct 26 00:05:38 2021
+++ src/bin/sh/memalloc.h	Tue Oct 26 10:07:20 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: memalloc.h,v 1.19 2021/10/26 00:05:38 kre Exp $	*/
+/*	$NetBSD: memalloc.h,v 1.20 2021/10/26 10:07:20 kre Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993
@@ -61,7 +61,9 @@ void grabstackblock(int);
 char *growstackstr(void);
 char *makestrspace(void);
 void ungrabstackstr(char *, char *);
+
 char *ststrcat(size_t *, ...);
+#define STSTRC_END	((const char *)0)
 
 
 

Reply via email to