What is wrong with this program?

Roger Hale roger at ll-sst
Tue Aug 13 02:58:19 AEST 1985


> In article <117 at graffiti.UUCP> bruce at graffiti.UUCP (Bruce Jilek) writes:
> >Why does printf insist that data->ut_line is a null string while
> >putchar can display all of the characters of this array?
> > ...
[ > main() {
  >	struct { char	ut_line[8]; ... } *data; ...	/* in effect */
] > }
> >
> >Sample output:
> >	
> >	tty03                 /*  This shows that data->ut_line isn't null */
> >bruce	(null)	492299400     /*  So what's the problem in this line?      */
> 
> Are you *sure* it really isn't null?  If the first character were \0, your
> 'putchar' loop would give no indication of that fact; the \0 would be
> invisible and the remaining characters would print normally.  Of course,
> 'printf' would consider the string null.
> ...
> AWR

[I tried sending to graffiti!bruce at Berkeley.ARPA, but no luck.]

In fact data->ut_line is a null \emphasis{pointer}, since printf is recognizing
a null string pointer for %s and printing '(null)' instead.  This check is in
doprnt.s in our V7 PDP11 system, though not in 4.2bsd's.

Data->ut_line is null because data is null and ut_line is at offset zero;
data is null by accident because this is your first time at this stack level
and the stack happens to start out zero filled.

If any of this were different it would have printed 'tty03' instead, leaving
no clue that *data had been trashed.  All this is Murphy-dependent; be glad
the symptoms were so clear!

Your servant,
Roger Hale  <roger at ll-sst.arpa>
	    (sorry, no usenet address)



More information about the Comp.lang.c mailing list