Use __predict_false on some fortify methods.

Give the compiler some hints that these error conditions
are unlikely to occur in practice.

Change-Id: Ifaf7322a12120ef663c8315c1a18c2dcbe4bda23
This commit is contained in:
Nick Kralevich 2013-05-02 14:31:51 -07:00
parent e4ac8feb58
commit 532d6f09b1
11 changed files with 11 additions and 11 deletions

View file

@ -45,7 +45,7 @@
extern "C" void *__memcpy_chk(void *dest, const void *src, extern "C" void *__memcpy_chk(void *dest, const void *src,
size_t copy_amount, size_t dest_len) size_t copy_amount, size_t dest_len)
{ {
if (__builtin_expect(copy_amount > dest_len, 0)) { if (__predict_false(copy_amount > dest_len)) {
__fortify_chk_fail("memcpy buffer overflow", __fortify_chk_fail("memcpy buffer overflow",
BIONIC_EVENT_MEMCPY_BUFFER_OVERFLOW); BIONIC_EVENT_MEMCPY_BUFFER_OVERFLOW);
} }

View file

@ -44,7 +44,7 @@
extern "C" void *__memmove_chk (void *dest, const void *src, extern "C" void *__memmove_chk (void *dest, const void *src,
size_t len, size_t dest_len) size_t len, size_t dest_len)
{ {
if (len > dest_len) { if (__predict_false(len > dest_len)) {
__fortify_chk_fail("memmove buffer overflow", __fortify_chk_fail("memmove buffer overflow",
BIONIC_EVENT_MEMMOVE_BUFFER_OVERFLOW); BIONIC_EVENT_MEMMOVE_BUFFER_OVERFLOW);
} }

View file

@ -42,7 +42,7 @@
* greater than 0. * greater than 0.
*/ */
extern "C" void *__memset_chk (void *dest, int c, size_t n, size_t dest_len) { extern "C" void *__memset_chk (void *dest, int c, size_t n, size_t dest_len) {
if (n > dest_len) { if (__predict_false(n > dest_len)) {
__fortify_chk_fail("memset buffer overflow", __fortify_chk_fail("memset buffer overflow",
BIONIC_EVENT_MEMSET_BUFFER_OVERFLOW); BIONIC_EVENT_MEMSET_BUFFER_OVERFLOW);
} }

View file

@ -44,7 +44,7 @@
extern "C" char *__strcpy_chk (char *dest, const char *src, size_t dest_len) { extern "C" char *__strcpy_chk (char *dest, const char *src, size_t dest_len) {
// TODO: optimize so we don't scan src twice. // TODO: optimize so we don't scan src twice.
size_t src_len = strlen(src) + 1; size_t src_len = strlen(src) + 1;
if (src_len > dest_len) { if (__predict_false(src_len > dest_len)) {
__fortify_chk_fail("strcpy buffer overflow", __fortify_chk_fail("strcpy buffer overflow",
BIONIC_EVENT_STRCPY_BUFFER_OVERFLOW); BIONIC_EVENT_STRCPY_BUFFER_OVERFLOW);
} }

View file

@ -45,7 +45,7 @@
extern "C" size_t __strlcat_chk(char *dest, const char *src, extern "C" size_t __strlcat_chk(char *dest, const char *src,
size_t supplied_size, size_t dest_len_from_compiler) size_t supplied_size, size_t dest_len_from_compiler)
{ {
if (supplied_size > dest_len_from_compiler) { if (__predict_false(supplied_size > dest_len_from_compiler)) {
__fortify_chk_fail("strlcat buffer overflow", 0); __fortify_chk_fail("strlcat buffer overflow", 0);
} }

View file

@ -45,7 +45,7 @@
extern "C" size_t __strlcpy_chk(char *dest, const char *src, extern "C" size_t __strlcpy_chk(char *dest, const char *src,
size_t supplied_size, size_t dest_len_from_compiler) size_t supplied_size, size_t dest_len_from_compiler)
{ {
if (supplied_size > dest_len_from_compiler) { if (__predict_false(supplied_size > dest_len_from_compiler)) {
__fortify_chk_fail("strlcpy buffer overflow", 0); __fortify_chk_fail("strlcpy buffer overflow", 0);
} }

View file

@ -56,7 +56,7 @@
extern "C" size_t __strlen_chk(const char *s, size_t s_len) { extern "C" size_t __strlen_chk(const char *s, size_t s_len) {
size_t ret = strlen(s); size_t ret = strlen(s);
if (__builtin_expect(ret >= s_len, 0)) { if (__predict_false(ret >= s_len)) {
__fortify_chk_fail("strlen read overflow", 0); __fortify_chk_fail("strlen read overflow", 0);
} }

View file

@ -44,7 +44,7 @@
extern "C" char *__strncpy_chk (char *dest, const char *src, extern "C" char *__strncpy_chk (char *dest, const char *src,
size_t len, size_t dest_len) size_t len, size_t dest_len)
{ {
if (len > dest_len) { if (__predict_false(len > dest_len)) {
__fortify_chk_fail("strncpy buffer overflow", __fortify_chk_fail("strncpy buffer overflow",
BIONIC_EVENT_STRNCPY_BUFFER_OVERFLOW); BIONIC_EVENT_STRNCPY_BUFFER_OVERFLOW);
} }

View file

@ -42,7 +42,7 @@
* greater than 0. * greater than 0.
*/ */
extern "C" mode_t __umask_chk(mode_t mode) { extern "C" mode_t __umask_chk(mode_t mode) {
if ((mode & 0777) != mode) { if (__predict_false((mode & 0777) != mode)) {
__fortify_chk_fail("umask called with invalid mask", 0); __fortify_chk_fail("umask called with invalid mask", 0);
} }

View file

@ -50,7 +50,7 @@ extern "C" int __vsnprintf_chk(
const char *format, const char *format,
va_list va) va_list va)
{ {
if (supplied_size > dest_len_from_compiler) { if (__predict_false(supplied_size > dest_len_from_compiler)) {
__fortify_chk_fail("vsnprintf buffer overflow", 0); __fortify_chk_fail("vsnprintf buffer overflow", 0);
} }

View file

@ -52,7 +52,7 @@ int open(const char *pathname, int flags, ...)
} }
int __open_2(const char *pathname, int flags) { int __open_2(const char *pathname, int flags) {
if (flags & O_CREAT) { if (__predict_false(flags & O_CREAT)) {
__fortify_chk_fail("open(O_CREAT) called without specifying a mode", 0); __fortify_chk_fail("open(O_CREAT) called without specifying a mode", 0);
} }