[Lldb-commits] [lldb] f7c961c - [LLDB][NFC][Reliability] Fixes for int overflow and uninitialized state

Slava Gurevich via lldb-commits lldb-commits at lists.llvm.org
Fri Jul 29 13:31:36 PDT 2022


Author: Slava Gurevich
Date: 2022-07-29T13:31:17-07:00
New Revision: f7c961cc6ba71c9d1fb845807e31b3a278d13c2f

URL: https://github.com/llvm/llvm-project/commit/f7c961cc6ba71c9d1fb845807e31b3a278d13c2f
DIFF: https://github.com/llvm/llvm-project/commit/f7c961cc6ba71c9d1fb845807e31b3a278d13c2f.diff

LOG: [LLDB][NFC][Reliability] Fixes for int overflow and uninitialized state

Fixing potential int overflow and uninitialized variables.
These were found by Coverity static code inspection.

Differential Revision: https://reviews.llvm.org/D130795

Added: 
    

Modified: 
    lldb/source/Plugins/Instruction/ARM64/EmulateInstructionARM64.cpp
    lldb/source/Plugins/Process/Utility/NativeRegisterContextDBReg_x86.cpp
    lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
    lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp
    lldb/source/Plugins/SystemRuntime/MacOSX/SystemRuntimeMacOSX.cpp
    lldb/source/Plugins/UnwindAssembly/x86/x86AssemblyInspectionEngine.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/source/Plugins/Instruction/ARM64/EmulateInstructionARM64.cpp b/lldb/source/Plugins/Instruction/ARM64/EmulateInstructionARM64.cpp
