Path: csiph.com!feeder.erje.net!2.eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Updating pointer with result of sprintf Date: Tue, 28 Nov 2017 08:58:21 -0800 Organization: None to speak of Lines: 37 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: reader02.eternal-september.org; posting-host="0a617e3b507af251ad64dcde7ced884b"; logging-data="30381"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Rtj2ow0q6sLTpofE3b1Pz" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:ykmm8i+r0zOZSoCOKEh75mpXws4= sha1:jO8vURB8mKHDNPLSNbjm+o/APX8= Xref: csiph.com comp.lang.c:123574 jacobnavia 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 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"