Debian Codegraph @

src/MaxMatchSegmentationTest.cpp

raw symbols SCIP
1 /*
2 * Open Chinese Convert
3 *
4 * Copyright 2015-2026 Carbo Kuo and contributors
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #include "MaxMatchSegmentation.hpp"
20 #include "DictGroupTestBase.hpp"
21
22 namespace opencc {
23
24 class MaxMatchSegmentationTest : public DictGroupTestBase {
25 protected:
26 MaxMatchSegmentationTest() {}
27
28 virtual void SetUp() {
29 dict = CreateDictGroupForConversion();
30 segmenter = SegmentationPtr(new MaxMatchSegmentation(dict));
31 }
32
33 DictPtr dict;
34 SegmentationPtr segmenter;
35 };
36
37 TEST_F(MaxMatchSegmentationTest, Segment) {
38 const auto& segments = segmenter->Segment(utf8("太后的头发干燥"));
39 EXPECT_EQ(4, segments->Length());
40 EXPECT_EQ(utf8("太后"), std::string(segments->At(0)));
41 EXPECT_EQ(utf8("的"), std::string(segments->At(1)));
42 EXPECT_EQ(utf8("头发"), std::string(segments->At(2)));
43 EXPECT_EQ(utf8("干燥"), std::string(segments->At(3)));
44 }
45
46 TEST_F(MaxMatchSegmentationTest, EmptyString) {
47 const auto& segments = segmenter->Segment("");
48 EXPECT_EQ(0, segments->Length());
49 }
50
51 TEST_F(MaxMatchSegmentationTest, StringViewSlice) {
52 const std::string text = utf8("x太后的头发干燥y");
53 const std::string_view slice(text.data() + 1, text.size() - 2);
54 const auto& segments = segmenter->Segment(slice);
55 EXPECT_EQ(4, segments->Length());
56 EXPECT_EQ(utf8("太后"), std::string(segments->At(0)));
57 EXPECT_EQ(utf8("的"), std::string(segments->At(1)));
58 EXPECT_EQ(utf8("头发"), std::string(segments->At(2)));
59 EXPECT_EQ(utf8("干燥"), std::string(segments->At(3)));
60 }
61
62 TEST_F(MaxMatchSegmentationTest, SingleCharacter) {
63 const auto& segments = segmenter->Segment(utf8("一"));
64 EXPECT_EQ(1, segments->Length());
65 EXPECT_EQ(utf8("一"), std::string(segments->At(0)));
66 }
67
68 TEST_F(MaxMatchSegmentationTest, PreserveIdeographicDescriptionSequence) {
69 const auto& segments = segmenter->Segment(utf8("⿰钅干干"));
70 EXPECT_EQ(2, segments->Length());
71 EXPECT_EQ(utf8("⿰钅干"), std::string(segments->At(0)));
72 EXPECT_EQ(utf8("干"), std::string(segments->At(1)));
73 }
74
75 TEST_F(MaxMatchSegmentationTest, TruncatedUtf8Sequence) {
76 // This test specifically triggers the buffer overflow bug in the old code.
77 // The bug occurs when a string ends with an incomplete UTF-8 sequence.
78 //
79 // Background: UTF8Util::NextCharLength() examines only the first byte to
80 // determine the expected character length (1-6 bytes), but doesn't verify
81 // that enough bytes actually remain in the buffer.
82 //
83 // Trigger condition: When the expected UTF-8 character length exceeds
84 // the actual remaining bytes, the old code's "length -= matchedLength"
85 // causes integer underflow (size_t wraps around to a huge value), leading
86 // to out-of-bounds reads in the next MatchPrefix() call.
87
88 // Construct a string ending with a truncated 3-byte UTF-8 sequence:
89 // - Normal text: "一" (valid 3-byte UTF-8: 0xE4 0xB8 0x80)
90 // - Followed by: 0xE4 0xB8 (incomplete 3-byte sequence - missing last byte)
91 std::string malformed;
92 malformed += utf8("一"); // Valid character
93 malformed += '\xE4'; // Start of 3-byte UTF-8 (NextCharLength returns 3)
94 malformed += '\xB8'; // Second byte
95 // Missing third byte - only 2 bytes remain but NextCharLength expects 3
96 // Old code: length=2, matchedLength=3 → length = 2-3 = SIZE_MAX (underflow)
97
98 // The fixed code should handle this gracefully without buffer overflow
99 EXPECT_NO_THROW({
100 const auto& segments = segmenter->Segment(malformed);
101 // The valid character "一" plus the incomplete sequence form a single segment
102 // (incomplete sequence doesn't match dictionary, gets accumulated with previous)
103 EXPECT_EQ(1, segments->Length());
104 // Output should preserve all input bytes (including incomplete sequence)
105 // This is correct behavior - we don't discard data, we preserve it
106 EXPECT_EQ(malformed, std::string(segments->At(0)));
107 });
108 }
109
110 } // namespace opencc