kou commented on code in PR #49660: URL: https://github.com/apache/arrow/pull/49660#discussion_r3069292613
########## cpp/src/arrow/util/base64_test.cc: ########## @@ -0,0 +1,84 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "arrow/util/base64.h" +#include "arrow/testing/gtest_util.h" + +namespace arrow { +namespace util { + +TEST(Base64DecodeTest, ValidInputs) { + ASSERT_OK_AND_ASSIGN(auto empty, arrow::util::base64_decode("")); + EXPECT_EQ(empty, ""); + + ASSERT_OK_AND_ASSIGN(auto two_paddings, arrow::util::base64_decode("Zg==")); + EXPECT_EQ(two_paddings, "f"); + + ASSERT_OK_AND_ASSIGN(auto one_padding, arrow::util::base64_decode("Zm8=")); + EXPECT_EQ(one_padding, "fo"); + + ASSERT_OK_AND_ASSIGN(auto no_padding, arrow::util::base64_decode("Zm9v")); + EXPECT_EQ(no_padding, "foo"); + + ASSERT_OK_AND_ASSIGN(auto multiblock, arrow::util::base64_decode("SGVsbG8gd29ybGQ=")); + EXPECT_EQ(multiblock, "Hello world"); +} + +TEST(Base64DecodeTest, BinaryOutput) { + // 'A' maps to index 0 — same zero value used for padding slots + // verifies the 'A' bug is not present + ASSERT_OK_AND_ASSIGN(auto all_A, arrow::util::base64_decode("AAAA")); + EXPECT_EQ(all_A, std::string("\x00\x00\x00", 3)); + + // Arbitrary non-ASCII output bytes + ASSERT_OK_AND_ASSIGN(auto binary, arrow::util::base64_decode("AP8A")); + EXPECT_EQ(binary, std::string("\x00\xff\x00", 3)); +} + +TEST(Base64DecodeTest, InvalidLength) { + ASSERT_RAISES_WITH_MESSAGE( + Invalid, "Invalid: Invalid base64 input: length is not a multiple of 4", + arrow::util::base64_decode("abc")); +} + +TEST(Base64DecodeTest, InvalidCharacters) { + ASSERT_RAISES(Invalid, arrow::util::base64_decode("ab$=")); Review Comment: Could you use `ASSERT_RAISES_WITH_MESSAGE()`? ########## cpp/src/arrow/vendored/base64.cpp: ########## @@ -93,38 +89,67 @@ std::string base64_encode(std::string_view string_to_encode) { return base64_encode(bytes_to_encode, in_len); } -std::string base64_decode(std::string_view encoded_string) { +Result<std::string> base64_decode(std::string_view encoded_string) { size_t in_len = encoded_string.size(); int i = 0; - int j = 0; int in_ = 0; + int padding_count = 0; + int block_padding = 0; + bool padding_started = false; unsigned char char_array_4[4], char_array_3[3]; std::string ret; - while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) { - char_array_4[i++] = encoded_string[in_]; in_++; - if (i ==4) { - for (i = 0; i <4; i++) - char_array_4[i] = base64_chars.find(char_array_4[i]) & 0xff; + if (encoded_string.size() % 4 != 0) { + return Status::Invalid("Invalid base64 input: length is not a multiple of 4"); + } - char_array_3[0] = ( char_array_4[0] << 2 ) + ((char_array_4[1] & 0x30) >> 4); - char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); - char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; + while (in_len--) { + unsigned char c = encoded_string[in_]; - for (i = 0; (i < 3); i++) - ret += char_array_3[i]; - i = 0; + if (c == '=') { + padding_started = true; + padding_count++; + + if (padding_count > 2) { + return Status::Invalid("Invalid base64 input: too many padding characters"); + } + + char_array_4[i++] = 0; + } else { + if (padding_started) { + return Status::Invalid("Invalid base64 input: padding characters must be at the end"); + } + + if (base64_chars.find(c) == std::string::npos) { + return Status::Invalid( + "Invalid base64 input: contains non-base64 byte at position " + + std::to_string(in_)); + } Review Comment: It makes sense. ########## cpp/src/gandiva/gdv_function_stubs.cc: ########## @@ -269,7 +269,15 @@ const char* gdv_fn_base64_decode_utf8(int64_t context, const char* in, int32_t i return ""; } // use arrow method to decode base64 string - std::string decoded_str = arrow::util::base64_decode(std::string_view(in, in_len)); + auto result = arrow::util::base64_decode(std::string_view(in, in_len)); + if (!result.ok()) { + gdv_fn_context_set_error_msg(context, result.status().message().c_str()); + *out_len = 0; + return ""; + } + + std::string decoded_str = std::move(result).ValueOrDie(); Review Comment: We can use `operator*()`: ```suggestion std::string decoded_str = *result;; ``` ########## cpp/src/arrow/util/base64_test.cc: ########## @@ -0,0 +1,84 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "arrow/util/base64.h" +#include "arrow/testing/gtest_util.h" + +namespace arrow { +namespace util { + +TEST(Base64DecodeTest, ValidInputs) { + ASSERT_OK_AND_ASSIGN(auto empty, arrow::util::base64_decode("")); Review Comment: Can we remove `arrow::util::` because we are in the `arrow::util` namespace? ```suggestion ASSERT_OK_AND_ASSIGN(auto empty, base64_decode("")); ``` ########## cpp/src/parquet/arrow/fuzz_internal.cc: ########## @@ -83,8 +83,13 @@ class FuzzDecryptionKeyRetriever : public DecryptionKeyRetriever { } // Is it a key generated by MakeEncryptionKey? if (key_id.starts_with(kInlineKeyPrefix)) { - return SecureString( - ::arrow::util::base64_decode(key_id.substr(kInlineKeyPrefix.length()))); + auto result = + ::arrow::util::base64_decode(key_id.substr(kInlineKeyPrefix.length())); + if (!result.ok()) { + throw ParquetException(result.status().message()); + } Review Comment: Could you use `PARQUET_ASSIGN_OR_THROW()`? ```suggestion PARQUET_ASSIGN_OR_THROW(auto decoded_key, ::arrow::util::base64_decode(key_id.substr(kInlineKeyPrefix.length()))); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
