[Lldb-commits] [PATCH] D57964: Fix potential UB when target_file directory is null

Stefan Gränitz via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Fri Feb 8 10:56:55 PST 2019


sgraenitz created this revision.
sgraenitz added a reviewer: jingham.

As seen in a crash report, the C-string returned for the directory component of `target_file` can null. It should not be assigned to `std::string` directly as this is undefined behavior.


https://reviews.llvm.org/D57964

Files:
  source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp


Index: source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
===================================================================
--- source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
+++ source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
@@ -2780,7 +2780,13 @@
       basename = pathname; // not a filename, probably a package of some sort,
                            // let it go through
     } else if (is_directory(st) || is_regular_file(st)) {
-      std::string directory = target_file.GetDirectory().GetCString();
+      const char *dir_cstr = target_file.GetDirectory().GetCString();
+      if (!dir_cstr) {
+        error.SetErrorString("invalid directory name");
+        return false;
+      }
+
+      std::string directory(dir_cstr);
       replace_all(directory, "\\", "\\\\");
       replace_all(directory, "'", "\\'");
 
@@ -2843,6 +2849,7 @@
     }
 
     // now actually do the import
+
     command_stream.Clear();
 
     if (was_imported) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D57964.186001.patch
Type: text/x-patch
Size: 1010 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20190208/18494c9e/attachment.bin>


More information about the lldb-commits mailing list