Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 24 Aug 2004 21:03:02 +0200 (CEST)
From:      Harti Brandt <harti@freebsd.org>
To:        Sean McNeil <sean@mcneil.com>
Cc:        current@freebsd.org
Subject:   Re: bsdtar core dumps
Message-ID:  <20040824205417.L517@beagle.kn.op.dlr.de>
In-Reply-To: <1093371600.10464.23.camel@server.mcneil.com>
References:  <1092777586.92327.9.camel@server.mcneil.com>  <20040817213813.GE3827@gothmog.gr><4127841D.6050104@freebsd.org>  <20040824200225.V517@beagle.kn.op.dlr.de> <1093371600.10464.23.camel@server.mcneil.com>

index | next in thread | previous in thread | raw e-mail

On Tue, 24 Aug 2004, Sean McNeil wrote:

SM>On Tue, 2004-08-24 at 11:08, Harti Brandt wrote:
SM>> On Tue, 24 Aug 2004, Sean McNeil wrote:
SM>> 
SM>[snip]
SM>> 
SM>> Sorry to jump in.
SM>> 
SM>> You cannot use a va_list twice. As soon as someone call va_arg() on the
SM>> ap all the aps in the calling functions get invalid. The only thing that 
SM>> can and must be done is that the function that did the va_start() must 
SM>> call va_end.
SM>> 
SM>> If you need it twice you must make a copy as in the patch below.
SM>> But the function call va_copy must also call va_end() on that copy
SM>> (this seems missing in the patch).
SM>
SM>I took the lead from /usr/src/lib/libc/stdio/vfprintf.c
SM>
SM>It happens to do a va_copy(orgap,ap) but never does a va_end(orgap). 
SM>Either vfprintf.c is incorrect or it is not necessary to call va_end().

vfprintf.c is wrong. Paragraph 1 of 7.15.1 of C99 states: "Each invocation 
of the va_start and va_copy macros shall be matched by a corresponding 
invocation of the va_end macro in the same function."

SM>One point I would like to make clear - you _can_ use a va_list twice on
SM>architectures that will pass the va_list argument by value (i.e. i386). 
SM>This makes it easy to produce faulty code for those architectures that
SM>pass by reference and thus have a side-effect.  Since most people here
SM>are developing and testing on x86 machines I would think this problem to
SM>have happened more often than it has.

This depends on what you call _can_. Again citing C99 (paragraph 3 of 
7.15): "The object ap 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." (ap is a va_list 
here).

That is rather clear. I wouldn't call the fact that it incidentally 
happens to work on that concrete compiler/machine implementation an 
argument for 'can be used twice'. Especially in the case if stdarg.h it is 
very helpful to closly follow the standard.

harti


home | help

Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20040824205417.L517>