[libc-commits] [libc] [libc] Expand usage of libc null checks. (PR #116262)

Aly ElAshram via libc-commits libc-commits at lists.llvm.org
Sun Dec 22 14:15:22 PST 2024


https://github.com/AlyElashram updated https://github.com/llvm/llvm-project/pull/116262

>From 9b75c8abd38b14e1d8c90cc571d3d323f75903f4 Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Mon, 11 Nov 2024 20:18:21 +0200
Subject: [PATCH 01/32] Add NullChecks to fflush.cpp

---
 libc/src/stdio/generic/fflush.cpp | 3 +++
 libc/src/stdio/gpu/fflush.cpp     | 3 +++
 2 files changed, 6 insertions(+)

diff --git a/libc/src/stdio/generic/fflush.cpp b/libc/src/stdio/generic/fflush.cpp
index 5bdf71ad359406..8c71fbbd3d0999 100644
--- a/libc/src/stdio/generic/fflush.cpp
+++ b/libc/src/stdio/generic/fflush.cpp
@@ -11,11 +11,14 @@
 
 #include "hdr/types/FILE.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
 #include "src/errno/libc_errno.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, fflush, (::FILE * stream)) {
+
+  LIBC_CRASH_ON_NULLPTR(stream);
   int result = reinterpret_cast<LIBC_NAMESPACE::File *>(stream)->flush();
   if (result != 0) {
     libc_errno = result;
diff --git a/libc/src/stdio/gpu/fflush.cpp b/libc/src/stdio/gpu/fflush.cpp
index 5a5137be6a4af0..47843f4d08093b 100644
--- a/libc/src/stdio/gpu/fflush.cpp
+++ b/libc/src/stdio/gpu/fflush.cpp
@@ -9,12 +9,15 @@
 #include "src/stdio/fflush.h"
 #include "file.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
 
 #include "hdr/types/FILE.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, fflush, (::FILE * stream)) {
+
+  LIBC_CRASH_ON_NULLPTR(stream);
   int ret;
   rpc::Client::Port port = rpc::client.open<LIBC_FFLUSH>();
   port.send_and_recv(

>From 2829d8769aa816fa61c8f0ae59056f0577c90cc0 Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Mon, 11 Nov 2024 20:26:43 +0200
Subject: [PATCH 02/32] Add NullChecks to fopen.cpp

---
 libc/src/stdio/generic/fopen.cpp | 3 +++
 libc/src/stdio/gpu/fopen.cpp     | 3 +++
 2 files changed, 6 insertions(+)

diff --git a/libc/src/stdio/generic/fopen.cpp b/libc/src/stdio/generic/fopen.cpp
index d6e418bacf37e4..91219cb722cf59 100644
--- a/libc/src/stdio/generic/fopen.cpp
+++ b/libc/src/stdio/generic/fopen.cpp
@@ -11,12 +11,15 @@
 
 #include "hdr/types/FILE.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
 #include "src/errno/libc_errno.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(::FILE *, fopen,
                    (const char *__restrict name, const char *__restrict mode)) {
+  LIBC_CRASH_ON_NULLPTR(name);
+  LIBC_CRASH_ON_NULLPTR(mode);
   auto result = LIBC_NAMESPACE::openfile(name, mode);
   if (!result.has_value()) {
     libc_errno = result.error();
diff --git a/libc/src/stdio/gpu/fopen.cpp b/libc/src/stdio/gpu/fopen.cpp
index 18dd7195377893..dfba587de18f31 100644
--- a/libc/src/stdio/gpu/fopen.cpp
+++ b/libc/src/stdio/gpu/fopen.cpp
@@ -9,6 +9,7 @@
 #include "src/stdio/fopen.h"
 #include "src/__support/CPP/string_view.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
 #include "src/stdio/gpu/file.h"
 
 #include "hdr/types/FILE.h"
@@ -17,6 +18,8 @@ namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(::FILE *, fopen,
                    (const char *__restrict path, const char *__restrict mode)) {
+  LIBC_CRASH_ON_NULLPTR(path);
+  LIBC_CRASH_ON_NULLPTR(mode);
   uintptr_t file;
   rpc::Client::Port port = rpc::client.open<LIBC_OPEN_FILE>();
   port.send_n(path, internal::string_length(path) + 1);

>From b8e48a9bf302704018150ea021a5a65152bcb712 Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Mon, 11 Nov 2024 20:36:34 +0200
Subject: [PATCH 03/32] Add NullChecks to fprintf.cpp

---
 libc/src/stdio/generic/fprintf.cpp | 3 +++
 libc/src/stdio/gpu/fprintf.cpp     | 3 +++
 2 files changed, 6 insertions(+)

diff --git a/libc/src/stdio/generic/fprintf.cpp b/libc/src/stdio/generic/fprintf.cpp
index 087aeadfc52c5c..478093355f3cd0 100644
--- a/libc/src/stdio/generic/fprintf.cpp
+++ b/libc/src/stdio/generic/fprintf.cpp
@@ -11,6 +11,7 @@
 #include "src/__support/File/file.h"
 #include "src/__support/arg_list.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
 #include "src/stdio/printf_core/vfprintf_internal.h"
 
 #include "hdr/types/FILE.h"
@@ -21,6 +22,8 @@ namespace LIBC_NAMESPACE_DECL {
 LLVM_LIBC_FUNCTION(int, fprintf,
                    (::FILE *__restrict stream, const char *__restrict format,
                     ...)) {
+  LIBC_CRASH_ON_NULLPTR(stream);
+  LIBC_CRASH_ON_NULLPTR(format);
   va_list vlist;
   va_start(vlist, format);
   internal::ArgList args(vlist); // This holder class allows for easier copying
diff --git a/libc/src/stdio/gpu/fprintf.cpp b/libc/src/stdio/gpu/fprintf.cpp
index 46196d7d2b10f5..3273277be1c585 100644
--- a/libc/src/stdio/gpu/fprintf.cpp
+++ b/libc/src/stdio/gpu/fprintf.cpp
@@ -11,6 +11,7 @@
 #include "hdr/types/FILE.h"
 #include "src/__support/CPP/string_view.h"
 #include "src/__support/arg_list.h"
+#include "src/__support/macros/null_check.h"
 #include "src/errno/libc_errno.h"
 #include "src/stdio/gpu/vfprintf_utils.h"
 
@@ -21,6 +22,8 @@ namespace LIBC_NAMESPACE_DECL {
 LLVM_LIBC_FUNCTION(int, fprintf,
                    (::FILE *__restrict stream, const char *__restrict format,
                     ...)) {
+  LIBC_CRASH_ON_NULLPTR(stream);
+  LIBC_CRASH_ON_NULLPTR(format);
   va_list vlist;
   va_start(vlist, format);
   cpp::string_view str_view(format);

>From 043ed9eea917589c83b80186b9b68f8aeaf4fea0 Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Thu, 14 Nov 2024 00:57:22 +0200
Subject: [PATCH 04/32] Add NullChecks to fscanf.cpp

---
 libc/src/stdio/fscanf.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/libc/src/stdio/fscanf.cpp b/libc/src/stdio/fscanf.cpp
index 94b843978749e2..c79c92671b659f 100644
--- a/libc/src/stdio/fscanf.cpp
+++ b/libc/src/stdio/fscanf.cpp
@@ -11,6 +11,7 @@
 #include "src/__support/File/file.h"
 #include "src/__support/arg_list.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
 #include "src/stdio/scanf_core/vfscanf_internal.h"
 
 #include "hdr/types/FILE.h"
@@ -21,6 +22,8 @@ namespace LIBC_NAMESPACE_DECL {
 LLVM_LIBC_FUNCTION(int, fscanf,
                    (::FILE *__restrict stream, const char *__restrict format,
                     ...)) {
+  LIBC_CRASH_ON_NULLPTR(stream);
+  LIBC_CRASH_ON_NULLPTR(format);
   va_list vlist;
   va_start(vlist, format);
   internal::ArgList args(vlist); // This holder class allows for easier copying

>From 26f370f14e264d98d2845ccfc214a2891d1fe240 Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Thu, 14 Nov 2024 18:40:11 +0200
Subject: [PATCH 05/32] Add NullChecks to vprintf.cpp

---
 libc/src/stdio/baremetal/vprintf.cpp | 2 ++
 libc/src/stdio/generic/vprintf.cpp   | 2 ++
 libc/src/stdio/gpu/vprintf.cpp       | 2 ++
 3 files changed, 6 insertions(+)

diff --git a/libc/src/stdio/baremetal/vprintf.cpp b/libc/src/stdio/baremetal/vprintf.cpp
index 617b5f488e7728..c80f6052efea3a 100644
--- a/libc/src/stdio/baremetal/vprintf.cpp
+++ b/libc/src/stdio/baremetal/vprintf.cpp
@@ -10,6 +10,7 @@
 #include "src/__support/OSUtil/io.h"
 #include "src/__support/arg_list.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
 #include "src/stdio/printf_core/core_structs.h"
 #include "src/stdio/printf_core/printf_main.h"
 #include "src/stdio/printf_core/writer.h"
@@ -30,6 +31,7 @@ LIBC_INLINE int raw_write_hook(cpp::string_view new_str, void *) {
 
 LLVM_LIBC_FUNCTION(int, vprintf,
                    (const char *__restrict format, va_list vlist)) {
+  LIBC_CRASH_ON_NULLPTR(format);
   internal::ArgList args(vlist); // This holder class allows for easier copying
                                  // and pointer semantics, as well as handling
                                  // destruction automatically.
diff --git a/libc/src/stdio/generic/vprintf.cpp b/libc/src/stdio/generic/vprintf.cpp
index 08d71515646ed0..6c51ba31319980 100644
--- a/libc/src/stdio/generic/vprintf.cpp
+++ b/libc/src/stdio/generic/vprintf.cpp
@@ -11,6 +11,7 @@
 #include "src/__support/File/file.h"
 #include "src/__support/arg_list.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
 #include "src/stdio/printf_core/vfprintf_internal.h"
 
 #include "hdr/types/FILE.h"
@@ -26,6 +27,7 @@ namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, vprintf,
                    (const char *__restrict format, va_list vlist)) {
+  LIBC_CRASH_ON_NULLPTR(format);
   internal::ArgList args(vlist); // This holder class allows for easier copying
                                  // and pointer semantics, as well as handling
                                  // destruction automatically.
diff --git a/libc/src/stdio/gpu/vprintf.cpp b/libc/src/stdio/gpu/vprintf.cpp
index 54012f3071844d..49391aa4f3f127 100644
--- a/libc/src/stdio/gpu/vprintf.cpp
+++ b/libc/src/stdio/gpu/vprintf.cpp
@@ -10,6 +10,7 @@
 
 #include "src/__support/CPP/string_view.h"
 #include "src/__support/arg_list.h"
+#include "src/__support/macros/null_check.h"
 #include "src/errno/libc_errno.h"
 #include "src/stdio/gpu/vfprintf_utils.h"
 
@@ -17,6 +18,7 @@ namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, vprintf,
                    (const char *__restrict format, va_list vlist)) {
+  LIBC_CRASH_ON_NULLPTR(format);
   cpp::string_view str_view(format);
   int ret_val = vfprintf_internal(stdout, format, str_view.size() + 1, vlist);
   return ret_val;

>From 178976fdb83cf2d925b56c577746f6ab9430d4b2 Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Fri, 13 Dec 2024 18:16:36 +0200
Subject: [PATCH 06/32] Add NullChecks to memory_utils

---
 libc/src/string/memory_utils/inline_strcmp.h | 5 +++++
 libc/src/string/memory_utils/inline_strstr.h | 3 +++
 2 files changed, 8 insertions(+)

diff --git a/libc/src/string/memory_utils/inline_strcmp.h b/libc/src/string/memory_utils/inline_strcmp.h
index 281d5b14c6cbac..db0bba909a539a 100644
--- a/libc/src/string/memory_utils/inline_strcmp.h
+++ b/libc/src/string/memory_utils/inline_strcmp.h
@@ -10,6 +10,7 @@
 #define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_INLINE_STRCMP_H
 
 #include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
 #include <stddef.h>
 
 namespace LIBC_NAMESPACE_DECL {
@@ -17,6 +18,8 @@ namespace LIBC_NAMESPACE_DECL {
 template <typename Comp>
 LIBC_INLINE constexpr int inline_strcmp(const char *left, const char *right,
                                         Comp &&comp) {
+  LIBC_CRASH_ON_NULLPTR(left);
+  LIBC_CRASH_ON_NULLPTR(right);
   // TODO: Look at benefits for comparing words at a time.
   for (; *left && !comp(*left, *right); ++left, ++right)
     ;
@@ -27,6 +30,8 @@ LIBC_INLINE constexpr int inline_strcmp(const char *left, const char *right,
 template <typename Comp>
 LIBC_INLINE constexpr int inline_strncmp(const char *left, const char *right,
                                          size_t n, Comp &&comp) {
+  LIBC_CRASH_ON_NULLPTR(left);
+  LIBC_CRASH_ON_NULLPTR(right);
   if (n == 0)
     return 0;
 
diff --git a/libc/src/string/memory_utils/inline_strstr.h b/libc/src/string/memory_utils/inline_strstr.h
index 9c99e920b4b570..8b00f8565376c3 100644
--- a/libc/src/string/memory_utils/inline_strstr.h
+++ b/libc/src/string/memory_utils/inline_strstr.h
@@ -10,6 +10,7 @@
 #define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_INLINE_STRSTR_H
 
 #include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
 #include "src/string/memory_utils/inline_memmem.h"
 #include "src/string/string_utils.h"
 #include <stddef.h>
@@ -19,6 +20,8 @@ namespace LIBC_NAMESPACE_DECL {
 template <typename Comp>
 LIBC_INLINE constexpr char *inline_strstr(const char *haystack,
                                           const char *needle, Comp &&comp) {
+  LIBC_CRASH_ON_NULLPTR(haystack);
+  LIBC_CRASH_ON_NULLPTR(needle);
   void *result = inline_memmem(
       static_cast<const void *>(haystack), internal::string_length(haystack),
       static_cast<const void *>(needle), internal::string_length(needle), comp);

>From 8036f44e5436c90c1a795b6de126289001069536 Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Fri, 13 Dec 2024 18:16:48 +0200
Subject: [PATCH 07/32] Add NullChecks to string_utils.h

---
 libc/src/string/string_utils.h | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/libc/src/string/string_utils.h b/libc/src/string/string_utils.h
index fc617bd18e8f64..51a3769759a280 100644
--- a/libc/src/string/string_utils.h
+++ b/libc/src/string/string_utils.h
@@ -16,6 +16,7 @@
 
 #include "src/__support/CPP/bitset.h"
 #include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
 #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
 #include "src/string/memory_utils/inline_bzero.h"
 #include "src/string/memory_utils/inline_memcpy.h"
@@ -89,6 +90,7 @@ LIBC_INLINE size_t string_length_byte_read(const char *src) {
 // Returns the length of a string, denoted by the first occurrence
 // of a null terminator.
 LIBC_INLINE size_t string_length(const char *src) {
+  LIBC_CRASH_ON_NULLPTR(src);
 #ifdef LIBC_COPT_STRING_UNSAFE_WIDE_READ
   // Unsigned int is the default size for most processors, and on x86-64 it
   // performs better than larger sizes when the src pointer can't be assumed to
@@ -144,6 +146,7 @@ LIBC_INLINE void *find_first_character_byte_read(const unsigned char *src,
 // 'src'. If 'ch' is not found, returns nullptr.
 LIBC_INLINE void *find_first_character(const unsigned char *src,
                                        unsigned char ch, size_t max_strlen) {
+  LIBC_CRASH_ON_NULLPTR(src);
 #ifdef LIBC_COPT_STRING_UNSAFE_WIDE_READ
   // If the maximum size of the string is small, the overhead of aligning to a
   // word boundary and generating a bitmask of the appropriate size may be
@@ -163,6 +166,8 @@ LIBC_INLINE void *find_first_character(const unsigned char *src,
 // Returns the maximum length span that contains only characters not found in
 // 'segment'. If no characters are found, returns the length of 'src'.
 LIBC_INLINE size_t complementary_span(const char *src, const char *segment) {
+  LIBC_CRASH_ON_NULLPTR(src);
+  LIBC_CRASH_ON_NULLPTR(segment);
   const char *initial = src;
   cpp::bitset<256> bitset;
 
@@ -216,6 +221,8 @@ LIBC_INLINE char *string_token(char *__restrict src,
 
 LIBC_INLINE size_t strlcpy(char *__restrict dst, const char *__restrict src,
                            size_t size) {
+  LIBC_CRASH_ON_NULLPTR(dst);
+  LIBC_CRASH_ON_NULLPTR(src);
   size_t len = internal::string_length(src);
   if (!size)
     return len;
@@ -228,6 +235,7 @@ LIBC_INLINE size_t strlcpy(char *__restrict dst, const char *__restrict src,
 template <bool ReturnNull = true>
 LIBC_INLINE constexpr static char *strchr_implementation(const char *src,
                                                          int c) {
+  LIBC_CRASH_ON_NULLPTR(src);
   char ch = static_cast<char>(c);
   for (; *src && *src != ch; ++src)
     ;
@@ -237,6 +245,7 @@ LIBC_INLINE constexpr static char *strchr_implementation(const char *src,
 
 LIBC_INLINE constexpr static char *strrchr_implementation(const char *src,
                                                           int c) {
+  LIBC_CRASH_ON_NULLPTR(src);
   char ch = static_cast<char>(c);
   char *last_occurrence = nullptr;
   while (true) {

>From d485ba2753443a5c775d4f0802a0f0526201bbab Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Fri, 13 Dec 2024 18:17:13 +0200
Subject: [PATCH 08/32] Add NullChecks to memchr.cpp

---
 libc/src/string/memchr.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libc/src/string/memchr.cpp b/libc/src/string/memchr.cpp
index ba52f14afa9d65..dcc5ee99bbe545 100644
--- a/libc/src/string/memchr.cpp
+++ b/libc/src/string/memchr.cpp
@@ -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"
@@ -17,6 +18,7 @@ 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)) {
+  LIBC_CRASH_ON_NULLPTR(src);
   return internal::find_first_character(
       reinterpret_cast<const unsigned char *>(src),
       static_cast<unsigned char>(c), n);

>From f453fe664d4f1a4dff36da467b903f4eead82dff Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Fri, 13 Dec 2024 18:17:20 +0200
Subject: [PATCH 09/32] Add NullChecks to memcmp.cpp

---
 libc/src/string/memcmp.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/libc/src/string/memcmp.cpp b/libc/src/string/memcmp.cpp
index 68996fb7872368..71c6840d14ca83 100644
--- a/libc/src/string/memcmp.cpp
+++ b/libc/src/string/memcmp.cpp
@@ -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
@@ -16,6 +17,8 @@ namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, memcmp,
                    (const void *lhs, const void *rhs, size_t count)) {
+  LIBC_CRASH_ON_NULLPTR(lhs);
+  LIBC_CRASH_ON_NULLPTR(rhs);
   return inline_memcmp(lhs, rhs, count);
 }
 

>From d48e042eddd3703da0dbe6e849eda233e5a03bf1 Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Fri, 13 Dec 2024 18:17:27 +0200
Subject: [PATCH 10/32] Add NullChecks to memcpy.cpp

---
 libc/src/string/memcpy.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/libc/src/string/memcpy.cpp b/libc/src/string/memcpy.cpp
index 0eb7f2c170e085..9f0406d08d6456 100644
--- a/libc/src/string/memcpy.cpp
+++ b/libc/src/string/memcpy.cpp
@@ -9,6 +9,7 @@
 #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 {
@@ -16,6 +17,8 @@ namespace LIBC_NAMESPACE_DECL {
 LLVM_LIBC_FUNCTION(void *, memcpy,
                    (void *__restrict dst, const void *__restrict src,
                     size_t size)) {
+  LIBC_CRASH_ON_NULLPTR(dst);
+  LIBC_CRASH_ON_NULLPTR(src);
   inline_memcpy(dst, src, size);
   return dst;
 }

>From e89c1b70869279c15a0b5297b046863d6d30b169 Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Fri, 13 Dec 2024 18:17:37 +0200
Subject: [PATCH 11/32] Add NullChecks to memmove.cpp

---
 libc/src/string/memmove.cpp | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/libc/src/string/memmove.cpp b/libc/src/string/memmove.cpp
index 26a8c4187f3d31..ca77795cc16474 100644
--- a/libc/src/string/memmove.cpp
+++ b/libc/src/string/memmove.cpp
@@ -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
@@ -16,6 +17,9 @@ namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(void *, memmove,
                    (void *dst, const void *src, size_t 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

>From f37ba5188c10f3e8c7afb6914a86eaa0c096961b Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Fri, 13 Dec 2024 18:17:47 +0200
Subject: [PATCH 12/32] Add NullChecks to mempcpy.cpp

---
 libc/src/string/mempcpy.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/libc/src/string/mempcpy.cpp b/libc/src/string/mempcpy.cpp
index 09392ceb966d6b..a7b5c7efbadb72 100644
--- a/libc/src/string/mempcpy.cpp
+++ b/libc/src/string/mempcpy.cpp
@@ -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"
@@ -18,6 +19,8 @@ namespace LIBC_NAMESPACE_DECL {
 LLVM_LIBC_FUNCTION(void *, mempcpy,
                    (void *__restrict dst, const void *__restrict src,
                     size_t count)) {
+  LIBC_CRASH_ON_NULLPTR(dst);
+  LIBC_CRASH_ON_NULLPTR(src);
   inline_memcpy(dst, src, count);
   return reinterpret_cast<char *>(dst) + count;
 }

>From 5b0ce97435f662a74dc45e3b26e047f4e9117c5b Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Fri, 13 Dec 2024 18:17:52 +0200
Subject: [PATCH 13/32] Add NullChecks to memrchr.cpp

---
 libc/src/string/memrchr.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libc/src/string/memrchr.cpp b/libc/src/string/memrchr.cpp
index d665e225bbb7dd..319bec97ae7177 100644
--- a/libc/src/string/memrchr.cpp
+++ b/libc/src/string/memrchr.cpp
@@ -9,11 +9,13 @@
 #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)) {
+  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) {

>From c9371cdb6689bbdb87b0f47a0932a6f5b0c5f99d Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Fri, 13 Dec 2024 18:17:57 +0200
Subject: [PATCH 14/32] Add NullChecks to memset.cpp

---
 libc/src/string/memset.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libc/src/string/memset.cpp b/libc/src/string/memset.cpp
index c2868afa91031d..53448af9d97945 100644
--- a/libc/src/string/memset.cpp
+++ b/libc/src/string/memset.cpp
@@ -9,11 +9,13 @@
 #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)) {
+  LIBC_CRASH_ON_NULLPTR(dst);
   inline_memset(dst, static_cast<uint8_t>(value), count);
   return dst;
 }

>From f2a0b5eadd95026a9ad3f8c69f0943b863d80ecf Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Fri, 13 Dec 2024 18:18:04 +0200
Subject: [PATCH 15/32] Add NullChecks to rindex.cpp

---
 libc/src/strings/rindex.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libc/src/strings/rindex.cpp b/libc/src/strings/rindex.cpp
index 1242e0faf85fc5..2540222ffb0eb6 100644
--- a/libc/src/strings/rindex.cpp
+++ b/libc/src/strings/rindex.cpp
@@ -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);
 }
 

>From 1f552b924cb2d5000ffb9e47fe174aead28f4b27 Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Fri, 13 Dec 2024 18:18:09 +0200
Subject: [PATCH 16/32] Add NullChecks to stpncpy.cpp

---
 libc/src/string/stpncpy.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/libc/src/string/stpncpy.cpp b/libc/src/string/stpncpy.cpp
index d2a6e04749820e..b545b5de777d21 100644
--- a/libc/src/string/stpncpy.cpp
+++ b/libc/src/string/stpncpy.cpp
@@ -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"
@@ -17,6 +18,8 @@ namespace LIBC_NAMESPACE_DECL {
 LLVM_LIBC_FUNCTION(char *, stpncpy,
                    (char *__restrict dest, const char *__restrict src,
                     size_t 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)

>From 450f8cd500546a5585b505b6a92dcc40391dc4ed Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Fri, 13 Dec 2024 18:18:14 +0200
Subject: [PATCH 17/32] Add NullChecks to strcat.cpp

---
 libc/src/string/strcat.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/libc/src/string/strcat.cpp b/libc/src/string/strcat.cpp
index 0eb189ce204f0c..6a6f068bd47594 100644
--- a/libc/src/string/strcat.cpp
+++ b/libc/src/string/strcat.cpp
@@ -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"
 
@@ -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);

>From 4df5939536a12f36f28c53530a2a65ffbf5be101 Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Fri, 13 Dec 2024 18:18:30 +0200
Subject: [PATCH 18/32] Add NullChecks to strcoll.cpp and strcoll_l.cpp

---
 libc/src/string/strcoll.cpp   | 3 +++
 libc/src/string/strcoll_l.cpp | 3 +++
 2 files changed, 6 insertions(+)

diff --git a/libc/src/string/strcoll.cpp b/libc/src/string/strcoll.cpp
index eeb2c79e380742..aa08f7194c9e52 100644
--- a/libc/src/string/strcoll.cpp
+++ b/libc/src/string/strcoll.cpp
@@ -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);
diff --git a/libc/src/string/strcoll_l.cpp b/libc/src/string/strcoll_l.cpp
index f664a3c7c03f37..e820efa564a3db 100644
--- a/libc/src/string/strcoll_l.cpp
+++ b/libc/src/string/strcoll_l.cpp
@@ -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);

>From 981eab0dba78f4fa6ffcb57f16036343de42e396 Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Fri, 13 Dec 2024 18:18:41 +0200
Subject: [PATCH 19/32] Add NullChecks to strcpy and strncpy

---
 libc/src/string/strcpy.cpp  | 2 ++
 libc/src/string/strncpy.cpp | 3 +++
 2 files changed, 5 insertions(+)

diff --git a/libc/src/string/strcpy.cpp b/libc/src/string/strcpy.cpp
index 60b73ab3aa823f..2013593db24a6e 100644
--- a/libc/src/string/strcpy.cpp
+++ b/libc/src/string/strcpy.cpp
@@ -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"
 
@@ -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;
diff --git a/libc/src/string/strncpy.cpp b/libc/src/string/strncpy.cpp
index 4976ad94708c71..303ec4f44880e1 100644
--- a/libc/src/string/strncpy.cpp
+++ b/libc/src/string/strncpy.cpp
@@ -10,6 +10,7 @@
 
 #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 {
@@ -17,6 +18,8 @@ namespace LIBC_NAMESPACE_DECL {
 LLVM_LIBC_FUNCTION(char *, strncpy,
                    (char *__restrict dest, const char *__restrict src,
                     size_t 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)

>From beb57d9b01de24f519ef45324a12fe746456751d Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Fri, 13 Dec 2024 18:18:51 +0200
Subject: [PATCH 20/32] Add NullChecks to strsep.cpp

---
 libc/src/string/strsep.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libc/src/string/strsep.cpp b/libc/src/string/strsep.cpp
index 4c275122de52fc..976fefe1008bc2 100644
--- a/libc/src/string/strsep.cpp
+++ b/libc/src/string/strsep.cpp
@@ -9,6 +9,7 @@
 #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 {
@@ -17,6 +18,7 @@ LLVM_LIBC_FUNCTION(char *, strsep,
                    (char **__restrict stringp, const char *__restrict delim)) {
   if (!*stringp)
     return nullptr;
+  LIBC_CRASH_ON_NULLPTR(delim);
   return internal::string_token<false>(*stringp, delim, stringp);
 }
 

>From f7da133623882cbf3b5f15ac7cae78bac110df03 Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Fri, 13 Dec 2024 18:18:59 +0200
Subject: [PATCH 21/32] Add NullChecks to strspn.cpp

---
 libc/src/string/strspn.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/libc/src/string/strspn.cpp b/libc/src/string/strspn.cpp
index 66bb39903ab382..b205bedf80caba 100644
--- a/libc/src/string/strspn.cpp
+++ b/libc/src/string/strspn.cpp
@@ -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;
 

>From 4f8ff81343cedb8fb250eb2d4b66081f0ebffd64 Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Fri, 13 Dec 2024 22:06:27 +0200
Subject: [PATCH 22/32] Revert "Add NullChecks to vprintf.cpp"

This reverts commit 26f370f14e264d98d2845ccfc214a2891d1fe240.
---
 libc/src/stdio/baremetal/vprintf.cpp | 2 --
 libc/src/stdio/generic/vprintf.cpp   | 2 --
 libc/src/stdio/gpu/vprintf.cpp       | 2 --
 3 files changed, 6 deletions(-)

diff --git a/libc/src/stdio/baremetal/vprintf.cpp b/libc/src/stdio/baremetal/vprintf.cpp
index c80f6052efea3a..617b5f488e7728 100644
--- a/libc/src/stdio/baremetal/vprintf.cpp
+++ b/libc/src/stdio/baremetal/vprintf.cpp
@@ -10,7 +10,6 @@
 #include "src/__support/OSUtil/io.h"
 #include "src/__support/arg_list.h"
 #include "src/__support/macros/config.h"
-#include "src/__support/macros/null_check.h"
 #include "src/stdio/printf_core/core_structs.h"
 #include "src/stdio/printf_core/printf_main.h"
 #include "src/stdio/printf_core/writer.h"
@@ -31,7 +30,6 @@ LIBC_INLINE int raw_write_hook(cpp::string_view new_str, void *) {
 
 LLVM_LIBC_FUNCTION(int, vprintf,
                    (const char *__restrict format, va_list vlist)) {
-  LIBC_CRASH_ON_NULLPTR(format);
   internal::ArgList args(vlist); // This holder class allows for easier copying
                                  // and pointer semantics, as well as handling
                                  // destruction automatically.
diff --git a/libc/src/stdio/generic/vprintf.cpp b/libc/src/stdio/generic/vprintf.cpp
index 6c51ba31319980..08d71515646ed0 100644
--- a/libc/src/stdio/generic/vprintf.cpp
+++ b/libc/src/stdio/generic/vprintf.cpp
@@ -11,7 +11,6 @@
 #include "src/__support/File/file.h"
 #include "src/__support/arg_list.h"
 #include "src/__support/macros/config.h"
-#include "src/__support/macros/null_check.h"
 #include "src/stdio/printf_core/vfprintf_internal.h"
 
 #include "hdr/types/FILE.h"
@@ -27,7 +26,6 @@ namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, vprintf,
                    (const char *__restrict format, va_list vlist)) {
-  LIBC_CRASH_ON_NULLPTR(format);
   internal::ArgList args(vlist); // This holder class allows for easier copying
                                  // and pointer semantics, as well as handling
                                  // destruction automatically.
diff --git a/libc/src/stdio/gpu/vprintf.cpp b/libc/src/stdio/gpu/vprintf.cpp
index 49391aa4f3f127..54012f3071844d 100644
--- a/libc/src/stdio/gpu/vprintf.cpp
+++ b/libc/src/stdio/gpu/vprintf.cpp
@@ -10,7 +10,6 @@
 
 #include "src/__support/CPP/string_view.h"
 #include "src/__support/arg_list.h"
-#include "src/__support/macros/null_check.h"
 #include "src/errno/libc_errno.h"
 #include "src/stdio/gpu/vfprintf_utils.h"
 
@@ -18,7 +17,6 @@ namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, vprintf,
                    (const char *__restrict format, va_list vlist)) {
-  LIBC_CRASH_ON_NULLPTR(format);
   cpp::string_view str_view(format);
   int ret_val = vfprintf_internal(stdout, format, str_view.size() + 1, vlist);
   return ret_val;

>From 59d991c07116419ba7b24d457a808b061fa89cb0 Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Fri, 13 Dec 2024 22:06:35 +0200
Subject: [PATCH 23/32] Revert "Add NullChecks to fscanf.cpp"

This reverts commit 043ed9eea917589c83b80186b9b68f8aeaf4fea0.
---
 libc/src/stdio/fscanf.cpp | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/libc/src/stdio/fscanf.cpp b/libc/src/stdio/fscanf.cpp
index c79c92671b659f..94b843978749e2 100644
--- a/libc/src/stdio/fscanf.cpp
+++ b/libc/src/stdio/fscanf.cpp
@@ -11,7 +11,6 @@
 #include "src/__support/File/file.h"
 #include "src/__support/arg_list.h"
 #include "src/__support/macros/config.h"
-#include "src/__support/macros/null_check.h"
 #include "src/stdio/scanf_core/vfscanf_internal.h"
 
 #include "hdr/types/FILE.h"
@@ -22,8 +21,6 @@ namespace LIBC_NAMESPACE_DECL {
 LLVM_LIBC_FUNCTION(int, fscanf,
                    (::FILE *__restrict stream, const char *__restrict format,
                     ...)) {
-  LIBC_CRASH_ON_NULLPTR(stream);
-  LIBC_CRASH_ON_NULLPTR(format);
   va_list vlist;
   va_start(vlist, format);
   internal::ArgList args(vlist); // This holder class allows for easier copying

>From 597f5fc34e7e2dd39dbfb5d2b2088cfd7a9f803e Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Fri, 13 Dec 2024 22:06:35 +0200
Subject: [PATCH 24/32] Revert "Add NullChecks to fprintf.cpp"

This reverts commit b8e48a9bf302704018150ea021a5a65152bcb712.
---
 libc/src/stdio/generic/fprintf.cpp | 3 ---
 libc/src/stdio/gpu/fprintf.cpp     | 3 ---
 2 files changed, 6 deletions(-)

diff --git a/libc/src/stdio/generic/fprintf.cpp b/libc/src/stdio/generic/fprintf.cpp
index 478093355f3cd0..087aeadfc52c5c 100644
--- a/libc/src/stdio/generic/fprintf.cpp
+++ b/libc/src/stdio/generic/fprintf.cpp
@@ -11,7 +11,6 @@
 #include "src/__support/File/file.h"
 #include "src/__support/arg_list.h"
 #include "src/__support/macros/config.h"
-#include "src/__support/macros/null_check.h"
 #include "src/stdio/printf_core/vfprintf_internal.h"
 
 #include "hdr/types/FILE.h"
@@ -22,8 +21,6 @@ namespace LIBC_NAMESPACE_DECL {
 LLVM_LIBC_FUNCTION(int, fprintf,
                    (::FILE *__restrict stream, const char *__restrict format,
                     ...)) {
-  LIBC_CRASH_ON_NULLPTR(stream);
-  LIBC_CRASH_ON_NULLPTR(format);
   va_list vlist;
   va_start(vlist, format);
   internal::ArgList args(vlist); // This holder class allows for easier copying
diff --git a/libc/src/stdio/gpu/fprintf.cpp b/libc/src/stdio/gpu/fprintf.cpp
index 3273277be1c585..46196d7d2b10f5 100644
--- a/libc/src/stdio/gpu/fprintf.cpp
+++ b/libc/src/stdio/gpu/fprintf.cpp
@@ -11,7 +11,6 @@
 #include "hdr/types/FILE.h"
 #include "src/__support/CPP/string_view.h"
 #include "src/__support/arg_list.h"
-#include "src/__support/macros/null_check.h"
 #include "src/errno/libc_errno.h"
 #include "src/stdio/gpu/vfprintf_utils.h"
 
@@ -22,8 +21,6 @@ namespace LIBC_NAMESPACE_DECL {
 LLVM_LIBC_FUNCTION(int, fprintf,
                    (::FILE *__restrict stream, const char *__restrict format,
                     ...)) {
-  LIBC_CRASH_ON_NULLPTR(stream);
-  LIBC_CRASH_ON_NULLPTR(format);
   va_list vlist;
   va_start(vlist, format);
   cpp::string_view str_view(format);

>From fe9ae0fb363b10db62d9925559f47ec7c648a48e Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Fri, 13 Dec 2024 22:06:35 +0200
Subject: [PATCH 25/32] Revert "Add NullChecks to fopen.cpp"

This reverts commit 2829d8769aa816fa61c8f0ae59056f0577c90cc0.
---
 libc/src/stdio/generic/fopen.cpp | 3 ---
 libc/src/stdio/gpu/fopen.cpp     | 3 ---
 2 files changed, 6 deletions(-)

diff --git a/libc/src/stdio/generic/fopen.cpp b/libc/src/stdio/generic/fopen.cpp
index 91219cb722cf59..d6e418bacf37e4 100644
--- a/libc/src/stdio/generic/fopen.cpp
+++ b/libc/src/stdio/generic/fopen.cpp
@@ -11,15 +11,12 @@
 
 #include "hdr/types/FILE.h"
 #include "src/__support/macros/config.h"
-#include "src/__support/macros/null_check.h"
 #include "src/errno/libc_errno.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(::FILE *, fopen,
                    (const char *__restrict name, const char *__restrict mode)) {
-  LIBC_CRASH_ON_NULLPTR(name);
-  LIBC_CRASH_ON_NULLPTR(mode);
   auto result = LIBC_NAMESPACE::openfile(name, mode);
   if (!result.has_value()) {
     libc_errno = result.error();
diff --git a/libc/src/stdio/gpu/fopen.cpp b/libc/src/stdio/gpu/fopen.cpp
index dfba587de18f31..18dd7195377893 100644
--- a/libc/src/stdio/gpu/fopen.cpp
+++ b/libc/src/stdio/gpu/fopen.cpp
@@ -9,7 +9,6 @@
 #include "src/stdio/fopen.h"
 #include "src/__support/CPP/string_view.h"
 #include "src/__support/macros/config.h"
-#include "src/__support/macros/null_check.h"
 #include "src/stdio/gpu/file.h"
 
 #include "hdr/types/FILE.h"
@@ -18,8 +17,6 @@ namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(::FILE *, fopen,
                    (const char *__restrict path, const char *__restrict mode)) {
-  LIBC_CRASH_ON_NULLPTR(path);
-  LIBC_CRASH_ON_NULLPTR(mode);
   uintptr_t file;
   rpc::Client::Port port = rpc::client.open<LIBC_OPEN_FILE>();
   port.send_n(path, internal::string_length(path) + 1);

>From 1f731069d971c36e12328c0a629a2d75a94088fb Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Fri, 13 Dec 2024 22:06:35 +0200
Subject: [PATCH 26/32] Revert "Add NullChecks to fflush.cpp"

This reverts commit 9b75c8abd38b14e1d8c90cc571d3d323f75903f4.
---
 libc/src/stdio/generic/fflush.cpp | 3 ---
 libc/src/stdio/gpu/fflush.cpp     | 3 ---
 2 files changed, 6 deletions(-)

diff --git a/libc/src/stdio/generic/fflush.cpp b/libc/src/stdio/generic/fflush.cpp
index 8c71fbbd3d0999..5bdf71ad359406 100644
--- a/libc/src/stdio/generic/fflush.cpp
+++ b/libc/src/stdio/generic/fflush.cpp
@@ -11,14 +11,11 @@
 
 #include "hdr/types/FILE.h"
 #include "src/__support/macros/config.h"
-#include "src/__support/macros/null_check.h"
 #include "src/errno/libc_errno.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, fflush, (::FILE * stream)) {
-
-  LIBC_CRASH_ON_NULLPTR(stream);
   int result = reinterpret_cast<LIBC_NAMESPACE::File *>(stream)->flush();
   if (result != 0) {
     libc_errno = result;
diff --git a/libc/src/stdio/gpu/fflush.cpp b/libc/src/stdio/gpu/fflush.cpp
index 47843f4d08093b..5a5137be6a4af0 100644
--- a/libc/src/stdio/gpu/fflush.cpp
+++ b/libc/src/stdio/gpu/fflush.cpp
@@ -9,15 +9,12 @@
 #include "src/stdio/fflush.h"
 #include "file.h"
 #include "src/__support/macros/config.h"
-#include "src/__support/macros/null_check.h"
 
 #include "hdr/types/FILE.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, fflush, (::FILE * stream)) {
-
-  LIBC_CRASH_ON_NULLPTR(stream);
   int ret;
   rpc::Client::Port port = rpc::client.open<LIBC_FFLUSH>();
   port.send_and_recv(

>From 994501c65cc0632b2082cf47c59a77b4436d1bc4 Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Fri, 13 Dec 2024 22:54:25 +0200
Subject: [PATCH 27/32] Modify NullChecks to cast pointers as it will fail
 dereferencing a nullptr

---
 libc/src/string/memchr.cpp  | 3 ++-
 libc/src/string/memcmp.cpp  | 6 ++++--
 libc/src/string/memcpy.cpp  | 6 ++++--
 libc/src/string/memmove.cpp | 6 ++++--
 libc/src/string/mempcpy.cpp | 6 ++++--
 libc/src/string/memrchr.cpp | 4 +++-
 libc/src/string/memset.cpp  | 3 ++-
 7 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/libc/src/string/memchr.cpp b/libc/src/string/memchr.cpp
index dcc5ee99bbe545..d69b80a4f8bd44 100644
--- a/libc/src/string/memchr.cpp
+++ b/libc/src/string/memchr.cpp
@@ -18,7 +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)) {
-  LIBC_CRASH_ON_NULLPTR(src);
+  const unsigned char *src_cpy = (const unsigned char *)src;
+  LIBC_CRASH_ON_NULLPTR(src_cpy);
   return internal::find_first_character(
       reinterpret_cast<const unsigned char *>(src),
       static_cast<unsigned char>(c), n);
diff --git a/libc/src/string/memcmp.cpp b/libc/src/string/memcmp.cpp
index 71c6840d14ca83..82a1126a558f27 100644
--- a/libc/src/string/memcmp.cpp
+++ b/libc/src/string/memcmp.cpp
@@ -17,8 +17,10 @@ namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, memcmp,
                    (const void *lhs, const void *rhs, size_t count)) {
-  LIBC_CRASH_ON_NULLPTR(lhs);
-  LIBC_CRASH_ON_NULLPTR(rhs);
+  const unsigned char *left = (const unsigned char *)lhs;
+  const unsigned char *right = (const unsigned char *)rhs;
+  LIBC_CRASH_ON_NULLPTR(left);
+  LIBC_CRASH_ON_NULLPTR(right);
   return inline_memcmp(lhs, rhs, count);
 }
 
diff --git a/libc/src/string/memcpy.cpp b/libc/src/string/memcpy.cpp
index 9f0406d08d6456..712443a6a5c040 100644
--- a/libc/src/string/memcpy.cpp
+++ b/libc/src/string/memcpy.cpp
@@ -17,8 +17,10 @@ namespace LIBC_NAMESPACE_DECL {
 LLVM_LIBC_FUNCTION(void *, memcpy,
                    (void *__restrict dst, const void *__restrict src,
                     size_t size)) {
-  LIBC_CRASH_ON_NULLPTR(dst);
-  LIBC_CRASH_ON_NULLPTR(src);
+  const unsigned char *dst_cpy = (const unsigned char *)dst;
+  const unsigned char *src_cpy = (const unsigned char *)src;
+  LIBC_CRASH_ON_NULLPTR(dst_cpy);
+  LIBC_CRASH_ON_NULLPTR(src_cpy);
   inline_memcpy(dst, src, size);
   return dst;
 }
diff --git a/libc/src/string/memmove.cpp b/libc/src/string/memmove.cpp
index ca77795cc16474..3ef020253ec31b 100644
--- a/libc/src/string/memmove.cpp
+++ b/libc/src/string/memmove.cpp
@@ -17,8 +17,10 @@ namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(void *, memmove,
                    (void *dst, const void *src, size_t count)) {
-  LIBC_CRASH_ON_NULLPTR(dst);
-  LIBC_CRASH_ON_NULLPTR(src);
+  const unsigned char *dst_cpy = (const unsigned char *)dst;
+  const unsigned char *src_cpy = (const unsigned char *)src;
+  LIBC_CRASH_ON_NULLPTR(dst_cpy);
+  LIBC_CRASH_ON_NULLPTR(src_cpy);
 
   // Memmove may handle some small sizes as efficiently as inline_memcpy.
   // For these sizes we may not do is_disjoint check.
diff --git a/libc/src/string/mempcpy.cpp b/libc/src/string/mempcpy.cpp
index a7b5c7efbadb72..c6d4c027df749a 100644
--- a/libc/src/string/mempcpy.cpp
+++ b/libc/src/string/mempcpy.cpp
@@ -19,8 +19,10 @@ namespace LIBC_NAMESPACE_DECL {
 LLVM_LIBC_FUNCTION(void *, mempcpy,
                    (void *__restrict dst, const void *__restrict src,
                     size_t count)) {
-  LIBC_CRASH_ON_NULLPTR(dst);
-  LIBC_CRASH_ON_NULLPTR(src);
+  const unsigned char *dst_cpy = (const unsigned char *)dst;
+  const unsigned char *src_cpy = (const unsigned char *)src;
+  LIBC_CRASH_ON_NULLPTR(dst_cpy);
+  LIBC_CRASH_ON_NULLPTR(src_cpy);
   inline_memcpy(dst, src, count);
   return reinterpret_cast<char *>(dst) + count;
 }
diff --git a/libc/src/string/memrchr.cpp b/libc/src/string/memrchr.cpp
index 319bec97ae7177..6e7e05d0773472 100644
--- a/libc/src/string/memrchr.cpp
+++ b/libc/src/string/memrchr.cpp
@@ -15,7 +15,9 @@
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(void *, memrchr, (const void *src, int c, size_t n)) {
-  LIBC_CRASH_ON_NULLPTR(src);
+  const unsigned char *src_cpy = (const unsigned char *)src;
+  LIBC_CRASH_ON_NULLPTR(src_cpy);
+
   const unsigned char *str = reinterpret_cast<const unsigned char *>(src);
   const unsigned char ch = static_cast<unsigned char>(c);
   for (; n != 0; --n) {
diff --git a/libc/src/string/memset.cpp b/libc/src/string/memset.cpp
index 53448af9d97945..2af587bcfcbee6 100644
--- a/libc/src/string/memset.cpp
+++ b/libc/src/string/memset.cpp
@@ -15,7 +15,8 @@
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(void *, memset, (void *dst, int value, size_t count)) {
-  LIBC_CRASH_ON_NULLPTR(dst);
+  const unsigned char *dst_cpy = (const unsigned char *)dst;
+  LIBC_CRASH_ON_NULLPTR(dst_cpy);
   inline_memset(dst, static_cast<uint8_t>(value), count);
   return dst;
 }

>From 1096d4baf3501e5c5b8d9c36b976ac0b95397393 Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Sun, 22 Dec 2024 22:28:45 +0200
Subject: [PATCH 28/32] Modify Unit Tests to include crashing on nullptrs

---
 libc/test/src/string/CMakeLists.txt   | 9 +++++++++
 libc/test/src/string/memchr_test.cpp  | 5 +++++
 libc/test/src/string/memcmp_test.cpp  | 5 +++++
 libc/test/src/string/memcpy_test.cpp  | 5 +++++
 libc/test/src/string/mempcpy_test.cpp | 6 +++++-
 libc/test/src/string/memrchr_test.cpp | 5 +++++
 libc/test/src/string/stpncpy_test.cpp | 4 ++++
 libc/test/src/string/strcat_test.cpp  | 5 +++++
 libc/test/src/string/strcoll_test.cpp | 4 ++++
 libc/test/src/string/strcpy_test.cpp  | 5 +++++
 libc/test/src/string/strlcpy_test.cpp | 4 ++--
 libc/test/src/string/strsep_test.cpp  | 4 ++++
 libc/test/src/string/strspn_test.cpp  | 5 +++++
 13 files changed, 63 insertions(+), 3 deletions(-)

diff --git a/libc/test/src/string/CMakeLists.txt b/libc/test/src/string/CMakeLists.txt
index a675373938e996..109ef3e4da6a2c 100644
--- a/libc/test/src/string/CMakeLists.txt
+++ b/libc/test/src/string/CMakeLists.txt
@@ -20,6 +20,7 @@ add_libc_test(
 
 add_libc_test(
   mempcpy_test
+  UNIT_TEST_ONLY
   SUITE
     libc-string-tests
   SRCS
@@ -40,6 +41,7 @@ add_libc_test(
 
 add_libc_test(
   memchr_test
+  UNIT_TEST_ONLY
   SUITE
     libc-string-tests
   SRCS
@@ -50,6 +52,7 @@ add_libc_test(
 
 add_libc_test(
   memrchr_test
+  UNIT_TEST_ONLY
   SUITE
     libc-string-tests
   SRCS
@@ -70,6 +73,7 @@ add_libc_test(
 
 add_libc_test(
   stpncpy_test
+  UNIT_TEST_ONLY
   SUITE
     libc-string-tests
   SRCS
@@ -80,6 +84,7 @@ add_libc_test(
 
 add_libc_test(
   strcat_test
+  UNIT_TEST_ONLY
   SUITE
     libc-string-tests
   SRCS
@@ -131,6 +136,7 @@ add_libc_test(
 
 add_libc_test(
   strcoll_test
+  UNIT_TEST_ONLY
   SUITE
     libc-string-tests
   SRCS
@@ -141,6 +147,7 @@ add_libc_test(
 
 add_libc_test(
   strcpy_test
+  UNIT_TEST_ONLY
   SUITE
     libc-string-tests
   SRCS
@@ -296,6 +303,7 @@ add_libc_test(
 
 add_libc_test(
   strsep_test
+  UNIT_TEST_ONLY
   SUITE
     libc-string-tests
   SRCS
@@ -316,6 +324,7 @@ add_libc_test(
 
 add_libc_test(
   strspn_test
+  UNIT_TEST_ONLY
   SUITE
     libc-string-tests
   SRCS
diff --git a/libc/test/src/string/memchr_test.cpp b/libc/test/src/string/memchr_test.cpp
index 343958234edcee..bcf895c9276479 100644
--- a/libc/test/src/string/memchr_test.cpp
+++ b/libc/test/src/string/memchr_test.cpp
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "hdr/signal_macros.h"
 #include "src/string/memchr.h"
 #include "test/UnitTest/Test.h"
 #include <stddef.h>
@@ -120,3 +121,7 @@ TEST(LlvmLibcMemChrTest, SignedCharacterFound) {
   // Should find the first character 'c'.
   ASSERT_EQ(actual[0], c);
 }
+
+TEST(LlvmLibcMemChrTest, CrashOnNullPtr) {
+  ASSERT_DEATH([](){ LIBC_NAMESPACE::memchr(nullptr, 1, 1); } , WITH_SIGNAL(SIGSEGV));
+}
\ No newline at end of file
diff --git a/libc/test/src/string/memcmp_test.cpp b/libc/test/src/string/memcmp_test.cpp
index 9f85a6d4f2229a..00118cedb92200 100644
--- a/libc/test/src/string/memcmp_test.cpp
+++ b/libc/test/src/string/memcmp_test.cpp
@@ -65,4 +65,9 @@ TEST(LlvmLibcMemcmpTest, SizeSweep) {
   }
 }
 
+#include "hdr/signal_macros.h"
+TEST(LlvmLibcMemcmpTest, CrashOnNullPtr) {
+  ASSERT_DEATH([](){ LIBC_NAMESPACE::memcmp(nullptr, 1, 1); } , WITH_SIGNAL(SIGSEGV));
+}
+
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/string/memcpy_test.cpp b/libc/test/src/string/memcpy_test.cpp
index adf6ef7e66d3f2..e30fd9687d1790 100644
--- a/libc/test/src/string/memcpy_test.cpp
+++ b/libc/test/src/string/memcpy_test.cpp
@@ -72,4 +72,9 @@ TEST(LlvmLibcMemcpyTest, CheckAccess) {
 
 #endif // !defined(LIBC_FULL_BUILD) && defined(LIBC_TARGET_OS_IS_LINUX)
 
+#include "hdr/signal_macros.h"
+TEST(LlvmLibcMemcpyTest, CrashOnNullPtr) {
+  ASSERT_DEATH(LIBC_NAMESPACE::memcpy(nullptr, nullptr, 1), WITH_SIGNAL(SIGSEGV));
+}
+
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/string/mempcpy_test.cpp b/libc/test/src/string/mempcpy_test.cpp
index 877ee8104880eb..32097589d41118 100644
--- a/libc/test/src/string/mempcpy_test.cpp
+++ b/libc/test/src/string/mempcpy_test.cpp
@@ -5,7 +5,7 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
-
+#include "hdr/signal_macros.h"
 #include "src/string/mempcpy.h"
 #include "test/UnitTest/Test.h"
 
@@ -26,3 +26,7 @@ TEST(LlvmLibcMempcpyTest, ZeroCount) {
   void *result = LIBC_NAMESPACE::mempcpy(dest, src, 0);
   ASSERT_EQ(static_cast<char *>(result), dest + 0);
 }
+
+TEST(LlvmLibcMempcpyTest, CrashOnNullPtr) {
+  ASSERT_DEATH([](){ LIBC_NAMESPACE::mempcpy(nullptr, nullptr, 0); } , WITH_SIGNAL(SIGSEGV));
+}
\ No newline at end of file
diff --git a/libc/test/src/string/memrchr_test.cpp b/libc/test/src/string/memrchr_test.cpp
index 421cb9b98516e5..d07b751effbce8 100644
--- a/libc/test/src/string/memrchr_test.cpp
+++ b/libc/test/src/string/memrchr_test.cpp
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "hdr/signal_macros.h"
 #include "src/string/memrchr.h"
 #include "test/UnitTest/Test.h"
 #include <stddef.h>
@@ -112,3 +113,7 @@ TEST(LlvmLibcMemRChrTest, ZeroLengthShouldReturnNullptr) {
   // This will iterate over exactly zero characters, so should return nullptr.
   ASSERT_STREQ(call_memrchr(src, 'd', 0), nullptr);
 }
+
+TEST(LlvmLibcMemRChrTest, CrashOnNullPtr) {
+  ASSERT_DEATH([](){ LIBC_NAMESPACE::memrchr(nullptr, 'd', 1); } , WITH_SIGNAL(SIGSEGV));
+}
diff --git a/libc/test/src/string/stpncpy_test.cpp b/libc/test/src/string/stpncpy_test.cpp
index 247fa92a0c7bfa..2b0bc3519849e2 100644
--- a/libc/test/src/string/stpncpy_test.cpp
+++ b/libc/test/src/string/stpncpy_test.cpp
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "hdr/signal_macros.h"
 #include "src/__support/CPP/span.h"
 #include "src/string/stpncpy.h"
 #include "test/UnitTest/Test.h"
@@ -71,3 +72,6 @@ TEST_F(LlvmLibcStpncpyTest, CopyTwoWithNull) {
   const char expected[] = {'x', '\0'};
   check_stpncpy(dst, src, 2, expected, 1);
 }
+TEST_F(LlvmLibcStpncpyTest, CrashOnNullPtr) {
+  ASSERT_DEATH( [](){LIBC_NAMESPACE::stpncpy(nullptr, nullptr, 1);} , WITH_SIGNAL(SIGSEGV));
+}
diff --git a/libc/test/src/string/strcat_test.cpp b/libc/test/src/string/strcat_test.cpp
index e4f6c1ee75992e..0e77ba12a8cd90 100644
--- a/libc/test/src/string/strcat_test.cpp
+++ b/libc/test/src/string/strcat_test.cpp
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "hdr/signal_macros.h"
 #include "src/string/strcat.h"
 #include "test/UnitTest/Test.h"
 
@@ -35,3 +36,7 @@ TEST(LlvmLibcStrCatTest, NonEmptyDest) {
   ASSERT_STREQ(dest, result);
   ASSERT_STREQ(dest, "xyzabc");
 }
+
+TEST(LlvmLibcStrCatTest, CrashOnNullPtr) {
+  ASSERT_DEATH( [](){LIBC_NAMESPACE::strcat(nullptr, nullptr);} , WITH_SIGNAL(SIGSEGV));
+}
\ No newline at end of file
diff --git a/libc/test/src/string/strcoll_test.cpp b/libc/test/src/string/strcoll_test.cpp
index a10f98f1ca4d6a..ee501fa7eeee18 100644
--- a/libc/test/src/string/strcoll_test.cpp
+++ b/libc/test/src/string/strcoll_test.cpp
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "hdr/signal_macros.h"
 #include "src/string/strcoll.h"
 #include "test/UnitTest/Test.h"
 
@@ -28,3 +29,6 @@ TEST(LlvmLibcStrcollTest, SimpleTest) {
   result = LIBC_NAMESPACE::strcoll(s3, s1);
   ASSERT_GT(result, 0);
 }
+TEST(LlvmLibcStrcollTest, CrashOnNullPtr) {
+  ASSERT_DEATH( [](){LIBC_NAMESPACE::strcoll(nullptr, nullptr);} , WITH_SIGNAL(SIGSEGV));
+}
diff --git a/libc/test/src/string/strcpy_test.cpp b/libc/test/src/string/strcpy_test.cpp
index 1a1227aac5d2f8..a0b7c6bd9e1ac2 100644
--- a/libc/test/src/string/strcpy_test.cpp
+++ b/libc/test/src/string/strcpy_test.cpp
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "hdr/signal_macros.h"
 #include "src/string/strcpy.h"
 #include "test/UnitTest/Test.h"
 
@@ -42,3 +43,7 @@ TEST(LlvmLibcStrCpyTest, OffsetDest) {
   ASSERT_STREQ(dest + 3, result);
   ASSERT_STREQ(dest, "xyzabc");
 }
+
+TEST(LlvmLibcStrCpyTest, CrashOnNullPtr) {
+  ASSERT_DEATH( [](){LIBC_NAMESPACE::strcpy(nullptr, nullptr); } , WITH_SIGNAL(SIGSEGV));
+}
diff --git a/libc/test/src/string/strlcpy_test.cpp b/libc/test/src/string/strlcpy_test.cpp
index 0914257ecc1f34..b42954ca6137aa 100644
--- a/libc/test/src/string/strlcpy_test.cpp
+++ b/libc/test/src/string/strlcpy_test.cpp
@@ -14,8 +14,8 @@ TEST(LlvmLibcStrlcpyTest, TooBig) {
   char buf[2];
   EXPECT_EQ(LIBC_NAMESPACE::strlcpy(buf, str, 2), size_t(3));
   EXPECT_STREQ(buf, "a");
-
-  EXPECT_EQ(LIBC_NAMESPACE::strlcpy(nullptr, str, 0), size_t(3));
+  char dst[] = "";
+  EXPECT_EQ(LIBC_NAMESPACE::strlcpy(dst, str, 0), size_t(3));
 }
 
 TEST(LlvmLibcStrlcpyTest, Smaller) {
diff --git a/libc/test/src/string/strsep_test.cpp b/libc/test/src/string/strsep_test.cpp
index 0daa29f6a7ad59..3aacf941a655f2 100644
--- a/libc/test/src/string/strsep_test.cpp
+++ b/libc/test/src/string/strsep_test.cpp
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "hdr/signal_macros.h"
 #include "src/string/strsep.h"
 #include "test/UnitTest/Test.h"
 
@@ -51,3 +52,6 @@ TEST(LlvmLibcStrsepTest, DelimitersShouldNotBeIncludedInToken) {
     ASSERT_STREQ(LIBC_NAMESPACE::strsep(&string, "_:"), expected[i]);
   }
 }
+TEST(LlvmLibcStrsepTest, CrashOnNullPtr) {
+  ASSERT_DEATH( [](){LIBC_NAMESPACE::strsep(nullptr, nullptr);} , WITH_SIGNAL(SIGSEGV));
+}
diff --git a/libc/test/src/string/strspn_test.cpp b/libc/test/src/string/strspn_test.cpp
index cdd12af07ee7da..c53d05e1db4d90 100644
--- a/libc/test/src/string/strspn_test.cpp
+++ b/libc/test/src/string/strspn_test.cpp
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "hdr/signal_macros.h"
 #include "src/string/strspn.h"
 
 #include "test/UnitTest/Test.h"
@@ -83,3 +84,7 @@ TEST(LlvmLibcStrSpnTest, DuplicatedCharactersToBeSearchedForShouldStillMatch) {
   EXPECT_EQ(LIBC_NAMESPACE::strspn("aaa", "aa"), size_t{3});
   EXPECT_EQ(LIBC_NAMESPACE::strspn("aaaa", "aa"), size_t{4});
 }
+
+TEST(LlvmLibcStrSpnTest, CrashOnNullPtr) {
+  ASSERT_DEATH([](){ LIBC_NAMESPACE::strspn(nullptr, nullptr); } , WITH_SIGNAL(SIGSEGV));
+}
\ No newline at end of file

>From a320a19b1242e6e952062e6ac578db742a7c3206 Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Sun, 22 Dec 2024 22:28:57 +0200
Subject: [PATCH 29/32] Formatting

---
 libc/test/src/string/memchr_test.cpp  | 3 ++-
 libc/test/src/string/memcmp_test.cpp  | 3 ++-
 libc/test/src/string/memcpy_test.cpp  | 3 ++-
 libc/test/src/string/mempcpy_test.cpp | 3 ++-
 libc/test/src/string/memrchr_test.cpp | 3 ++-
 libc/test/src/string/stpncpy_test.cpp | 3 ++-
 libc/test/src/string/strcat_test.cpp  | 3 ++-
 libc/test/src/string/strcoll_test.cpp | 3 ++-
 libc/test/src/string/strcpy_test.cpp  | 3 ++-
 libc/test/src/string/strsep_test.cpp  | 3 ++-
 libc/test/src/string/strspn_test.cpp  | 3 ++-
 11 files changed, 22 insertions(+), 11 deletions(-)

diff --git a/libc/test/src/string/memchr_test.cpp b/libc/test/src/string/memchr_test.cpp
index bcf895c9276479..2950672d751846 100644
--- a/libc/test/src/string/memchr_test.cpp
+++ b/libc/test/src/string/memchr_test.cpp
@@ -123,5 +123,6 @@ TEST(LlvmLibcMemChrTest, SignedCharacterFound) {
 }
 
 TEST(LlvmLibcMemChrTest, CrashOnNullPtr) {
-  ASSERT_DEATH([](){ LIBC_NAMESPACE::memchr(nullptr, 1, 1); } , WITH_SIGNAL(SIGSEGV));
+  ASSERT_DEATH([]() { LIBC_NAMESPACE::memchr(nullptr, 1, 1); },
+               WITH_SIGNAL(SIGSEGV));
 }
\ No newline at end of file
diff --git a/libc/test/src/string/memcmp_test.cpp b/libc/test/src/string/memcmp_test.cpp
index 00118cedb92200..45180c18df0711 100644
--- a/libc/test/src/string/memcmp_test.cpp
+++ b/libc/test/src/string/memcmp_test.cpp
@@ -67,7 +67,8 @@ TEST(LlvmLibcMemcmpTest, SizeSweep) {
 
 #include "hdr/signal_macros.h"
 TEST(LlvmLibcMemcmpTest, CrashOnNullPtr) {
-  ASSERT_DEATH([](){ LIBC_NAMESPACE::memcmp(nullptr, 1, 1); } , WITH_SIGNAL(SIGSEGV));
+  ASSERT_DEATH([]() { LIBC_NAMESPACE::memcmp(nullptr, 1, 1); },
+               WITH_SIGNAL(SIGSEGV));
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/string/memcpy_test.cpp b/libc/test/src/string/memcpy_test.cpp
index e30fd9687d1790..430252e39f013c 100644
--- a/libc/test/src/string/memcpy_test.cpp
+++ b/libc/test/src/string/memcpy_test.cpp
@@ -74,7 +74,8 @@ TEST(LlvmLibcMemcpyTest, CheckAccess) {
 
 #include "hdr/signal_macros.h"
 TEST(LlvmLibcMemcpyTest, CrashOnNullPtr) {
-  ASSERT_DEATH(LIBC_NAMESPACE::memcpy(nullptr, nullptr, 1), WITH_SIGNAL(SIGSEGV));
+  ASSERT_DEATH(LIBC_NAMESPACE::memcpy(nullptr, nullptr, 1),
+               WITH_SIGNAL(SIGSEGV));
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/string/mempcpy_test.cpp b/libc/test/src/string/mempcpy_test.cpp
index 32097589d41118..af4a05444ec467 100644
--- a/libc/test/src/string/mempcpy_test.cpp
+++ b/libc/test/src/string/mempcpy_test.cpp
@@ -28,5 +28,6 @@ TEST(LlvmLibcMempcpyTest, ZeroCount) {
 }
 
 TEST(LlvmLibcMempcpyTest, CrashOnNullPtr) {
-  ASSERT_DEATH([](){ LIBC_NAMESPACE::mempcpy(nullptr, nullptr, 0); } , WITH_SIGNAL(SIGSEGV));
+  ASSERT_DEATH([]() { LIBC_NAMESPACE::mempcpy(nullptr, nullptr, 0); },
+               WITH_SIGNAL(SIGSEGV));
 }
\ No newline at end of file
diff --git a/libc/test/src/string/memrchr_test.cpp b/libc/test/src/string/memrchr_test.cpp
index d07b751effbce8..b138801bc27878 100644
--- a/libc/test/src/string/memrchr_test.cpp
+++ b/libc/test/src/string/memrchr_test.cpp
@@ -115,5 +115,6 @@ TEST(LlvmLibcMemRChrTest, ZeroLengthShouldReturnNullptr) {
 }
 
 TEST(LlvmLibcMemRChrTest, CrashOnNullPtr) {
-  ASSERT_DEATH([](){ LIBC_NAMESPACE::memrchr(nullptr, 'd', 1); } , WITH_SIGNAL(SIGSEGV));
+  ASSERT_DEATH([]() { LIBC_NAMESPACE::memrchr(nullptr, 'd', 1); },
+               WITH_SIGNAL(SIGSEGV));
 }
diff --git a/libc/test/src/string/stpncpy_test.cpp b/libc/test/src/string/stpncpy_test.cpp
index 2b0bc3519849e2..9f3a8292b5ca67 100644
--- a/libc/test/src/string/stpncpy_test.cpp
+++ b/libc/test/src/string/stpncpy_test.cpp
@@ -73,5 +73,6 @@ TEST_F(LlvmLibcStpncpyTest, CopyTwoWithNull) {
   check_stpncpy(dst, src, 2, expected, 1);
 }
 TEST_F(LlvmLibcStpncpyTest, CrashOnNullPtr) {
-  ASSERT_DEATH( [](){LIBC_NAMESPACE::stpncpy(nullptr, nullptr, 1);} , WITH_SIGNAL(SIGSEGV));
+  ASSERT_DEATH([]() { LIBC_NAMESPACE::stpncpy(nullptr, nullptr, 1); },
+               WITH_SIGNAL(SIGSEGV));
 }
diff --git a/libc/test/src/string/strcat_test.cpp b/libc/test/src/string/strcat_test.cpp
index 0e77ba12a8cd90..9a0eb7a1645aba 100644
--- a/libc/test/src/string/strcat_test.cpp
+++ b/libc/test/src/string/strcat_test.cpp
@@ -38,5 +38,6 @@ TEST(LlvmLibcStrCatTest, NonEmptyDest) {
 }
 
 TEST(LlvmLibcStrCatTest, CrashOnNullPtr) {
-  ASSERT_DEATH( [](){LIBC_NAMESPACE::strcat(nullptr, nullptr);} , WITH_SIGNAL(SIGSEGV));
+  ASSERT_DEATH([]() { LIBC_NAMESPACE::strcat(nullptr, nullptr); },
+               WITH_SIGNAL(SIGSEGV));
 }
\ No newline at end of file
diff --git a/libc/test/src/string/strcoll_test.cpp b/libc/test/src/string/strcoll_test.cpp
index ee501fa7eeee18..b308bf1653059e 100644
--- a/libc/test/src/string/strcoll_test.cpp
+++ b/libc/test/src/string/strcoll_test.cpp
@@ -30,5 +30,6 @@ TEST(LlvmLibcStrcollTest, SimpleTest) {
   ASSERT_GT(result, 0);
 }
 TEST(LlvmLibcStrcollTest, CrashOnNullPtr) {
-  ASSERT_DEATH( [](){LIBC_NAMESPACE::strcoll(nullptr, nullptr);} , WITH_SIGNAL(SIGSEGV));
+  ASSERT_DEATH([]() { LIBC_NAMESPACE::strcoll(nullptr, nullptr); },
+               WITH_SIGNAL(SIGSEGV));
 }
diff --git a/libc/test/src/string/strcpy_test.cpp b/libc/test/src/string/strcpy_test.cpp
index a0b7c6bd9e1ac2..f0c328c7342bd2 100644
--- a/libc/test/src/string/strcpy_test.cpp
+++ b/libc/test/src/string/strcpy_test.cpp
@@ -45,5 +45,6 @@ TEST(LlvmLibcStrCpyTest, OffsetDest) {
 }
 
 TEST(LlvmLibcStrCpyTest, CrashOnNullPtr) {
-  ASSERT_DEATH( [](){LIBC_NAMESPACE::strcpy(nullptr, nullptr); } , WITH_SIGNAL(SIGSEGV));
+  ASSERT_DEATH([]() { LIBC_NAMESPACE::strcpy(nullptr, nullptr); },
+               WITH_SIGNAL(SIGSEGV));
 }
diff --git a/libc/test/src/string/strsep_test.cpp b/libc/test/src/string/strsep_test.cpp
index 3aacf941a655f2..937e3ea35e0872 100644
--- a/libc/test/src/string/strsep_test.cpp
+++ b/libc/test/src/string/strsep_test.cpp
@@ -53,5 +53,6 @@ TEST(LlvmLibcStrsepTest, DelimitersShouldNotBeIncludedInToken) {
   }
 }
 TEST(LlvmLibcStrsepTest, CrashOnNullPtr) {
-  ASSERT_DEATH( [](){LIBC_NAMESPACE::strsep(nullptr, nullptr);} , WITH_SIGNAL(SIGSEGV));
+  ASSERT_DEATH([]() { LIBC_NAMESPACE::strsep(nullptr, nullptr); },
+               WITH_SIGNAL(SIGSEGV));
 }
diff --git a/libc/test/src/string/strspn_test.cpp b/libc/test/src/string/strspn_test.cpp
index c53d05e1db4d90..55d237d832cd97 100644
--- a/libc/test/src/string/strspn_test.cpp
+++ b/libc/test/src/string/strspn_test.cpp
@@ -86,5 +86,6 @@ TEST(LlvmLibcStrSpnTest, DuplicatedCharactersToBeSearchedForShouldStillMatch) {
 }
 
 TEST(LlvmLibcStrSpnTest, CrashOnNullPtr) {
-  ASSERT_DEATH([](){ LIBC_NAMESPACE::strspn(nullptr, nullptr); } , WITH_SIGNAL(SIGSEGV));
+  ASSERT_DEATH([]() { LIBC_NAMESPACE::strspn(nullptr, nullptr); },
+               WITH_SIGNAL(SIGSEGV));
 }
\ No newline at end of file

>From 76071b34de600460d8f9f02960f74189f044b163 Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Mon, 23 Dec 2024 00:14:21 +0200
Subject: [PATCH 30/32] Patch up memcmp test

---
 libc/test/src/string/memcmp_test.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/test/src/string/memcmp_test.cpp b/libc/test/src/string/memcmp_test.cpp
index 45180c18df0711..ac0ef4192c64ae 100644
--- a/libc/test/src/string/memcmp_test.cpp
+++ b/libc/test/src/string/memcmp_test.cpp
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "hdr/signal_macros.h"
 #include "memory_utils/memory_check_utils.h"
 #include "src/__support/macros/config.h"
 #include "src/string/memcmp.h"
@@ -65,9 +66,8 @@ TEST(LlvmLibcMemcmpTest, SizeSweep) {
   }
 }
 
-#include "hdr/signal_macros.h"
 TEST(LlvmLibcMemcmpTest, CrashOnNullPtr) {
-  ASSERT_DEATH([]() { LIBC_NAMESPACE::memcmp(nullptr, 1, 1); },
+  ASSERT_DEATH([]() { LIBC_NAMESPACE::memcmp(nullptr, nullptr, 1); },
                WITH_SIGNAL(SIGSEGV));
 }
 

>From ce8b1670e71dd9796cc114f9fd542223628dd9df Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Mon, 23 Dec 2024 00:14:33 +0200
Subject: [PATCH 31/32] Patch up memcpy test

---
 libc/test/src/string/memcpy_test.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/test/src/string/memcpy_test.cpp b/libc/test/src/string/memcpy_test.cpp
index 430252e39f013c..20e01ec8f6e2ec 100644
--- a/libc/test/src/string/memcpy_test.cpp
+++ b/libc/test/src/string/memcpy_test.cpp
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "hdr/signal_macros.h"
 #include "memory_utils/memory_check_utils.h"
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/properties/os.h" // LIBC_TARGET_OS_IS_LINUX
@@ -72,9 +73,8 @@ TEST(LlvmLibcMemcpyTest, CheckAccess) {
 
 #endif // !defined(LIBC_FULL_BUILD) && defined(LIBC_TARGET_OS_IS_LINUX)
 
-#include "hdr/signal_macros.h"
 TEST(LlvmLibcMemcpyTest, CrashOnNullPtr) {
-  ASSERT_DEATH(LIBC_NAMESPACE::memcpy(nullptr, nullptr, 1),
+  ASSERT_DEATH([](){LIBC_NAMESPACE::memcpy(nullptr, nullptr, 1);},
                WITH_SIGNAL(SIGSEGV));
 }
 

>From 2c4f2592b0b9d5d3137cb69b577110da7bdb95bb Mon Sep 17 00:00:00 2001
From: AlyElashram <alyahelashram at gmail.com>
Date: Mon, 23 Dec 2024 00:14:54 +0200
Subject: [PATCH 32/32] Only USe unit tests for memcmp test suites

---
 libc/cmake/modules/LLVMLibCTestRules.cmake | 3 ++-
 libc/test/src/string/CMakeLists.txt        | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index 84f3125d557eae..31218b9ba71778 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -768,7 +768,7 @@ function(add_libc_test test_name)
 endfunction(add_libc_test)
 
 # Tests all implementations that can run on the target CPU.
-function(add_libc_multi_impl_test name suite)
+function(add_libc_multi_impl_test unit_test_only name suite)
   get_property(fq_implementations GLOBAL PROPERTY ${name}_implementations)
   foreach(fq_config_name IN LISTS fq_implementations)
     get_target_property(required_cpu_features ${fq_config_name} REQUIRE_CPU_FEATURES)
@@ -779,6 +779,7 @@ function(add_libc_multi_impl_test name suite)
       string(SUBSTRING ${fq_config_name} ${name_loc} -1 target_name)
       add_libc_test(
         ${target_name}_test
+        ${unit_test_only}
         SUITE
           ${suite}
         COMPILE_OPTIONS
diff --git a/libc/test/src/string/CMakeLists.txt b/libc/test/src/string/CMakeLists.txt
index 109ef3e4da6a2c..e14c97e1063218 100644
--- a/libc/test/src/string/CMakeLists.txt
+++ b/libc/test/src/string/CMakeLists.txt
@@ -383,7 +383,7 @@ add_libc_test(
     libc.src.string.memset_explicit
 )
 
-add_libc_multi_impl_test(memcmp libc-string-tests SRCS memcmp_test.cpp)
+add_libc_multi_impl_test(memcmp UNIT_TEST_ONLY libc-string-tests SRCS memcmp_test.cpp)
 add_libc_multi_impl_test(memcpy libc-string-tests SRCS memcpy_test.cpp)
 add_libc_multi_impl_test(memmove libc-string-tests SRCS memmove_test.cpp)
 add_libc_multi_impl_test(memset libc-string-tests SRCS memset_test.cpp)



More information about the libc-commits mailing list