https://github.com/JDevlieghere created 
https://github.com/llvm/llvm-project/pull/175181

This is another instance where we weren't checking that the result of 
FileSystem::CreateDataBuffer and unconditionally accessing it, similar to the 
bug in SourceManager last week. In this particular case, ObjectFile was 
assuming that we can read the contents non-zero, which isn't true for directory 
nodes.

Jim figured this one out yesterday. I'm just putting up the patch and adding a 
test.

rdar://167796036

>From b8cc29289904021de4588306edd0785b09806c0b Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <[email protected]>
Date: Fri, 9 Jan 2026 07:00:38 -0800
Subject: [PATCH] [lldb] Fix crash when passing a folder in as the executable

This is another instance where we weren't checking that the result of
FileSystem::CreateDataBuffer and unconditionally accessing it, similar
to the bug in SourceManager last week. In this particular case,
ObjectFile was assuming that we can read the contents non-zero, which
isn't true for directory nodes.

Jim figured this one out yesterday. I'm just putting up the patch and
adding a test.

rdar://167796036
---
 lldb/source/Symbol/ObjectFile.cpp       | 13 ++++++++-----
 lldb/test/Shell/ObjectFile/invalid.test |  4 ++++
 2 files changed, 12 insertions(+), 5 deletions(-)
 create mode 100644 lldb/test/Shell/ObjectFile/invalid.test

diff --git a/lldb/source/Symbol/ObjectFile.cpp 
b/lldb/source/Symbol/ObjectFile.cpp
index 28d811db22a1c..1dfd84c4b61e3 100644
--- a/lldb/source/Symbol/ObjectFile.cpp
+++ b/lldb/source/Symbol/ObjectFile.cpp
@@ -84,11 +84,14 @@ ObjectFileSP ObjectFile::FindPlugin(const lldb::ModuleSP 
&module_sp,
     // container plug-ins can use these bytes to see if they can parse this
     // file.
     if (file_size > 0) {
-      DataBufferSP buffer_sp = FileSystem::Instance().CreateDataBuffer(
-          file->GetPath(), g_initial_bytes_to_read, file_offset);
-      extractor_sp = std::make_shared<DataExtractor>();
-      extractor_sp->SetData(buffer_sp, data_offset, buffer_sp->GetByteSize());
-      data_offset = 0;
+      // Check that we made a data buffer. For instance, a directory node is
+      // not 0 size, but we can't make a data buffer for it.
+      if (DataBufferSP buffer_sp = FileSystem::Instance().CreateDataBuffer(
+              file->GetPath(), g_initial_bytes_to_read, file_offset)) {
+        extractor_sp = std::make_shared<DataExtractor>();
+        extractor_sp->SetData(buffer_sp, data_offset, 
buffer_sp->GetByteSize());
+        data_offset = 0;
+      }
     }
   }
 
diff --git a/lldb/test/Shell/ObjectFile/invalid.test 
b/lldb/test/Shell/ObjectFile/invalid.test
new file mode 100644
index 0000000000000..5e30f6aee753d
--- /dev/null
+++ b/lldb/test/Shell/ObjectFile/invalid.test
@@ -0,0 +1,4 @@
+RUN: mkdir -p %t.dir
+RUN: %lldb %t.dir 2>&1| FileCheck %s
+
+CHECK: error: '{{.*}}.dir' is not a valid executable

_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to