From 7046e7cd8ab7c86dc6f3ee9d6d3ec95bda7c8a05 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Tue, 28 Mar 2023 15:09:41 -0400 Subject: [PATCH] strace: Cleanup read_string. Our std::string is not broken; rather, the bytesRead may include the terminating NULL byte, which was then added to the string and caused misbehaviors. So we need to use strnlen with std::string also, and then everything works as expected. --- src/bin/debug/strace/TypeHandler.cpp | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/src/bin/debug/strace/TypeHandler.cpp b/src/bin/debug/strace/TypeHandler.cpp index 0fb1100428..ca5037089a 100644 --- a/src/bin/debug/strace/TypeHandler.cpp +++ b/src/bin/debug/strace/TypeHandler.cpp @@ -136,22 +136,10 @@ read_string(Context &context, void *data) int32 bytesRead; status_t error = context.Reader().Read(data, buffer, sizeof(buffer), bytesRead); if (error == B_OK) { -// return string("\"") + string(buffer, bytesRead) + "\""; -//string result("\""); -//result += string(buffer, bytesRead); -//result += "\""; -//return result; - -// TODO: Unless I'm missing something obvious, our STL string class is broken. -// The appended "\"" doesn't appear in either of the above cases. - - int32 len = strnlen(buffer, sizeof(buffer)); - char largeBuffer[259]; - largeBuffer[0] = '"'; - memcpy(largeBuffer + 1, buffer, len); - largeBuffer[len + 1] = '"'; - largeBuffer[len + 2] = '\0'; - return largeBuffer; + string result("\""); + result += string(buffer, strnlen(buffer, sizeof(buffer))); + result += "\""; + return result; } return context.FormatPointer(data) + " (" + strerror(error) + ")";