Skip to content

Commit 45c2974

Browse files
authored
[libc] added nullptr checks for wcspbrk (#142216)
Added CRASH_ON_NULLPTR macro to wcspbrk function and related test
1 parent 9422abf commit 45c2974

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

libc/src/wchar/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ add_entrypoint_object(
7676
DEPENDS
7777
libc.hdr.wchar_macros
7878
libc.src.__support.wctype_utils
79+
libc.src.__support.macros.null_check
7980
)
8081

8182
add_entrypoint_object(

libc/src/wchar/wcspbrk.cpp

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

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

1415
namespace LIBC_NAMESPACE_DECL {
1516

@@ -23,6 +24,9 @@ bool contains_char(const wchar_t *str, wchar_t target) {
2324

2425
LLVM_LIBC_FUNCTION(const wchar_t *, wcspbrk,
2526
(const wchar_t *src, const wchar_t *breakset)) {
27+
LIBC_CRASH_ON_NULLPTR(src);
28+
LIBC_CRASH_ON_NULLPTR(breakset);
29+
2630
// currently O(n * m), can be further optimized to O(n + m) with a hash set
2731
for (int src_idx = 0; src[src_idx] != 0; src_idx++)
2832
if (contains_char(breakset, src[src_idx]))

libc/test/src/wchar/wcspbrk_test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,13 @@ TEST(LlvmLibcWCSPBrkTest, FindsFirstInBreakset) {
6060
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"34"), src + 2);
6161
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"43"), src + 2);
6262
}
63+
64+
#if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER)
65+
TEST(LlvmLibcWCSPBrkTest, NullptrCrash) {
66+
// Passing in a nullptr should crash the program.
67+
EXPECT_DEATH([] { LIBC_NAMESPACE::wcspbrk(L"aaaaaaaaaaaaaa", nullptr); },
68+
WITH_SIGNAL(-1));
69+
EXPECT_DEATH([] { LIBC_NAMESPACE::wcspbrk(nullptr, L"aaaaaaaaaaaaaa"); },
70+
WITH_SIGNAL(-1));
71+
}
72+
#endif // LIBC_HAS_ADDRESS_SANITIZER

0 commit comments

Comments
 (0)