0

I know this is not new, but recently I came across the topic of GCC __builtin_object_size() function. For what I read from the documentation, it states that the intended use can be as follows:

The intended use can be e.g.

#undef memcpy
#define bos0(dest) __builtin_object_size (dest, 0)
#define memcpy(dest, src, n) \
  __builtin___memcpy_chk (dest, src, n, bos0 (dest))

char *volatile p;
char buf[10];
/* It is unknown what object p points to, so this is optimized
   into plain memcpy - no checking is possible.  */
memcpy (p, "abcde", n);

... // more codes

Is there any advantage of directly using it at the source code level instead of enabling the D_FORTIFY_SOURCE flag when compiling? Are there actually real life examples that benefit from the use of __builtin_object_size()?

Calance Hong
  • 1
  • 1
  • 1

1 Answers1

0

This example shows how the _FORTIFY_SOURCE works with memcpy!

But there might certainly be other functions besides the standard library ones that would benefit from object pointer range check.

  • But i checked on the preprocessing output of GCC compiler and it seems that FORTIFY_SOURCE doesn't replace functions using macro approach. Is it possible there could be a difference between these two approaches? – Calance Hong Oct 25 '19 at 06:46