An Ubiquitous C bug

Dan Swartzendruber dswartz at bigbootay.sw.stratus.com
Tue Jan 22 07:19:52 AEST 1991


In article <2831 at casbah.acns.nwu.edu> hpa at casbah.acns.nwu.edu (Peter Anvin) writes:
:
:  [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.
:

Gee, this sounds familiar!  I remember when the first PDP 11 with separate
I/D space came out and no one had fixed 'ld', so it blithely put the first
initialized data at data offset 0.  Want to guess what happened when some
poor sucker's program took the address of that variable and passed it to
someone who checked for a NULL pointer?  You'd be right.  Kind of strange
that this problem is STILL cropping up 13 years later.

:
:
:-- 
: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


--

Dan S.



More information about the Comp.lang.c mailing list