Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions cpp/src/gandiva/gdv_function_stubs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <utf8proc.h>

#include <algorithm>
#include <boost/crc.hpp>
#include <cstdio>
#include <sstream>
Expand All @@ -28,6 +29,7 @@
#include "arrow/util/base64.h"
#include "arrow/util/bit_util.h"
#include "arrow/util/double_conversion_internal.h"
#include "arrow/util/int_util_overflow.h"
#include "arrow/util/value_parsing.h"

#include "gandiva/encrypt_utils.h"
Expand Down Expand Up @@ -663,8 +665,16 @@ const char* mask_utf8_utf8_utf8_utf8(int64_t context, const char* data, int32_t
return nullptr;
}

int32_t max_length =
std::max(upper_length, std::max(lower_length, num_length)) * data_len;
// Characters that are not replaced are copied as-is, so reserve at least one
// output byte per input byte even when all replacements are empty.
int32_t max_length;
if (ARROW_PREDICT_FALSE(arrow::internal::MultiplyWithOverflow(
std::max({upper_length, lower_length, num_length, 1}), data_len,
&max_length))) {
gdv_fn_context_set_error_msg(context, "Would overflow maximum output size");
*out_len = 0;
return nullptr;
}
char* out = reinterpret_cast<char*>(gdv_fn_context_arena_malloc(context, max_length));
if (out == nullptr) {
gdv_fn_context_set_error_msg(context, "Could not allocate memory for output string");
Expand Down
20 changes: 20 additions & 0 deletions cpp/src/gandiva/gdv_function_stubs_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,26 @@ TEST(TestGdvFnStubs, TestMask) {
result = mask_utf8_utf8_utf8_utf8(ctx_ptr, data.c_str(), data_len, "\?", 1, "*", 1, "#",
1, &out_len);
EXPECT_EQ(std::string(result, out_len), expected);

// With empty replacements the unmasked characters must still get their own
// output space, so a later allocation cannot overwrite them.
data = "QwErTy:4)";
expected = ":)";
data_len = static_cast<int32_t>(data.length());
result = mask_utf8_utf8_utf8_utf8(ctx_ptr, data.c_str(), data_len, "", 0, "", 0, "", 0,
&out_len);
int32_t other_len = 0;
mask_utf8_utf8_utf8_utf8(ctx_ptr, "#$", 2, "", 0, "", 0, "", 0, &other_len);
EXPECT_EQ(std::string(result, out_len), expected);

data = std::string(46341, 'A');
data_len = static_cast<int32_t>(data.length());
result = mask_utf8_utf8_utf8_utf8(ctx_ptr, data.c_str(), data_len, data.c_str(),
data_len, "x", 1, "n", 1, &out_len);
EXPECT_EQ(out_len, 0);
EXPECT_THAT(ctx.get_error(),
::testing::HasSubstr("Would overflow maximum output size"));
ctx.Reset();
}

TEST(TestGdvFnStubs, TestAesEncryptDecrypt16) {
Expand Down
Loading