-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[compiler-rt][nsan] Fix strsep interceptor #106307
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
[compiler-rt][nsan] Fix strsep interceptor #106307
Conversation
@llvm/pr-subscribers-compiler-rt-sanitizer Author: Alexander Shaposhnikov (alexander-shaposhnikov) ChangesFix strsep interceptor. Full diff: https://github.com/llvm/llvm-project/pull/106307.diff 2 Files Affected:
diff --git a/compiler-rt/lib/nsan/nsan_interceptors.cpp b/compiler-rt/lib/nsan/nsan_interceptors.cpp
index d607c8d6a636b8..f3422b92d0aeec 100644
--- a/compiler-rt/lib/nsan/nsan_interceptors.cpp
+++ b/compiler-rt/lib/nsan/nsan_interceptors.cpp
@@ -93,11 +93,14 @@ INTERCEPTOR(char *, strfry, char *s) {
}
INTERCEPTOR(char *, strsep, char **Stringp, const char *delim) {
+ if (*Stringp == nullptr)
+ return nullptr;
char *OrigStringp = REAL(strsep)(Stringp, delim);
- if (Stringp != nullptr) {
- // The previous character has been overwritten with a '\0' char.
- __nsan_set_value_unknown(reinterpret_cast<u8 *>(*Stringp) - 1, 1);
- }
+ // No delimiter was found, the token is taken to be the entire string.
+ if (*Stringp == nullptr)
+ return nullptr;
+ // The previous character has been overwritten with a '\0' char.
+ __nsan_set_value_unknown(reinterpret_cast<u8 *>(*Stringp) - 1, 1);
return OrigStringp;
}
diff --git a/compiler-rt/test/nsan/intercep_strsep.cpp b/compiler-rt/test/nsan/intercep_strsep.cpp
new file mode 100644
index 00000000000000..69accd53c4d3ad
--- /dev/null
+++ b/compiler-rt/test/nsan/intercep_strsep.cpp
@@ -0,0 +1,25 @@
+// RUN: %clangxx_nsan -O2 %s -o %t
+// RUN: %run %t 2>&1 | FileCheck %s
+
+#include <iostream>
+#include <cstring>
+
+extern "C" void __nsan_dump_shadow_mem(const char *addr, size_t size_bytes,
+ size_t bytes_per_line, size_t reserved);
+
+int main() {
+ // Define a C-style string with commas as delimiters
+ char input[] = "apple,banana,cherry,date";
+ char* token;
+ char* rest = input; // Pointer to keep track of the rest of the string
+
+ // Tokenize the string using strsep
+ while ((token = strsep(&rest, ",")) != NULL) {
+ std::cout << token << std::endl;
+ }
+
+ __nsan_dump_shadow_mem(&input[5], 1, 1, 0);
+ // CHECK: 0x{{[a-f0-9]*}}: _
+ return 0;
+}
+
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
3509d7f
to
aba1a72
Compare
aba1a72
to
62fe6c5
Compare
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/73/builds/4556 Here is the relevant piece of the build log for the reference
|
Fix strsep interceptor.
For strsep description see https://www.man7.org/linux/man-pages/man3/strsep.3.html