News for hackers. Stuff that matters.
Re:Xcode 4 ships a buggy compiler by wtallis on Friday December 30, 2011 @09:59PM (#3409971)
Files have metadata, and exist in an environment where the OS ensures that appending to a file doesn't overwrite another file. Neither of those apply to pointers in C.
Re:Xcode 4 ships a buggy compiler by FooBarWidget on Friday December 30, 2011 @10:07PM (#3409985)
Not sure why you mention overwriting. If you allocate a memory block of 0 bytes then it would seem logical to me that you can't write anything to it. NULL would also satisfy that definition, but alloca(0) is supposed to return a region of 0 bytes at the end of the stack... meaning a pointer to the end of the stack.
Re:Xcode 4 ships a buggy compiler by mikeash on Friday December 30, 2011 @10:12PM (#3409993)
Everything in C operates according to the "as if" rule. In other words, the resulting program merely needs to behave as if the result was executed according to the spec. How execution happens is left entirely up to the compiler.

C also doesn't define "the stack" in any way. And of course alloca() isn't part of C at all, but on the systems where it exists, it's not very specific about just where it allocates stuff, just that it's on that nebulous "the stack".

Given the above, I believe you cannot write a conforming program that can detect the difference between alloca(0) returning a region of 0 bytes at the end of the stack and alloca(0) returning something else. Since no conforming program can detect it, the "as if" rule allows the return value to be considered to be anything at all.

Re:Xcode 4 ships a buggy compiler by asomiv on Friday December 30, 2011 @10:25PM (#3410027)
The program in question is not a conforming program in the first place. It's the Ruby interpreter and it does all kinds of low-level stuff. alloca(0) is called from the garbage collector in order to detect the end of the stack so that the garbage collector can scan the stack for pointers. The code assumes that it's running on a system where there is a stack at all, which is pretty much all systems nowadays.
Re:Xcode 4 ships a buggy compiler by mikeash on Friday December 30, 2011 @10:29PM (#3410035)
Of course it's not a conforming program. That's rather the point: as a non-conforming program, the compiler is allowed to apply optimizations which may behave differently from what the programmer wants it to do. That this code works on one compiler and fails on another doesn't make it a compiler bug, though. It merely means that this code relies on the compiler behaving in a certain way which isn't actually mandated.
Re:Xcode 4 ships a buggy compiler by xyzzyz on Friday December 30, 2011 @10:31PM (#3410045)
Pointers you get from malloc() usually have metadata alongside too, you're just not supposed to read it.
Re:Xcode 4 ships a buggy compiler by mikeash on Friday December 30, 2011 @10:55PM (#3410092)
On many systems, malloc(0) returns NULL. I recall one man page describing it as "a stupid answer to a stupid question" or some such.
Re:Xcode 4 ships a buggy compiler by tptacek on Friday December 30, 2011 @11:03PM (#3410113)
Which, it's not remotely a stupid question: malloc gets passed a zero-length argument when it's used to allocate variable length data. That malloc(0) works and is handled by free() without crashing the program is a simplifying assumption, as is the assumption that free(NULL) won't corrupt the heap.

Any use of alloca() on the other hand seems risky. Similar arguments could be made about the semantics of jmp_bufs, which also get used to get a handle on the stack.

Re:Xcode 4 ships a buggy compiler by mikeash on Saturday December 31, 2011 @10:14AM (#3410995)
"the assumption that free(NULL) won't corrupt the heap."

That's not an assumption, that's how the free() function is defined to work by the language standard. It never ceases to astound me how many otherwise good C programmers think free(NULL) is an error.

Re:Xcode 4 ships a buggy compiler by tptacek on Saturday December 31, 2011 @10:58AM (#3411106)
... yes, that's what I'm saying.
Re:Xcode 4 ships a buggy compiler by mikeash on Saturday December 31, 2011 @11:04AM (#3411116)
I see. The word "assumption" confused me, as in context it would generally mean something that isn't necessarily true.
Re:Xcode 4 ships a buggy compiler by nitrogen on Saturday December 31, 2011 @03:45AM (#3410500)
as is the assumption that free(NULL) won't corrupt the heap.

When you refer to simplifying assumptions, are you talking about assumptions made by the programmer, or by the compiler and libc? For example, the POSIX manual page[0] for free( void* ptr ) says, "If ptr is a null pointer, no action shall occur." The malloc manpage says, "If size is 0, either a null pointer or a unique pointer that can successfully passed to free() shall be returned." That sounds more like a definition than an assumption to me. What am I missing?

[0] Obtained by installing manpages-posix-dev on Ubuntu and running man 3posix free.