🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #118

Re: can I call vprintf() twice in a row?

#118
From: Chris Torek
Date: Sat, 25 Jul 1992 06:13
45 lines
2007 bytes
In article <ZJ2FoB1w165w@tsoft.sf-bay.org> bbs.dennis@tsoft.sf-bay.org
(Dennis Yelle) writes:
>    Is the following legal ansi c?
>    I beleave that it is, but if not, I would like to know why not.

>#include <stdarg.h>
>#include <stdio.h>
>
>void report( char *fmt, ...)
>{
>    va_list arg_ptr;
>    
>    va_start( arg_ptr, fmt);
>    vprintf( fmt, arg_ptr);
>    vprintf( fmt, arg_ptr);  /* Yes, we want to print it twice.     */
>                             /* In the original program, the second */
>                             /* was vfprintf to a log file.         */
>    va_end( arg_ptr);
>}

X3.159-1989, section 4.8, p. 123, ll. 13--15:

    The object |ap| [equivalently, |arg_ptr| above] may be passed as
    an argument to another function; if that function invokes the |va_arg|
    macro with parameter |ap|, the value of |ap| in the calling function
    is indeterminate and shall be passed to the |va_end| macro prior to
    any further reference to |ap|.

Since |vprintf| certainly uses |va_arg| on |arg_ptr|, the code quoted
above violates this `shall' (which, note, is outside a `constraints'
section, so that this violation does not require a diagnostic).

There is a trivial fix for the report() function:

	va_start(arg_ptr, fmt);
	vprintf(fmt, arg_ptr);
	va_end(arg_ptr);
	va_start(arg_ptr, fmt);
	vprintf(fmt, arg_ptr);
	va_end(arg_ptr);

will do the trick.
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
Berkeley, CA		Domain:	torek@ee.lbl.gov
Technical Details
Message-ID: <24888@dog.ee.lbl.gov>
Path: arc1-ca.pugleaf.net!sparky!uunet!sun-barr!ames!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
References: <ZJ2FoB1w165w@tsoft.sf-bay.org>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!sun-barr!ames!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
From: torek@horse.ee.lbl.gov (Chris Torek)
Newsgroups: comp.std.c
Subject: Re: can I call vprintf() twice in a row?
Date: 25 Jul 1992 06:13:45 GMT
Organization: Lawrence Berkeley Laboratory, Berkeley
Lines: 45
Message-ID: <24888@dog.ee.lbl.gov>
References: <ZJ2FoB1w165w@tsoft.sf-bay.org>
Reply-To: torek@horse.ee.lbl.gov (Chris Torek)
NNTP-Posting-Host: 128.3.112.15