how has C bitten you?

der Mouse mouse at mcgill-vision.UUCP
Sat Sep 7 18:16:19 AEST 1985


>> ... so the two procedure calls in
>>	bar(0);
>>	bar((struct frobozz *)0);
>>are very definitely *not* equivalent.
>>	Guy Harris

>Is it also possible to give a NULL-pointer to a procedure as a parameter,
>if for example the procedure would return several values, and we are not
>interested in all of them?
>
>wait(0) works at least here (4.2BSD), but something like this does not:
>
>skip(fd, n)	/* skip n bytes om streams that don't allow lseek() */
>int fd, n;
>{
>	(void)read(fd, 0, n);
>}

     Let me be the 18th of  69 netters (we are a  leaf node so there's a
k-day delay, for some small integer k, between you and us)  to point out
that....

     Wait(0) works  because  the wait code *specifically* checks for a 0
argument.  I believe the code reads something like

	wait(stpointer)
	struct status *stpointer;
	{
	 ....
	 if (stpointer)
	  { *stpointer = ststruct;
	  }
	 ....
	}

     A lot of code (sigvec, for instance) works this way.  However,  for
calls like read(), where the lack  of interest  is a *very*  exceptional
case,  this  check  is  omitted.  Some machines, notably the 68K family,
will  catch  a  zero pointer  because  there's  no memory there.   Some,
notably VAXen,  will not.  However, for  syscalls involving writing into
memory, for most (-z format, see ld(1)) executable files,  attempting to
write into  address 0 will  fault (syscalls return EFAULT, user code get
SEGV errors).

     Read(fd,0,n)  *should* give you  a memory error  (EFAULT returned).
The only case I know  of in which it won't is when  you are running on a
VAX, so there is memory  at address 0  (it's usually the C startup  code
from crt0.o), and the executable file is in the old old old format which
doesn't do sharing of  text segments, so the text segment  is writeable.
In this case, read will happily overwrite the first n bytes  of the text
segment.   Normally (because that *is* the crt0 code, which  doesn't get
reentered), you won't notice unless n is big.
-- 
					der Mouse

{ihnp4,decvax,akgua,etc}!utcsri!mcgill-vision!mouse
philabs!micomvax!musocs!mcgill-vision!mouse

Hacker: One responsible for destroying /
Wizard: One responsible for recovering it afterward



More information about the Comp.lang.c mailing list