Commit db90d274 for tesseract
commit db90d27458cebc02e71887d93087790bbd356193
Author: Stefan Weil <sw@weilnetz.de>
Date: Fri Nov 27 13:46:38 2026 +0100
Fix read beyond the end of the codes in the Javanese validator
ValidateJavanese::ConsumeViramaIfValid() copied the code which
follows a virama (pangkon) to the output without a test whether there
is one. For text which ends with a pangkon, which is valid and
common, it read one element beyond the end of codes_ and appended
that value to the text (a NUL character in a test).
A code which is neither a consonant nor a ZWNJ (punctuation, a
digit) was copied in the same way, as if it were the ZWNJ of an
explicit virama. It made one part with the pangkon, which was split
from its consonant.
Only copy a ZWNJ. A pangkon without one stays with its consonant,
and no ZWNJ is added.
Both branches of the condition were identical, which g++ reports with
-Wduplicated-branches.
Add a unit test.
Assisted-by: Claude Fable 5.2 (Anthropic)
Signed-off-by: Stefan Weil <sw@weilnetz.de>
diff --git a/Makefile.am b/Makefile.am
index 9e7a173f..1c07dee9 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1289,6 +1289,7 @@ check_PROGRAMS += unicharset_load_test
check_PROGRAMS += unicharset_test
check_PROGRAMS += validate_grapheme_test
check_PROGRAMS += validate_indic_test
+check_PROGRAMS += validate_javanese_test
check_PROGRAMS += validate_khmer_test
check_PROGRAMS += validate_myanmar_test
check_PROGRAMS += validator_test
@@ -1684,6 +1685,10 @@ validate_indic_test_SOURCES = unittest/validate_indic_test.cc
validate_indic_test_CPPFLAGS = $(unittest_CPPFLAGS)
validate_indic_test_LDADD = $(TRAINING_LIBS) $(ICU_I18N_LIBS) $(ICU_UC_LIBS)
+validate_javanese_test_SOURCES = unittest/validate_javanese_test.cc
+validate_javanese_test_CPPFLAGS = $(unittest_CPPFLAGS)
+validate_javanese_test_LDADD = $(TRAINING_LIBS) $(ICU_I18N_LIBS) $(ICU_UC_LIBS)
+
validate_khmer_test_SOURCES = unittest/validate_khmer_test.cc
validate_khmer_test_CPPFLAGS = $(unittest_CPPFLAGS)
validate_khmer_test_LDADD = $(TRAINING_LIBS) $(ICU_I18N_LIBS) $(ICU_UC_LIBS)
diff --git a/src/training/unicharset/validate_javanese.cpp b/src/training/unicharset/validate_javanese.cpp
index 422c0e7c..b935afb3 100644
--- a/src/training/unicharset/validate_javanese.cpp
+++ b/src/training/unicharset/validate_javanese.cpp
@@ -68,8 +68,8 @@ bool ValidateJavanese::ConsumeGraphemeIfValid() {
// A linking virama (with either type of pre-virama joiner, post-virama ZWJ, or
// no joiner at all) must be followed by a consonant.
// A non-linking (explicit) virama is indicated by a ZWNJ after it, or a non
-// consonant, space, or character from a different script. We clean up the
-// representation to make it consistent by adding a ZWNJ if missing from a
+// consonant, space, or character from a different script. Unlike
+// validate_indic.cpp, we do not add a ZWNJ if it is missing from a
// non-linking virama. Returns false with an invalid sequence.
bool ValidateJavanese::ConsumeViramaIfValid(IndicPair joiner, bool post_matra) {
const unsigned num_codes = codes_.size();
@@ -110,14 +110,14 @@ bool ValidateJavanese::ConsumeViramaIfValid(IndicPair joiner, bool post_matra) {
}
} else if (codes_used_ == num_codes || codes_[codes_used_].first != CharClass::kConsonant ||
post_matra) {
- if (codes_used_ == num_codes || codes_[codes_used_].second != kZeroWidthNonJoiner) {
- // It is valid to have an unterminated virama at the end of a word, but
- // for consistency, we will always add ZWNJ if not present.
- CodeOnlyToOutput();
- } else {
+ // It is valid to have an unterminated virama at the end of a word or
+ // before a code which is not a consonant. Unlike validate_indic.cpp,
+ // no ZWNJ is added then, and the following code is not part of the
+ // virama.
+ if (codes_used_ < num_codes && codes_[codes_used_].second == kZeroWidthNonJoiner) {
CodeOnlyToOutput();
}
- // Explicit virama [H z]
+ // Explicit virama [H z], or an unterminated virama with its consonant.
MultiCodePart(2);
}
} else {
diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt
index 3e65620b..1e71d624 100644
--- a/unittest/CMakeLists.txt
+++ b/unittest/CMakeLists.txt
@@ -57,6 +57,7 @@ set(TRAINING_TESTS
unicharset_test.cc
validate_grapheme_test.cc
validate_indic_test.cc
+ validate_javanese_test.cc
validate_khmer_test.cc
validate_myanmar_test.cc
validator_test.cc)
diff --git a/unittest/validate_javanese_test.cc b/unittest/validate_javanese_test.cc
new file mode 100644
index 00000000..e5d912a5
--- /dev/null
+++ b/unittest/validate_javanese_test.cc
@@ -0,0 +1,87 @@
+// (C) Copyright 2026, Stefan Weil
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "include_gunit.h"
+#include "normstrngs.h"
+#include "normstrngs_test.h"
+
+namespace tesseract {
+
+// A word which ends with a pangkon (virama) is valid. The validator used to
+// read one element beyond the end of its codes for such a word and appended
+// that value to the text.
+TEST(ValidateJavaneseTest, FinalPangkon) {
+ // "anak": HA NA KA PANGKON.
+ std::string str = "ꦲꦤꦏ꧀";
+ std::vector<std::string> glyphs;
+ EXPECT_TRUE(NormalizeCleanAndSegmentUTF8(UnicodeNormMode::kNFC, OCRNorm::kNone,
+ GraphemeNormMode::kCombined, true, str.c_str(),
+ &glyphs));
+ // The pangkon stays with its consonant.
+ ASSERT_EQ(glyphs.size(), 3) << PrintStringVectorWithUnicodes(glyphs);
+ EXPECT_EQ(glyphs[0], "ꦲ");
+ EXPECT_EQ(glyphs[1], "ꦤ");
+ EXPECT_EQ(glyphs[2], "ꦏ꧀");
+ std::string result;
+ EXPECT_TRUE(NormalizeUTF8String(UnicodeNormMode::kNFC, OCRNorm::kNone, GraphemeNorm::kNormalize,
+ str.c_str(), &result));
+ EXPECT_EQ(str, result);
+}
+
+// A pangkon before a code which is neither a consonant nor a ZWNJ stays with
+// its consonant, too. The validator used to make one part of the pangkon and
+// the code which follows it.
+TEST(ValidateJavaneseTest, PangkonBeforePunctuation) {
+ // KA PANGKON PADA LINGSA.
+ std::string str = "ꦏ꧀꧈";
+ std::vector<std::string> glyphs;
+ EXPECT_TRUE(NormalizeCleanAndSegmentUTF8(UnicodeNormMode::kNFC, OCRNorm::kNone,
+ GraphemeNormMode::kGlyphSplit, true, str.c_str(),
+ &glyphs));
+ ASSERT_EQ(glyphs.size(), 2) << PrintStringVectorWithUnicodes(glyphs);
+ EXPECT_EQ(glyphs[0], "ꦏ꧀");
+ EXPECT_EQ(glyphs[1], "꧈");
+ // The same word at the end of the text gives the same part.
+ str = "ꦏ꧀";
+ glyphs.clear();
+ EXPECT_TRUE(NormalizeCleanAndSegmentUTF8(UnicodeNormMode::kNFC, OCRNorm::kNone,
+ GraphemeNormMode::kGlyphSplit, true, str.c_str(),
+ &glyphs));
+ ASSERT_EQ(glyphs.size(), 1) << PrintStringVectorWithUnicodes(glyphs);
+ EXPECT_EQ(glyphs[0], str);
+}
+
+// An explicit pangkon keeps its ZWNJ.
+TEST(ValidateJavaneseTest, PangkonWithZwnj) {
+ // KA PANGKON ZWNJ.
+ std::string str = "ꦏ꧀";
+ std::vector<std::string> glyphs;
+ EXPECT_TRUE(NormalizeCleanAndSegmentUTF8(UnicodeNormMode::kNFC, OCRNorm::kNone,
+ GraphemeNormMode::kGlyphSplit, true, str.c_str(),
+ &glyphs));
+ ASSERT_EQ(glyphs.size(), 2) << PrintStringVectorWithUnicodes(glyphs);
+ EXPECT_EQ(glyphs[0], "ꦏ");
+ EXPECT_EQ(glyphs[1], "꧀");
+}
+
+// A pangkon between two consonants makes a conjunct, which is one grapheme.
+TEST(ValidateJavaneseTest, Conjunct) {
+ // KA PANGKON NA.
+ std::string str = "ꦏ꧀ꦤ";
+ std::vector<std::string> glyphs;
+ EXPECT_TRUE(NormalizeCleanAndSegmentUTF8(UnicodeNormMode::kNFC, OCRNorm::kNone,
+ GraphemeNormMode::kCombined, true, str.c_str(),
+ &glyphs));
+ ASSERT_EQ(glyphs.size(), 1) << PrintStringVectorWithUnicodes(glyphs);
+ EXPECT_EQ(glyphs[0], str);
+}
+
+} // namespace tesseract