Skip to content

[lldb][windows] fix unchecked Expected<T>#178681

Merged
charles-zablit merged 1 commit intollvm:mainfrom
charles-zablit:charles-zablit/lldb/fix-unchecked-expected-upstream
Jan 29, 2026
Merged

[lldb][windows] fix unchecked Expected<T>#178681
charles-zablit merged 1 commit intollvm:mainfrom
charles-zablit:charles-zablit/lldb/fix-unchecked-expected-upstream

Conversation

@charles-zablit
Copy link
Contributor

This patch fixes unchecked Expected<T> returns from the CompilerType::GetByteSize method.

@charles-zablit charles-zablit changed the title [lldb][windows] fix unchecked Expected [lldb][windows] fix unchecked Expected<T> Jan 29, 2026
@llvmbot llvmbot added the lldb label Jan 29, 2026
@charles-zablit charles-zablit self-assigned this Jan 29, 2026
@llvmbot
Copy link
Member

llvmbot commented Jan 29, 2026

@llvm/pr-subscribers-lldb

Author: Charles Zablit (charles-zablit)

Changes

This patch fixes unchecked Expected&lt;T&gt; returns from the CompilerType::GetByteSize method.


Full diff: https://github.com/llvm/llvm-project/pull/178681.diff

3 Files Affected:

  • (modified) lldb/include/lldb/Target/ProcessStructReader.h (+20-8)
  • (modified) lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp (+9-5)
  • (modified) lldb/source/ValueObject/ValueObject.cpp (+2-2)
diff --git a/lldb/include/lldb/Target/ProcessStructReader.h b/lldb/include/lldb/Target/ProcessStructReader.h
index 0f0b3f69d5509..df3203698afd0 100644
--- a/lldb/include/lldb/Target/ProcessStructReader.h
+++ b/lldb/include/lldb/Target/ProcessStructReader.h
@@ -16,6 +16,7 @@
 #include "lldb/Target/Process.h"
 #include "lldb/Utility/DataBufferHeap.h"
 #include "lldb/Utility/DataExtractor.h"
+#include "lldb/Utility/LLDBLog.h"
 #include "lldb/Utility/Status.h"
 
 #include "llvm/ADT/StringMap.h"
@@ -59,21 +60,32 @@ class ProcessStructReader {
       // no support for bitfields in here (yet)
       if (is_bitfield)
         return;
-      auto size = field_type.GetByteSize(nullptr);
+      auto size_or_err = field_type.GetByteSize(nullptr);
+      if (!size_or_err) {
+        LLDB_LOG_ERROR(GetLog(LLDBLog::Target), size_or_err.takeError(), "{0}");
+        return;
+      }
+      size_t size = *size_or_err;
+
       // no support for things larger than a uint64_t (yet)
-      if (!size || *size > 8)
+      if (size > 8)
         return;
       size_t byte_index = static_cast<size_t>(bit_offset / 8);
-      m_fields.insert({name, FieldImpl{field_type, byte_index,
-                                       static_cast<size_t>(*size)}});
+      m_fields.insert(
+          {name, FieldImpl{field_type, byte_index, static_cast<size_t>(size)}});
     }
-    auto total_size = struct_type.GetByteSize(nullptr);
-    if (!total_size)
+    auto total_size_or_err = struct_type.GetByteSize(nullptr);
+    if (!total_size_or_err) {
+      LLDB_LOG_ERROR(GetLog(LLDBLog::Target), total_size_or_err.takeError(),
+                     "{0}");
       return;
-    lldb::WritableDataBufferSP buffer_sp(new DataBufferHeap(*total_size, 0));
+    }
+    size_t total_size = *total_size_or_err;
+
+    lldb::WritableDataBufferSP buffer_sp(new DataBufferHeap(total_size, 0));
     Status error;
     process->ReadMemoryFromInferior(base_addr, buffer_sp->GetBytes(),
-                                    *total_size, error);
+                                    total_size, error);
     if (error.Fail())
       return;
     m_data = DataExtractor(buffer_sp, m_byte_order, m_addr_byte_size);
diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
index 34d97a07d73fc..478f10a60a865 100644
--- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
+++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
@@ -885,11 +885,15 @@ lldb::BasicType TypeSystemClang::GetBasicTypeEnumeration(llvm::StringRef name) {
 }
 
 uint32_t TypeSystemClang::GetPointerByteSize() {
-  if (m_pointer_byte_size == 0)
-    if (auto size = GetBasicType(lldb::eBasicTypeVoid)
-                        .GetPointerType()
-                        .GetByteSize(nullptr))
-      m_pointer_byte_size = *size;
+  if (m_pointer_byte_size != 0)
+    return m_pointer_byte_size;
+  auto size_or_err =
+      GetBasicType(lldb::eBasicTypeVoid).GetPointerType().GetByteSize(nullptr);
+  if (!size_or_err) {
+    LLDB_LOG_ERROR(GetLog(LLDBLog::Types), size_or_err.takeError(), "{0}");
+    return m_pointer_byte_size;
+  }
+  m_pointer_byte_size = *size_or_err;
   return m_pointer_byte_size;
 }
 
diff --git a/lldb/source/ValueObject/ValueObject.cpp b/lldb/source/ValueObject/ValueObject.cpp
index 121054e3e92ed..9c453e06b1c31 100644
--- a/lldb/source/ValueObject/ValueObject.cpp
+++ b/lldb/source/ValueObject/ValueObject.cpp
@@ -744,8 +744,8 @@ size_t ValueObject::GetPointeeData(DataExtractor &data, uint32_t item_idx,
       }
     } break;
     case eAddressTypeHost: {
-      auto max_bytes =
-          GetCompilerType().GetByteSize(exe_ctx.GetBestExecutionContextScope());
+      auto max_bytes = llvm::expectedToOptional(GetCompilerType().GetByteSize(
+          exe_ctx.GetBestExecutionContextScope()));
       if (max_bytes && *max_bytes > offset) {
         size_t bytes_read = std::min<uint64_t>(*max_bytes - offset, bytes);
         addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);

Copy link
Collaborator

@adrian-prantl adrian-prantl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow I am impressed this survived so long!

@charles-zablit charles-zablit merged commit 8a4244b into llvm:main Jan 29, 2026
10 of 11 checks passed
charles-zablit added a commit to charles-zablit/llvm-project that referenced this pull request Jan 29, 2026
This patch fixes unchecked `Expected<T>` returns from the
`CompilerType::GetByteSize` method.

(cherry picked from commit 8a4244b)
honeygoyal pushed a commit to honeygoyal/llvm-project that referenced this pull request Jan 30, 2026
This patch fixes unchecked `Expected<T>` returns from the
`CompilerType::GetByteSize` method.
sshrestha-aa pushed a commit to sshrestha-aa/llvm-project that referenced this pull request Feb 4, 2026
This patch fixes unchecked `Expected<T>` returns from the
`CompilerType::GetByteSize` method.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants