Function-local variable declared with static storage duration

ID

c.correctness.static_local_variable

Severity

low

Remediation Complexity

medium

Remediation Risk

medium

Remediation Effort

medium

Resource

Concurrency

Language

C / C++

Description

This local variable is declared static, so a single instance is shared across every call to the function instead of each call getting its own. That breaks reentrancy: a recursive call, or a second call before the first finishes (a callback invoked during I/O, a signal handler), sees state left behind by the caller still on the stack. It is also a data race if two threads can call the function concurrently, since no synchronization guards the shared state. Pass the state explicitly, return it to the caller, or store it in caller-owned storage instead.

Rationale

This local variable is declared static, so a single instance is shared across every call to the function instead of each call getting its own. That breaks reentrancy: a recursive call, or a second call before the first finishes (a callback invoked during I/O, a signal handler), sees state left behind by the caller still on the stack. It is also a data race if two threads can call the function concurrently, since no synchronization guards the shared state. Pass the state explicitly, return it to the caller, or store it in caller-owned storage instead.

The following code illustrates the pattern detected by this rule:

int next_id(void)
{
    // FLAGGED: Function-local variable declared with static storage duration
    static int counter = 0;
    counter++;
    return counter;
}

Remediation

Follow secure coding practices and review the references below for detailed remediation guidance.

Configuration

This detector does not need any configuration.

References

© 2026 Xygeni. All rights reserved