> >> > > dereference memory location NULL, which will always, always, segfault your
> >> > > system.
> >> > Actually not always, you could make NULL a valid pointer by
> >> > mmaping something on that address. (But that is so rarely
> >> > needed that you shouldn't even think about it, and it will
> >> > also make debugging harder.)
> >> I believe the CPU hardware will always trap NULL accesses. I can't
> >> find anything on it right now, but I remember reading that for IA32
> >> CPUs selector zero is reserved and cannot be used without generating a
> >> fault.
> >Yes, but pointers on i386 Linux only contains the offset
> >not the selector. AFAIK no software on Linux uses selctors
> >except for runing DOS/Windows software. The compiler will
> >not generate code using selectors, by default any Linux
> >program will use the selector set up by the kernel.
> >This program actually works:
> However, if it works it is totally an accident.
How can a working program be an accident?
> You've invoked undefined behavior as far as the C compiler is
> concerned. The reason is because NULL is guaranteed to generate
> a "null pointer", which the C Standard guarantees to be
> *invalid* as a memory address. Using NULL as an address for
> mmap() and or later accessing memory via a NULL pointer results
> in undefined behavior.
It is undefined in C and posibly undefined in Posix as
well. But it is well defined on Linux. This works on any
Linux system, but it is not portable to other systems.
Using the MAP_FIXED flag to mmap is rarely a good idea
and is never portable. And as I already said, you should
not mmap a page on address NULL unless you really need
to, it makes debuging harder.
> That behavior on your particular system might result in what
> appears to be the exact result you expected... but on another
> system it may cause the traditional "format your hard disk" or
> "demons to fly out your nose" actions used to explain what
> "undefined behavior" means.
No, some of that cannot happen. Linux has a security
model so a program runing as a nonpriveleged user
cannot format the hardisk even by doing something
> Even on your system the next upgrade to the compiler may
> result in a program which no longer works the same.
Another compiler should also call the library functions
with the parameters i specify. But of course changing
the libraries might have an effect. mmap and write are
simple wrappers, and you shouldn't expect them to do
anything but passing the arguments to the system call.
But of course somebody might come up with the idea that
sprintf and strlen should detect NULL pointers.
> >#include <stdlib.h>
> >#include <stdio.h>
> >#include <string.h>
> >#include <unistd.h>
> >#include <sys/mman.h>
> >int main()
> > mmap(NULL,getpagesize(),PROT_READ|PROT_WRITE,
> > MAP_FIXED|MAP_PRIVATE|MAP_ANONYMOUS,0,0);
> > sprintf(NULL,"%s %s!\n","Hello","world");
> > write(0,NULL,strlen(NULL));
> > return 0;
> Floyd L. Davidson <http://www.ptialaska.net/~floyd>
Notice: By sending SPAM (UCE/BCE) to this address, you are
accepting and agreeing to our charging a $1000 fee, per
email, for handling and processing, and you agree to pay any
and all costs for collecting this fee.