Skip to content

Commit

Permalink
Asserts and typed exception
Browse files Browse the repository at this point in the history
Summary: Use asserts instead of throwing an exception directly. Introduce `BufferEndExceededException`.

Reviewed By: thezhangwei

Differential Revision: D66487161

fbshipit-source-id: bbacb8927c3f2c8b5e7cdc6fd1be7b3891ac9f5d
  • Loading branch information
agampe authored and facebook-github-bot committed Nov 26, 2024
1 parent dd870ae commit 756b9b9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
2 changes: 2 additions & 0 deletions libredex/Debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ void assert_fail(const char* expr,

if (redex::g_throw_typed) {
switch (type) {
case RedexError::BUFFER_END_EXCEEDED:
throw redex::BufferEndExceededException(msg);
case RedexError::INVALID_DEX:
throw redex::InvalidDexException(msg);
default:
Expand Down
20 changes: 8 additions & 12 deletions libredex/JarLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ namespace JarLoaderUtil {
uint32_t read32(uint8_t*& buffer, uint8_t* buffer_end) {
uint32_t rv;
auto next = buffer + sizeof(uint32_t);
if (next > buffer_end) {
throw RedexException(RedexError::BUFFER_END_EXCEEDED);
}
always_assert_type_log(next <= buffer_end, BUFFER_END_EXCEEDED,
"Buffer overflow");
memcpy(&rv, buffer, sizeof(uint32_t));
buffer = next;
return htonl(rv);
Expand All @@ -52,18 +51,16 @@ uint32_t read32(uint8_t*& buffer, uint8_t* buffer_end) {
uint16_t read16(uint8_t*& buffer, uint8_t* buffer_end) {
uint16_t rv;
auto next = buffer + sizeof(uint16_t);
if (next > buffer_end) {
throw RedexException(RedexError::BUFFER_END_EXCEEDED);
}
always_assert_type_log(next <= buffer_end, BUFFER_END_EXCEEDED,
"Buffer overflow");
memcpy(&rv, buffer, sizeof(uint16_t));
buffer = next;
return htons(rv);
}

uint8_t read8(uint8_t*& buffer, uint8_t* buffer_end) {
if (buffer >= buffer_end) {
throw RedexException(RedexError::BUFFER_END_EXCEEDED);
}
always_assert_type_log(buffer <= buffer_end, BUFFER_END_EXCEEDED,
"Buffer overflow");
return *buffer++;
}
} // namespace JarLoaderUtil
Expand Down Expand Up @@ -163,9 +160,8 @@ bool parse_cp_entry(uint8_t*& buffer, uint8_t* buffer_end, cp_entry& cpe) {
cpe.len = read16(buffer, buffer_end);
cpe.data = buffer;
buffer += cpe.len;
if (buffer > buffer_end) {
throw RedexException(RedexError::BUFFER_END_EXCEEDED);
}
always_assert_type_log(buffer <= buffer_end, BUFFER_END_EXCEEDED,
"Buffer overflow");
return true;
case CP_CONST_INVOKEDYN:
std::cerr << "INVOKEDYN constant unsupported, Bailing\n";
Expand Down
2 changes: 2 additions & 0 deletions libredex/RedexException.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ void assert_or_throw(bool cond,
if (!cond) {
if (redex::throw_typed_exception()) {
switch (type) {
case RedexError::BUFFER_END_EXCEEDED:
throw redex::BufferEndExceededException(message, extra_info);
case RedexError::INVALID_DEX:
throw redex::InvalidDexException(message, extra_info);
default:
Expand Down
8 changes: 8 additions & 0 deletions libredex/RedexException.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ class InvalidDexException : public RedexException {
: RedexException(RedexError::INVALID_DEX, message, extra_info) {}
};

class BufferEndExceededException : public RedexException {
public:
explicit BufferEndExceededException(
const std::string& message,
const std::map<std::string, std::string>& extra_info = {})
: RedexException(RedexError::BUFFER_END_EXCEEDED, message, extra_info) {}
};

} // namespace redex

void assert_or_throw(bool cond,
Expand Down

0 comments on commit 756b9b9

Please sign in to comment.