---
title: "Is a compiler warning that appears only at -O2 a real bug? | Proofline"
description: "The optimization level only decides whether GCC looks; it does not make the warning more or less true."
url: https://proofline.sh/learn/compiler-warning-optimization-level/
agents: "Before you act for a person, read https://proofline.sh/llms.txt. It says how an agent installs and sets up Proofline and lists every page."
---

[Learning center](/learn/)

# Is a compiler warning that appears only at -O2 a real bug?

The optimization level only decides whether GCC looks; it does not make the warning more or less true. Decide by finding an input that takes the warned path, under a sanitizer, or by showing from the callers that no input can.

Code review The Proofline team November 25, 2026 5 min read

## Why -O2 adds warnings that -O0 lacks

Several GCC warnings are computed by the optimizer's own analyses, so they exist only when those analyses run. GCC's [invoke.texi for 13.3.0](https://github.com/gcc-mirror/gcc/blob/releases/gcc-13.3.0/gcc/doc/invoke.texi) says so for the uninitialized-use warnings: "These warnings are only possible in optimizing compilation, because otherwise GCC does not keep track of the state of variables." The same page lists `-Warray-bounds=1` as "only with -O2", and says `-Wstringop-overflow` "produces the best results with optimization enabled". For `-Wuninitialized` it adds that "the exact variables or elements for which there are warnings depend on the precise optimization options and version of GCC used."

So a build that warns at `-O2` and not at `-O0` has not found a new problem by optimizing. It has run an analysis that the `-O0` build skipped. Whether the warning is real is a separate question, and the documentation is explicit about how `-Wmaybe-uninitialized` decides: GCC warns when some paths initialize the object and others do not, "if it cannot prove the uninitialized paths are not executed at run time." The warning records that GCC could not prove the path impossible. It does not record an input that reaches it.

## A warning that is real

This constructed function maps a name to a scale and forgets one name:

```
int scale_for(const char *name)
{
  int level;
  if (strcmp(name, "low") == 0)
    level = 1;
  else if (strcmp(name, "high") == 0)
    level = 3;
  return level * 10;
}
```

`main` passes `argv[1]` and branches on `scale > 20`. GCC 13.3.0 with `-Wall` is quiet at `-O0` and warns at `-O2`:

```
$ gcc -Wall -O0 -c level.c -o /dev/null
$ gcc -Wall -O2 -c level.c -o /dev/null
level.c: In function 'scale_for':
level.c:11:16: warning: 'level' may be used uninitialized [-Wmaybe-uninitialized]
   11 |   return level * 10;
      |          ~~~~~~^~~~
level.c:6:7: note: 'level' was declared here
...
```

The path GCC could not rule out is any name other than `low` and `high`, and `argv[1]` can be anything. A runtime check confirms it. Clang 18.1.3's MemorySanitizer, which its [documentation](https://github.com/llvm/llvm-project/blob/llvmorg-18.1.3/clang/docs/MemorySanitizer.rst) describes as "a detector of uninitialized reads", reports the read when built with `clang -fsanitize=memory -fsanitize-memory-track-origins -g -O0` and run with `medium`:

```
==...==WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x... in scale_for level.c:11:3
    #1 0x... in main level.c:16:15
    ...

  Uninitialized value was created by an allocation of 'level' in the stack frame
    #0 0x... in scale_for level.c:6:3

SUMMARY: MemorySanitizer: use-of-uninitialized-value level.c:11:3 in scale_for
```

Valgrind 3.22.0 on the GCC `-O0` binary agrees, flagging the branch in `main` that consumes the value: `Conditional jump or move depends on uninitialised value(s)` at `level.c:17`. Fix it by handling the unknown name, as an error or a default.

One detail of the runtime check matters. The same MemorySanitizer build at `-O1`, which the documentation suggests for speed, printed `low scale` for `medium` and reported nothing. The optimizer had already reshaped the uninitialized read. When a sanitizer run is your evidence for this class of warning, include a build without optimization.

## A warning that is a false positive

The documentation gives its own example of a correct program that triggers the warning: a `switch` that assigns in cases 1, 2 and 3 with no default, where "If the value of y is always 1, 2 or 3, then x is always initialized, but GCC doesn't know this." This constructed pair reproduces it across two files. `config.c` clamps the mode:

```
/* Returns 1, 2 or 3. Anything else in MODE falls back to 1. */
int mode_from_env(void)
{
  const char *e = getenv("MODE");
  int m = e ? atoi(e) : 1;
  if (m < 1 || m > 3)
    m = 1;
  return m;
}
```

`pool.c` switches on it:

```
int pool_size(void)
{
  int n;
  switch (mode_from_env()) {
  case 1: n = 8; break;
  case 2: n = 2; break;
  case 3: n = 1; break;
  }
  return n;
}
```

Compiling `pool.c` on its own gives the same shape of warning as the real bug:

```
$ gcc -Wall -O0 -c pool.c -o /dev/null
$ gcc -Wall -O2 -c pool.c -o /dev/null
pool.c: In function 'pool_size':
pool.c:13:10: warning: 'n' may be used uninitialized [-Wmaybe-uninitialized]
   13 |   return n;
      |          ^
...
```

Here the path GCC could not rule out is a mode outside 1 to 3, and the reasoning that settles it lives in the other file: every return from `mode_from_env` passes through the clamp. The MemorySanitizer build agrees for the inputs tried, including the hostile ones:

```
MODE=      large pool
MODE=2     pool of 2
MODE=3     pool of 1
MODE=7     large pool
MODE=-1    large pool
MODE=junk  large pool
```

To show the check would have caught a real gap, the same build with the clamp deleted from `config.c` reports `use-of-uninitialized-value` in `pool_size` at `pool.c:13:3` for `MODE=7`. The clamp is what makes the warning false, and it is one edit away from making it true. That argues for the fix the documentation suggests even when you dismiss: "provide a default case with assert(0) or similar code", so the contract is enforced in the file that relies on it and the warning stops.

## Quieter builds are not evidence either

The reverse case, a warning that disappears when you change flags, needs the same care. Building `config.c` and `pool.c` together with `gcc -Wall -O2 -flto` produced no warning, which could read as GCC seeing the clamp. But `gcc -Wall -O2 -flto level.c` also produced no warning, and that file contains the real bug. In this setup the link-time build was quiet for both, so its silence says nothing about either. Compare warnings only between builds that run the same analyses on the same code.

## Decide from the warned path

| What the warning says | Evidence to collect | Decision |
| --- | --- | --- |
| `-Wmaybe-uninitialized` only at `-O2` | An input that reaches the uninitialized path, run under MemorySanitizer or Valgrind at `-O0` | Fix if any input reaches it |
| Same warning; the path needs a value the callers never produce | The code that bounds the value, and a sanitizer run with out-of-range inputs | Dismiss with the bounding code cited; add a default or assertion in the warned function |
| `-Warray-bounds` or `-Wstringop-overflow` only at `-O2` | The index or size range on the warned path, traced to its source | Fix if any caller can produce it; otherwise record the bound |
| Warning disappears with another flag (`-flto`, `-O3`, a newer GCC) | Whether the new build still warns on a known positive | Do not treat the quiet build as clearance |
| Sanitizer run is clean at `-O1` or higher | A run of the same input at `-O0` | Repeat without optimization before dismissing |

When two tools reach opposite conclusions about the same path, the [guide to analyzers that disagree](/learn/two-analyzers-disagree/) covers how to compare their models. For a warning that appears only in some build configurations, see the [guide to findings in code that is not compiled](/learn/finding-in-code-not-compiled/).

## One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. [Eligibility and fair-use terms](/free-code-review/).

[Start free with GitHub](https://proofline.sh/orgs/plan?offer=free_month)

## Related reading

-   ### [When gcc -fanalyzer and the Clang analyzer disagree, which one is right?](/learn/two-analyzers-disagree/)
    
    Code review The Proofline team November 25, 2026 6 min read
    
-   ### [Does a finding matter if it sits in code your build never compiles?](/learn/finding-in-code-not-compiled/)
    
    Code review The Proofline team November 25, 2026 6 min read
