4 ms·
That code ought to be a compile error. "On this code path, your code assumes ptr is null and not-null at the same time!" Not all UB can be detected at compile
by iainmerrick 9y ago
That code ought to be a compile error. "On this code path, your code assumes ptr is null and not-null at the same time!"
Not all UB can be detected at compile time, but when it can be detected, it should be flagged to the user.
- simias 9y agoBut what if it's the result of some inlining or simply macro expansion? It's a tough call to make.
- inetknght 9y agoIf it's from inlining, then the compiler has a bug. If it's from a macro, then stop using macros in a broken fashion. Let it be an error.
- jjnoakes 9y agoHow would that be a compiler bug? Assuming g() is never called with a NULL pointer, this code should not produce any compile-time errors: int f(int *x) { if (x == NULL) return 0; return *x; } int g(int *p) { return (*p) + f(p); }