fix: -Wunsafe-buffer-usage warnings in asar file IO (#43649)

* fix: -Wunsafe-buffer-usage warnings in ScopedTemporaryFile::InitFromFile()

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* fix: -Wunsafe-buffer-usage warnings in Archive::Init()

Co-authored-by: Charles Kerr <charles@charleskerr.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
This commit is contained in:
trop[bot] 2024-09-09 17:58:41 -05:00 committed by GitHub
parent e4113ddd69
commit f6bc0b0953
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 25 deletions

View file

@ -201,22 +201,19 @@ bool Archive::Init() {
return false; return false;
} }
std::vector<char> buf; std::vector<uint8_t> buf;
int len;
buf.resize(8); buf.resize(8);
{ {
electron::ScopedAllowBlockingForElectron allow_blocking; electron::ScopedAllowBlockingForElectron allow_blocking;
len = file_.ReadAtCurrentPos(buf.data(), buf.size()); if (!file_.ReadAtCurrentPosAndCheck(buf)) {
}
if (len != static_cast<int>(buf.size())) {
PLOG(ERROR) << "Failed to read header size from " << path_.value(); PLOG(ERROR) << "Failed to read header size from " << path_.value();
return false; return false;
} }
}
uint32_t size; uint32_t size;
if (!base::PickleIterator(base::Pickle::WithData(base::as_byte_span(buf))) if (!base::PickleIterator(base::Pickle::WithData(buf)).ReadUInt32(&size)) {
.ReadUInt32(&size)) {
LOG(ERROR) << "Failed to parse header size from " << path_.value(); LOG(ERROR) << "Failed to parse header size from " << path_.value();
return false; return false;
} }
@ -224,16 +221,14 @@ bool Archive::Init() {
buf.resize(size); buf.resize(size);
{ {
electron::ScopedAllowBlockingForElectron allow_blocking; electron::ScopedAllowBlockingForElectron allow_blocking;
len = file_.ReadAtCurrentPos(buf.data(), buf.size()); if (!file_.ReadAtCurrentPosAndCheck(buf)) {
}
if (len != static_cast<int>(buf.size())) {
PLOG(ERROR) << "Failed to read header from " << path_.value(); PLOG(ERROR) << "Failed to read header from " << path_.value();
return false; return false;
} }
}
std::string header; std::string header;
if (!base::PickleIterator(base::Pickle::WithData(base::as_byte_span(buf))) if (!base::PickleIterator(base::Pickle::WithData(buf)).ReadString(&header)) {
.ReadString(&header)) {
LOG(ERROR) << "Failed to parse header from " << path_.value(); LOG(ERROR) << "Failed to parse header from " << path_.value();
return false; return false;
} }

View file

@ -63,20 +63,15 @@ bool ScopedTemporaryFile::InitFromFile(
return false; return false;
electron::ScopedAllowBlockingForElectron allow_blocking; electron::ScopedAllowBlockingForElectron allow_blocking;
std::vector<char> buf(size); std::vector<uint8_t> buf(size);
int len = src->Read(offset, buf.data(), buf.size()); if (!src->ReadAndCheck(offset, buf))
if (len != static_cast<int>(size))
return false; return false;
if (integrity) if (integrity)
ValidateIntegrityOrDie(base::as_byte_span(buf), *integrity); ValidateIntegrityOrDie(buf, *integrity);
base::File dest(path_, base::File::FLAG_OPEN | base::File::FLAG_WRITE); base::File dest(path_, base::File::FLAG_OPEN | base::File::FLAG_WRITE);
if (!dest.IsValid()) return dest.IsValid() && dest.WriteAtCurrentPosAndCheck(buf);
return false;
return dest.WriteAtCurrentPos(buf.data(), buf.size()) ==
static_cast<int>(size);
} }
} // namespace asar } // namespace asar