3 ms·
> If you want to go with that analogy, UB is a kin of you intentionally pointing your gun to your foot, taking your time to aim it precisely on your foot, and i
by masklinn 7y ago
> If you want to go with that analogy, UB is a kin of you intentionally pointing your gun to your foot, taking your time to aim it precisely on your foot, and in spite of all the possible warnings and error messages that are shouted at you…
Yeaaaah not really:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char a[128];
static char b[128];
int main(int argc, char *argv[]) {
(void)argc;
strcpy(a + atoi(argv[1]), "owned.");
printf("%s\n", b);
return 0;
}
compiles with no warning using clang (8), even with -Weverything
And that’s a super easy case, it’s not a use after a dynamic callback somewhere freed a pointer it did not own, or a function 5 levels deep (or worse in a dll) didn’t check its input pointer which only became occasionally null years later.
- deleted 7y ago[deleted]
- saagarjha 7y agoAnd many (myself included) think that the compiler should not warn about this code, even if it can potentially exhibit undefined behavior, because it would make it impossible to write code at all without warnings. (A good static analyzer or code reviewer should call it out, though.)
- masklinn 7y agoThat's completely fair. I'm not criticising the compiler not complaining about this code, I'm providing a counter-point to rumanator's assertion that: > UB is a kin of you intentionally pointing your gun to your foot, taking your time to aim it precisely on your foot, and in spite of all the possible warnings and error messages that are shouted at you… Because they could hardly be more wrong, the average UB is completely invisible to static analysers because it's a dynamic condition which the type system is not smart enough to move to static. That's why it's an UB and not, say, a compile error.