Fix StringFileWriter::Seek().

The final range check needs to check that the value fits in a size_t,
because that’s what StringFileWriter uses for its offset_.

TEST=util_test StringFileWriter.SeekInvalid
R=rsesek@chromium.org

Review URL: https://codereview.chromium.org/434103003
This commit is contained in:
Mark Mentovai 2014-08-01 13:30:27 -04:00
parent 995012534e
commit 6e720f1421

View File

@ -110,26 +110,26 @@ off_t StringFileWriter::Seek(off_t offset, int whence) {
return -1; return -1;
} }
off_t offset_offt; off_t base_offset_offt;
if (!AssignIfInRange(&offset_offt, base_offset)) { if (!AssignIfInRange(&base_offset_offt, base_offset)) {
LOG(ERROR) << "Seek(): base_offset " << base_offset << " invalid for off_t"; LOG(ERROR) << "Seek(): base_offset " << base_offset << " invalid for off_t";
return -1; return -1;
} }
base::CheckedNumeric<off_t> new_offset(base_offset_offt);
base::CheckedNumeric<off_t> new_offset(offset_offt);
new_offset += offset; new_offset += offset;
if (!new_offset.IsValid()) { if (!new_offset.IsValid()) {
LOG(ERROR) << "Seek(): new_offset invalid"; LOG(ERROR) << "Seek(): new_offset invalid";
return -1; return -1;
} }
off_t new_offset_offt = new_offset.ValueOrDie();
if (!AssignIfInRange(&offset_offt, new_offset.ValueOrDie())) { size_t new_offset_sizet;
LOG(ERROR) << "Seek(): new_offset " << new_offset.ValueOrDie() if (!AssignIfInRange(&new_offset_sizet, new_offset_offt)) {
LOG(ERROR) << "Seek(): new_offset " << new_offset_offt
<< " invalid for size_t"; << " invalid for size_t";
return -1; return -1;
} }
offset_ = offset_offt; offset_ = new_offset_sizet;
return offset_.ValueOrDie(); return offset_.ValueOrDie();
} }