Path: csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: snprintf alternatives in iso9899 Date: Tue, 08 Dec 2015 09:18:52 -0800 Organization: None to speak of Lines: 45 Message-ID: References: <87lh956li9.fsf@bsb.me.uk> <87fuzc7uq3.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="15435"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/yrZd3roy/r/6to/QwDUqi" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:OYMHffmsCGHGv4COymFfieXfaWw= sha1:R0kgPCfkl6um/6NZZ5V6o+ApxJY= Xref: csiph.com comp.lang.c:78188 ram@zedat.fu-berlin.de (Stefan Ram) writes: > Ben Bacarisse writes: >>Did you ever show it to a compiler? There's no way that code got > > The last version of salfmt was not tested, indeed. > (I think, after I inserted »va_copy«, I did not > test again.) > > But previous versions were tested, and I hope to > find the time to one day maintain that page again. > > If you refer to a specific location in the code, > feel free to point out where you see the problem. www.purl.org/stefan_ram/pub/c_faq_de I haven't tried compiling it myself, but a couple of things jump out. s_type salfmt( s_type const f, ... ) { va_list a; char * b = 0; va_copy(a,f); { int const s = vsnprintf( 0, 0, f, a ); va_end(a); } if( s >= 0 ) { size_t const k = 1 + s; if( b = malloc( k )) { va_start(a,f); { vsprintf( b, f, a ); va_end(a); }}} return b; } The code layout makes it difficult to read. s_type is a typedef for char*. (IMHO the code would be much easier to read if it used char* directly.) You call va_copy with an argument of type char*. That might happen to compile depending on how va_list is defined, but it's clearly wrong. You define `s` inside a block and then refer to it outside the block. (Personally I wouldn't publish code on a web page without at least compiling it first.) -- 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"