Subject: Re: bin/23210: usr.bin/make/util.c needs asprintf
To: None <dmcmahill@NetBSD.org>
From: Alan Barrett <apb@cequrux.com>
List: netbsd-bugs
Date: 10/21/2003 10:16:50
On Mon, 20 Oct 2003, dmcmahill@NetBSD.org wrote:
> Rev 1.81 from 2003/09/27 of usr.bin/make/var.c introduces the
> use of asprintf() to make.  There should be an autoconf check
> in src/tools/make/configure.ac as well as a portable asprintf()
> implementation in usr.bin/make/util.c to allow cross building from
> systems which do not have asprintf() such as solaris 2.6 with
> gcc-2.95.1.

Please try the appended patch, which removes the use of asprintf()
from make/var.c.

--apb (Alan Barrett)

Index: usr.bin/make/var.c
--- var.c	27 Sep 2003 21:29:37 -0000	1.81
+++ var.c	21 Oct 2003 08:02:09 -0000
@@ -129,6 +129,7 @@
 #endif
 #include    <ctype.h>
 #include    <stdlib.h>
+#include    <limits.h>
 
 #include    "make.h"
 #include    "buf.h"
@@ -2489,9 +2490,22 @@
 			goto bad_modifier;
 		    } else if (estr[0] == '#' && estr[1] == '\0') {
 			/* Found ":[#]" */
-			if (parsestate.oneBigWord)
-			    asprintf(&newStr, "1");
-			else {
+
+			/*
+			 * We will need enough space for the decimal
+			 * representation of an int.  We calculate the
+			 * space needed for the octal representation,
+			 * and add enough slop to cope with a '-' sign
+			 * (which should never be needed) and a '\0'
+			 * string terminator.
+			 */
+			int newStrSize =
+				(sizeof(int) * CHAR_BIT + 2) / 3 + 2;
+
+			newStr = emalloc(newStrSize);
+			if (parsestate.oneBigWord) {
+			    strncpy(newStr, "1", newStrSize);
+			} else {
 			    /* XXX: brk_string() is a rather expensive
 			     * way of counting words. */
 			    char **av;
@@ -2499,7 +2513,7 @@
 			    int ac;
 
 			    av = brk_string(nstr, &ac, FALSE, &as);
-			    asprintf(&newStr, "%d", ac);
+			    snprintf(newStr, newStrSize,  "%d", ac);
 			    free(as);
 			    free(av);
 			}