News for hackers. Stuff that matters.
Re:Both true and false: a Zen moment with C by haberman on Wednesday June 27, 2012 @11:40AM (#4167527)
It's crazy to me that we as C programmers are flying blind about when we invoke undefined behavior. There is no blinking red light that warns us that something needs to be fixed, only strange and unexplainable behavior, or in some cases things just work fine because the undefined behavior happens to follow our expectations on some combination of platform and compiler.

I keep wishing that there was a Valgrind-like tool that could detect and report undefined behavior. It would have to be a dynamic, runtime tool, since many/most cases of undefined behavior cannot robustly detected statically. But it would need to have more information at runtime about the original C program than Valgrind has; the assembly alone does not contain enough information to know if the source C program is invoking undefined behavior.

I feel certain that if this tool existed, we would find scores of undefined behavior in all but the most conscientious C programs.

Re:Both true and false: a Zen moment with C by Argorak on Wednesday June 27, 2012 @11:52AM (#4167580)
Clang has -fcatch-undefined-behavior, "Turn on runtime code generation to check for undefined behavior.". ( http://clang.llvm.org/docs/UsersManual.html )

Afaik, GCC has some flags for specific types of undefined behavior.

Re:Both true and false: a Zen moment with C by haberman on Wednesday June 27, 2012 @01:22PM (#4167987)
That flag catches four specific instances of undefined behavior, a tiny speck in a sea of all possible ways that undefined behavior can be invoked.
Re:Both true and false: a Zen moment with C by gregholmberg on Wednesday June 27, 2012 @02:24PM (#4168341)
If you are building your project with Clang, and find that your code causes undefined behavior not caught by any existing in-compiler test, and you can reproduce it, you might want to ...

- sign up for the developers' list at http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev ,

- skim over http://clang.llvm.org/hacking.html ,

- build from 'trunk' by carefully following a few simple steps in http://clang.llvm.org/get_started.html ,

- wade in and reproduce your bug ,

... and then share your findings.

Together we can build better tools. Anyone with patience and interest can help.

Re:Both true and false: a Zen moment with C by haberman on Wednesday June 27, 2012 @02:53PM (#4168538)
I think we may have our wires crossed. I'm concerned about undefined behavior in my own programs, not undefined behavior in Clang itself. Someone mentioned Clang as a tool that can help detect UB in other programs, but I mentioned that these checks are far from exhaustive.
Re:Both true and false: a Zen moment with C by gregholmberg on Wednesday June 27, 2012 @03:03PM (#4168611)
Sorry, the fault is mine alone. I used an ambiguous description above, now edited.

The only point I wanted to make was that a collective effort might help cut down on ambiguity in expression that the compiler does not identify. I think we could use "more warnings".

Re:Both true and false: a Zen moment with C by Ralith on Wednesday June 27, 2012 @04:02PM (#4168951)
He means to say you're welcome to submit patches to make them more exhaustive.
Re:Both true and false: a Zen moment with C by aidenn0 on Wednesday June 27, 2012 @12:29PM (#4167756)
Build this code with any reasonable warning setting (gcc's defaults are really minimal) and you will get a warning for this, since it's detectable at compile time. Other undefined behavior is harder to catch at compile time, but clang (and possibly other compilers) have tools to catch at runtime.
Re:Both true and false: a Zen moment with C by brlewis on Wednesday June 27, 2012 @12:39PM (#4167803)
As is mentioned in the comments, in the author's original code that inspired the post, it was an uninitialized struct member. With pointer arithmetic and everything, I would have a really hard time detecting an uninitialized struct member at compile time.
Re:Both true and false: a Zen moment with C by mbrubeck on Wednesday June 27, 2012 @01:14PM (#4167948)
http://embed.cs.utah.edu/ioc/ will detect one common cause of undefined behavior (integer overflow), which is useful and can catch a lot of exploitable security bugs, but of course it's far from complete.
Re:Both true and false: a Zen moment with C by pbiggar on Wednesday June 27, 2012 @07:14PM (#4169686)
I feel like valgrind would catch this particular error though.
Re:Both true and false: a Zen moment with C by gsg on Thursday June 28, 2012 @04:37AM (#4170794)
Re:Both true and false: a Zen moment with C by shasty on Thursday June 28, 2012 @04:50AM (#4170818)
Thats what Java programmers say.

Dont code C if you dont know what you are doing.

Re:Both true and false: a Zen moment with C by haberman on Thursday June 28, 2012 @09:12PM (#4174760)
Only a fool spurns tools out of machismo.

Also, I'm willing to bet that that if you've written any significant amount of C, you have some undefined behavior that you're not aware of. Just because it works doesn't mean it's defined behavior according to the standard.