Skip to content

Commit 35211d9

Browse files
derekmaurocopybara-github
authored andcommitted
absl::StrFormat: Avoid passing null to memcpy
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
1 parent 322c4d6 commit 35211d9

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

absl/strings/internal/str_format/output.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ struct ClearErrnoGuard {
3333

3434
void BufferRawSink::Write(string_view v) {
3535
size_t to_write = std::min(v.size(), size_);
36-
std::memcpy(buffer_, v.data(), to_write);
37-
buffer_ += to_write;
38-
size_ -= to_write;
36+
if (to_write > 0) {
37+
std::memcpy(buffer_, v.data(), to_write);
38+
buffer_ += to_write;
39+
size_ -= to_write;
40+
}
3941
total_written_ += v.size();
4042
}
4143

absl/strings/internal/str_format/parser.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,10 @@ class ParsedFormatBase {
132132
has_error_ = other.has_error_;
133133
items_ = other.items_;
134134
size_t text_size = items_.empty() ? 0 : items_.back().text_end;
135-
data_.reset(new char[text_size]);
136-
memcpy(data_.get(), other.data_.get(), text_size);
135+
data_ = std::make_unique<char[]>(text_size);
136+
if (text_size > 0) {
137+
memcpy(data_.get(), other.data_.get(), text_size);
138+
}
137139
return *this;
138140
}
139141

0 commit comments

Comments
 (0)