10 ms·
Does this piece of code have UB? Could it invoke UB depending on the input to the program? void count(int x) { for (int i = 0; i < x; ++i)
by MauranKilom 6y ago
Does this piece of code have UB? Could it invoke UB depending on the input to the program?
void count(int x)
{
for (int i = 0; i < x; ++i)
printf("%d ", i);
}
The answer to the latter question is of course "yes" - signed integer overflow is UB, so you invoke UB by passing a negative x.
Would you like every loop to be flagged as potential UB? I don't think you'd last a single day programming in that C dialect.
- wtetzner 6y agoYou wouldn't use the compiler to build you programs, you'd just use it to detect UB. It would be especially useful if you could specify your entry point(s), and let it find all cases where user inputs could cause UB. I think it would be especially helpful for checking the output of compile-to-c languages.