You wrote a line of code to wipe a password from memory. The compiler decided the line did nothing useful and removed it.
CWE-1038 covers this: the product uses a mechanism that automatically optimizes code, for example to improve performance, but the optimizations have an unintended side effect that violates a security assumption. MITRE lists it as a Class weakness, not tied to a language.
How it happens
Optimizers are allowed to change a program in any way that preserves its visible behavior as the language defines it. Security intent is not part of that definition.
MITRE's example reads a password, connects to a back end, and then scrubs the password with memset():
void GetData(char *MFAddr) {
char pwd[64];
if (GetPasswordFromUser(pwd, sizeof(pwd))) {
if (ConnectToMainframe(MFAddr, pwd)) {
// Interaction with mainframe
}
}
memset(pwd, 0, sizeof(pwd));
}
An optimizing compiler can treat the memset() as a dead store, since pwd is never read afterward, and drop it. The password then stays in memory. If attackers can reach the right region of memory, they may recover it.
The record's observed examples show two different layers:
- CVE-2008-1685: a C compiler optimization, allowed by the specification, removes code that detects integer overflows.
- CVE-2017-5715: processor speculative execution and branch prediction optimizations cause access control checks to be bypassed, often known as Spectre. See also CWE-1037.
What an attacker gains
MITRE lists an integrity effect: the optimizations alter the order or presence of execution, causing side effects the developer did not intend. In the examples, that means leftover secrets in memory or missing overflow checks.
How to find it
MITRE gives no detection methods for this entry. Practical approaches:
- Inspect compiled output or assembly for security critical routines.
- Compare behavior across optimization levels.
- Test that wiped buffers are actually wiped, for example in a debug build harness.
- Read compiler documentation about what it may remove.
How to fix it
MITRE lists no formal mitigations. Steps that follow from the weakness:
- Use platform provided secure wipe functions designed not to be optimized away, instead of plain
memset(). - Write overflow and bounds checks so they do not depend on behavior the language leaves undefined.
- Verify security critical code at the optimization level you actually ship.
- Keep compilers and platforms updated.
- Minimize how long secrets stay in memory.
You can check the libraries you ship with the free in-browser scan at gold.safeguard.sh/scan. No login, up to 100 dependencies, and the file stays on your machine.
Quick check
Why can the compiler remove the memset call?
The buffer is not used after being overwritten, so the compiler treats the write as a dead store, with no visible effect on the program.
Does the bug show up when you read the source?
No. The source is correct as written. The problem appears only in the optimized output.