[libc-commits] [libc] Expand usage of libc null checks (PR #116262)
Aly ElAshram via libc-commits
libc-commits at lists.llvm.org
Thu Nov 14 09:51:08 PST 2024
https://github.com/AlyElashram created https://github.com/llvm/llvm-project/pull/116262
None
>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 1/5] 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 2/5] 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 3/5] 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 4/5] 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 5/5] 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;
More information about the libc-commits
mailing list