Re: Reading from invalid memory
- From: mwojcik@xxxxxxxxxxx (Michael Wojcik)
- Date: 5 Apr 2006 19:49:25 GMT
In article <1144191920.974107.80230@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>, "soccertl" <lambert54@xxxxxxx> writes:
I am trying to do some tests and have found that if I write to invalid
memory I get a segment violation, (expected of course), but if I read
from the same invalid memory location I do not get any errors. I was
wondering why this would be? I can pass a NULL pointer to a function
and read thousands of bytes from it without a problem. Write 1 byte and
it faults.
In your environment there is a read-only page mapped at address 0.
Thus you can read a page's worth of data from address 0, but you
cannot write to it. This is a matter of basic virtual memory
management in typical Unix implementations: a process' address
space is partitioned into pages (often of 4KB), which may be
unmapped or may be mapped with various access rights.
Many (most?) Unix implementations do not map a page at address 0 at
all, so an attempt to read or write there faults. Some map a read-
only page, always or optionally (HP-UX is an example of the latter).
All of these behaviors - and any other behavior - are allowed by the
C standard, since constructing or dereferencing an invalid pointer
value causes undefined behavior. Undefined behavior can be anything,
as far as the language is concerned. It does not need to be
predictable or consistent.
The Unix standards (the now-unified SUS, POSIX, etc) impose
additional constraints on many of the things that the C standard
makes implementation-defined or undefined, but for the most part
they leave dereferencing an invalid pointer undefined. You may get
a signal, if you're lucky. You may not.
(By the way, there is no such thing as a "NULL pointer" in C; there
are "null pointers", and there is NULL, which is an object-style
macro that expands to either an integer constant with value 0, or
such a constant cast to void*; like all such constants, it is a null
pointer constant when used in a pointer context.)
--
Michael Wojcik michael.wojcik@xxxxxxxxxxxxxx
The Utahraptor's been having a bad time here, and I'm to credit! I
wonder how long he'll stay? (I can't wait till he finds out I replaced
his toothpaste with A COMPETING BRAND OF TOOTHPASTE!) -- Ryan North
.
- References:
- Reading from invalid memory
- From: soccertl
- Reading from invalid memory
- Prev by Date: Re: Y2038 and time_t
- Next by Date: Re: Reading from invalid memory
- Previous by thread: Re: Reading from invalid memory
- Next by thread: changing environment variables
- Index(es):
Relevant Pages
|
Loading