Re: bsdtar core dumps

From: Harti Brandt <harti_at_freebsd.org>
Date: Tue, 24 Aug 2004 21:03:02 +0200 (CEST)
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
Received on Tue Aug 24 2004 - 17:03:08 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:38:08 UTC