Skip to content

Commit ff844df

Browse files
[libc] Expand usage of libc null checks. (#116262)
Fixes #111546 --------- Co-authored-by: alyyelashram <[email protected]>
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

libc/src/string/strcat.cpp

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

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

@@ -17,6 +18,8 @@ namespace LIBC_NAMESPACE_DECL {
1718

1819
LLVM_LIBC_FUNCTION(char *, strcat,
1920
(char *__restrict dest, const char *__restrict src)) {
21+
LIBC_CRASH_ON_NULLPTR(dest);
22+
LIBC_CRASH_ON_NULLPTR(src);
2023
size_t dest_length = internal::string_length(dest);
2124
size_t src_length = internal::string_length(src);
2225
LIBC_NAMESPACE::strcpy(dest + dest_length, src);

libc/src/string/strcoll.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010

1111
#include "src/__support/common.h"
1212
#include "src/__support/macros/config.h"
13+
#include "src/__support/macros/null_check.h"
1314

1415
namespace LIBC_NAMESPACE_DECL {
1516

1617
// TODO: Add support for locales.
1718
LLVM_LIBC_FUNCTION(int, strcoll, (const char *left, const char *right)) {
19+
LIBC_CRASH_ON_NULLPTR(left);
20+
LIBC_CRASH_ON_NULLPTR(right);
1821
for (; *left && *left == *right; ++left, ++right)
1922
;
2023
return static_cast<int>(*left) - static_cast<int>(*right);

libc/src/string/strcoll_l.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010

1111
#include "src/__support/common.h"
1212
#include "src/__support/macros/config.h"
13+
#include "src/__support/macros/null_check.h"
1314

1415
namespace LIBC_NAMESPACE_DECL {
1516

1617
// TODO: Add support for locales.
1718
LLVM_LIBC_FUNCTION(int, strcoll_l,
1819
(const char *left, const char *right, locale_t)) {
20+
LIBC_CRASH_ON_NULLPTR(left);
21+
LIBC_CRASH_ON_NULLPTR(right);
1922
for (; *left && *left == *right; ++left, ++right)
2023
;
2124
return static_cast<int>(*left) - static_cast<int>(*right);

libc/src/string/strcpy.cpp

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

99
#include "src/string/strcpy.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/string_utils.h"
1314

@@ -17,6 +18,7 @@ namespace LIBC_NAMESPACE_DECL {
1718

1819
LLVM_LIBC_FUNCTION(char *, strcpy,
1920
(char *__restrict dest, const char *__restrict src)) {
21+
LIBC_CRASH_ON_NULLPTR(dest);
2022
size_t size = internal::string_length(src) + 1;
2123
inline_memcpy(dest, src, size);
2224
return dest;

libc/src/string/strlen.cpp

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

99
#include "src/string/strlen.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,7 @@ namespace LIBC_NAMESPACE_DECL {
1718
// TODO: investigate the performance of this function.
1819
// There might be potential for compiler optimization.
1920
LLVM_LIBC_FUNCTION(size_t, strlen, (const char *src)) {
21+
LIBC_CRASH_ON_NULLPTR(src);
2022
return internal::string_length(src);
2123
}
2224

libc/src/string/strncat.cpp

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

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

@@ -18,6 +19,10 @@ namespace LIBC_NAMESPACE_DECL {
1819
LLVM_LIBC_FUNCTION(char *, strncat,
1920
(char *__restrict dest, const char *__restrict src,
2021
size_t count)) {
22+
if (count) {
23+
LIBC_CRASH_ON_NULLPTR(dest);
24+
LIBC_CRASH_ON_NULLPTR(src);
25+
}
2126
size_t src_length = internal::string_length(src);
2227
size_t copy_amount = src_length > count ? count : src_length;
2328
size_t dest_length = internal::string_length(dest);

libc/src/string/strncmp.cpp

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

1111
#include "src/__support/common.h"
1212
#include "src/__support/macros/config.h"
13+
#include "src/__support/macros/null_check.h"
1314
#include "src/string/memory_utils/inline_strcmp.h"
1415

1516
#include <stddef.h>
@@ -18,6 +19,10 @@ namespace LIBC_NAMESPACE_DECL {
1819

1920
LLVM_LIBC_FUNCTION(int, strncmp,
2021
(const char *left, const char *right, size_t n)) {
22+
if (n) {
23+
LIBC_CRASH_ON_NULLPTR(left);
24+
LIBC_CRASH_ON_NULLPTR(right);
25+
}
2126
auto comp = [](char l, char r) -> int { return l - r; };
2227
return inline_strncmp(left, right, n, comp);
2328
}

libc/src/string/strncpy.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(char *, strncpy,
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 = 0;
2126
// Copy up until \0 is found.
2227
for (; i < n && src[i] != '\0'; ++i)

libc/src/string/strsep.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99
#include "src/string/strsep.h"
1010

1111
#include "src/__support/macros/config.h"
12+
#include "src/__support/macros/null_check.h"
1213
#include "src/string/string_utils.h"
1314

1415
namespace LIBC_NAMESPACE_DECL {
1516

1617
LLVM_LIBC_FUNCTION(char *, strsep,
1718
(char **__restrict stringp, const char *__restrict delim)) {
19+
LIBC_CRASH_ON_NULLPTR(stringp);
1820
if (!*stringp)
1921
return nullptr;
22+
LIBC_CRASH_ON_NULLPTR(delim);
2023
return internal::string_token<false>(*stringp, delim, stringp);
2124
}
2225

libc/src/string/strspn.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
#include "src/__support/CPP/bitset.h"
1212
#include "src/__support/common.h"
1313
#include "src/__support/macros/config.h"
14+
#include "src/__support/macros/null_check.h"
1415
#include <stddef.h>
1516

1617
namespace LIBC_NAMESPACE_DECL {
1718

1819
LLVM_LIBC_FUNCTION(size_t, strspn, (const char *src, const char *segment)) {
20+
LIBC_CRASH_ON_NULLPTR(src);
21+
LIBC_CRASH_ON_NULLPTR(segment);
1922
const char *initial = src;
2023
cpp::bitset<256> bitset;
2124

libc/src/string/strstr.cpp

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

1111
#include "src/__support/common.h"
1212
#include "src/__support/macros/config.h"
13+
#include "src/__support/macros/null_check.h"
1314
#include "src/string/memory_utils/inline_strstr.h"
1415

1516
namespace LIBC_NAMESPACE_DECL {
@@ -18,6 +19,8 @@ namespace LIBC_NAMESPACE_DECL {
1819
// improved upon using well known string matching algorithms.
1920
LLVM_LIBC_FUNCTION(char *, strstr, (const char *haystack, const char *needle)) {
2021
auto comp = [](char l, char r) -> int { return l - r; };
22+
LIBC_CRASH_ON_NULLPTR(haystack);
23+
LIBC_CRASH_ON_NULLPTR(needle);
2124
return inline_strstr(haystack, needle, comp);
2225
}
2326

libc/src/strings/rindex.cpp

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

1111
#include "src/__support/common.h"
1212
#include "src/__support/macros/config.h"
13+
#include "src/__support/macros/null_check.h"
1314
#include "src/string/string_utils.h"
1415

1516
namespace LIBC_NAMESPACE_DECL {
1617

1718
LLVM_LIBC_FUNCTION(char *, rindex, (const char *src, int c)) {
19+
LIBC_CRASH_ON_NULLPTR(src);
1820
return internal::strrchr_implementation(src, c);
1921
}
2022

0 commit comments

Comments
 (0)