In message <[EMAIL PROTECTED]> 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 [EMAIL PROTECTED]
with "unsubscribe freebsd-current" in the body of the message

Reply via email to