[libc-commits] [libc] [libc] Expand usage of libc null checks. (PR #116262)
Aly ElAshram via libc-commits
libc-commits at lists.llvm.org
Fri Dec 13 08:53:43 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/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 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/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 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/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 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/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 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/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 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/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 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/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 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/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 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/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 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/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 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/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 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/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 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/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 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/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 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/21] 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/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 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/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 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/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 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/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 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/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 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/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