Skip to content

[libc] added nullptr checks for wcspbrk #142216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/src/wchar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ add_entrypoint_object(
DEPENDS
libc.hdr.wchar_macros
libc.src.__support.wctype_utils
libc.src.__support.macros.null_check
)

add_entrypoint_object(
Expand Down
4 changes: 4 additions & 0 deletions libc/src/wchar/wcspbrk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "hdr/types/wchar_t.h"
#include "src/__support/common.h"
#include "src/__support/macros/null_check.h"

namespace LIBC_NAMESPACE_DECL {

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

LLVM_LIBC_FUNCTION(const wchar_t *, wcspbrk,
(const wchar_t *src, const wchar_t *breakset)) {
LIBC_CRASH_ON_NULLPTR(src);
LIBC_CRASH_ON_NULLPTR(breakset);

// currently O(n * m), can be further optimized to O(n + m) with a hash set
for (int src_idx = 0; src[src_idx] != 0; src_idx++)
if (contains_char(breakset, src[src_idx]))
Expand Down
10 changes: 10 additions & 0 deletions libc/test/src/wchar/wcspbrk_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,13 @@ TEST(LlvmLibcWCSPBrkTest, FindsFirstInBreakset) {
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"34"), src + 2);
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"43"), src + 2);
}

#if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER)
TEST(LlvmLibcWCSPBrkTest, NullptrCrash) {
// Passing in a nullptr should crash the program.
EXPECT_DEATH([] { LIBC_NAMESPACE::wcspbrk(L"aaaaaaaaaaaaaa", nullptr); },
WITH_SIGNAL(-1));
EXPECT_DEATH([] { LIBC_NAMESPACE::wcspbrk(nullptr, L"aaaaaaaaaaaaaa"); },
WITH_SIGNAL(-1));
}
#endif // LIBC_HAS_ADDRESS_SANITIZER
Loading