Skip to content

Commit 6edc893

Browse files
committed
Add count checks for memrchr,memset and stpncpy
1 parent 914b4bf commit 6edc893

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

libc/src/string/memrchr.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
namespace LIBC_NAMESPACE_DECL {
1616

1717
LLVM_LIBC_FUNCTION(void *, memrchr, (const void *src, int c, size_t n)) {
18-
LIBC_CRASH_ON_NULLPTR(src);
18+
19+
if (n)
20+
LIBC_CRASH_ON_NULLPTR(src);
1921

2022
const unsigned char *str = reinterpret_cast<const unsigned char *>(src);
2123
const unsigned char ch = static_cast<unsigned char>(c);

libc/src/string/memset.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
namespace LIBC_NAMESPACE_DECL {
1616

1717
LLVM_LIBC_FUNCTION(void *, memset, (void *dst, int value, size_t count)) {
18-
LIBC_CRASH_ON_NULLPTR(dst);
18+
if (count) {
19+
LIBC_CRASH_ON_NULLPTR(dst);
20+
}
1921
inline_memset(dst, static_cast<uint8_t>(value), count);
2022
return dst;
2123
}

libc/src/string/stpncpy.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ namespace LIBC_NAMESPACE_DECL {
1818
LLVM_LIBC_FUNCTION(char *, stpncpy,
1919
(char *__restrict dest, const char *__restrict src,
2020
size_t n)) {
21-
LIBC_CRASH_ON_NULLPTR(dest);
22-
LIBC_CRASH_ON_NULLPTR(src);
21+
if (n) {
22+
LIBC_CRASH_ON_NULLPTR(dest);
23+
LIBC_CRASH_ON_NULLPTR(src);
24+
}
2325
size_t i;
2426
// Copy up until \0 is found.
2527
for (i = 0; i < n && src[i] != '\0'; ++i)

0 commit comments

Comments
 (0)