Skip to content

Conversation

@JDevlieghere
Copy link
Member

Use std::numeric_limits<uint32_t>::max() for all overflow checks in ObjectFileWasm and fix a few locations where I incorrectly used >= instead of >.

Use std::numeric_limits<uint32_t>::max() for all overflow checks in
ObjectFileWasm and fix a few locations where I incorrectly used `>=`
instead of `>`.
@JDevlieghere JDevlieghere enabled auto-merge (squash) August 13, 2025 01:43
@llvmbot llvmbot added the lldb label Aug 13, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 13, 2025

@llvm/pr-subscribers-lldb

Author: Jonas Devlieghere (JDevlieghere)

Changes

Use std::numeric_limits<uint32_t>::max() for all overflow checks in ObjectFileWasm and fix a few locations where I incorrectly used &gt;= instead of &gt;.


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

1 Files Affected:

  • (modified) lldb/source/Plugins/ObjectFile/wasm/ObjectFileWasm.cpp (+6-6)
diff --git a/lldb/source/Plugins/ObjectFile/wasm/ObjectFileWasm.cpp b/lldb/source/Plugins/ObjectFile/wasm/ObjectFileWasm.cpp
index a489b05acfcb4..919cc21c32ffd 100644
--- a/lldb/source/Plugins/ObjectFile/wasm/ObjectFileWasm.cpp
+++ b/lldb/source/Plugins/ObjectFile/wasm/ObjectFileWasm.cpp
@@ -63,7 +63,7 @@ GetWasmString(llvm::DataExtractor &data, llvm::DataExtractor::Cursor &c) {
     return std::nullopt;
   }
 
-  if (len >= (uint64_t(1) << 32)) {
+  if (len > std::numeric_limits<uint32_t>::max()) {
     return std::nullopt;
   }
 
@@ -175,7 +175,7 @@ bool ObjectFileWasm::DecodeNextSection(lldb::offset_t *offset_ptr) {
   if (!c)
     return !llvm::errorToBool(c.takeError());
 
-  if (payload_len >= (uint64_t(1) << 32))
+  if (payload_len > std::numeric_limits<uint32_t>::max())
     return false;
 
   if (section_id == llvm::wasm::WASM_SEC_CUSTOM) {
@@ -256,7 +256,7 @@ ParseFunctions(SectionSP code_section_sp) {
   lldb::offset_t offset = 0;
 
   const uint64_t function_count = code_section_data.GetULEB128(&offset);
-  if (function_count >= std::numeric_limits<uint32_t>::max())
+  if (function_count > std::numeric_limits<uint32_t>::max())
     return llvm::createStringError("function count overflows uint32_t");
 
   std::vector<AddressRange> functions;
@@ -264,7 +264,7 @@ ParseFunctions(SectionSP code_section_sp) {
 
   for (uint32_t i = 0; i < function_count; ++i) {
     const uint64_t function_size = code_section_data.GetULEB128(&offset);
-    if (function_size >= std::numeric_limits<uint32_t>::max())
+    if (function_size > std::numeric_limits<uint32_t>::max())
       return llvm::createStringError("function size overflows uint32_t");
     // llvm-objdump considers the ULEB with the function size to be part of the
     // function. We can't do that here because that would break symbolic
@@ -293,13 +293,13 @@ ParseNames(SectionSP name_section_sp,
   while (c && c.tell() < data.size()) {
     const uint8_t type = data.getU8(c);
     const uint64_t size = data.getULEB128(c);
-    if (size >= std::numeric_limits<uint32_t>::max())
+    if (size > std::numeric_limits<uint32_t>::max())
       return llvm::createStringError("size overflows uint32_t");
 
     switch (type) {
     case llvm::wasm::WASM_NAMES_FUNCTION: {
       const uint64_t count = data.getULEB128(c);
-      if (count >= std::numeric_limits<uint32_t>::max())
+      if (count > std::numeric_limits<uint32_t>::max())
         return llvm::createStringError("function count overflows uint32_t");
 
       for (uint64_t i = 0; c && i < count; ++i) {

@JDevlieghere JDevlieghere merged commit 84c5b95 into llvm:main Aug 13, 2025
11 checks passed
@JDevlieghere JDevlieghere deleted the numeric_limits branch August 13, 2025 01:50
JDevlieghere added a commit to swiftlang/llvm-project that referenced this pull request Aug 14, 2025
…lvm#153332)

Use std::numeric_limits<uint32_t>::max() for all overflow checks in
ObjectFileWasm and fix a few locations where I incorrectly used `>=`
instead of `>`.

(cherry picked from commit 84c5b95)
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.

2 participants