Skip to content

[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

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler-rt/lib/nsan/nsan_interceptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ INTERCEPTOR(char *, strfry, char *s) {

INTERCEPTOR(char *, strsep, char **Stringp, const char *delim) {
char *OrigStringp = REAL(strsep)(Stringp, delim);
if (Stringp != nullptr) {
if (*Stringp != nullptr) {
// The previous character has been overwritten with a '\0' char.
__nsan_set_value_unknown(reinterpret_cast<u8 *>(*Stringp) - 1, 1);
}
Expand Down
24 changes: 24 additions & 0 deletions compiler-rt/test/nsan/intercep_strsep.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// RUN: %clangxx_nsan -O2 %s -o %t
// RUN: %run %t 2>&1 | FileCheck %s

#include <cstring>
#include <iostream>

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;
}
Loading