An Ubiquitous C bug

Peter Anvin hpa at casbah.acns.nwu.edu
Tue Jan 22 05:30:23 AEST 1991


  [Program comparing a function pointer to NULL deleted]
>>This sort of program CAN print the message if x() happens to be
>>loaded by the linker at the start of the code segment (address 0).
>
>According to section 3.2.2.3:  "An integral constant expression with the
>value 0, or such an expression cast to type void *, is called a null pointer
>constant.  If a null pointer constant is assigned to or compared for equality
>to a pointer, the constant is converted to a pointer of that type.  Such a
>pointer, called a null pointer, is guaranteed to compare unequal to a pointer
>to any object type or function."
>Every single DOS compiler that you can find is non standard-conforming.

All DOS compilers I know of use, in small-code models, the beginning of the
code segment to store their startup code.  Thus, functions cannot be
allocated to CS:0000h, and thus no functions correspond to NULL.  The
startup cod, which is written in assembler, sets up the stack, local heap,
floating point emulation and the arguments to main(), as appropriate.
main() just acts like a subroutine to the startup code.

Should NULL be all ones?  Performance issues aside, such a machine would
only need to subtract one when converting an int to a pointer, and add one
the other way.  In constant expressions, such as when using the macro NULL,
that can of course be done at compile time.



-- 
H. Peter Anvin +++ A Strange Stranger +++ N9ITP/SM4TKN +++
INTERNET:  hpa at casbah.acns.nwu.edu   FIDONET:  1:115/989.4
BITNET:    HPA at NUACC                 RBBSNET:  8:970/101.4



More information about the Comp.lang.c mailing list