Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #61623

Re: Return value for strcat and similar functions

From Morris Dovey <mrdovey@iedu.com>
Newsgroups comp.lang.c
Subject Re: Return value for strcat and similar functions
Date 2015-05-06 12:01 -0500
Organization http://www.iedu.com/Solar/
Message-ID <midhct$t7$1@speranza.aioe.org> (permalink)
References <michpc$2b0$1@dont-email.me> <87r3qtlmtn.fsf@kst-u.example.com> <mide6l$e05$2@dont-email.me>

Show all headers | View raw


On 5/6/15 11:07 AM, Richard Heathfield wrote:
> On 06/05/15 16:58, Keith Thompson wrote:
>
> <snip>
>
>> Speculation: The early design of C did not go out of its way to allow
>> for the possibility of buffer overflows.  If you assume that your target
>> array is always going to be big enough, it's reasonable to write:
>>
>>      char s[BIG_ENOUGH];
>>      strcat(strcat(strcpy(s, this), that), the_other_thing);
>>
>> Personally, even if I were willing to assume the target array is big
>> enough, I'd probably still break up the calls:
>>
>>      strcpy(s, this);
>>      strcat(s, that);
>>      strcat(s, the_other_thing);
>
> And I'd probably use sprintf(s, "%s%s%s", this,  that, the_other_thing)
> instead. Not because it's quicker (which it might or might not be, since
> strcat has to find the end of the string each time, but sprintf has to
> read and interpret the format string), but because to my eyes it's a
> tiny little bit clearer.

Another option is to write a variadic function that takes a 
NULL-terminated list of string pointers, and returns a pointer to a 
dynamically-allocated exact-size buffer containing the concatenation:

    rtss(this,that,the_other_thing,NULL);

It's convenient, clear, smaller, and faster than sprintf() - but 
requires remembering to free() the buffer.

A note in the prolog reminds me that I got help from Chris Torek and 
posted the code here sometime around August 29, 1999.

-- 
Morris Dovey
http://www.iedu.com/Solar

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Return value for strcat and similar functions David Brown <david.brown@hesbynett.no> - 2015-05-06 10:02 +0200
  Re: Return value for strcat and similar functions Xavier Roche <xroche@free.fr.NOSPAM.invalid> - 2015-05-06 10:34 +0200
  Re: Return value for strcat and similar functions Lőrinczy Zsigmond <zsiga@nospam.for.me> - 2015-05-06 16:51 +0200
  Re: Return value for strcat and similar functions Keith Thompson <kst-u@mib.org> - 2015-05-06 08:58 -0700
    Re: Return value for strcat and similar functions Richard Heathfield <rjh@cpax.org.uk> - 2015-05-06 17:07 +0100
      Re: Return value for strcat and similar functions Morris Dovey <mrdovey@iedu.com> - 2015-05-06 12:01 -0500
    Re: Return value for strcat and similar functions David Brown <david.brown@hesbynett.no> - 2015-05-07 10:19 +0200
      Re: Return value for strcat and similar functions glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-05-07 13:21 +0000
        Re: Return value for strcat and similar functions David Brown <david.brown@hesbynett.no> - 2015-05-07 16:24 +0200
          Re: Return value for strcat and similar functions glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-05-07 18:02 +0000
      Re: Return value for strcat and similar functions Morris Dovey <mrdovey@iedu.com> - 2015-05-07 10:17 -0500
        Re: Return value for strcat and similar functions Morris Dovey <mrdovey@iedu.com> - 2015-05-07 10:56 -0500
          Re: Return value for strcat and similar functions asetofsymbols@gmail.com - 2015-05-07 11:00 -0700
            Re: Return value for strcat and similar functions Morris Dovey <mrdovey@iedu.com> - 2015-05-07 14:28 -0500
              Re: Return value for strcat and similar functions asetofsymbols@gmail.com - 2015-05-07 12:47 -0700
          Re: Return value for strcat and similar functions Nathan Wagner <nw@hydaspes.if.org> - 2015-05-08 09:31 +0000
        Re: Return value for strcat and similar functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-05-07 15:55 -0700
        Re: Return value for strcat and similar functions David Brown <david.brown@hesbynett.no> - 2015-05-08 11:03 +0200
          Re: Return value for strcat and similar functions Morris Dovey <mrdovey@iedu.com> - 2015-05-08 05:07 -0500
  Re: Return value for strcat and similar functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-05-06 18:25 -0700
    Re: Return value for strcat and similar functions David Brown <david.brown@hesbynett.no> - 2015-05-07 10:22 +0200
      Re: Return value for strcat and similar functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-05-07 15:39 -0700
        Re: Return value for strcat and similar functions David Brown <david.brown@hesbynett.no> - 2015-05-08 11:09 +0200
          Re: Return value for strcat and similar functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-05-10 08:44 -0700
            Re: Return value for strcat and similar functions David Brown <david.brown@hesbynett.no> - 2015-05-10 21:30 +0200
              Re: Return value for strcat and similar functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-05-11 20:42 -0700
                Re: Return value for strcat and similar functions David Brown <david.brown@hesbynett.no> - 2015-05-12 09:52 +0200
                Re: Return value for strcat and similar functions Bartc <bc@freeuk.com> - 2015-05-12 13:33 +0100
                Re: Return value for strcat and similar functions Ike Naar <ike@iceland.freeshell.org> - 2015-05-14 20:15 +0000
                Re: Return value for strcat and similar functions David Brown <david.brown@hesbynett.no> - 2015-05-14 23:06 +0200
                Re: Return value for strcat and similar functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-05-20 11:08 -0700
                Re: Return value for strcat and similar functions David Brown <david.brown@hesbynett.no> - 2015-05-21 10:55 +0200
                Re: Return value for strcat and similar functions glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-05-21 13:05 +0000
                Re: Return value for strcat and similar functions David Brown <david.brown@hesbynett.no> - 2015-05-21 20:42 +0200
                Re: Return value for strcat and similar functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-10 10:40 -0700
    Re: Return value for strcat and similar functions raltbos@xs4all.nl (Richard Bos) - 2015-05-08 21:56 +0000
      Re: Return value for strcat and similar functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-05-10 09:07 -0700
  Re: Return value for strcat and similar functions Bartc <bc@freeuk.com> - 2015-05-07 10:11 +0100

csiph-web