From 27c30ec671cb7b5ba84c4e79feff7fd0b0ac6338 Mon Sep 17 00:00:00 2001 From: Roman Gershman Date: Thu, 23 May 2019 06:03:35 +0300 Subject: Avoid undefined behavior when nullptr is passed to memcpy with size 0 --- absl/strings/str_cat.cc | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'absl/strings/str_cat.cc') diff --git a/absl/strings/str_cat.cc b/absl/strings/str_cat.cc index 2667976..ffe99db 100644 --- a/absl/strings/str_cat.cc +++ b/absl/strings/str_cat.cc @@ -89,7 +89,9 @@ static char* Append(char* out, const AlphaNum& x) { // memcpy is allowed to overwrite arbitrary memory, so doing this after the // call would force an extra fetch of x.size(). char* after = out + x.size(); - memcpy(out, x.data(), x.size()); + if (x.size() != 0) { + memcpy(out, x.data(), x.size()); + } return after; } @@ -146,8 +148,10 @@ std::string CatPieces(std::initializer_list pieces) { char* out = begin; for (const absl::string_view piece : pieces) { const size_t this_size = piece.size(); - memcpy(out, piece.data(), this_size); - out += this_size; + if (this_size != 0) { + memcpy(out, piece.data(), this_size); + out += this_size; + } } assert(out == begin + result.size()); return result; @@ -176,8 +180,10 @@ void AppendPieces(std::string* dest, char* out = begin + old_size; for (const absl::string_view piece : pieces) { const size_t this_size = piece.size(); - memcpy(out, piece.data(), this_size); - out += this_size; + if (this_size != 0) { + memcpy(out, piece.data(), this_size); + out += this_size; + } } assert(out == begin + dest->size()); } -- cgit v1.2.3