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


Groups > comp.lang.c > #141755

Re: Safer and more convenient strcpy()

Message-ID <4h66lf-lpm.ln1@wilbur.25thandClement.com> (permalink)
From William Ahern <william@25thandClement.com>
Subject Re: Safer and more convenient strcpy()
Newsgroups comp.lang.c
References <0c54f650-7ece-4120-9ca4-c59972d8971a@googlegroups.com> <6e60c0ba-bc6e-4be9-893e-de64f538c495@googlegroups.com> <89a3104f-6b3b-42c7-9571-2969d25ff248@googlegroups.com> <1fd74636-5a97-4a51-b99f-d2966d42e09a@googlegroups.com> <6f3e97e8-9070-4ea3-9ed8-6b668c42b70c@googlegroups.com>
Date 2019-03-06 16:51 -0800

Show all headers | View raw


Howard Chu <highlandsun@gmail.com> wrote:
> On Wednesday, March 6, 2019 at 1:19:15 PM UTC, I Set My Girlfriend's Pantyhose on Fire wrote:
>> On Wednesday, March 6, 2019 at 4:31:28 AM UTC-8, Howard Chu wrote:
>> > On Wednesday, March 6, 2019 at 11:58:29 AM UTC, Malcolm McLean wrote:
>> > > On Wednesday, 6 March 2019 11:45:24 UTC, Howard Chu  wrote:
>> > > > I was recently reminded of an old rant of mine, which reminded me of the even older discussion about better versions of strcpy().
>> > > > 
>> > > > https://www.openldap.org/lists/openldap-software/200303/msg00560.html
>> > > > 
>> > > > The function I advocated there eventually became more common, known as
>> > > > stpcpy(), but it's still not a safer function. It's easy to write a safer
>> > > > function that's still convenient to use though, like so:
>> > > > 
>> > > > char *stecpy(char *dst, const char *src, const char *end)
>> > > > {
>> > > >   while (*src && dst < end)
>> > > >    *dst++ = *src++;
>> > > >   if (dst < end)
>> > > >    *dst = '\0';
>> > > >   return dst;
>> > > > }
>> > > > 
>> > > If the buffer is too small to fit the string, it is not nul-terminated.
>> > > So you'll likely get a crash when you try to use the string. Better
>> > > to lose a character and nul-terminate.
>> > 
>> > I suppose. Easily fixed:
>> > 
>> > char *stecpy(char *dst, const char *src, const char *end)
>> > {
>> >   end--;
>> >   while (*src && dst < end)
>> >    *dst++ = *src++;
>> >   *dst = '\0';
>> >   return dst;
>> > }
>> 
>> 
>> Wouldn't that fix introduce memory overflow error? Just curious because this looks like something similar that I encountered at my job recently.
> 
> Look again. No.

Yes, it does, if end - dst == 0:

  char buf[100], *p = buf, *pe = &buf[sizeof buf];
  ... // series of operations that increments p to pe
  stecpy(p, src, pe);

Also, end-- invokes undefined behavior if end is the start of an array, such
as with:

  char buf[100];
  size_t limit = 1000;
  ... // series of operations that decrements limit to 0
  stecpy(buf, src, &buf[MIN(sizeof buf, limit)]);

Both situations are rather common, IME.

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


Thread

