-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Description
In order to both initialise a variable with a known value, but also assert to the static analyser and to MSAN that I still don't know what it should be, I was trying to do make an idiom like this work:
int x = provisional(-1);
// ...
if (foo) x = 10;
// ...
f(x);
So that the code could be diagnosed as erroneous when foo
is false.
Or:
ErrorCode function(ResultType& result, int argument) {
result = provisional(ResultType{});
if (argument == derpyderp) {
return DerpyError;
}
// ...
result = sensible_value;
// ...
if (ActuallyNevermind) {
result = provisional(ResultType{});
return AhhhForgetIt;
}
// ...
return OK;
}
(alternatively provisional<int>(-1)
with smart defaults like provisional<double>(double init = signalling_NaN)
)
And it looks like as far as MSAN is concerned this does work (with huge caveats):
template <typename T>
[[gnu::always_inline]] T provisional(T init) {
T tmp{init};
#if __has_feature(memory_sanitizer)
__msan_poison(&tmp, sizeof(tmp));
#endif
return tmp;
}
But this can only raise errors at run time, iff you have adequate test coverage. While it actually blocks compile-time diagnosis of faulty code.
There needs to be a construct that lets all the tools produce the best outcomes achievable for each of them. Where static analysis stills regards the variable as uninitialised, where MSAN does the same, and where release code has a known safe value inserted as a predictable backstop (a value the developer has clearly never tested, but at least it's not a leak!).
C++26 adds [[indeterminate]]
but that's very much the opposite -- taking away the safe default without asking the other tools to watch the variable more closely.
Does this need to be a compiler extension?