[Lldb-commits] [PATCH] D74096: [lldb/API] Fix the dangling pointer issue in SBThread::GetStopDescription

Med Ismail Bennani via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Wed Feb 5 15:40:06 PST 2020


mib created this revision.
mib added reviewers: friss, JDevlieghere.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

Instead of creating a char pointer to hold the stop reason description,
the reason is stored in a std::string.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D74096

Files:
  lldb/source/API/SBThread.cpp


Index: lldb/source/API/SBThread.cpp
===================================================================
--- lldb/source/API/SBThread.cpp
+++ lldb/source/API/SBThread.cpp
@@ -325,15 +325,14 @@
 
       StopInfoSP stop_info_sp = exe_ctx.GetThreadPtr()->GetStopInfo();
       if (stop_info_sp) {
-        const char *stop_desc =
-            exe_ctx.GetThreadPtr()->GetStopDescription().c_str();
-        if (stop_desc) {
+        std::string stop_desc = exe_ctx.GetThreadPtr()->GetStopDescription();
+        if (!stop_desc.empty()) {
           if (dst)
-            return ::snprintf(dst, dst_len, "%s", stop_desc);
+            return ::snprintf(dst, dst_len, "%s", stop_desc.c_str());
           else {
             // NULL dst passed in, return the length needed to contain the
             // description
-            return ::strlen(stop_desc) + 1; // Include the NULL byte for size
+            return stop_desc.size() + 1; // Include the NULL byte for size
           }
         } else {
           size_t stop_desc_len = 0;
@@ -362,7 +361,7 @@
             stop_desc =
                 exe_ctx.GetProcessPtr()->GetUnixSignals()->GetSignalAsCString(
                     stop_info_sp->GetValue());
-            if (stop_desc == nullptr || stop_desc[0] == '\0') {
+            if (stop_desc.empty()) {
               static char signal_desc[] = "signal";
               stop_desc = signal_desc;
               stop_desc_len =
@@ -391,13 +390,13 @@
             break;
           }
 
-          if (stop_desc && stop_desc[0]) {
+          if (!stop_desc.empty()) {
             if (dst)
-              return ::snprintf(dst, dst_len, "%s", stop_desc) +
+              return ::snprintf(dst, dst_len, "%s", stop_desc.c_str()) +
                      1; // Include the NULL byte
 
             if (stop_desc_len == 0)
-              stop_desc_len = ::strlen(stop_desc) + 1; // Include the NULL byte
+              stop_desc_len = stop_desc.size() + 1; // Include the NULL byte
 
             return stop_desc_len;
           }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D74096.242773.patch
Type: text/x-patch
Size: 2030 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20200205/34316edf/attachment-0001.bin>


More information about the lldb-commits mailing list