You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
False positive: UninitializedLocalRead on a value-initialized local #189
$ stack_usage_analyzer braced-init-read.cpp
at line 20, column 12
[ !!Warn ] potential read of uninitialized local variable 'normal'
↳ this load may execute before any definite initialization on all control-flow paths
Why it is wrong
Transformer normal{}; value-initializes normal: every member gets its default member initializer or is zero-initialized, before the loop and before result = normal; reads it.
Where it shows
The self-analysis reports it in referenceTransformers (test/unit/ownership_engine_unit_tests.cpp), on ParamTransformer normal{}; and ParamTransformer exceptional{}; (#184).
The trigger is narrower than value-initialization: an array member initialized element by element. Clang emits a loop that stores each element through a pointer stepping from the first element to the end. It does so in the implicit constructor (T x;) as well as inline (T x{};). UninitializedLocalRead does not count those stores as initializing the array, so a later read of the whole object, such as a copy, reports it.
Reduced from the example above, with the analyzer of #283 (917decb):
The same cause gives the alerts on ArgPath path; in src/analysis/ownership/ResourceFactCollector.cpp, whose last member is std::uint8_t reservedPadding[3] = {};:
The trigger is narrower than value-initialization: an array member initialized element by element. Clang emits a loop that stores each element through a pointer stepping from the first element to the end: inline for T x{};, and, for an array of scalars, in the implicit constructor that T x; calls. UninitializedLocalRead does not count those stores as initializing the array, so a later read of the whole object, such as a copy, is reported.
Reproduced with the analyzer built from #283 (917decb, LLVM 20.1.2, macOS), run as stack_usage_analyzer <file>. The outputs below keep the Function: lines of the functions written in each file and the diagnostics.
1. The example above, without <array>
issue189-variants.cpp:
// The example of issue #189 without <array>, with and without an array member.structBits
{
unsignedchar value = 0;
};
structWithArray
{
Bits images[4];
Bits uncertain;
};
structWithoutArray
{
Bits i0, i1, i2, i3;
Bits uncertain;
};
WithArray buildWithArray(int n)
{
WithArray normal{};
for (int i = 0; i < n; ++i)
normal.images[static_cast<unsigned>(i) % 4].value |= 1;
WithArray result;
result = normal;
return result;
}
WithoutArray buildWithoutArray(int n)
{
WithoutArray normal{};
for (int i = 0; i < n; ++i)
normal.i0.value |= 1;
WithoutArray result;
result = normal;
return result;
}
Function: _Z14buildWithArrayi buildWithArray(int)
at line 26, column 12
[ !!Warn ] potential read of uninitialized local variable 'normal'
↳ this load may execute before any definite initialization on all control-flow paths
Function: _Z17buildWithoutArrayi buildWithoutArray(int)
buildWithArray is reported on result = normal; (line 26). buildWithoutArray, where the four elements are separate members, is not (line 36).
2. A struct shaped like ArgPath
uninit-repro.cpp:
// Variants of code-scanning alert #297 on PR #283 (ResourceFactCollector.cpp:996):// `ArgPath path;` where every member of ArgPath has a default member initializer.structPathA// like ArgPath
{
unsignedlonglong offset = 0;
unsigned argIndex = 0;
bool viaPointerSlot = false;
unsignedchar reservedPadding[3] = {};
};
structPathB// the same without the array member
{
unsignedlonglong offset = 0;
unsigned argIndex = 0;
bool viaPointerSlot = false;
};
structEffects
{
PathA a;
PathB b;
};
static Effects sink;
__attribute__((noinline)) void recordA(const PathA& path)
{
sink.a = path;
}
__attribute__((noinline)) void recordB(const PathB& path)
{
sink.b = path;
}
static PathA byValue(unsigned argIndex)
{
PathA path;
path.argIndex = argIndex;
return path;
}
voiddefaultInitA(unsigned i) // the pattern of line 996
{
PathA path;
path.argIndex = i;
recordA(path);
}
voiddefaultInitB(unsigned i) // no array member
{
PathB path;
path.argIndex = i;
recordB(path);
}
voidvalueInitA(unsigned i) // `{}`: issue #189
{
PathA path{};
path.argIndex = i;
recordA(path);
}
voidhelperA(unsigned i) // through a helper returning the struct
{
recordA(byValue(i));
}
voiduntouchedA() // nothing written after the constructor
{
PathA path;
recordA(path);
}
Function: _Z7recordARK5PathA recordA(PathA const&)
Function: _Z7recordBRK5PathB recordB(PathB const&)
Function: _Z12defaultInitAj defaultInitA(unsigned int)
at line 48, column 5
[ !!Warn ] potential read of uninitialized local variable 'path'
↳ this call may read the value before any definite initialization in '_Z7recordARK5PathA'
Function: _Z12defaultInitBj defaultInitB(unsigned int)
Function: _Z10valueInitAj valueInitA(unsigned int)
at line 62, column 5
[ !!Warn ] potential read of uninitialized local variable 'path'
↳ this call may read the value before any definite initialization in '_Z7recordARK5PathA'
Function: _Z7helperAj helperA(unsigned int)
Function: _Z10untouchedAv untouchedA()
at line 73, column 5
[ !!Warn ] potential read of uninitialized local variable 'path'
↳ this call may read the value before any definite initialization in '_Z7recordARK5PathA'
Reported: PathA path; (line 48), PathA path{}; (line 62), and PathA path; with nothing written after the constructor (line 73).
Not reported: the same struct without the array member (line 55), and a PathA returned by a function (line 67).
With clang++ -std=c++20 -O0 -S -emit-llvm, valueInitA initializes reservedPadding with this loop, and the implicit constructor PathA::PathA() contains the same one:
ArgPath (include/analysis/ownership/OwnershipFacts.hpp) ends with std::uint8_t reservedPadding[3] = {};. The alerts on ArgPath path; in src/analysis/ownership/ResourceFactCollector.cpp have this cause:
Activity
SizzleUnrlsd commentedon Nov 15, 2026
The trigger is narrower than value-initialization: an array member initialized element by element. Clang emits a loop that stores each element through a pointer stepping from the first element to the end. It does so in the implicit constructor (
T x;) as well as inline (T x{};).UninitializedLocalReaddoes not count those stores as initializing the array, so a later read of the whole object, such as a copy, reports it.Reduced from the example above, with the analyzer of #283 (
917decb):The same cause gives the alerts on
ArgPath path;insrc/analysis/ownership/ResourceFactCollector.cpp, whose last member isstd::uint8_t reservedPadding[3] = {};:main: https://github.com/CoreTrace/coretrace-stack-analyzer/security/code-scanning/162 and https://github.com/CoreTrace/coretrace-stack-analyzer/security/code-scanning/163;SizzleUnrlsd commentedon Nov 16, 2026
The trigger is narrower than value-initialization: an array member initialized element by element. Clang emits a loop that stores each element through a pointer stepping from the first element to the end: inline for
T x{};, and, for an array of scalars, in the implicit constructor thatT x;calls.UninitializedLocalReaddoes not count those stores as initializing the array, so a later read of the whole object, such as a copy, is reported.Reproduced with the analyzer built from #283 (
917decb, LLVM 20.1.2, macOS), run asstack_usage_analyzer <file>. The outputs below keep theFunction:lines of the functions written in each file and the diagnostics.1. The example above, without
<array>issue189-variants.cpp:buildWithArrayis reported onresult = normal;(line 26).buildWithoutArray, where the four elements are separate members, is not (line 36).2. A struct shaped like
ArgPathuninit-repro.cpp:PathA path;(line 48),PathA path{};(line 62), andPathA path;with nothing written after the constructor (line 73).PathAreturned by a function (line 67).With
clang++ -std=c++20 -O0 -S -emit-llvm,valueInitAinitializesreservedPaddingwith this loop, and the implicit constructorPathA::PathA()contains the same one:Alerts with this cause
ArgPath(include/analysis/ownership/OwnershipFacts.hpp) ends withstd::uint8_t reservedPadding[3] = {};. The alerts onArgPath path;insrc/analysis/ownership/ResourceFactCollector.cpphave this cause:main, open: https://github.com/CoreTrace/coretrace-stack-analyzer/security/code-scanning/162 and https://github.com/CoreTrace/coretrace-stack-analyzer/security/code-scanning/163;