[libc-commits] [libc] Expand usage of libc null checks (PR #116262)
Aly ElAshram via libc-commits
libc-commits at lists.llvm.org
Fri Dec 13 08:19:23 PST 2024
https://github.com/AlyElashram updated https://github.com/llvm/llvm-project/pull/116262
>From e21eea9109fa51637619b69bf75d3a72b5cf052a 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/21] 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 577325b70c4e70..d5f7680513a921 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<RPC_FFLUSH>();
port.send_and_recv(
>From 5cff53f326899a887aab9e34d4512ff12a1a70f2 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/21] 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 e165d2acd2109a..d3d5a9d8187302 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<RPC_OPEN_FILE>();
port.send_n(path, internal::string_length(path) + 1);
>From f614196b060eff9c6e61c825218d8e27c46b6b76 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/21] 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 a7c43b1a8613118251f79a7a188c5dc0f551d064 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/21] 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 f66e6e54b4f02a1a89278e9589a4cf3fd02cd194 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/21] 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 7127206ad7436bcf4edbdc1e250f72aa34e7f495 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/21] 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 6bad228765f7b0803190e9fb2af1e628b33c5a86 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/21] 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 22a1876da5369c..552f577e29fe13 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 40e79a8830a2c3c364e7a9f1ee29deb5154e58d8 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/21] 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 eadbe9528fa64f1d29eb436eb4c5a33257364f24 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/21] 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 9c307ad44b20e1a71615e85d11e873b2468c9944 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/21] 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 aab8e35001c2f06498393ea5481f06ee0be23bca 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/21] 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 07d6c07ad172b7e99f3508cd44b3a8428a461a4f 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/21] 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 8bfa9e26f61fedef1d75a5653cea4f9eb7ab5780 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/21] 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 01f9af071483af0ecfb4b57cea01698b149ef966 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/21] 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 54d28f33f5f6ec98981380cf106295bb43ab05a5 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/21] Add NullChecks to rindex.cpp
---
libc/src/string/rindex.cpp | 2 ++
1 file changed, 2 insertions(+)
diff --git a/libc/src/string/rindex.cpp b/libc/src/string/rindex.cpp
index 25879ddb07f627..f124c56a68bea6 100644
--- a/libc/src/string/rindex.cpp
+++ b/libc/src/string/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 f53ee33168b89aed841e56be560d83ce41fc22be 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/21] 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 7d2403a0954555955cb459de0d231276dadd5fe6 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/21] 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 a8009d30bc492a780943b4646ea05323044b815e 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/21] 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 5449abc76b51df1471e7ed70240da0cd4f1d625f 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/21] 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 ab242e27f323c86798465a4f594aa9f7a49fed0e 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/21] 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 e895dce7817552887b68de885c36118364a43d51 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/21] 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;
More information about the libc-commits
mailing list