Skip to content

Commit

Permalink
fix null-termination before passing to kj::String
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Sep 24, 2024
1 parent e80f4ca commit 92e93b1
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/workerd/api/util.c++
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,14 @@ void maybeWarnIfNotText(jsg::Lock& js, kj::StringPtr str) {
}

kj::String fastEncodeBase64Url(kj::ArrayPtr<const byte> bytes) {
if (KJ_UNLIKELY(bytes.size() == 0)) {
return {};
}
auto expected_length = simdutf::base64_length_from_binary(bytes.size(), simdutf::base64_url);
auto output = kj::heapArray<char>(expected_length);
auto output = kj::heapArray<char>(expected_length + 1);
auto actual_length = simdutf::binary_to_base64(
bytes.asChars().begin(), bytes.size(), output.asChars().begin(), simdutf::base64_url);
KJ_ASSERT(expected_length == actual_length);
output[actual_length] = '\0';
return kj::String(kj::mv(output));
}

Expand Down

0 comments on commit 92e93b1

Please sign in to comment.