Skip to content

Commit ff844df

Browse files
[libc] Expand usage of libc null checks. (#116262)
Fixes #111546 --------- Co-authored-by: alyyelashram <150528548+alyyelashram@users.noreply.github.com>
1 parent ec5610c commit ff844df

36 files changed

+216
-3
lines changed

libc/src/string/memccpy.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@
1010

1111
#include "src/__support/common.h"
1212
#include "src/__support/macros/config.h"
13+
#include "src/__support/macros/null_check.h"
1314
#include <stddef.h> // For size_t.
1415

1516
namespace LIBC_NAMESPACE_DECL {
1617

1718
LLVM_LIBC_FUNCTION(void *, memccpy,
1819
(void *__restrict dest, const void *__restrict src, int c,
1920
size_t count)) {
21+
if (count) {
22+
LIBC_CRASH_ON_NULLPTR(dest);
23+
LIBC_CRASH_ON_NULLPTR(src);
24+
}
2025
unsigned char end = static_cast<unsigned char>(c);
2126
const unsigned char *uc_src = static_cast<const unsigned char *>(src);
2227
unsigned char *uc_dest = static_cast<unsigned char *>(dest);

libc/src/string/memchr.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "src/string/memchr.h"
1010
#include "src/__support/macros/config.h"
11+
#include "src/__support/macros/null_check.h"
1112
#include "src/string/string_utils.h"
1213

1314
#include "src/__support/common.h"
@@ -17,6 +18,8 @@ namespace LIBC_NAMESPACE_DECL {
1718

1819
// TODO: Look at performance benefits of comparing words.
1920
LLVM_LIBC_FUNCTION(void *, memchr, (const void *src, int c, size_t n)) {
21+
if (n)
22+
LIBC_CRASH_ON_NULLPTR(src);
2023
return internal::find_first_character(
2124
reinterpret_cast<const unsigned char *>(src),
2225
static_cast<unsigned char>(c), n);

libc/src/string/memcmp.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "src/string/memcmp.h"
1010
#include "src/__support/macros/config.h"
11+
#include "src/__support/macros/null_check.h"
1112
#include "src/string/memory_utils/inline_memcmp.h"
1213

1314
#include <stddef.h> // size_t
@@ -16,6 +17,10 @@ namespace LIBC_NAMESPACE_DECL {
1617

1718
LLVM_LIBC_FUNCTION(int, memcmp,
1819
(const void *lhs, const void *rhs, size_t count)) {
20+
if (count) {
21+
LIBC_CRASH_ON_NULLPTR(lhs);
22+
LIBC_CRASH_ON_NULLPTR(rhs);
23+
}
1924
return inline_memcmp(lhs, rhs, count);
2025
}
2126

libc/src/string/memcpy.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@
99
#include "src/string/memcpy.h"
1010
#include "src/__support/common.h"
1111
#include "src/__support/macros/config.h"
12+
#include "src/__support/macros/null_check.h"
1213
#include "src/string/memory_utils/inline_memcpy.h"
1314

1415
namespace LIBC_NAMESPACE_DECL {
1516

1617
LLVM_LIBC_FUNCTION(void *, memcpy,
1718
(void *__restrict dst, const void *__restrict src,
1819
size_t size)) {
20+
if (size) {
21+
LIBC_CRASH_ON_NULLPTR(dst);
22+
LIBC_CRASH_ON_NULLPTR(src);
23+
}
1924
inline_memcpy(dst, src, size);
2025
return dst;
2126
}

libc/src/string/memmove.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "src/string/memmove.h"
1010
#include "src/__support/macros/config.h"
11+
#include "src/__support/macros/null_check.h"
1112
#include "src/string/memory_utils/inline_memcpy.h"
1213
#include "src/string/memory_utils/inline_memmove.h"
1314
#include <stddef.h> // size_t
@@ -16,6 +17,10 @@ namespace LIBC_NAMESPACE_DECL {
1617

1718
LLVM_LIBC_FUNCTION(void *, memmove,
1819
(void *dst, const void *src, size_t count)) {
20+
if (count) {
21+
LIBC_CRASH_ON_NULLPTR(dst);
22+
LIBC_CRASH_ON_NULLPTR(src);
23+
}
1924
// Memmove may handle some small sizes as efficiently as inline_memcpy.
2025
// For these sizes we may not do is_disjoint check.
2126
// This both avoids additional code for the most frequent smaller sizes

libc/src/string/mempcpy.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "src/string/mempcpy.h"
1010
#include "src/__support/macros/config.h"
11+
#include "src/__support/macros/null_check.h"
1112
#include "src/string/memory_utils/inline_memcpy.h"
1213

1314
#include "src/__support/common.h"
@@ -18,6 +19,10 @@ namespace LIBC_NAMESPACE_DECL {
1819
LLVM_LIBC_FUNCTION(void *, mempcpy,
1920
(void *__restrict dst, const void *__restrict src,
2021
size_t count)) {
22+
if (count) {
23+
LIBC_CRASH_ON_NULLPTR(dst);
24+
LIBC_CRASH_ON_NULLPTR(src);
25+
}
2126
inline_memcpy(dst, src, count);
2227
return reinterpret_cast<char *>(dst) + count;
2328
}

libc/src/string/memrchr.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@
99
#include "src/string/memrchr.h"
1010
#include "src/__support/common.h"
1111
#include "src/__support/macros/config.h"
12+
#include "src/__support/macros/null_check.h"
1213
#include <stddef.h>
1314

1415
namespace LIBC_NAMESPACE_DECL {
1516

1617
LLVM_LIBC_FUNCTION(void *, memrchr, (const void *src, int c, size_t n)) {
18+
19+
if (n)
20+
LIBC_CRASH_ON_NULLPTR(src);
21+
1722
const unsigned char *str = reinterpret_cast<const unsigned char *>(src);
1823
const unsigned char ch = static_cast<unsigned char>(c);
1924
for (; n != 0; --n) {

libc/src/string/memset.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@
99
#include "src/string/memset.h"
1010
#include "src/__support/common.h"
1111
#include "src/__support/macros/config.h"
12+
#include "src/__support/macros/null_check.h"
1213
#include "src/string/memory_utils/inline_memset.h"
1314

1415
namespace LIBC_NAMESPACE_DECL {
1516

1617
LLVM_LIBC_FUNCTION(void *, memset, (void *dst, int value, size_t count)) {
18+
if (count)
19+
LIBC_CRASH_ON_NULLPTR(dst);
20+
1721
inline_memset(dst, static_cast<uint8_t>(value), count);
1822
return dst;
1923
}

libc/src/string/stpncpy.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "src/string/stpncpy.h"
1010
#include "src/__support/macros/config.h"
11+
#include "src/__support/macros/null_check.h"
1112
#include "src/string/memory_utils/inline_bzero.h"
1213

1314
#include "src/__support/common.h"
@@ -17,6 +18,10 @@ namespace LIBC_NAMESPACE_DECL {
1718
LLVM_LIBC_FUNCTION(char *, stpncpy,
1819
(char *__restrict dest, const char *__restrict src,
1920
size_t n)) {
21+
if (n) {
22+
LIBC_CRASH_ON_NULLPTR(dest);
23+
LIBC_CRASH_ON_NULLPTR(src);
24+
}
2025
size_t i;
2126
// Copy up until \0 is found.
2227
for (i = 0; i < n && src[i] != '\0'; ++i)

libc/src/string/strcasestr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "src/__support/common.h"
1212
#include "src/__support/ctype_utils.h"
1313
#include "src/__support/macros/config.h"
14+
#include "src/__support/macros/null_check.h"
1415
#include "src/string/memory_utils/inline_strstr.h"
1516

1617
namespace LIBC_NAMESPACE_DECL {
@@ -23,6 +24,9 @@ LLVM_LIBC_FUNCTION(char *, strcasestr,
2324
return LIBC_NAMESPACE::internal::tolower(a) -
2425
LIBC_NAMESPACE::internal::tolower(b);
2526
};
27+
28+
LIBC_CRASH_ON_NULLPTR(haystack);
29+
LIBC_CRASH_ON_NULLPTR(needle);
2630
return inline_strstr(haystack, needle, case_cmp);
2731
}
2832

0 commit comments

Comments
 (0)