Newsgroups: comp.os.minix
Subject: Paternalistic null pointer code?
Organization: Syracuse University, Syracuse
From: mcconnel@hydra.syr.edu (Terry R. McConnell)
NNTP-Posting-Host: hydra.syr.edu
Message-ID: <3b0a8365.0@news.syr.edu>
Date: 22 May 2001 11:19:01 -0500
X-Trace: 22 May 2001 11:19:01 -0500, hydra.syr.edu
Lines: 43
Path: news.adfa.edu.au!clarion.carno.net.au!news0.optus.net.au!news1.optus.net.au!optus!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news.syr.edu!hydra.syr.edu!mcconnel
Xref: news.adfa.edu.au comp.os.minix:37417

I've been working on porting lcc and the GNU assembler and linker to Minix
2.0+ and have finally gotten to the point where I can correctly generate
"hello world." Strangely, though, the following version wouldn't work:

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

char buf[] = "hello world\n";

int
main(void){

	printf("%s",buf);
	return 0;
}

This prints "(null)" instead of hello world. If buf is allocated on the
stack it works correctly, and the statically allocated version works correctly
if I use putc directly rather than printf.

Huh?!

After much head-scratching, I finally found the problem, and it is rather
instructive. I've set things up to always generate separate I and D programs
and the linker to generate addresses for the data segment starting from offset
zero. Thus buf in the program above generates a 0 pointer since it is at the
very beginning of the data segment. Unfortunately, there is a check for zero
pointers in the doprint.c code that handles the %s format. If a null pointer
is passed it gets converted into a pointer to the static string "(null)".

I guess that code is in there to prevent students from crashing their
machines all the time. Is that right?

I always thought the C philosophy was to give the users enough rope to hang
themselves. I'd be interested to hear the designers comments on this 
philosopy. I'd also be interested to know how ACK gets around this problem.


-- 
************************************************************************
Terry R. McConnell   Mathematics/304B Carnegie/Syracuse, N.Y. 13244-1150
trmcconn@syr.edu     http://barnyard.syr.edu/~tmc    Question Authority?
************************************************************************
