Skip to content

Commit

Permalink
prevent undefined behaviour at a language level while probing getenv
Browse files Browse the repository at this point in the history
This test in Configure tries to probe for undefined behaviour in
getenv(), but provokes undefined behaviour in C/C++ by falling off
the end of a function with a non-void return type.

Without optimization clang++ generated a ud2 instruction here on
amd64 producing an illegal instruction exception.  With optimization
the test case fell off the end and started re-executing main(),
eventually producing a SIGBUS.

Simply dropping the value of getenv() here and returning NULL wasn't
useful, under -O2 the compiler optimized away the getenv() call,
voiding the whole point of the test.
  • Loading branch information
tonycoz committed Jun 3, 2021
1 parent 9085b4e commit 0f4ef5f
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Configure
Expand Up @@ -14249,7 +14249,7 @@ $cat >try.c <<EOCP
void *
thread_start(void * arg)
{
(void *) getenv("HOME");
return (void *) getenv("HOME");
}

int main() {
Expand Down Expand Up @@ -14280,7 +14280,7 @@ int main() {
exit(2);
}

exit(! strcmp(main_buffer, save_main_buffer) == 0);
exit(! (strcmp(main_buffer, save_main_buffer) == 0));
}
EOCP
val=
Expand Down

0 comments on commit 0f4ef5f

Please sign in to comment.