Commit f6c70a2c authored by Etienne Pierre-doray's avatar Etienne Pierre-doray Committed by Commit Bot

[Zucchini]: Fix implicit conversions.

Fix compile error with -Wshorten-64-to-32.

Bug: 881008
Change-Id: I52a1bab9cb7b4cb67ea4c275143390d42b192120
Reviewed-on: https://chromium-review.googlesource.com/1216524Reviewed-by: default avatarWill Harris <wfh@chromium.org>
Reviewed-by: default avatarSamuel Huang <huangs@chromium.org>
Commit-Queue: Etienne Pierre-Doray <etiennep@chromium.org>
Cr-Commit-Position: refs/heads/master@{#590285}
parent af1d1279
......@@ -270,7 +270,8 @@ bool DisassemblerElf<Traits>::ParseHeader() {
segment != segments_ + segments_count_; ++segment) {
if (!RangeIsBounded(segment->p_offset, segment->p_filesz, kOffsetBound))
return false;
offset_t segment_end = segment->p_offset + segment->p_filesz;
offset_t segment_end =
base::checked_cast<offset_t>(segment->p_offset + segment->p_filesz);
offset_bound = std::max(offset_bound, segment_end);
}
......@@ -366,8 +367,10 @@ void DisassemblerElfIntel<Traits>::ParseExecSection(
auto& abs32_locations_ = this->abs32_locations_;
std::ptrdiff_t from_offset_to_rva = section.sh_addr - section.sh_offset;
rva_t start_rva = section.sh_addr;
rva_t end_rva = start_rva + section.sh_size;
// Range of values was ensured in ParseHeader().
rva_t start_rva = base::checked_cast<rva_t>(section.sh_addr);
rva_t end_rva = base::checked_cast<rva_t>(start_rva + section.sh_size);
AddressTranslator::RvaToOffsetCache target_rva_checker(this->translator_);
......@@ -380,7 +383,8 @@ void DisassemblerElfIntel<Traits>::ParseExecSection(
finder->Reset(gap.value());
for (auto rel32 = finder->GetNext(); rel32.has_value();
rel32 = finder->GetNext()) {
offset_t rel32_offset = offset_t(rel32->location - image_.begin());
offset_t rel32_offset =
base::checked_cast<offset_t>(rel32->location - image_.begin());
rva_t rel32_rva = rva_t(rel32_offset + from_offset_to_rva);
rva_t target_rva = rel32_rva + 4 + image_.read<uint32_t>(rel32_offset);
if (target_rva_checker.IsValid(target_rva) &&
......
......@@ -7,11 +7,13 @@
#include <algorithm>
#include <cmath>
#include <iterator>
#include <limits>
#include <numeric>
#include "base/logging.h"
#include "base/macros.h"
#include "base/numerics/checked_math.h"
#include "base/numerics/safe_conversions.h"
#include "components/zucchini/algorithm.h"
#include "components/zucchini/buffer_source.h"
#include "components/zucchini/buffer_view.h"
......@@ -384,6 +386,8 @@ bool ReadZtfHeader(ConstBufferView image) {
// Reject empty images and "ZTxtxTZ\n" (missing 't').
if (source.size() < kTotalMagicSize)
return false;
if (source.size() > std::numeric_limits<offset_t>::max())
return false;
return source.CheckNextBytes({'Z', 'T', 'x', 't'});
}
......@@ -405,7 +409,7 @@ bool ZtfTranslator::Init(ConstBufferView image) {
// sentinel.
if (line_starts_.size() >= ztf::kMaxDimValue)
return false;
line_starts_.push_back(i + 1);
line_starts_.push_back(base::checked_cast<offset_t>(i + 1));
// Check that the line length is reachable from an absolute reference.
if (line_starts_.back() - *std::next(line_starts_.rbegin()) >=
ztf::kMaxDimValue) {
......
......@@ -6,6 +6,8 @@
#include <algorithm>
#include "base/numerics/safe_conversions.h"
namespace zucchini {
/******** Abs32GapFinder ********/
......@@ -22,7 +24,8 @@ Abs32GapFinder::Abs32GapFinder(ConstBufferView image,
DCHECK_GE(region.begin(), image.begin());
DCHECK_LE(region.end(), image.end());
const offset_t begin_offset = region.begin() - image.begin();
const offset_t begin_offset =
base::checked_cast<offset_t>(region.begin() - image.begin());
// Find the first |abs32_current_| with |*abs32_current_ >= begin_offset|.
abs32_current_ = std::lower_bound(abs32_locations.begin(),
abs32_locations.end(), begin_offset);
......
......@@ -40,7 +40,8 @@ bool RelocRvaReaderWin32::FindRelocBlocks(
ConstBufferView reloc_data = image[reloc_region];
reloc_block_offsets->clear();
while (reloc_data.size() >= sizeof(pe::RelocHeader)) {
reloc_block_offsets->push_back(reloc_data.begin() - image.begin());
reloc_block_offsets->push_back(
base::checked_cast<offset_t>(reloc_data.begin() - image.begin()));
auto size = reloc_data.read<pe::RelocHeader>(0).size;
// |size| must be aligned to 4-bytes.
if (size < sizeof(pe::RelocHeader) || size % 4 || size > reloc_data.size())
......@@ -78,7 +79,8 @@ RelocRvaReaderWin32::RelocRvaReaderWin32(
return; // Nothing left.
// Skip |cur_reloc_units_| to |lo|, truncating up.
offset_t cur_reloc_units_offset = cur_reloc_units_.begin() - image_.begin();
offset_t cur_reloc_units_offset =
base::checked_cast<offset_t>(cur_reloc_units_.begin() - image_.begin());
if (lo > cur_reloc_units_offset) {
offset_t delta =
AlignCeil<offset_t>(lo - cur_reloc_units_offset, kRelocUnitSize);
......@@ -100,7 +102,8 @@ base::Optional<RelocUnitWin32> RelocRvaReaderWin32::GetNext() {
if (end_it_ - cur_reloc_units_.begin() < kRelocUnitSize)
return base::nullopt;
// "Inner loop" to extract single reloc unit.
offset_t location = cur_reloc_units_.begin() - image_.begin();
offset_t location =
base::checked_cast<offset_t>(cur_reloc_units_.begin() - image_.begin());
uint16_t entry = cur_reloc_units_.read<uint16_t>(0);
uint8_t type = static_cast<uint8_t>(entry >> 12);
rva_t rva = rva_hi_bits_ + (entry & 0xFFF);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment