This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Question: at which version gcc started supporting -D_FORTIFY_SOURCE for c++ files?


On Wed, Aug 22, 2012 at 1:33 PM, Oleg Pekar (olpekar) <olpekar@cisco.com> wrote:
>>On Wed, Aug 22, 2012 at 12:38 PM, Oleg Pekar (olpekar) <olpekar@cisco.com> wrote:
>>> I'm using gcc 4.1.2, it supports -D_FORTIFY_SOURCE option for c files but not for c++. I'm looking for gcc version number where support for this option in c++ files was added.
>
>>I'm not sure how to answer your question, because -D_FORTIFY_SOURCE is not a GCC feature.  It is a glibc feature.  It causes glibc to change some function definitions to use features that are provided by GCC.
>>As far as I know, all the _FORTIFY_SOURCE features that GCC provides are available in both C and C++.
>
>>So, please give us an example of something that fails with g++ and -D_FORTIFY_SOURCE that you expect to work.
>
>>Ian
>
> I created a tiny program test.c:
> #include <memory.h>
> int main()
> {
>     char buf[4];
>     memcpy(buf, "1234", 5);
>     return 0;
> }
>
> Then I compile it with gcc 4.1.2 on Red Hat Linux 5.5. When I compile it as c file - gcc performs the check specified by FORTIFY_SOURCE, when I compile it as c++ file - it doesn't.
>
> dev /tmp>gcc -x c -D_FORTIFY_SOURCE=2 -O2 -o test.exe -lstdc++ test.c
> test.c: In function \u2018main\u2019:
> test.c:6: warning: call to __builtin___memcpy_chk will always overflow destination buffer
>
> dev /tmp>gcc -x c++ -D_FORTIFY_SOURCE=2 -O2 -o test.exe -lstdc++ test.c
> dev /tmp>

Thanks for the example.  I could recreate the problem with GCC 4.1 on
my system.  It doesn't have anything to do with GCC, though, at least
not directly; it has to do with glibc.  In the version of glibc on my
system, it works with g++ 4.3 or later.

I won't go into all the details, but it's because of these lines in
<sys/cdefs.h>:

/* GCC 4.3 and above with -std=c99 or -std=gnu99 implements ISO C99
   inline semantics, unless -fgnu89-inline is used.  */
#if !defined __cplusplus || __GNUC_PREREQ (4,3)
# if defined __GNUC_STDC_INLINE__ || defined __cplusplus
#  define __extern_inline extern __inline __attribute__ ((__gnu_inline__))
#  if __GNUC_PREREQ (4,3)
...

Basically, the feature is only available if not C++ or if GCC 4.3 or later.

Ian


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]