[tlbuild] [tlbuilders] NULL pointers in printf() calls

Nelson H. F. Beebe beebe at math.utah.edu
Sat Aug 16 00:45:11 CEST 2014


Most Unix developers have encountered software that worked on one
platform, and then failed with a segment violation on another, when a
NULL pointer was matched to a printf() item.  Traffic today on that
topic on the Gnupg-devel mailing list

	http://lists.gnupg.org/mailman/listinfo/gnupg-devel

pointed me to this site:

	http://docs.oracle.com/cd/E19253-01/816-5165/ld.so.1-1/index.html

The very bottom of that page contains this description:

	The user compatibility library /usr/lib/0 at 0.so.1 provides a
	mechanism that establishes a value of 0 at location 0. Some
	applications exist that erroneously assume a null character
	pointer should be treated the same as a pointer to a null
	string. A segmentation violation occurs in these applications
	when a null character pointer is accessed. If this library is
	added to such an application at runtime using LD_PRELOAD, the
	library provides an environment that is sympathetic to this
	errant behavior. However, the user compatibility library is
	intended neither to enable the generation of such
	applications, nor to endorse this particular programming
	practice.

Even though I've used Sun SunOS, and later, Solaris systems for about
three decades, I confess that I was unaware of the 0 at 0 library.  It
might have been helpful at times in getting around the default
behavior of issuing a segment violation on dereferencing a NULL
pointer.

Here is a test program, with embedded comments that report its
results on a Solaris 10 SPARC system:

% cat deref-null.c
/***********************************************************************
Test the Solaris -l0 at 0 library for hiding NULL-pointer dereferencing:
Usage:

	## expect core dump: 
	% cc deref-null.c && ./a.out
	This should produce text: Now is the time...
	Segmentation fault

	## Try the usual way:
	% cc deref-null.c -l0 at 0 && ./a.out
	ld: fatal: library -l0 at 0: not found
	ld: fatal: File processing errors. No output written to a.out

	## Retry, and expect normal output	
	% cc deref-null.c /usr/lib/0 at 0.so.1 && ./a.out
	This should produce text: Now is the time...
	This MIGHT produce garbage text: 

[15-Aug-2014]
***********************************************************************/

#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
    (void)printf("This should produce text: %s\n", "Now is the time...");
    (void)printf("This MIGHT produce garbage text: %s\n", (char *)NULL);

    return (EXIT_SUCCESS);
}

-------------------------------------------------------------------------------
- Nelson H. F. Beebe                    Tel: +1 801 581 5254                  -
- University of Utah                    FAX: +1 801 581 4148                  -
- Department of Mathematics, 110 LCB    Internet e-mail: beebe at math.utah.edu  -
- 155 S 1400 E RM 233                       beebe at acm.org  beebe at computer.org -
- Salt Lake City, UT 84112-0090, USA    URL: http://www.math.utah.edu/~beebe/ -
-------------------------------------------------------------------------------


More information about the tlbuild mailing list