Skip to content

Commit 84c5b95

Browse files
authored
[lldb] Use numeric_limits for all overflow checks in ObjectFileWasm (llvm#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 `>`.
1 parent acef1db commit 84c5b95

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

lldb/source/Plugins/ObjectFile/wasm/ObjectFileWasm.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ GetWasmString(llvm::DataExtractor &data, llvm::DataExtractor::Cursor &c) {
6363
return std::nullopt;
6464
}
6565

66-
if (len >= (uint64_t(1) << 32)) {
66+
if (len > std::numeric_limits<uint32_t>::max()) {
6767
return std::nullopt;
6868
}
6969

@@ -175,7 +175,7 @@ bool ObjectFileWasm::DecodeNextSection(lldb::offset_t *offset_ptr) {
175175
if (!c)
176176
return !llvm::errorToBool(c.takeError());
177177

178-
if (payload_len >= (uint64_t(1) << 32))
178+
if (payload_len > std::numeric_limits<uint32_t>::max())
179179
return false;
180180

181181
if (section_id == llvm::wasm::WASM_SEC_CUSTOM) {
@@ -256,15 +256,15 @@ ParseFunctions(SectionSP code_section_sp) {
256256
lldb::offset_t offset = 0;
257257

258258
const uint64_t function_count = code_section_data.GetULEB128(&offset);
259-
if (function_count >= std::numeric_limits<uint32_t>::max())
259+
if (function_count > std::numeric_limits<uint32_t>::max())
260260
return llvm::createStringError("function count overflows uint32_t");
261261

262262
std::vector<AddressRange> functions;
263263
functions.reserve(function_count);
264264

265265
for (uint32_t i = 0; i < function_count; ++i) {
266266
const uint64_t function_size = code_section_data.GetULEB128(&offset);
267-
if (function_size >= std::numeric_limits<uint32_t>::max())
267+
if (function_size > std::numeric_limits<uint32_t>::max())
268268
return llvm::createStringError("function size overflows uint32_t");
269269
// llvm-objdump considers the ULEB with the function size to be part of the
270270
// function. We can't do that here because that would break symbolic
@@ -293,13 +293,13 @@ ParseNames(SectionSP name_section_sp,
293293
while (c && c.tell() < data.size()) {
294294
const uint8_t type = data.getU8(c);
295295
const uint64_t size = data.getULEB128(c);
296-
if (size >= std::numeric_limits<uint32_t>::max())
296+
if (size > std::numeric_limits<uint32_t>::max())
297297
return llvm::createStringError("size overflows uint32_t");
298298

299299
switch (type) {
300300
case llvm::wasm::WASM_NAMES_FUNCTION: {
301301
const uint64_t count = data.getULEB128(c);
302-
if (count >= std::numeric_limits<uint32_t>::max())
302+
if (count > std::numeric_limits<uint32_t>::max())
303303
return llvm::createStringError("function count overflows uint32_t");
304304

305305
for (uint64_t i = 0; c && i < count; ++i) {

0 commit comments

Comments
 (0)