[phobos] enforce() vs. assert() for range primitives

Michel Fortin michel.fortin at michelf.com
Sun Aug 22 14:28:51 PDT 2010


Le 2010-08-22 à 17:07, Walter Bright a écrit :

> Sean Kelly wrote:
>> I think it depends on how the compiler dereferences fields of an object. GCC on OSX if I have a null ptr to a struct and I try to access a field through it I'll get the data at 0x4 or wherever and the code will keep on running.  Actual segfaults happen depressingly rarely. 
> 
> On Windows, the entire first 64K of address space is reserved by the OS, and any attempt to access it results in a seg fault. I'm very surprised that OS X would not do the same.

A quick test with DMD on OS X reveals that reading anything below the first 4K of the address space causes a segfault.

For instance, reading member b of a null pointer to this struct will not cause a segfault:

	struct S {
		ubyte[4*1024] a;
		ubyte b;
	}

	S* s = null;
	ubyte c;

	@safe void main() { 
		c = s.b;
	}

Remove one byte from array a and you have a segfault.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/





More information about the phobos mailing list