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
2 changes: 1 addition & 1 deletion cpp/src/gandiva/precompiled/string_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ gdv_int32 utf8_length_ignore_invalid(const char* data, gdv_int32 data_len) {
}
for (int j = 1; j < char_len; ++j) {
if ((data[i + j] & 0xC0) != 0x80) { // bytes following head-byte of glyph
char_len += 1;
break;
}
}
++count;
Expand Down
26 changes: 26 additions & 0 deletions cpp/src/gandiva/precompiled/string_ops_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <gtest/gtest.h>

#include <limits>
#include <vector>

#include "gandiva/execution_context.h"
#include "gandiva/precompiled/types.h"
Expand Down Expand Up @@ -1608,6 +1609,31 @@ TEST(TestStringOps, TestRpadString) {
EXPECT_EQ(std::string(out_str + 5000, 2), "α");
}

TEST(TestStringOps, TestPadMalformedUtf8NoOverread) {
gandiva::ExecutionContext ctx;
uint64_t ctx_ptr = reinterpret_cast<gdv_int64>(&ctx);
gdv_int32 out_len = 0;

// A 4-byte utf8 lead byte followed by non-continuation bytes and no trailing
// space. utf8_length_ignore_invalid() used to extend the glyph length past
// the end of the buffer while scanning the continuation bytes. The input is
// held in an exactly-sized heap buffer so any over-read trips AddressSanitizer.
std::vector<char> text = {'\xF0', 'a', 'a', 'a'};
const auto text_len = static_cast<gdv_int32>(text.size());
const std::string text_str(text.begin(), text.end());

// The malformed sequence is counted as one glyph, so padding to width 6 adds
// five fill characters.
const char* out_str =
lpad_utf8_int32_utf8(ctx_ptr, text.data(), text_len, 6, " ", 1, &out_len);
EXPECT_EQ(out_len, 9);
EXPECT_EQ(std::string(out_str, out_len), " " + text_str);

out_str = rpad_utf8_int32_utf8(ctx_ptr, text.data(), text_len, 6, " ", 1, &out_len);
EXPECT_EQ(out_len, 9);
EXPECT_EQ(std::string(out_str, out_len), text_str + " ");
}

TEST(TestStringOps, TestRtrim) {
gandiva::ExecutionContext ctx;
uint64_t ctx_ptr = reinterpret_cast<gdv_int64>(&ctx);
Expand Down
Loading