Safer and more convenient strcpy() Howard Chu <highlandsun@gmail.com> - 2019-03-06 03:45 -0800
  Re: Safer and more convenient strcpy() Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-03-06 03:58 -0800
    Re: Safer and more convenient strcpy() Howard Chu <highlandsun@gmail.com> - 2019-03-06 04:31 -0800
      Re: Safer and more convenient strcpy() "I Set My Girlfriend's Pantyhose on Fire" <cdalten@gmail.com> - 2019-03-06 05:18 -0800
        Re: Safer and more convenient strcpy() Howard Chu <highlandsun@gmail.com> - 2019-03-06 13:03 -0800
          Re: Safer and more convenient strcpy() William Ahern <william@25thandClement.com> - 2019-03-06 16:51 -0800
            Re: Safer and more convenient strcpy() Howard Chu <highlandsun@gmail.com> - 2019-03-07 07:40 -0800
              Re: Safer and more convenient strcpy() scott@slp53.sl.home (Scott Lurndal) - 2019-03-07 16:12 +0000
                Re: Safer and more convenient strcpy() Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-03-07 08:28 -0800
                Re: Safer and more convenient strcpy() scott@slp53.sl.home (Scott Lurndal) - 2019-03-07 16:51 +0000
                Re: Safer and more convenient strcpy() Howard Chu <highlandsun@gmail.com> - 2019-03-07 09:31 -0800
                Re: Safer and more convenient strcpy() Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-03-07 11:17 -0800
                Re: Safer and more convenient strcpy() Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-03-07 22:35 +0000
                Re: Safer and more convenient strcpy() Howard Chu <highlandsun@gmail.com> - 2019-03-07 15:32 -0800
                Re: Safer and more convenient strcpy() scott@slp53.sl.home (Scott Lurndal) - 2019-03-08 14:29 +0000
                Re: Safer and more convenient strcpy() Howard Chu <highlandsun@gmail.com> - 2019-03-08 07:09 -0800
                Re: Safer and more convenient strcpy() Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-03-08 15:39 +0000
                Re: Safer and more convenient strcpy() Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-03-08 07:56 -0800
                Re: Safer and more convenient strcpy() Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-03-09 08:12 +0000
                Re: Safer and more convenient strcpy() William Ahern <william@25thandClement.com> - 2019-03-07 15:51 -0800
                Re: Safer and more convenient strcpy() William Ahern <william@25thandClement.com> - 2019-03-07 16:38 -0800
                Re: Safer and more convenient strcpy() Keith Thompson <kst-u@mib.org> - 2019-03-07 18:32 -0800
                Re: Safer and more convenient strcpy() William Ahern <william@25thandClement.com> - 2019-03-07 20:00 -0800
                Re: Safer and more convenient strcpy() Howard Chu <highlandsun@gmail.com> - 2019-03-08 04:15 -0800
            Re: Safer and more convenient strcpy() "I Set My Girlfriend's Pantyhose on Fire" <cdalten@gmail.com> - 2019-03-07 12:15 -0800
      Re: Safer and more convenient strcpy() Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-03-06 09:12 -0800
  Re: Safer and more convenient strcpy() mark.bluemel@gmail.com - 2019-03-06 08:27 -0800
  Re: Safer and more convenient strcpy() Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-03-06 09:22 -0800
  Re: Safer and more convenient strcpy() Rosario19 <Ros@invalid.invalid> - 2019-03-07 09:10 +0100
    Re: Safer and more convenient strcpy() Rosario19 <Ros@invalid.invalid> - 2019-03-07 09:12 +0100
      Re: Safer and more convenient strcpy() mark.bluemel@gmail.com - 2019-03-07 00:55 -0800
  Re: Safer and more convenient strcpy() William Ahern <william@25thandClement.com> - 2019-03-07 16:09 -0800
    Re: Safer and more convenient strcpy() Howard Chu <highlandsun@gmail.com> - 2019-03-08 04:04 -0800
    Re: Safer and more convenient strcpy() eric.sanchis@iut-rodez.fr - 2019-04-15 04:40 -0700
      Re: Safer and more convenient strcpy() nobody@example.org (Scott) - 2019-04-16 03:33 +0000
        Re: Safer and more convenient strcpy() eric.sanchis@iut-rodez.fr - 2019-04-16 05:35 -0700
          Re: Safer and more convenient strcpy() nobody@example.org (Scott) - 2019-04-17 01:19 +0000
            Re: Safer and more convenient strcpy() eric.sanchis@iut-rodez.fr - 2019-04-17 05:35 -0700
              Re: Safer and more convenient strcpy() nobody@example.org (Scott) - 2019-04-17 16:59 +0000
                Re: Safer and more convenient strcpy() eric.sanchis@iut-rodez.fr - 2019-04-17 23:40 -0700
  Re: Safer and more convenient strcpy() Philipp Klaus Krause <pkk@spth.de> - 2019-03-20 11:10 +0100
    Re: Safer and more convenient strcpy() Philipp Klaus Krause <pkk@spth.de> - 2019-03-20 11:13 +0100

csiph-web