From 92e93b1a287aa70f3c56835ea27687a6ce541c7f Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Tue, 24 Sep 2024 09:14:02 -0400 Subject: [PATCH] fix null-termination before passing to kj::String --- src/workerd/api/util.c++ | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/workerd/api/util.c++ b/src/workerd/api/util.c++ index 46527451f5f..275857aaed5 100644 --- a/src/workerd/api/util.c++ +++ b/src/workerd/api/util.c++ @@ -292,11 +292,14 @@ void maybeWarnIfNotText(jsg::Lock& js, kj::StringPtr str) { } kj::String fastEncodeBase64Url(kj::ArrayPtr 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(expected_length); + auto output = kj::heapArray(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)); }