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 @@ -206,7 +206,7 @@ gdv_int32 utf8_length_ignore_invalid(const char* data, gdv_int32 data_len) {
// if invalid byte or incomplete glyph, ignore it
char_len = 1;
}
for (int j = 1; j < char_len; ++j) {
for (int j = 1; j < char_len && i + j < data_len; ++j) {
if ((data[i + j] & 0xC0) != 0x80) { // bytes following head-byte of glyph
char_len += 1;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, should we break here instead?

Suggested change
char_len += 1;
break;

}
Expand Down
24 changes: 24 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,29 @@ 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 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_len, text_len),
std::string(text.begin(), text.end()));
Comment on lines +1627 to +1628

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you check out_str data entirely instead of checking only part of out_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, text_len), std::string(text.begin(), text.end()));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto.

}

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