https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104690
Bug ID: 104690
Summary: UBSan does not detect undefined behavior on function
without a specified return value
Product: gcc
Version: 12.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: sanitizer
Assignee: unassigned at gcc dot gnu.org
Reporter: vincent-gcc at vinc17 dot net
CC: dodji at gcc dot gnu.org, dvyukov at gcc dot gnu.org,
jakub at gcc dot gnu.org, kcc at gcc dot gnu.org, marxin at
gcc dot gnu.org
Target Milestone: ---
Consider the following C code:
#include <stdio.h>
static int f (void) { }
int main (void)
{
printf ("%d\n", f ());
return 0;
}
According to ISO C17 6.9.1p12, the behavior is undefined: "If the } that
terminates a function is reached, and the value of the function call is used by
the caller, the behavior is undefined."
I don't know what "used by the caller" means exactly, but in the above code,
the value is clearly used, since it is printed. However, when one compiles it
with "gcc -std=c17 -fsanitize=undefined" (with or without -O), running the code
does not trigger an error. (Well, I hope that UBSan doesn't think that the
value isn't necessarily used because the printf may fail before printing the
value.)
Tested with gcc-12 (Debian 12-20220222-1) 12.0.1 20220222 (experimental)
[master r12-7325-g2f59f067610] and some earlier versions.
Note: with g++, one gets a "runtime error: execution reached the end of a
value-returning function without returning a value" as expected.