From 69a3b8bd179e139c04d1c644d010206c938c2797 Mon Sep 17 00:00:00 2001 From: Andreas Gampe Date: Mon, 25 Nov 2024 16:21:00 -0800 Subject: [PATCH] Change asserts to use typed exceptions Summary: As title. Reviewed By: ssj933 Differential Revision: D66473889 fbshipit-source-id: de8800fbba85f0c924904401fcb2eff8268ce040 --- libredex/DexLoader.cpp | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/libredex/DexLoader.cpp b/libredex/DexLoader.cpp index b4e4c706c53..2e5cb991860 100644 --- a/libredex/DexLoader.cpp +++ b/libredex/DexLoader.cpp @@ -42,8 +42,8 @@ struct AssertHelper { // Yeah, it's not good form to throw in a destructor. But... // NOLINTNEXTLINE(bugprone-exception-escape) ~AssertHelper() noexcept(false) { - throw RedexException( - RedexError::INVALID_DEX, message ? *message : std::string(""), + assert_or_throw( + false, RedexError::INVALID_DEX, message ? *message : std::string(""), extra_info ? *extra_info : std::map()); } @@ -88,18 +88,17 @@ void dex_range_assert(uint64_t offset, const std::string& msg_invalid, const std::string& msg_invalid_extent, const std::string& offset_name) { - if (offset >= dexsize) { - throw RedexException(RedexError::INVALID_DEX, msg_invalid, - {{offset_name, std::to_string(offset)}, - {"extent", std::to_string(extent)}, - {"dex_size", std::to_string(dexsize)}}); - } - if (extent > dexsize || offset > dexsize - extent) { - throw RedexException(RedexError::INVALID_DEX, msg_invalid_extent, - {{offset_name, std::to_string(offset)}, - {"extent", std::to_string(extent)}, - {"dex_size", std::to_string(dexsize)}}); - } + assert_or_throw(offset < dexsize, RedexError::INVALID_DEX, msg_invalid, + {{offset_name, std::to_string(offset)}, + {"extent", std::to_string(extent)}, + {"dex_size", std::to_string(dexsize)}}); + + assert_or_throw(extent <= dexsize && offset <= dexsize - extent, + RedexError::INVALID_DEX, + msg_invalid_extent, + {{offset_name, std::to_string(offset)}, + {"extent", std::to_string(extent)}, + {"dex_size", std::to_string(dexsize)}}); } template