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


Groups > comp.lang.c > #123574

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 08:58 -0800
Organization None to speak of
Message-ID <lnd142z6fm.fsf@kst-u.example.com> (permalink)
References <ovjd76$sbt$1@dont-email.me> <ovjg4f$gcn$1@dont-email.me>

Show all headers | View raw


jacobnavia <jacob@jacob.remcomp.fr> writes:
> Le 28/11/2017 à 11:19, Noob a écrit :
>> buf += sprintf(buf, "%02x", src[i]);
>
> If sprintf fails, it usually returns -1 (EOF)

N1570 7.21.6.6p3:
    The sprintf function returns the number of characters written
    in the array, not counting the terminating null character,
    or a negative value if an encoding error occurred.

Don't assume that the error return is EOF.

> Why would it fail?
>
> Memory constraints, being unable to allocate some temporary space, for 
> instance.

The only documented failure condition for sprintf is an encoding error
(7.21.3p14).  In this particular case, you can be sure that an encoding
error cannot occur.  Any other failure would be the result of undefined
behavior.  It *might* be worth checking that the value returned by
sprintf() is non-negative, just in case the implementation handles other
errors cleanly, but if such an error occurs then something has gone
seriously wrong.

> That is very unlikely, but it *could* happen. In that case your pointer
> loses the last character written or it becomes invalid (supposing buf-1 
> is outside the bounds of buf).

Or buf-N for any arbitrary N.

-- 
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