Skip to content

[libc] Expand usage of libc null checks. #116262

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 59 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
909e318
Add NullChecks to fflush.cpp
AlyElashram Nov 11, 2024
c95c335
Add NullChecks to fopen.cpp
AlyElashram Nov 11, 2024
67e7cf1
Add NullChecks to fprintf.cpp
AlyElashram Nov 11, 2024
1d75d8b
Add NullChecks to fscanf.cpp
AlyElashram Nov 13, 2024
009712f
Add NullChecks to vprintf.cpp
AlyElashram Nov 14, 2024
7ea30f2
Add NullChecks to memchr.cpp
AlyElashram Dec 13, 2024
f60019c
Add NullChecks to memcmp.cpp
AlyElashram Dec 13, 2024
664e3ba
Add NullChecks to memcpy.cpp
AlyElashram Dec 13, 2024
1786835
Add NullChecks to memmove.cpp
AlyElashram Dec 13, 2024
b934143
Add NullChecks to mempcpy.cpp
AlyElashram Dec 13, 2024
56a9bda
Add NullChecks to memrchr.cpp
AlyElashram Dec 13, 2024
1b9902a
Add NullChecks to memset.cpp
AlyElashram Dec 13, 2024
199f029
Add NullChecks to rindex.cpp
AlyElashram Dec 13, 2024
43cd69a
Add NullChecks to stpncpy.cpp
AlyElashram Dec 13, 2024
f9bb084
Add NullChecks to strcat.cpp
AlyElashram Dec 13, 2024
dbec9c4
Add NullChecks to strcoll.cpp and strcoll_l.cpp
AlyElashram Dec 13, 2024
4baf7e2
Add NullChecks to strcpy and strncpy
AlyElashram Dec 13, 2024
d9b3681
Add NullChecks to strsep.cpp
AlyElashram Dec 13, 2024
dafd61a
Add NullChecks to strspn.cpp
AlyElashram Dec 13, 2024
06da963
Revert "Add NullChecks to vprintf.cpp"
AlyElashram Dec 13, 2024
c936096
Revert "Add NullChecks to fscanf.cpp"
AlyElashram Dec 13, 2024
b6dd178
Revert "Add NullChecks to fprintf.cpp"
AlyElashram Dec 13, 2024
ccaec3f
Revert "Add NullChecks to fopen.cpp"
AlyElashram Dec 13, 2024
9f86f3f
Revert "Add NullChecks to fflush.cpp"
AlyElashram Dec 13, 2024
35e0367
Modify NullChecks to cast pointers as it will fail dereferencing a nu…
AlyElashram Dec 13, 2024
1d704e2
Modify Unit Tests to include crashing on nullptrs
AlyElashram Dec 22, 2024
7253320
Formatting
AlyElashram Dec 22, 2024
f842fa9
Patch up memcmp test
AlyElashram Dec 22, 2024
c2ed5dd
Patch up memcpy test
AlyElashram Dec 22, 2024
6cdc4c4
Only USe unit tests for memcmp test suites
AlyElashram Dec 22, 2024
d52bad8
Add Unit Tests to remaining suites
AlyElashram Dec 27, 2024
19ab698
Formatting
AlyElashram Dec 27, 2024
778adf9
add if def guards for linux to all AssertDeath tests
AlyElashram Dec 27, 2024
a600ecd
Add Missing extra lines at the eof
AlyElashram Jan 14, 2025
f88e326
Crash on nullptr in string_length
AlyElashram Jan 26, 2025
15896aa
Remove pattern of making pointer copies
AlyElashram Jan 26, 2025
01ccacd
Convert Seg fault to SIGILL
AlyElashram Jan 26, 2025
7335aac
Convert Seg fault to SIGILL
AlyElashram Jan 26, 2025
41d9a4f
Trap on both pointers in strsep.cpp
AlyElashram Jan 26, 2025
de748e5
Only Crash when count is greater than 0
AlyElashram Feb 7, 2025
82e5418
Formatting
AlyElashram Feb 7, 2025
9baa777
Remove Crashing on null ptr in util function
AlyElashram Feb 7, 2025
bccdf5b
Remove > 0 check (tautology)
AlyElashram Feb 10, 2025
5aa2a30
count instead of c
alyyelashram Apr 17, 2025
d05ef6e
Move inline_strstr.h crash to entrypoints
alyyelashram Apr 17, 2025
961cbce
Change all tests to use ifdef guards
alyyelashram Apr 17, 2025
644770d
Patch Tests' cmake
AlyElashram May 2, 2025
ee9e873
Patch all tests to use the correct ifdef guards and change the signal…
AlyElashram May 2, 2025
e6a336f
Update tests to match any signal
AlyElashram May 30, 2025
b380370
Update memmove to pass a non zero count
AlyElashram May 30, 2025
3fab6bd
Missing braxe in memcpy_test.cpp
AlyElashram May 31, 2025
b72022b
Remove unit test only flag.
AlyElashram May 31, 2025
cee9a32
Update endif comment
AlyElashram May 31, 2025
914b4bf
remove unit test only flag from add_libc_tests
AlyElashram May 31, 2025
6edc893
Add count checks for memrchr,memset and stpncpy
AlyElashram May 31, 2025
8340c41
follow code convention with braces
AlyElashram Jun 2, 2025
50ea5bc
Mass update comments
AlyElashram Jun 4, 2025
5e85e39
Change Mempcpy to be consistent with memcpy
AlyElashram Jun 4, 2025
7c8a0a6
Update mempcpy_test.cpp to have n > 0
AlyElashram Jun 4, 2025
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
5 changes: 5 additions & 0 deletions libc/src/string/memccpy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@

