Skip to content

Commit 328f40f

Browse files
sribee8Sriya Pratipati
and
Sriya Pratipati
authored
[libc] wmemcmp nullptr handling (#142058)
Added nullptr handling for wmemcmp --------- Co-authored-by: Sriya Pratipati <sriyap@google.com>
1 parent 4a7b53f commit 328f40f

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

libc/src/wchar/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ add_entrypoint_object(
9090
libc.hdr.types.size_t
9191
libc.hdr.wchar_macros
9292
libc.src.__support.wctype_utils
93+
libc.src.__support.macros.null_check
9394
)
9495

9596
add_entrypoint_object(

libc/src/wchar/wmemcmp.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
#include "hdr/types/wchar_t.h"
1313
#include "src/__support/common.h"
1414
#include "src/__support/macros/config.h"
15+
#include "src/__support/macros/null_check.h" // LIBC_CRASH_ON_NULLPTR
1516

1617
namespace LIBC_NAMESPACE_DECL {
1718

1819
LLVM_LIBC_FUNCTION(int, wmemcmp,
1920
(const wchar_t *s1, const wchar_t *s2, size_t n)) {
21+
LIBC_CRASH_ON_NULLPTR(s1);
22+
LIBC_CRASH_ON_NULLPTR(s2);
2023
for (size_t i = 0; i < n; ++i) {
2124
if (s1[i] != s2[i])
2225
return (int)(s1[i] - s2[i]);

libc/test/src/wchar/wmemcmp_test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,13 @@ TEST(LlvmLibcWMemcmpTest, LhsRhsAreTheSameLong) {
6666
const wchar_t *rhs = L"aaaaaaaaaaaaaa";
6767
EXPECT_EQ(LIBC_NAMESPACE::wmemcmp(lhs, rhs, 15), 0);
6868
}
69+
70+
#if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER)
71+
TEST(LlvmLibcWMemcmpTest, NullptrCrash) {
72+
// Passing in a nullptr should crash the program.
73+
EXPECT_DEATH([] { LIBC_NAMESPACE::wmemcmp(L"aaaaaaaaaaaaaa", nullptr, 15); },
74+
WITH_SIGNAL(-1));
75+
EXPECT_DEATH([] { LIBC_NAMESPACE::wmemcmp(nullptr, L"aaaaaaaaaaaaaa", 15); },
76+
WITH_SIGNAL(-1));
77+
}
78+
#endif // LIBC_HAS_ADDRESS_SANITIZER

0 commit comments

Comments
 (0)