Live data from Hacker News

The Following Code Causes Segfault in Clang

llvm.org

21–30 of 40 posts

Re: The Following Code Causes Segfault in Clang

#21

Something tells me C++ isn't the best thing to implement a compiler with.

I modded you up because clang is written in C++ and even if I didn't know this I'd suspect it because segfaults in languages that are not weakly typed (i.e., C and C++) are incredibly rare.

There are better languages to write compilers in. OCaml is one.

Re: The Following Code Causes Segfault in Clang

#22
post #14
post #4

While we're at segfaulting compiler's, here's what I found just a few days ago: python -S -c 'print("void f(){} int main(){return (" + "*"*10**7 + "f)();}")' | gcc -xc - (This is legal C -- look it up. Don't argue with me over the practical relevance of this please)

Sure it's legal, but the bug you are demonstrating is uninteresting. It may not even technically be a violation of the C standard

The error message is unhelpful and requests the user to submit a bug report. I would argue this is not good behavior no matter how absurd the input is.

Re: The Following Code Causes Segfault in Clang

#23
post #16
post #3

Earlier quoted context omitted.

Yeah, maybe this is just a standard-conforming implementation of undefined behavior.

It's nothing to do with standard-conforming or not. A correct implementation shall never crash.

That's a pretty broad and idealistic claim to be making.

"Correctness" itself isn't an absolute. While something either does or does not conform to whatever has been defined as "correct", it's perfectly fine for the defined "correct" behavior in a given situation to be a crash.

In some cases a crash is the best that can be hoped for. Continuing on, even in an attempt to handle the failure more gracefully, can potentially be more harmful than just crashing.

Re: The Following Code Causes Segfault in Clang

#24

Something tells me C++ isn't the best thing to implement a compiler with.

I modded you up because clang is written in C++ and even if I didn't know this I'd suspect it because segfaults in languages that are not weakly typed (i.e., C and C++) are incredibly rare. There are better languages to write compilers in. OCaml is one.

C++ probably isn't 'weakly typed', whatever that means.

Re: The Following Code Causes Segfault in Clang

#25
post #24

Earlier quoted context omitted.

I modded you up because clang is written in C++ and even if I didn't know this I'd suspect it because segfaults in languages that are not weakly typed (i.e., C and C++) are incredibly rare. There are better languages to write compilers in. OCaml is one.

C++ probably isn't 'weakly typed', whatever that means.

You probably aren't "qualified to make that statement", if you don't know what weak typing is and are too lazy to google it.

Re: The Following Code Causes Segfault in Clang

#26
post #4

While we're at segfaulting compiler's, here's what I found just a few days ago: python -S -c 'print("void f(){} int main(){return (" + "*"*10**7 + "f)();}")' | gcc -xc - (This is legal C -- look it up. Don't argue with me over the practical relevance of this please)

This segfaults too, so it's "just" a problem of too many dereferences.

  python -c 'print ("int main(){" + "*" * (10**6) + "}")' | gcc -x c -

Re: The Following Code Causes Segfault in Clang

#27
post #23
post #16

Earlier quoted context omitted.

It's nothing to do with standard-conforming or not. A correct implementation shall never crash.

That's a pretty broad and idealistic claim to be making. "Correctness" itself isn't an absolute. While something either does or does not conform to whatever has been defined as "correct", it's perfectly fine for the defined "correct" behavior in a given situation to be a crash. In some cases a crash is the best that can be hoped for. Continuing on, even in an attempt to handle the failure more gracefully, can potenti…

I think you're confusing a crash in the generated program with a crash in the compiler itself. The latter is what's happening here.

Re: The Following Code Causes Segfault in Clang

#28

Something tells me C++ isn't the best thing to implement a compiler with.

I modded you up because clang is written in C++ and even if I didn't know this I'd suspect it because segfaults in languages that are not weakly typed (i.e., C and C++) are incredibly rare. There are better languages to write compilers in. OCaml is one.

I'd rather say that C++ "fixes" C's unsafe type system by making some of the castings legal, instead of safer :)

Re: The Following Code Causes Segfault in Clang

#29

Something tells me C++ isn't the best thing to implement a compiler with.

This looks to be an assertion failure, i.e. code that was thought to be unreachable is not. So there's no evidence that any of the negatives of C++ (memory safety, etc.) are in play here.

If that's the case I was being too harsh on clang.

Re: The Following Code Causes Segfault in Clang

#30
post #4

While we're at segfaulting compiler's, here's what I found just a few days ago: python -S -c 'print("void f(){} int main(){return (" + "*"*10**7 + "f)();}")' | gcc -xc - (This is legal C -- look it up. Don't argue with me over the practical relevance of this please)

Depending on available memory, attempting to compile an expression with ridiculously large numbers of nested parentheses (e.g. "return (((...(42)...)));" will also do the trick.

I don't expect a compiler to be able to compile programs that exceed its internal limits, but a better error message would be much appreciated, instead of a hard crash. For comparison, with MSVC the parent's code produces

    fatal error C1026: parser stack overflow, program too complex
and the nested parentheses results in

    fatal error C1013: compiler limit : too many open parentheses
Post reply on HN