Question Details

No question body available.

Tags

compiler-optimization compiler-warnings avr-gcc

Answers (1)

Accepted Answer Available
Accepted Answer
November 1, 2025 Score: 2 Rep: 62,809 Quality: High Completeness: 80%

For warnings about the behavior of a code, rather than strictly its syntax, it's normal for optimization level to have an effect, as this determines the level of analysis that the compiler performs. For instance, at lower optimization levels, the compiler may not keep track of the sizes of objects when compiling reads or writes involving pointers into them.

This is documented in general:

The effectiveness of some warnings depends on optimizations also being enabled. For example, -Wsuggest-final-types is more effective with link-time optimization. Some other warnings may not be issued at all unless optimization is enabled. While optimization in general improves the efficacy of warnings about control and data-flow problems, in some cases it may also cause false positives.

509 presumably represents the total size of the src object, minus one (255 * 2 - 1 = 509), as if it were one big string with a null terminator. GCC probably chooses to be conservative and warn about the "worst case".

GCC's stringop warnings documentation is linked to Object Size Checking, whose builtinobjectsize docs discuss how it handles the case where a pointer could point to any of several different objects (e.g. just src[0], which is of size 2, or all of src, which is of size 510). It has options to return either the largest or smallest of the possibilities. This warning probably uses "largest", again to err to the side of caution.