Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Sorry, I don't understand what's so hard about this problem? Why not just emit a warning when the compiler exploits undefined behavior to make some line of code unreachable. By "line of code" I mean code that's written by the user, not code after macroexpansions, inlinings or whatever. So the warning would mean that either you have a bug, or you can safely delete some code. Both of these are helpful.


Technically the compiler doesn't exploit the undefined behaviour. It exploits the assumption that it cannot happen and thus it's free to assume everywhere that only defined behaviour happens. Which means, the optimisations are for optimising the defined cases with no regard at all to the undefined behaviour.

You'll notice in a lot of cases that the exploitation of UB looks different for the same cases with different compilers or even compiler versions. This is because the compiler doesn't see »Oh, UB, I can optimise that« but rather »In this case I can do this which remains valid for all defined cases«.

Also, as others have pointed out, even if the compiler would emit a warning, it would be way too much noise because such things happen all the time.


> Also, as others have pointed out, even if the compiler would emit a warning, it would be way too much noise because such things happen all the time.

How so? For example, this code:

    printf("the pointer's value is %d", *p);
    free(p);
would not cause a warning under my proposal, even if free() contains a NULL check. The source code contains no unreachable lines, only the inlined/macroexpanded code does. On the other hand, most "gotcha" examples proposed so far do have unreachable source lines, and would lead to warnings.

Can you give an example of useful code that contains unreachable lines before macroexpansion and inlining? What's wrong with emitting a warning so the programmer can delete the useless line?

> You'll notice in a lot of cases that the exploitation of UB looks different for the same cases with different compilers or even compiler versions.

That's OK. The problem is with each individual compiler deleting code without warning. If compiler X deletes a line of my code, then it should warn me about it. If compiler Y doesn't delete that line, it doesn't have to warn me.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: