From 020282e885f5ce1339cbe1e5ea72bf55a49ddf27 Mon Sep 17 00:00:00 2001 From: Cui Kaifeng <48012748+azurenake@users.noreply.github.com> Date: Tue, 14 Sep 2021 11:19:55 +0800 Subject: [PATCH] [Bug] Fix aes_decrypt to handle null input correctly. (#6636) --- be/src/exprs/encryption_functions.cpp | 4 ++-- be/test/exprs/encryption_functions_test.cpp | 24 ++++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/be/src/exprs/encryption_functions.cpp b/be/src/exprs/encryption_functions.cpp index 29c14d7d04..6aa3140c7f 100644 --- a/be/src/exprs/encryption_functions.cpp +++ b/be/src/exprs/encryption_functions.cpp @@ -33,7 +33,7 @@ void EncryptionFunctions::init() {} StringVal EncryptionFunctions::aes_encrypt(FunctionContext* ctx, const StringVal& src, const StringVal& key) { - if (src.len == 0) { + if (src.len == 0 || src.is_null) { return StringVal::null(); } @@ -53,7 +53,7 @@ StringVal EncryptionFunctions::aes_encrypt(FunctionContext* ctx, const StringVal StringVal EncryptionFunctions::aes_decrypt(FunctionContext* ctx, const StringVal& src, const StringVal& key) { - if (src.len == 0) { + if (src.len == 0 || src.is_null) { return StringVal::null(); } diff --git a/be/test/exprs/encryption_functions_test.cpp b/be/test/exprs/encryption_functions_test.cpp index c73b9e74f3..2c13e05c88 100644 --- a/be/test/exprs/encryption_functions_test.cpp +++ b/be/test/exprs/encryption_functions_test.cpp @@ -76,6 +76,28 @@ TEST_F(EncryptionFunctionsTest, to_base64) { } } +TEST_F(EncryptionFunctionsTest, aes_decrypt) { + std::unique_ptr context(new doris_udf::FunctionContext()); + { + StringVal encryptWord = EncryptionFunctions::aes_encrypt(context.get(), doris_udf::StringVal("hello"), + doris_udf::StringVal("key")); + StringVal result = EncryptionFunctions::aes_decrypt(context.get(), encryptWord, + doris_udf::StringVal("key")); + StringVal expected = doris_udf::StringVal("hello"); + ASSERT_EQ(expected, result); + } + { + StringVal encryptWord = EncryptionFunctions::aes_encrypt(context.get(), doris_udf::StringVal("hello"), + doris_udf::StringVal("key")); + encryptWord.is_null = true; + StringVal result = EncryptionFunctions::aes_decrypt(context.get(), encryptWord, + doris_udf::StringVal("key")); + StringVal expected = doris_udf::StringVal::null(); + ASSERT_EQ(expected, result); + } + +} + } // namespace doris int main(int argc, char** argv) { @@ -86,4 +108,4 @@ int main(int argc, char** argv) { } ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); -} \ No newline at end of file +}