Date: Wed, 24 Nov 1999 00:50:53 -0700 From: Warner Losh <imp@village.org> To: Brian Fundakowski Feldman <green@FreeBSD.ORG> Cc: Kris Kennaway <kris@hub.freebsd.org>, current@FreeBSD.ORG Subject: Re: Overflow in banner(1) Message-ID: <199911240750.AAA18917@harmony.village.org> In-Reply-To: Your message of "Wed, 24 Nov 1999 00:44:11 EST." <Pine.BSF.4.10.9911240033221.40905-100000@green.dyndns.org> References: <Pine.BSF.4.10.9911240033221.40905-100000@green.dyndns.org>
next in thread | previous in thread | raw e-mail | index | archive | help
In message <Pine.BSF.4.10.9911240033221.40905-100000@green.dyndns.org> Brian Fundakowski Feldman writes:
: I'd prefer something like this that I've attached. The move over the
: years has been away from artificial limits...
: @@ -1058,14 +1058,24 @@
:
: /* Have now read in the data. Next get the message to be printed. */
: if (*argv) {
: - strcpy(message, *argv);
: + message = strdup(*argv);
: + if (message == NULL)
: + err(1, "strdup");
: while (*++argv) {
: - strcat(message, " ");
: - strcat(message, *argv);
: + char *omessage;
: +
: + omessage = message;
: + asprintf(&message, "%s %s", message, *argv);
: + if (message == NULL)
: + err(1, "asprintf");
: + free(omessage);
: }
: nchars = strlen(message);
: } else {
: fprintf(stderr,"Message: ");
: + message = malloc(MAXMSG);
: + if (message == NULL)
: + err(1, "malloc");
: (void)fgets(message, sizeof(message), stdin);
: nchars = strlen(message);
: message[nchars--] = '\0'; /* get rid of newline */
I'd have used the realloc primitive in place of the asprintf primitive
here to avoid too many calls to realloc/malloc. Actually, it is
clearer and easier to understand if you precompute the length first
and malloc enough space.
len = argc; (one each space, plus null at end)
for (i = 1; i <= argc; i++)
len += strlen(argv[i]);
message = malloc(len);
// original code here, maybe marked with /* XXX SAFE XXX */
// to show that the code was reviewed at least once.
Warner
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-current" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199911240750.AAA18917>
