tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: must use gcc builtin?



On Tue, 21 Feb 2012, James K. Lowden wrote:
Just for fun, I thought I'd try compiling Gary Capell's "wily" editor. The code is from 2006, hardly ancient. Its library manipulates a va_list as pointer using assignment and subtraction.

I believe that the definition of "undefined behavior" in section 3.4.3 of the C99 standard applies:

     behavior, upon use of a nonportable or erroneous
     program construct or of erroneous data, for which this
     International Standard imposes no requirements".

I believe that using a va_list as if it were a pointer, and doing arithmetic on it, is something on which the Standard imposes no requirements, and that the resulting behaviour is therefore undefined.

Section 4 paragraph 2 is also pretty clear that "the omission of any explicit definition of behaviour" also counts as undefined behaviour. So it's not necessary for the Standard to say "if you do <X> then the behaviour is undefined"; if the Standard merely does not talk about <X> at all, the the behaviour if you do <X> is undefined.

I can't #undef __GNUC__. Do we really want to require the use of builtins? I'd like a knob to compile idiomatic C, especially *good* idiomatic C.

In my world, code that invokes undefined behaviour is not good idiomatic C.

http://pubs.opengroup.org/onlinepubs/009604599/basedefs/stdarg.h.html

does not define a type for va_list. Assuming it's a pointer might be nonportable, but it's not prohibited by the standard.

The standard hardly ever prohibits a program from doing something that's undefined.

--apb (Alan Barrett)


Home | Main Index | Thread Index | Old Index