Search code examples
c++gccoptimizationg++memory-safety

FORTIFY_SOURCE and Og optimization level


Is it possible to use -Wp,-D_FORTIFY_SOURCE=2 option (or =1) along with -Og optimization level? The patch which introduces FORTIFY_SOURCE contains a comment /* Object size checking is only intended for -O[s123]. */.

It is 2004 year. And I don't know when -Og key is introduced (maybe later).

Which optimizations (optimization flags) FORTIFY_SOURCE requires namely?


Solution

  • Fortification requires __OPTIMIZE__ macro greater than 0:

    $ cat /usr/include/features
    #if defined _FORTIFY_SOURCE && _FORTIFY_SOURCE > 0 \
        && __GNUC_PREREQ (4, 1) && defined __OPTIMIZE__ && __OPTIMIZE__ > 0
    # if _FORTIFY_SOURCE > 1
    #  define __USE_FORTIFY_LEVEL 2
    

    -Og is no different from other -O flags in defining __OPTIMIZE__:

    $ gcc -E -Og -dM -x c /dev/null 2>&1 | grep OPTIMIZE
    #define __OPTIMIZE__ 1
    

    so you should be fine.

    You can ask Glibc developers to update the comment.