index f86609f3c5c1c..80cee99ef0f8f 100644
--- a/lldb/source/Plugins/Instruction/ARM64/EmulateInstructionARM64.cpp
+++ b/lldb/source/Plugins/Instruction/ARM64/EmulateInstructionARM64.cpp
@@ -633,7 +633,7 @@ bool EmulateInstructionARM64::EmulateADDSUBImm(const uint32_t opcode) {
     imm = imm12;
     break;
   case 1:
-    imm = imm12 << 12;
+    imm = static_cast<uint64_t>(imm12) << 12;
     break;
   default:
     return false; // UNDEFINED;

diff  --git a/lldb/source/Plugins/Process/Utility/NativeRegisterContextDBReg_x86.cpp b/lldb/source/Plugins/Process/Utility/NativeRegisterContextDBReg_x86.cpp
index 222e4a2690e4f..f5525e3e3cb3d 100644
--- a/lldb/source/Plugins/Process/Utility/NativeRegisterContextDBReg_x86.cpp
+++ b/lldb/source/Plugins/Process/Utility/NativeRegisterContextDBReg_x86.cpp
@@ -18,7 +18,7 @@ using namespace lldb_private;
 static inline uint64_t GetStatusBit(uint32_t wp_index) {
   // DR6: ...BBBB
   //         3210 <- status bits for bp./wp. i; 1 if hit
-  return 1 << wp_index;
+  return 1ULL << wp_index;
 }
 
 // Returns mask/value for global enable bit of wp_index in DR7
@@ -27,14 +27,14 @@ static inline uint64_t GetEnableBit(uint32_t wp_index) {
   //         33221100 <- global/local enable for bp./wp.; 1 if enabled
   // we use global bits because NetBSD kernel does not preserve local
   // bits reliably; Linux seems fine with either
-  return 1 << (2 * wp_index + 1);
+  return 1ULL << (2 * wp_index + 1);
 }
 
 // Returns mask for both enable bits of wp_index in DR7
 static inline uint64_t GetBothEnableBitMask(uint32_t wp_index) {
   // DR7: ...GLGLGLGL
   //         33221100 <- global/local enable for bp./wp.; 1 if enabled
-  return 3 << (2 * wp_index + 1);
+  return 3ULL << (2 * wp_index + 1);
 }
 
 // Returns value for type bits of wp_index in DR7
@@ -47,7 +47,7 @@ static inline uint64_t GetWatchTypeBits(uint32_t watch_flags,
   // wp.: 3333222211110000...
   //
   // where T - type is 01 for write, 11 for r/w
-  return watch_flags << (16 + 4 * wp_index);
+  return static_cast<uint64_t>(watch_flags) << (16 + 4 * wp_index);
 }
 
 // Returns value for size bits of wp_index in DR7
@@ -63,7 +63,8 @@ static inline uint64_t GetWatchSizeBits(uint32_t size, uint32_t wp_index) {
   // 01 for 2 bytes
   // 10 for 8 bytes
   // 11 for 4 bytes
-  return (size == 8 ? 0x2 : size - 1) << (18 + 4 * wp_index);
+  return static_cast<uint64_t>(size == 8 ? 0x2 : size - 1)
+         << (18 + 4 * wp_index);
 }
 
 // Returns bitmask for all bits controlling wp_index in DR7

diff  --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
index 6de5ab44f26f7..b6407af18cfdf 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
@@ -2731,7 +2731,7 @@ void DWARFASTParserClang::ParseSingleMember(
 
   uint64_t field_bit_offset = (attrs.member_byte_offset == UINT32_MAX
                                    ? 0
-                                   : (attrs.member_byte_offset * 8));
+                                   : (attrs.member_byte_offset * 8ULL));
 
   if (attrs.bit_size > 0) {
     FieldInfo this_field_info;

diff  --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp b/lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp
index 7b4a5d8eca3ed..dacf2f4110a4f 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp
@@ -582,7 +582,7 @@ void DWARFUnit::SetStrOffsetsBase(dw_offset_t str_offsets_base) {
 dw_addr_t DWARFUnit::ReadAddressFromDebugAddrSection(uint32_t index) const {
   uint32_t index_size = GetAddressByteSize();
   dw_offset_t addr_base = GetAddrBase();
-  dw_addr_t offset = addr_base + index * index_size;
+  dw_addr_t offset = addr_base + static_cast<dw_addr_t>(index) * index_size;
   const DWARFDataExtractor &data =
       m_dwarf.GetDWARFContext().getOrLoadAddrData();
   if (data.ValidOffsetForDataOfSize(offset, index_size))
@@ -1033,7 +1033,8 @@ DWARFUnit::FindRnglistFromOffset(dw_offset_t offset) {
           GetAddressByteSize(), [&](uint32_t index) {
             uint32_t index_size = GetAddressByteSize();
             dw_offset_t addr_base = GetAddrBase();
-            lldb::offset_t offset = addr_base + index * index_size;
+            lldb::offset_t offset =
+                addr_base + static_cast<lldb::offset_t>(index) * index_size;
             return llvm::object::SectionedAddress{
                 m_dwarf.GetDWARFContext().getOrLoadAddrData().GetMaxU64(
                     &offset, index_size)};

diff  --git a/lldb/source/Plugins/SystemRuntime/MacOSX/SystemRuntimeMacOSX.cpp b/lldb/source/Plugins/SystemRuntime/MacOSX/SystemRuntimeMacOSX.cpp
index 7fc1d6ab49ec1..7a56264f87c9b 100644
--- a/lldb/source/Plugins/SystemRuntime/MacOSX/SystemRuntimeMacOSX.cpp
+++ b/lldb/source/Plugins/SystemRuntime/MacOSX/SystemRuntimeMacOSX.cpp
@@ -787,14 +787,14 @@ SystemRuntimeMacOSX::GetPendingItemRefsForQueue(lldb::addr_t queue) {
           //   }
 
           offset_t offset = 0;
-          int i = 0;
+          uint64_t i = 0;
           uint32_t version = extractor.GetU32(&offset);
           if (version == 1) {
             pending_item_refs.new_style = true;
             uint32_t item_size = extractor.GetU32(&offset);
             uint32_t start_of_array_offset = offset;
             while (offset < pending_items_pointer.items_buffer_size &&
-                   static_cast<size_t>(i) < pending_items_pointer.count) {
+                   i < pending_items_pointer.count) {
               offset = start_of_array_offset + (i * item_size);
               ItemRefAndCodeAddress item;
               item.item_ref = extractor.GetAddress(&offset);
@@ -806,7 +806,7 @@ SystemRuntimeMacOSX::GetPendingItemRefsForQueue(lldb::addr_t queue) {
             offset = 0;
             pending_item_refs.new_style = false;
             while (offset < pending_items_pointer.items_buffer_size &&
-                   static_cast<size_t>(i) < pending_items_pointer.count) {
+                   i < pending_items_pointer.count) {
               ItemRefAndCodeAddress item;
               item.item_ref = extractor.GetAddress(&offset);
               item.code_address = LLDB_INVALID_ADDRESS;

diff  --git a/lldb/source/Plugins/UnwindAssembly/x86/x86AssemblyInspectionEngine.cpp b/lldb/source/Plugins/UnwindAssembly/x86/x86AssemblyInspectionEngine.cpp
index c796cbc75c1b6..b6e07427a5e85 100644
--- a/lldb/source/Plugins/UnwindAssembly/x86/x86AssemblyInspectionEngine.cpp
+++ b/lldb/source/Plugins/UnwindAssembly/x86/x86AssemblyInspectionEngine.cpp
@@ -966,7 +966,7 @@ bool x86AssemblyInspectionEngine::GetNonCallSiteUnwindPlanFromAssembly(
   // path jumps over the mid-function epilogue
 
   UnwindPlan::RowSP prologue_completed_row; // copy of prologue row of CFI
-  int prologue_completed_sp_bytes_offset_from_cfa; // The sp value before the
+  int prologue_completed_sp_bytes_offset_from_cfa = 0; // The sp value before the
                                                    // epilogue started executed
   bool prologue_completed_is_aligned = false;
   std::vector<bool> prologue_completed_saved_registers;


        


More information about the lldb-commits mailing list