Skip to content

Commit

Permalink
absl::StrFormat: Avoid passing null to memcpy
Browse files Browse the repository at this point in the history
Passing null to memcpy is undefined behavior.

Our UBSAN tests were missing `-fno-sanitize-recover`, which
means UBSAN logs a warning, but the program continues,
causing the test not to fail.

`-fno-sanitize-recover` will be added once all errors are fixed.

PiperOrigin-RevId: 733466239
Change-Id: I9c2a2348638b78465b6d0880c93c11ba89a93efd
  • Loading branch information
derekmauro authored and copybara-github committed Mar 4, 2025
1 parent 322c4d6 commit 35211d9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 5 additions & 3 deletions absl/strings/internal/str_format/output.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ struct ClearErrnoGuard {

void BufferRawSink::Write(string_view v) {
size_t to_write = std::min(v.size(), size_);
std::memcpy(buffer_, v.data(), to_write);
buffer_ += to_write;
size_ -= to_write;
if (to_write > 0) {
std::memcpy(buffer_, v.data(), to_write);
buffer_ += to_write;
size_ -= to_write;
}
total_written_ += v.size();
}

Expand Down
6 changes: 4 additions & 2 deletions absl/strings/internal/str_format/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,10 @@ class ParsedFormatBase {
has_error_ = other.has_error_;
items_ = other.items_;
size_t text_size = items_.empty() ? 0 : items_.back().text_end;
data_.reset(new char[text_size]);
memcpy(data_.get(), other.data_.get(), text_size);
data_ = std::make_unique<char[]>(text_size);
if (text_size > 0) {
memcpy(data_.get(), other.data_.get(), text_size);
}
return *this;
}

Expand Down

0 comments on commit 35211d9

Please sign in to comment.