Process-wide locale changed at runtime

ID

c.correctness.global_locale_mutation

Severity

low

Remediation Complexity

medium

Remediation Risk

medium

Remediation Effort

medium

Resource

Concurrency

Language

C / C++

Description

Reports a call that changes the process-wide global locale: setlocale with a non-null locale argument, or std::locale::global.

Rationale

Both setlocale and std::locale::global change locale-dependent behavior — number and date formatting, string collation, character classification, and more — for the entire process, including every other thread and every library the program links against. Neither call is thread-safe with concurrent use of locale-sensitive functions, so a mutation racing with formatting or parsing elsewhere is undefined behavior, and even single-threaded, the effect is a hidden global side effect that any later locale-dependent call now depends on.

The following code illustrates the pattern detected by this rule:

void set_c_locale(void)
{
    // FLAGGED: Process-wide locale changed at runtime
    setlocale(LC_ALL, "");
}

Remediation

Prefer locale objects scoped to where they are needed: construct a std::locale and pass it explicitly to the stream, facet, or function that needs it, instead of mutating the global locale. If a process-wide locale is genuinely required, set it once during single-threaded startup before any other thread can observe or race the change.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved