Skip to content

Commit c9257e8

Browse files
committed
strings: fix data corruption in Base64Escape when aliasing
1 parent 5ec376e commit c9257e8

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

absl/strings/escaping.cc

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -954,17 +954,14 @@ bool WebSafeBase64Unescape(absl::string_view src,
954954
return Base64UnescapeInternal(src.data(), src.size(), dest, kUnWebSafeBase64);
955955
}
956956

957+
957958
void Base64Escape(absl::string_view src, std::string* absl_nonnull dest) {
958-
strings_internal::Base64EscapeInternal(
959-
reinterpret_cast<const unsigned char*>(src.data()), src.size(), dest,
960-
true, strings_internal::kBase64Chars);
959+
*dest = Base64Escape(src);
961960
}
962961

963962
void WebSafeBase64Escape(absl::string_view src,
964963
std::string* absl_nonnull dest) {
965-
strings_internal::Base64EscapeInternal(
966-
reinterpret_cast<const unsigned char*>(src.data()), src.size(), dest,
967-
false, strings_internal::kWebSafeBase64Chars);
964+
*dest = WebSafeBase64Escape(src);
968965
}
969966

970967
std::string Base64Escape(absl::string_view src) {

absl/strings/escaping_test.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,4 +762,12 @@ TEST(HexAndBack, HexStringToBytes_and_BytesToHexString) {
762762
EXPECT_EQ(hex_only_lower, hex_result);
763763
}
764764

765+
TEST(Base64Escape, AliasingSafety){
766+
std::string s = "Abseil";
767+
absl::Base64Escape(s, &s);
768+
// would corrupt or crash before fix
769+
// after fix: s should be "QWJzZWls"
770+
EXPECT_EQ(s, "QWJzZWls");
771+
}
772+
765773
} // namespace

0 commit comments

Comments
 (0)