Skip to content

False positive: UninitializedLocalRead on a value-initialized local #189

Open

Description

@SizzleUnrlsd

UninitializedLocalRead reports a read of a local that is value-initialized with {} before it is read.

Example

braced-init-read.cpp:

#include <array>

struct Bits
{
    unsigned char value = 0;
};

struct Transformer
{
    std::array<Bits, 4> images{};
    Bits uncertain;
};

Transformer build(int n)
{
    Transformer normal{};
    for (int i = 0; i < n; ++i)
        normal.images[static_cast<unsigned>(i) % 4].value |= 1;
    Transformer result;
    result = normal;
    return result;
}

With the analyzer built from main 112ffcf:

$ 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).

Alerts: https://github.com/CoreTrace/coretrace-stack-analyzer/security/code-scanning/257, https://github.com/CoreTrace/coretrace-stack-analyzer/security/code-scanning/258, https://github.com/CoreTrace/coretrace-stack-analyzer/security/code-scanning/259, https://github.com/CoreTrace/coretrace-stack-analyzer/security/code-scanning/260 (#187, warning: they fail the code-scanning check).
The same pattern is already reported on main, in computeSummary (src/analysis/ownership/OwnershipEngine.cpp, lines 512-513 at 112ffcf): alerts https://github.com/CoreTrace/coretrace-stack-analyzer/security/code-scanning/158 to https://github.com/CoreTrace/coretrace-stack-analyzer/security/code-scanning/161, open. #187 moves that loop into the unit test as a reference, which moves the alerts.

Activity

  1. SizzleUnrlsd commented on Nov 15, 2026

    @SizzleUnrlsd
    ContributorAuthor

    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):

    struct Bits { unsigned char value = 0; };
    struct WithArray { Bits images[4]; Bits uncertain; };          // reported on `result = normal;`
    struct WithoutArray { Bits i0, i1, i2, i3; Bits uncertain; };  // not reported

    The same cause gives the alerts on ArgPath path; in src/analysis/ownership/ResourceFactCollector.cpp, whose last member is std::uint8_t reservedPadding[3] = {};:

  2. SizzleUnrlsd commented on Nov 16, 2026

    @SizzleUnrlsd
    ContributorAuthor

    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.
    
    struct Bits
    {
        unsigned char value = 0;
    };
    
    struct WithArray
    {
        Bits images[4];
        Bits uncertain;
    };
    
    struct WithoutArray
    {
        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.
    
    struct PathA // like ArgPath
    {
        unsigned long long offset = 0;
        unsigned argIndex = 0;
        bool viaPointerSlot = false;
        unsigned char reservedPadding[3] = {};
    };
    
    struct PathB // the same without the array member
    {
        unsigned long long offset = 0;
        unsigned argIndex = 0;
        bool viaPointerSlot = false;
    };
    
    struct Effects
    {
        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;
    }
    
    void defaultInitA(unsigned i) // the pattern of line 996
    {
        PathA path;
        path.argIndex = i;
        recordA(path);
    }
    
    void defaultInitB(unsigned i) // no array member
    {
        PathB path;
        path.argIndex = i;
        recordB(path);
    }
    
    void valueInitA(unsigned i) // `{}`: issue #189
    {
        PathA path{};
        path.argIndex = i;
        recordA(path);
    }
    
    void helperA(unsigned i) // through a helper returning the struct
    {
        recordA(byValue(i));
    }
    
    void untouchedA() // 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:

      %7 = getelementptr inbounds nuw %struct.PathA, ptr %3, i32 0, i32 3
      %8 = getelementptr inbounds i8, ptr %7, i64 3
      br label %9
    
    9:                                                ; preds = %9, %1
      %10 = phi ptr [ %7, %1 ], [ %11, %9 ]
      store i8 0, ptr %10, align 1
      %11 = getelementptr inbounds i8, ptr %10, i64 1
      %12 = icmp eq ptr %11, %8
      br i1 %12, label %13, label %9

    Alerts with this cause

    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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions