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

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