Re: Reading from invalid memory




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
.



Relevant Pages

  • Re: Controlling Static Data and Memory Organization...If Possible...
    ... resides invokes the wrath of undefined behavior because you are ... different pages of memory, and for the physical location of those pages ... to do with what "exactly" a pointer is physically in your program ... character at an address that is 1 higher than the one before it. ...
    (comp.lang.c)
  • Re: interpreting a null pointer as an empty (null string)
    ... there requires doing something that has undefined behavior (or, at least, ... which is the usual destination of the null pointer. ... Looks like this is explicitly disallowed by the Standard, ... null pointers being "invalid" as operands of the indirection operator; ...
    (comp.lang.c)
  • Re: How to know the memory pointed by a ptr is freed?
    ... The pointer might be loaded in to an address register causing ... In most real-world implementations, you can successfully read both the ... It's undefined behavior, ... An address is an address, memory is memory, ...
    (comp.lang.c)
  • Re: How to know the memory pointed by a ptr is freed?
    ... > contents of ptr and the memory pointed to by ptr after a call to ... It's undefined behavior, ... An address is an address, memory is memory, ... a reference to just the pointer value is ...
    (comp.lang.c)
  • Re: Malloc/Free - freeing memory allocated by malloc
    ... >> Both the following lines would invoke undefined behavior. ... The memory allocated by x is on the function stack, ... The value of a pointer that refers to freed space is indeterminate. ...
    (comp.lang.c)

Loading