Skip to content

Commit be68967

Browse files
derekmaurocopybara-github
authored andcommitted
absl::Cord: Avoid passing null to memcpy and memset
Passing null to memcpy or memset 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: 733395230 Change-Id: I5477507999c4c67e53ab4cf40547b17b92d478b4
1 parent 0856410 commit be68967

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

absl/strings/cord.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,9 @@ ResultType GenericCompare(const Cord& lhs, const RHS& rhs,
977977

978978
size_t compared_size = std::min(lhs_chunk.size(), rhs_chunk.size());
979979
assert(size_to_compare >= compared_size);
980-
int memcmp_res = ::memcmp(lhs_chunk.data(), rhs_chunk.data(), compared_size);
980+
int memcmp_res = compared_size > 0 ? ::memcmp(lhs_chunk.data(),
981+
rhs_chunk.data(), compared_size)
982+
: 0;
981983
if (compared_size == size_to_compare || memcmp_res != 0) {
982984
return ComputeCompareResult<ResultType>(memcmp_res);
983985
}
@@ -1070,7 +1072,7 @@ void AppendCordToString(const Cord& src, absl::Nonnull<std::string*> dst) {
10701072
void Cord::CopyToArraySlowPath(absl::Nonnull<char*> dst) const {
10711073
assert(contents_.is_tree());
10721074
absl::string_view fragment;
1073-
if (GetFlatAux(contents_.tree(), &fragment)) {
1075+
if (GetFlatAux(contents_.tree(), &fragment) && !fragment.empty()) {
10741076
memcpy(dst, fragment.data(), fragment.size());
10751077
return;
10761078
}

0 commit comments

Comments
 (0)