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


Groups > comp.lang.c > #123577

Re: Updating pointer with result of sprintf

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Updating pointer with result of sprintf
Date 2017-11-28 09:24 -0800
Organization None to speak of
Message-ID <ln609uz57j.fsf@kst-u.example.com> (permalink)
References <ovjd76$sbt$1@dont-email.me> <87o9nmmzet.fsf@bsb.me.uk> <BeLYmcPHr4lIOPfbU99JinrP8q1vC@bongo-ra.co>

Show all headers | View raw


Spiros Bousbouras <spibou@gmail.com> writes:
> On Tue, 28 Nov 2017 11:10:50 +0000
> Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
>> Noob <root@127.0.0.1> writes:
>> 
>> > Consider the following code:
>> >
>> >   unsigned char src[32]; /* initialized somewhere */
>> >   char buf[65];
>> >
>> >   for (i = 0; i < sizeof src; ++i)
>> >     buf += sprintf(buf, "%02x", src[i]);
>> >
>> > I think the above code has well-defined behavior,
>> 
>> That should not compile because you can't assign to an array (buf).  I
>> think you have miss-transcribed the code.  You probably have a pointer
>> to the buffer like this:
>> 
>>   char buf[sizeof src + 1], *bp = buf;
>
> The size expression should be  2 * (sizeof src) + 1 .
>
>>   for (size_t i = 0; i < sizeof src; i++)
>>       bp += sprintf(bp, "%02x", src[i]);
>> 
>> Note that you can remove the mysterious 65 from the code.
>> 
>> > but I have
>> > a small doubt about modifying buf which is also an argument
>> > to sprintf. IIUC, there is a sequence point *after* the function
>> > call, so all should be well?
>> 
>> There are sequence points before and after a function call.  Mind you,
>> given the nature of the code you could just write
>> 
>>   sprintf(buf + 2*i, "%02x", src[i]);
>> 
>> That implicitly assumes that 2 bytes get written per call but then so
>> does the size calculation for buf.
>
> And such an assumption is  *** dangerous *** .There's no guarantee that
> the maximum value for  src[i]  is  0xFF .The  %02x  directive does not
> truncate the output , if it needs more than 2 characters to write the
> full number then it will write more than 2 ; in which case the writing
> will proceed beyond the end of the buffer and SHTF. So either the code
> should use  snprintf()  or be written as

src[i] is of type unsigned char.  If CHAR_BIT==8, then its value
cannot exceed 0xFF.  The value is promoted to type int, but the
value will be non-negative so using "%02x", (which expects an
argument of type unsigned int) should be ok.

I might cast the argument to unsigned:
    sprintf(buf + 2*i, "%02x", (unsigned) src[i]);
just so I don't have to think about implicit promotion.

If CHAR_BIT>8, then there is a possibility of undefined behavior.
I might consider adding:

    #if CHAR_BIT != 8
    #error "..."
    #endif

(The code I work on at $JOB will almost certainly never run on
anything with CHAR_BIT > 8, so in that context I wouldn't bother.)

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

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


Thread

Updating pointer with result of sprintf Noob <root@127.0.0.1> - 2017-11-28 11:19 +0100
  Re: Updating pointer with result of sprintf bartc <bc@freeuk.com> - 2017-11-28 11:02 +0000
  Re: Updating pointer with result of sprintf jacobnavia <jacob@jacob.remcomp.fr> - 2017-11-28 12:09 +0100
    Re: Updating pointer with result of sprintf Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-28 11:23 +0000
    Re: Updating pointer with result of sprintf Keith Thompson <kst-u@mib.org> - 2017-11-28 08:58 -0800
  Re: Updating pointer with result of sprintf Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-28 11:10 +0000
    Re: Updating pointer with result of sprintf Spiros Bousbouras <spibou@gmail.com> - 2017-11-28 16:51 +0000
      Re: Updating pointer with result of sprintf Keith Thompson <kst-u@mib.org> - 2017-11-28 09:24 -0800
      Re: Updating pointer with result of sprintf Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-29 00:33 +0000
        Re: Updating pointer with result of sprintf Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-29 00:36 +0000
        Re: Updating pointer with result of sprintf Spiros Bousbouras <spibou@gmail.com> - 2017-11-29 19:04 +0000
  Re: Updating pointer with result of sprintf "Pascal J. Bourguignon" <pjb@informatimago.com> - 2017-11-28 12:47 +0100
    Re: Updating pointer with result of sprintf "Pascal J. Bourguignon" <pjb@informatimago.com> - 2017-11-28 12:49 +0100
  Re: Updating pointer with result of sprintf scott@slp53.sl.home (Scott Lurndal) - 2017-11-28 14:19 +0000
    Re: Updating pointer with result of sprintf gazelle@shell.xmission.com (Kenny McCormack) - 2017-11-28 14:27 +0000
    Re: Updating pointer with result of sprintf Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-28 14:35 +0000
  Re: Updating pointer with result of sprintf "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-11-28 06:31 -0800
    Re: Updating pointer with result of sprintf "Pascal J. Bourguignon" <pjb@informatimago.com> - 2017-11-28 15:51 +0100
      Re: Updating pointer with result of sprintf "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-11-28 07:23 -0800
        Re: Updating pointer with result of sprintf "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-11-28 08:16 -0800
          Re: Updating pointer with result of sprintf Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-28 16:32 +0000
            Re: Updating pointer with result of sprintf "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-11-28 08:57 -0800
            Re: Updating pointer with result of sprintf "Pascal J. Bourguignon" <pjb@informatimago.com> - 2017-11-29 00:47 +0100
          Re: Updating pointer with result of sprintf "Pascal J. Bourguignon" <pjb@informatimago.com> - 2017-11-29 00:45 +0100
      Re: Updating pointer with result of sprintf Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-28 16:22 +0000
        Re: Updating pointer with result of sprintf Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-11-28 16:25 +0000
        Re: Updating pointer with result of sprintf "Pascal J. Bourguignon" <pjb@informatimago.com> - 2017-11-29 00:48 +0100
  Re: Updating pointer with result of sprintf Noob <root@127.0.0.1> - 2017-11-29 10:07 +0100
    Re: Updating pointer with result of sprintf Barry Schwarz <schwarzb@dqel.com> - 2017-11-29 09:18 -0800

csiph-web