#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/null_check.h"
#include <stddef.h> // For size_t.

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(void *, memccpy,
(void *__restrict dest, const void *__restrict src, int c,
size_t count)) {
if (count) {
LIBC_CRASH_ON_NULLPTR(dest);
LIBC_CRASH_ON_NULLPTR(src);
}
unsigned char end = static_cast<unsigned char>(c);
const unsigned char *uc_src = static_cast<const unsigned char *>(src);
unsigned char *uc_dest = static_cast<unsigned char *>(dest);
Expand Down
3 changes: 3 additions & 0 deletions libc/src/string/memchr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "src/string/memchr.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/null_check.h"
#include "src/string/string_utils.h"

#include "src/__support/common.h"
Expand All @@ -17,6 +18,8 @@ namespace LIBC_NAMESPACE_DECL {

// TODO: Look at performance benefits of comparing words.
LLVM_LIBC_FUNCTION(void *, memchr, (const void *src, int c, size_t n)) {
if (n)
LIBC_CRASH_ON_NULLPTR(src);
return internal::find_first_character(
reinterpret_cast<const unsigned char *>(src),
static_cast<unsigned char>(c), n);
Expand Down
5 changes: 5 additions & 0 deletions libc/src/string/memcmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "src/string/memcmp.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/null_check.h"
#include "src/string/memory_utils/inline_memcmp.h"

#include <stddef.h> // size_t
Expand All @@ -16,6 +17,10 @@ namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, memcmp,
(const void *lhs, const void *rhs, size_t count)) {
if (count) {
LIBC_CRASH_ON_NULLPTR(lhs);
LIBC_CRASH_ON_NULLPTR(rhs);
}
return inline_memcmp(lhs, rhs, count);
}

Expand Down
5 changes: 5 additions & 0 deletions libc/src/string/memcpy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
#include "src/string/memcpy.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/null_check.h"
#include "src/string/memory_utils/inline_memcpy.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(void *, memcpy,
(void *__restrict dst, const void *__restrict src,
size_t size)) {
if (size) {
LIBC_CRASH_ON_NULLPTR(dst);
LIBC_CRASH_ON_NULLPTR(src);
}
inline_memcpy(dst, src, size);
return dst;
}
Expand Down
5 changes: 5 additions & 0 deletions libc/src/string/memmove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "src/string/memmove.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/null_check.h"
#include "src/string/memory_utils/inline_memcpy.h"
#include "src/string/memory_utils/inline_memmove.h"
#include <stddef.h> // size_t
Expand All @@ -16,6 +17,10 @@ namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(void *, memmove,
(void *dst, const void *src, size_t count)) {
if (count) {
LIBC_CRASH_ON_NULLPTR(dst);
LIBC_CRASH_ON_NULLPTR(src);
}
// Memmove may handle some small sizes as efficiently as inline_memcpy.
// For these sizes we may not do is_disjoint check.
// This both avoids additional code for the most frequent smaller sizes
Expand Down
5 changes: 5 additions & 0 deletions libc/src/string/mempcpy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "src/string/mempcpy.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/null_check.h"
#include "src/string/memory_utils/inline_memcpy.h"

#include "src/__support/common.h"
Expand All @@ -18,6 +19,10 @@ namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(void *, mempcpy,
(void *__restrict dst, const void *__restrict src,
size_t count)) {
if (count) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a count check here , for consistency with memcpy and also since memcpy and mempcpy are well behaved at n = 0
@lntue

LIBC_CRASH_ON_NULLPTR(dst);
LIBC_CRASH_ON_NULLPTR(src);
}
inline_memcpy(dst, src, count);
return reinterpret_cast<char *>(dst) + count;
}
Expand Down
5 changes: 5 additions & 0 deletions libc/src/string/memrchr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
#include "src/string/memrchr.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/null_check.h"
#include <stddef.h>

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(void *, memrchr, (const void *src, int c, size_t n)) {

if (n)
LIBC_CRASH_ON_NULLPTR(src);

const unsigned char *str = reinterpret_cast<const unsigned char *>(src);
const unsigned char ch = static_cast<unsigned char>(c);
for (; n != 0; --n) {
Expand Down
4 changes: 4 additions & 0 deletions libc/src/string/memset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
#include "src/string/memset.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/null_check.h"
#include "src/string/memory_utils/inline_memset.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(void *, memset, (void *dst, int value, size_t count)) {
if (count)
LIBC_CRASH_ON_NULLPTR(dst);

inline_memset(dst, static_cast<uint8_t>(value), count);
return dst;
}
Expand Down
5 changes: 5 additions & 0 deletions libc/src/string/stpncpy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "src/string/stpncpy.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/null_check.h"
#include "src/string/memory_utils/inline_bzero.h"

#include "src/__support/common.h"
Expand All @@ -17,6 +18,10 @@ namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(char *, stpncpy,
(char *__restrict dest, const char *__restrict src,
size_t n)) {
if (n) {
LIBC_CRASH_ON_NULLPTR(dest);
LIBC_CRASH_ON_NULLPTR(src);
}
size_t i;
// Copy up until \0 is found.
for (i = 0; i < n && src[i] != '\0'; ++i)
Expand Down
4 changes: 4 additions & 0 deletions libc/src/string/strcasestr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "src/__support/common.h"
#include "src/__support/ctype_utils.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/null_check.h"
#include "src/string/memory_utils/inline_strstr.h"

namespace LIBC_NAMESPACE_DECL {
Expand All @@ -23,6 +24,9 @@ LLVM_LIBC_FUNCTION(char *, strcasestr,
return LIBC_NAMESPACE::internal::tolower(a) -
LIBC_NAMESPACE::internal::tolower(b);
};

LIBC_CRASH_ON_NULLPTR(haystack);
LIBC_CRASH_ON_NULLPTR(needle);
return inline_strstr(haystack, needle, case_cmp);
}

Expand Down
3 changes: 3 additions & 0 deletions libc/src/string/strcat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "src/string/strcat.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/null_check.h"
#include "src/string/strcpy.h"
#include "src/string/string_utils.h"

Expand All @@ -17,6 +18,8 @@ namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(char *, strcat,
(char *__restrict dest, const char *__restrict src)) {
LIBC_CRASH_ON_NULLPTR(dest);
LIBC_CRASH_ON_NULLPTR(src);
size_t dest_length = internal::string_length(dest);
size_t src_length = internal::string_length(src);
LIBC_NAMESPACE::strcpy(dest + dest_length, src);
Expand Down
3 changes: 3 additions & 0 deletions libc/src/string/strcoll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@

#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/null_check.h"

namespace LIBC_NAMESPACE_DECL {

// TODO: Add support for locales.
LLVM_LIBC_FUNCTION(int, strcoll, (const char *left, const char *right)) {
LIBC_CRASH_ON_NULLPTR(left);
LIBC_CRASH_ON_NULLPTR(right);
for (; *left && *left == *right; ++left, ++right)
;
return static_cast<int>(*left) - static_cast<int>(*right);
Expand Down
3 changes: 3 additions & 0 deletions libc/src/string/strcoll_l.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@

#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/null_check.h"

namespace LIBC_NAMESPACE_DECL {

// TODO: Add support for locales.
LLVM_LIBC_FUNCTION(int, strcoll_l,
(const char *left, const char *right, locale_t)) {
LIBC_CRASH_ON_NULLPTR(left);
LIBC_CRASH_ON_NULLPTR(right);
for (; *left && *left == *right; ++left, ++right)
;
return static_cast<int>(*left) - static_cast<int>(*right);
Expand Down
2 changes: 2 additions & 0 deletions libc/src/string/strcpy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "src/string/strcpy.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/null_check.h"
#include "src/string/memory_utils/inline_memcpy.h"
#include "src/string/string_utils.h"

Expand All @@ -17,6 +18,7 @@ namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(char *, strcpy,
(char *__restrict dest, const char *__restrict src)) {
LIBC_CRASH_ON_NULLPTR(dest);
size_t size = internal::string_length(src) + 1;
inline_memcpy(dest, src, size);
return dest;
Expand Down
2 changes: 2 additions & 0 deletions libc/src/string/strlen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "src/string/strlen.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/null_check.h"
#include "src/string/string_utils.h"

#include "src/__support/common.h"
Expand All @@ -17,6 +18,7 @@ namespace LIBC_NAMESPACE_DECL {
// TODO: investigate the performance of this function.
// There might be potential for compiler optimization.
LLVM_LIBC_FUNCTION(size_t, strlen, (const char *src)) {
LIBC_CRASH_ON_NULLPTR(src);
return internal::string_length(src);
}

Expand Down
5 changes: 5 additions & 0 deletions libc/src/string/strncat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "src/string/strncat.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/null_check.h"
#include "src/string/string_utils.h"
#include "src/string/strncpy.h"

Expand All @@ -18,6 +19,10 @@ namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(char *, strncat,
(char *__restrict dest, const char *__restrict src,
size_t count)) {
if (count) {
LIBC_CRASH_ON_NULLPTR(dest);
LIBC_CRASH_ON_NULLPTR(src);
}
size_t src_length = internal::string_length(src);
size_t copy_amount = src_length > count ? count : src_length;
size_t dest_length = internal::string_length(dest);
Expand Down
5 changes: 5 additions & 0 deletions libc/src/string/strncmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/null_check.h"
#include "src/string/memory_utils/inline_strcmp.h"

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

LLVM_LIBC_FUNCTION(int, strncmp,
(const char *left, const char *right, size_t n)) {
if (n) {
LIBC_CRASH_ON_NULLPTR(left);
LIBC_CRASH_ON_NULLPTR(right);
}
auto comp = [](char l, char r) -> int { return l - r; };
return inline_strncmp(left, right, n, comp);
}
Expand Down
5 changes: 5 additions & 0 deletions libc/src/string/strncpy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@

#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/null_check.h"
#include <stddef.h> // For size_t.

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(char *, strncpy,
(char *__restrict dest, const char *__restrict src,
size_t n)) {
if (n) {
LIBC_CRASH_ON_NULLPTR(dest);
LIBC_CRASH_ON_NULLPTR(src);
}
size_t i = 0;
// Copy up until \0 is found.
for (; i < n && src[i] != '\0'; ++i)
Expand Down
3 changes: 3 additions & 0 deletions libc/src/string/strsep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
#include "src/string/strsep.h"

#include "src/__support/macros/config.h"
#include "src/__support/macros/null_check.h"
#include "src/string/string_utils.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(char *, strsep,
(char **__restrict stringp, const char *__restrict delim)) {
LIBC_CRASH_ON_NULLPTR(stringp);
if (!*stringp)
return nullptr;
LIBC_CRASH_ON_NULLPTR(delim);
return internal::string_token<false>(*stringp, delim, stringp);
}

Expand Down
3 changes: 3 additions & 0 deletions libc/src/string/strspn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
#include "src/__support/CPP/bitset.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/null_check.h"
#include <stddef.h>

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(size_t, strspn, (const char *src, const char *segment)) {
LIBC_CRASH_ON_NULLPTR(src);
LIBC_CRASH_ON_NULLPTR(segment);
const char *initial = src;
cpp::bitset<256> bitset;

Expand Down
3 changes: 3 additions & 0 deletions libc/src/string/strstr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/null_check.h"
#include "src/string/memory_utils/inline_strstr.h"

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

Expand Down
2 changes: 2 additions & 0 deletions libc/src/strings/rindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@

#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/null_check.h"
#include "src/string/string_utils.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(char *, rindex, (const char *src, int c)) {
LIBC_CRASH_ON_NULLPTR(src);
return internal::strrchr_implementation(src, c);
}

Expand Down
Loading
Loading