Main Content

Static local variable initialized with non-constant expression (STALE_STATIC_LOCAL_VARIABLE)

R2026b

Static const local variable initialized using parameter or local variable

Since R2026b

Description

This defect occurs when a static const local variable is initialized with a non-constant expression such as a function parameter, a local variable, or a data member of a local object.

The checker applies to C++ code. In C, initializing a static variable with a non-constant expression is a compilation error.

The checker also flags thread_local const local variables initialized from parameters or local variables, because thread_local implies static storage duration within each thread.

Risk

The static const variable silently retains its initial value from the first function call. Callers that pass different arguments on subsequent calls observe incorrect results because the variable does not update. This pattern is difficult to detect in code reviews because the const qualifier suggests correctness and the one-time initialization is not obvious from the function signature.

If the stale variable controls program logic, the defect can produce intermittent failures that depend on call order.

Fix

To fix this defect:

  • Remove the static keyword so the variable is reinitialized on each call.

  • If the variable must persist across calls, remove the const qualifier so it can be reassigned.

  • If the variable is intended to capture the first-call value only, document this intent and justify the suppression.

  • If the variable is intended to be a static and a const, intialize it using a constexpr.

  • If you do not want to fix the issue, add comments to your result or code to avoid another review.

Examples

expand all

In this example, the function hasLedgerData declares a static const object initialized from the filename parameter. On the first call, ledger captures the value of filename. On subsequent calls with different file names, ledger retains the original value and does not reinitialize.

#include <string>

class Ledger {
public:
    Ledger(const std::string& s) : m_s(s) {}
    bool process() const;
private:
    std::string m_s;
};

bool hasLedgerData(const std::string& filename)
{
    static const Ledger ledger(filename); // Noncompliant
    return ledger.process();
}

Polyspace® Bug Finder™ reports a defect on the declaration of ledger because it is a static const local variable initialized from the function parameter filename. The variable is initialized only once and becomes stale on subsequent calls with different arguments.

Correction — Remove static Keyword

Remove the static keyword so the object is constructed on each function call with the current value of filename.

#include <string>

class Ledger {
public:
    Ledger(const std::string& s) : m_s(s) {}
    bool process() const;
private:
    std::string m_s;
};

bool hasLedgerData(const std::string& filename)
{
    const Ledger ledger(filename); // Compliant
    return ledger.process();
}

Result Information

Group: PROGRAMMING
Language: C++
Impact: Medium
Command-Line Syntax: STALE_STATIC_LOCAL_VARIABLE
PQL Name: std.defects.STALE_STATIC_LOCAL_VARIABLE

Version History

Introduced in R2026b