Source-Changes-HG archive

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

[src/trunk]: src/bin/sh Use a type-correct end marker for strstrcat() rather ...



details:   https://anonhg.NetBSD.org/src/rev/189dadbd2859
branches:  trunk
changeset: 990083:189dadbd2859
user:      kre <kre%NetBSD.org@localhost>
date:      Tue Oct 26 10:07:20 2021 +0000

description:
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)

diffstat:

 bin/sh/main.c     |   6 +++---
 bin/sh/memalloc.c |  10 ++++++----
 bin/sh/memalloc.h |   4 +++-
 3 files changed, 12 insertions(+), 8 deletions(-)

diffs (96 lines):

diff -r d3b5938637ae -r 189dadbd2859 bin/sh/main.c
--- a/bin/sh/main.c     Tue Oct 26 07:51:23 2021 +0000
+++ b/bin/sh/main.c     Tue Oct 26 10:07:20 2021 +0000
@@ -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 @@
 #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 @@
                        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);
                }
diff -r d3b5938637ae -r 189dadbd2859 bin/sh/memalloc.c
--- a/bin/sh/memalloc.c Tue Oct 26 07:51:23 2021 +0000
+++ b/bin/sh/memalloc.c Tue Oct 26 10:07:20 2021 +0000
@@ -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 @@
  * 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 @@
        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 @@
        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
diff -r d3b5938637ae -r 189dadbd2859 bin/sh/memalloc.h
--- a/bin/sh/memalloc.h Tue Oct 26 07:51:23 2021 +0000
+++ b/bin/sh/memalloc.h Tue Oct 26 10:07:20 2021 +0000
@@ -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 @@
 char *growstackstr(void);
 char *makestrspace(void);
 void ungrabstackstr(char *, char *);
+
 char *ststrcat(size_t *, ...);
+#define STSTRC_END     ((const char *)0)
 
 
 



Home | Main Index | Thread Index | Old Index