Source Fortification#
Introduction#
Clang provides built-in compile-time source fortification diagnostics under -Wfortify-source (enabled by default) to catch buffer overflows, format string truncations, and invalid arguments in calls to supported C and POSIX library functions.
-Wfortify-source diagnoses calls when buffer sizes and access lengths (or
argument values) can be constant-evaluated. It does not flag cases where
proving an overflow requires optimizations (such as inlining, constant
propagation, or value-range analysis).
Supported Checks#
-Wfortify-source can diagnose:
Destination buffer overflows and format truncation: When a constant-evaluated size argument, source string literal length, or minimum output length of a string literal format string exceeds the known size of the destination buffer (or, for bounded format functions, exceeds the constant-evaluated size bound):
<string.h>/<strings.h>:memcpy,memmove,memset,mempcpy,bcopy,bzero,strcpy,stpcpy,strcat,strncpy,stpncpy,strncat,strlcpy,strlcat(and their__builtin_variants).<stdio.h>:sprintf,vsprintf,snprintf,vsnprintf(format overflow and truncation are also controlled by -Wformat-overflow and -Wformat-truncation),scanf,fscanf,sscanf,fgets,fread.<poll.h>:poll,ppoll,ppoll64.<sys/socket.h>:recv,recvfrom.
Source buffer overreads: When a constant-evaluated byte count exceeds the known size of the source buffer:
<stdio.h>:fwrite.
Invalid constant arguments:
<sys/stat.h>:umaskwhen called with constant mode bits outside0777that are silently ignored.
Differences from _FORTIFY_SOURCE#
The _FORTIFY_SOURCE
macro in C standard libraries (such as glibc and Android’s Bionic) hardens
calls to C and POSIX functions by rewriting them in system headers into
fortified wrappers and __builtin___*_chk intrinsics (typically when
optimization is enabled), combining compile-time diagnostics with runtime
bounds checks. Clang’s -Wfortify-source flag differs from
-D_FORTIFY_SOURCE in a few ways:
Compile-time warnings only vs. runtime bounds checks:
-Wfortify-sourceis purely a compile-time diagnostic flag; it checks direct calls to recognized library functions (as well as__builtin___*_chkbuiltins) without altering generated code. By contrast,-D_FORTIFY_SOURCEcan also insert runtime bounds checks that abort if an overflow is detected at runtime.Constant evaluation vs. post-optimization checks:
-Wfortify-sourceis enabled by default even at-O0and without-D_FORTIFY_SOURCE, making warnings fast, consistent across build configurations, and free of false positives from control-flow transformations (such as jump threading or loop unrolling). Conversely,-Wfortify-sourceonly diagnoses calls whose buffer and access sizes can be constant-evaluated directly, whereas-D_FORTIFY_SOURCE(when compiled with-O1or higher) can diagnose additional overflows at compile time after optimizations (for example, viawarningorerrorfunction attributes).Function coverage:
-Wfortify-sourcedoes not yet cover the full set of functions that-D_FORTIFY_SOURCEdoes, though expanding coverage is a work in progress.