Check for negative return values from ReadFromOffset

In some places, we check if the amount we read is a multiple of some amount we are interested in. However, ReadFromOffset returns -1 when it errors. Certain record sizes can cause ReadFromOffset to cause us to think that we succeeded when we did not.

It also results in confusing messages in logs.

PiperOrigin-RevId: 461798762
Change-Id: I8c9c7f2cea4d1789e95e50833d5405239a47f02e
pull/1223/head
David Majnemer 3 years ago committed by Copybara-Service
parent e633c71109
commit 0c8bd82e90
  1. 8
      absl/debugging/symbolize_elf.inc

@ -489,6 +489,13 @@ static ABSL_ATTRIBUTE_NOINLINE bool GetSectionHeaderByType(
(buf_bytes > num_bytes_left) ? num_bytes_left : buf_bytes;
const off_t offset = sh_offset + i * sizeof(buf[0]);
const ssize_t len = ReadFromOffset(fd, buf, num_bytes_to_read, offset);
if (len < 0) {
ABSL_RAW_LOG(
WARNING,
"Reading %zd bytes from offset %ju returned %zd which is negative.",
num_bytes_to_read, static_cast<intmax_t>(offset), len);
return false;
}
if (len % sizeof(buf[0]) != 0) {
ABSL_RAW_LOG(
WARNING,
@ -693,6 +700,7 @@ static ABSL_ATTRIBUTE_NOINLINE FindSymbolResult FindSymbol(
const int entries_in_chunk = std::min(num_remaining_symbols, buf_entries);
const int bytes_in_chunk = entries_in_chunk * sizeof(buf[0]);
const ssize_t len = ReadFromOffset(fd, buf, bytes_in_chunk, offset);
SAFE_ASSERT(len >= 0);
SAFE_ASSERT(len % sizeof(buf[0]) == 0);
const ssize_t num_symbols_in_buf = len / sizeof(buf[0]);
SAFE_ASSERT(num_symbols_in_buf <= entries_in_chunk);

Loading…
Cancel
Save