1818#include < gmock/gmock.h>
1919#include < gtest/gtest.h>
2020
21+ #include < cstring>
2122#include < limits>
23+ #include < memory>
2224#include < vector>
2325
2426#include " gandiva/execution_context.h"
@@ -1618,18 +1620,20 @@ TEST(TestStringOps, TestPadMalformedUtf8NoOverread) {
16181620 // space. utf8_length_ignore_invalid() used to extend the glyph length past
16191621 // the end of the buffer while scanning the continuation bytes. The input is
16201622 // held in an exactly-sized heap buffer so any over-read trips AddressSanitizer.
1621- std::vector<char > text = {' \xF0 ' , ' a' , ' a' , ' a' };
1622- const auto text_len = static_cast <gdv_int32>(text.size ());
1623- const std::string text_str (text.begin (), text.end ());
1623+ const char bytes[] = {' \xF0 ' , ' a' , ' a' , ' a' };
1624+ const auto text_len = static_cast <gdv_int32>(sizeof (bytes));
1625+ std::unique_ptr<char []> text (new char [text_len]);
1626+ std::memcpy (text.get (), bytes, text_len);
1627+ const std::string text_str (text.get (), text_len);
16241628
16251629 // The lone lead byte counts as one invalid glyph and the three 'a's as one
16261630 // each, so the length is 4 and padding to width 6 adds two fill characters.
16271631 const char * out_str =
1628- lpad_utf8_int32_utf8 (ctx_ptr, text.data (), text_len, 6 , " " , 1 , &out_len);
1632+ lpad_utf8_int32_utf8 (ctx_ptr, text.get (), text_len, 6 , " " , 1 , &out_len);
16291633 EXPECT_EQ (out_len, 6 );
16301634 EXPECT_EQ (std::string (out_str, out_len), " " + text_str);
16311635
1632- out_str = rpad_utf8_int32_utf8 (ctx_ptr, text.data (), text_len, 6 , " " , 1 , &out_len);
1636+ out_str = rpad_utf8_int32_utf8 (ctx_ptr, text.get (), text_len, 6 , " " , 1 , &out_len);
16331637 EXPECT_EQ (out_len, 6 );
16341638 EXPECT_EQ (std::string (out_str, out_len), text_str + " " );
16351639}
@@ -1643,17 +1647,20 @@ TEST(TestStringOps, TestPadMalformedUtf8KeepsValidGlyph) {
16431647 // 0xF0 alone counts as one invalid glyph, then 'a' and € follow on their own,
16441648 // so the count is 3. If the inner scan kept char_len at 4 it would advance the
16451649 // outer loop past 'a', 0xE2, 0x82 and only see the orphaned 0xAC, giving 2.
1646- std::vector<char > text = {' \xF0 ' , ' a' , ' \xE2 ' , ' \x82 ' , ' \xAC ' };
1647- const auto text_len = static_cast <gdv_int32>(text.size ());
1648- const std::string text_str (text.begin (), text.end ());
1650+ // The input sits in an exactly-sized heap buffer so any over-read trips ASAN.
1651+ const char bytes[] = {' \xF0 ' , ' a' , ' \xE2 ' , ' \x82 ' , ' \xAC ' };
1652+ const auto text_len = static_cast <gdv_int32>(sizeof (bytes));
1653+ std::unique_ptr<char []> text (new char [text_len]);
1654+ std::memcpy (text.get (), bytes, text_len);
1655+ const std::string text_str (text.get (), text_len);
16491656
16501657 // 3 glyphs padded to width 5 adds two fill characters, out_len = 2 + 5 = 7.
16511658 const char * out_str =
1652- lpad_utf8_int32_utf8 (ctx_ptr, text.data (), text_len, 5 , " " , 1 , &out_len);
1659+ lpad_utf8_int32_utf8 (ctx_ptr, text.get (), text_len, 5 , " " , 1 , &out_len);
16531660 EXPECT_EQ (out_len, 7 );
16541661 EXPECT_EQ (std::string (out_str, out_len), " " + text_str);
16551662
1656- out_str = rpad_utf8_int32_utf8 (ctx_ptr, text.data (), text_len, 5 , " " , 1 , &out_len);
1663+ out_str = rpad_utf8_int32_utf8 (ctx_ptr, text.get (), text_len, 5 , " " , 1 , &out_len);
16571664 EXPECT_EQ (out_len, 7 );
16581665 EXPECT_EQ (std::string (out_str, out_len), text_str + " " );
16591666}
0 commit comments