-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[libc] Expand usage of libc null checks. #116262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[libc] Expand usage of libc null checks. #116262
Conversation
e895dce
to
f7da133
Compare
Hey @nickdesaulniers , I'm done with the changes however I'm currently creating unit tests to check that the functions segfault correctly. I'm failing to catch the segfault gracefully. Here's my current implementation which segfaults and coredumps for the
I tried the |
What's the error you observe? The precommit bot is pointing out:
which isn't a linkage failure. |
I reverted the changes in the functions that were failing in the prebuilt commit , My only failures now are in the tests. Here is the test I'm trying out , the test is added inside memchr_test.cpp:
Here is the result of the test :
What I understand from the error is that it's not seeing the |
@michaelrj-google pointed out to me that any tests that you add usage of Example:
Sounds like that should disable the hermetic test for that function, which is probably fine. |
✅ With the latest revision this PR passed the C/C++ code formatter. |
#123401 has been merged; please rebase on top of that. Then you can drop the extra temporary variables. |
Will patch it first thing tomorrow, I'll also remove checking for the signal and just leave the |
bed04e1
to
130bc73
Compare
@AlyElashram do you mind syncing the PR to resolve conflicts? |
@lntue can you please check the tests out when you have the time 👀 |
5d49deb
to
e6a336f
Compare
libc/src/string/memrchr.cpp
Outdated
#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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to check if n != 0
before checking nullptr?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These functions are probably GNU or BSD extensions and are not specified in the C standards, so technically we can define any behavior that we think it makes sense. Nonetheless, the implementation below is not UB and well-behaved when n = 0
, in addition to being consistent with memchr
.
libc/test/src/string/memcpy_test.cpp
Outdated
ASSERT_DEATH([]() { LIBC_NAMESPACE::memcpy(nullptr, nullptr, 1); }, | ||
WITH_SIGNAL(-1)); | ||
} | ||
#endif // defined(LIBC_TARGET_OS_IS_LINUX) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: You might want to do mass update the comments.
@@ -18,6 +19,10 @@ namespace LIBC_NAMESPACE_DECL { | |||
LLVM_LIBC_FUNCTION(void *, mempcpy, | |||
(void *__restrict dst, const void *__restrict src, | |||
size_t count)) { | |||
if (count) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a count check here , for consistency with memcpy and also since memcpy and mempcpy are well behaved at n = 0
@lntue
@lntue the unit tests are generating failures here that I don't understand. I'm not sure if this should've been merged. They generated errors that are also affecting all other PRs. I traced back the errors and they only occur on this PR , and not #142085 Failure in question :
|
|
Fixes #111546