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() { ...@@ -270,7 +270,8 @@ bool DisassemblerElf<Traits>::ParseHeader() {
segment != segments_ + segments_count_; ++segment) { segment != segments_ + segments_count_; ++segment) {
if (!RangeIsBounded(segment->p_offset, segment->p_filesz, kOffsetBound)) if (!RangeIsBounded(segment->p_offset, segment->p_filesz, kOffsetBound))
return false; 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); offset_bound = std::max(offset_bound, segment_end);
} }
...@@ -366,8 +367,10 @@ void DisassemblerElfIntel<Traits>::ParseExecSection( ...@@ -366,8 +367,10 @@ void DisassemblerElfIntel<Traits>::ParseExecSection(
auto& abs32_locations_ = this->abs32_locations_; auto& abs32_locations_ = this->abs32_locations_;
std::ptrdiff_t from_offset_to_rva = section.sh_addr - section.sh_offset; 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_); AddressTranslator::RvaToOffsetCache target_rva_checker(this->translator_);
...@@ -380,7 +383,8 @@ void DisassemblerElfIntel<Traits>::ParseExecSection( ...@@ -380,7 +383,8 @@ void DisassemblerElfIntel<Traits>::ParseExecSection(
finder->Reset(gap.value()); finder->Reset(gap.value());
for (auto rel32 = finder->GetNext(); rel32.has_value(); for (auto rel32 = finder->GetNext(); rel32.has_value();
rel32 = finder->GetNext()) { 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 rel32_rva = rva_t(rel32_offset + from_offset_to_rva);
rva_t target_rva = rel32_rva + 4 + image_.read<uint32_t>(rel32_offset); rva_t target_rva = rel32_rva + 4 + image_.read<uint32_t>(rel32_offset);
if (target_rva_checker.IsValid(target_rva) && if (target_rva_checker.IsValid(target_rva) &&
......
...@@ -7,11 +7,13 @@ ...@@ -7,11 +7,13 @@
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
#include <iterator> #include <iterator>
#include <limits>
#include <numeric> #include <numeric>
#include "base/logging.h" #include "base/logging.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/numerics/checked_math.h" #include "base/numerics/checked_math.h"
#include "base/numerics/safe_conversions.h"
#include "components/zucchini/algorithm.h" #include "components/zucchini/algorithm.h"
#include "components/zucchini/buffer_source.h" #include "components/zucchini/buffer_source.h"
#include "components/zucchini/buffer_view.h" #include "components/zucchini/buffer_view.h"
...@@ -384,6 +386,8 @@ bool ReadZtfHeader(ConstBufferView image) { ...@@ -384,6 +386,8 @@ bool ReadZtfHeader(ConstBufferView image) {
// Reject empty images and "ZTxtxTZ\n" (missing 't'). // Reject empty images and "ZTxtxTZ\n" (missing 't').
if (source.size() < kTotalMagicSize) if (source.size() < kTotalMagicSize)
return false; return false;
if (source.size() > std::numeric_limits<offset_t>::max())
return false;
return source.CheckNextBytes({'Z', 'T', 'x', 't'}); return source.CheckNextBytes({'Z', 'T', 'x', 't'});
} }
...@@ -405,7 +409,7 @@ bool ZtfTranslator::Init(ConstBufferView image) { ...@@ -405,7 +409,7 @@ bool ZtfTranslator::Init(ConstBufferView image) {
// sentinel. // sentinel.
if (line_starts_.size() >= ztf::kMaxDimValue) if (line_starts_.size() >= ztf::kMaxDimValue)
return false; 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. // Check that the line length is reachable from an absolute reference.
if (line_starts_.back() - *std::next(line_starts_.rbegin()) >= if (line_starts_.back() - *std::next(line_starts_.rbegin()) >=
ztf::kMaxDimValue) { ztf::kMaxDimValue) {
......
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
#include <algorithm> #include <algorithm>
#include "base/numerics/safe_conversions.h"
namespace zucchini { namespace zucchini {
/******** Abs32GapFinder ********/ /******** Abs32GapFinder ********/
...@@ -22,7 +24,8 @@ Abs32GapFinder::Abs32GapFinder(ConstBufferView image, ...@@ -22,7 +24,8 @@ Abs32GapFinder::Abs32GapFinder(ConstBufferView image,
DCHECK_GE(region.begin(), image.begin()); DCHECK_GE(region.begin(), image.begin());
DCHECK_LE(region.end(), image.end()); 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|. // Find the first |abs32_current_| with |*abs32_current_ >= begin_offset|.
abs32_current_ = std::lower_bound(abs32_locations.begin(), abs32_current_ = std::lower_bound(abs32_locations.begin(),
abs32_locations.end(), begin_offset); abs32_locations.end(), begin_offset);
......
...@@ -40,7 +40,8 @@ bool RelocRvaReaderWin32::FindRelocBlocks( ...@@ -40,7 +40,8 @@ bool RelocRvaReaderWin32::FindRelocBlocks(
ConstBufferView reloc_data = image[reloc_region]; ConstBufferView reloc_data = image[reloc_region];
reloc_block_offsets->clear(); reloc_block_offsets->clear();
while (reloc_data.size() >= sizeof(pe::RelocHeader)) { 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; auto size = reloc_data.read<pe::RelocHeader>(0).size;
// |size| must be aligned to 4-bytes. // |size| must be aligned to 4-bytes.
if (size < sizeof(pe::RelocHeader) || size % 4 || size > reloc_data.size()) if (size < sizeof(pe::RelocHeader) || size % 4 || size > reloc_data.size())
...@@ -78,7 +79,8 @@ RelocRvaReaderWin32::RelocRvaReaderWin32( ...@@ -78,7 +79,8 @@ RelocRvaReaderWin32::RelocRvaReaderWin32(
return; // Nothing left. return; // Nothing left.
// Skip |cur_reloc_units_| to |lo|, truncating up. // 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) { if (lo > cur_reloc_units_offset) {
offset_t delta = offset_t delta =
AlignCeil<offset_t>(lo - cur_reloc_units_offset, kRelocUnitSize); AlignCeil<offset_t>(lo - cur_reloc_units_offset, kRelocUnitSize);
...@@ -100,7 +102,8 @@ base::Optional<RelocUnitWin32> RelocRvaReaderWin32::GetNext() { ...@@ -100,7 +102,8 @@ base::Optional<RelocUnitWin32> RelocRvaReaderWin32::GetNext() {
if (end_it_ - cur_reloc_units_.begin() < kRelocUnitSize) if (end_it_ - cur_reloc_units_.begin() < kRelocUnitSize)
return base::nullopt; return base::nullopt;
// "Inner loop" to extract single reloc unit. // "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); uint16_t entry = cur_reloc_units_.read<uint16_t>(0);
uint8_t type = static_cast<uint8_t>(entry >> 12); uint8_t type = static_cast<uint8_t>(entry >> 12);
rva_t rva = rva_hi_bits_ + (entry & 0xFFF); 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