Commit 1e2735f2 for tesseract

commit 1e2735f2681503a38388841aa1d6f6071aeb0ff6
Author: zdenop <zdenop@gmail.com>
Date:   Thu Nov 19 12:40:24 2026 +0100

    Fix crashes in the result iterators for words without best_choice (#4451)

    A word without recognition result (best_choice == nullptr) made
    LTRResultIterator::GetUTF8Text and ResultIterator::GetUTF8Text fail
    with an assertion, see https://github.com/sirfz/tesserocr/issues/324.
    ResultIterator::GetUTF8Text(RIL_SYMBOL) also assigned a null pointer
    to a std::string when the word had no symbol at the current index.

    Handle such words in the result iterators and in the UNLV output:

    - GetUTF8Text returns nullptr at word and symbol level. At text line,
      paragraph and block level the word is skipped. It adds neither text
      nor spaces.
    - Confidence ignores words without best_choice.
    - WordIsFromDictionary, WordIsNumeric, WordNormedUTF8Text,
      SymbolIsSuperscript, SymbolIsSubscript and SymbolIsDropcap return
      false or nullptr.
    - write_results handles the word like an empty word.

    ResultIterator::GetUTF8Text(RIL_SYMBOL) no longer drops the LRM or RLM
    mark at the beginning of a minor run.

    Add regression tests for both iterators.

    Assisted-by: GitHub Copilot (GitHub)
    Assisted-by: Claude Fable 5.2 (Anthropic)
    Signed-off-by: Stefan Weil <sw@weilnetz.de>

diff --git a/src/ccmain/ltrresultiterator.cpp b/src/ccmain/ltrresultiterator.cpp
index a4d3529c..576b86f9 100644
--- a/src/ccmain/ltrresultiterator.cpp
+++ b/src/ccmain/ltrresultiterator.cpp
@@ -47,9 +47,15 @@ char *LTRResultIterator::GetUTF8Text(PageIteratorLevel level) const {
   std::string text;
   PAGE_RES_IT res_it(*it_);
   WERD_CHOICE *best_choice = res_it.word()->best_choice;
-  ASSERT_HOST(best_choice != nullptr);
+  if ((level == RIL_WORD || level == RIL_SYMBOL) && best_choice == nullptr) {
+    return nullptr; // No recognition results available
+  }
   if (level == RIL_SYMBOL) {
-    text = res_it.word()->BestUTF8(blob_index_, false);
+    const char *utf8 = res_it.word()->BestUTF8(blob_index_, false);
+    if (utf8 == nullptr) {
+      return nullptr;
+    }
+    text = utf8;
   } else if (level == RIL_WORD) {
     text = best_choice->unichar_string();
   } else {
@@ -57,15 +63,23 @@ char *LTRResultIterator::GetUTF8Text(PageIteratorLevel level) const {
     bool eop = false; // end of paragraph?
     do {              // for each paragraph in a block
       do {            // for each text line in a paragraph
+        const auto line_start = text.length();
         do {          // for each word in a text line
           best_choice = res_it.word()->best_choice;
-          ASSERT_HOST(best_choice != nullptr);
+          if (best_choice == nullptr) {
+            res_it.forward();
+            eol = res_it.row() != res_it.prev_row();
+            continue; // Skip words without recognition results.
+          }
           text += best_choice->unichar_string();
           text += " ";
           res_it.forward();
           eol = res_it.row() != res_it.prev_row();
         } while (!eol);
-        text.resize(text.length() - 1);
+        if (text.length() > line_start) {
+          // Remove the space after the last word of the text line.
+          text.resize(text.length() - 1);
+        }
         text += line_separator_;
         eop = res_it.block() != res_it.prev_block() ||
               res_it.row()->row->para() != res_it.prev_row()->row->para();
@@ -102,16 +116,20 @@ float LTRResultIterator::Confidence(PageIteratorLevel level) const {
     case RIL_BLOCK:
       do {
         best_choice = res_it.word()->best_choice;
-        mean_certainty += best_choice->certainty();
-        ++certainty_count;
+        if (best_choice != nullptr) {
+          mean_certainty += best_choice->certainty();
+          ++certainty_count;
+        }
         res_it.forward();
       } while (res_it.block() == res_it.prev_block());
       break;
     case RIL_PARA:
       do {
         best_choice = res_it.word()->best_choice;
-        mean_certainty += best_choice->certainty();
-        ++certainty_count;
+        if (best_choice != nullptr) {
+          mean_certainty += best_choice->certainty();
+          ++certainty_count;
+        }
         res_it.forward();
       } while (res_it.block() == res_it.prev_block() &&
                res_it.row()->row->para() == res_it.prev_row()->row->para());
@@ -119,20 +137,26 @@ float LTRResultIterator::Confidence(PageIteratorLevel level) const {
     case RIL_TEXTLINE:
       do {
         best_choice = res_it.word()->best_choice;
-        mean_certainty += best_choice->certainty();
-        ++certainty_count;
+        if (best_choice != nullptr) {
+          mean_certainty += best_choice->certainty();
+          ++certainty_count;
+        }
         res_it.forward();
       } while (res_it.row() == res_it.prev_row());
       break;
     case RIL_WORD:
       best_choice = res_it.word()->best_choice;
-      mean_certainty = best_choice->certainty();
-      certainty_count = 1;
+      if (best_choice != nullptr) {
+        mean_certainty = best_choice->certainty();
+        certainty_count = 1;
+      }
       break;
     case RIL_SYMBOL:
       best_choice = res_it.word()->best_choice;
-      mean_certainty = best_choice->certainty(blob_index_);
-      certainty_count = 1;
+      if (best_choice != nullptr) {
+        mean_certainty = best_choice->certainty(blob_index_);
+        certainty_count = 1;
+      }
   }
   if (certainty_count > 0) {
     mean_certainty /= certainty_count;
@@ -224,8 +248,8 @@ StrongScriptDirection LTRResultIterator::WordDirection() const {

 // Returns true if the current word was found in a dictionary.
 bool LTRResultIterator::WordIsFromDictionary() const {
-  if (it_->word() == nullptr) {
-    return false; // Already at the end!
+  if (it_->word() == nullptr || it_->word()->best_choice == nullptr) {
+    return false; // Already at the end or no recognition results!
   }
   int permuter = it_->word()->best_choice->permuter();
   return permuter == SYSTEM_DAWG_PERM || permuter == FREQ_DAWG_PERM || permuter == USER_DAWG_PERM;
@@ -241,8 +265,8 @@ int LTRResultIterator::BlanksBeforeWord() const {

 // Returns true if the current word is numeric.
 bool LTRResultIterator::WordIsNumeric() const {
-  if (it_->word() == nullptr) {
-    return false; // Already at the end!
+  if (it_->word() == nullptr || it_->word()->best_choice == nullptr) {
+    return false; // Already at the end or no recognition results!
   }
   int permuter = it_->word()->best_choice->permuter();
   return permuter == NUMBER_PERM;
@@ -313,8 +337,11 @@ char *LTRResultIterator::WordNormedUTF8Text() const {
   if (it_->word() == nullptr) {
     return nullptr; // Already at the end!
   }
-  std::string ocr_text;
   WERD_CHOICE *best_choice = it_->word()->best_choice;
+  if (best_choice == nullptr) {
+    return nullptr; // No recognition results available
+  }
+  std::string ocr_text;
   const UNICHARSET *unicharset = it_->word()->uch_set;
   for (unsigned i = 0; i < best_choice->length(); ++i) {
     ocr_text += unicharset->get_normed_unichar(best_choice->unichar_id(i));
@@ -339,7 +366,7 @@ const char *LTRResultIterator::WordLattice(int *lattice_size) const {
 // If iterating at a higher level object than symbols, eg words, then
 // this will return the attributes of the first symbol in that word.
 bool LTRResultIterator::SymbolIsSuperscript() const {
-  if (cblob_it_ == nullptr && it_->word() != nullptr) {
+  if (cblob_it_ == nullptr && it_->word() != nullptr && it_->word()->best_choice != nullptr) {
     return it_->word()->best_choice->BlobPosition(blob_index_) == SP_SUPERSCRIPT;
   }
   return false;
@@ -349,7 +376,7 @@ bool LTRResultIterator::SymbolIsSuperscript() const {
 // If iterating at a higher level object than symbols, eg words, then
 // this will return the attributes of the first symbol in that word.
 bool LTRResultIterator::SymbolIsSubscript() const {
-  if (cblob_it_ == nullptr && it_->word() != nullptr) {
+  if (cblob_it_ == nullptr && it_->word() != nullptr && it_->word()->best_choice != nullptr) {
     return it_->word()->best_choice->BlobPosition(blob_index_) == SP_SUBSCRIPT;
   }
   return false;
@@ -359,7 +386,7 @@ bool LTRResultIterator::SymbolIsSubscript() const {
 // If iterating at a higher level object than symbols, eg words, then
 // this will return the attributes of the first symbol in that word.
 bool LTRResultIterator::SymbolIsDropcap() const {
-  if (cblob_it_ == nullptr && it_->word() != nullptr) {
+  if (cblob_it_ == nullptr && it_->word() != nullptr && it_->word()->best_choice != nullptr) {
     return it_->word()->best_choice->BlobPosition(blob_index_) == SP_DROPCAP;
   }
   return false;
diff --git a/src/ccmain/output.cpp b/src/ccmain/output.cpp
index a530c15e..788c33fc 100644
--- a/src/ccmain/output.cpp
+++ b/src/ccmain/output.cpp
@@ -103,7 +103,9 @@ void Tesseract::write_results(PAGE_RES_IT &page_res_it,
   const UNICHARSET &uchset = *word->uch_set;
   UNICHAR_ID space = uchset.unichar_to_id(" ");

-  if ((word->unlv_crunch_mode != CR_NONE || word->best_choice->empty()) &&
+  // A word without recognition results is handled like an empty word.
+  if ((word->unlv_crunch_mode != CR_NONE || word->best_choice == nullptr ||
+       word->best_choice->empty()) &&
       !tessedit_zero_kelvin_rejection && !tessedit_word_for_word) {
     bool need_reject = false;
     if ((word->unlv_crunch_mode != CR_DELETE) &&
@@ -149,7 +151,7 @@ void Tesseract::write_results(PAGE_RES_IT &page_res_it,

   if (unlv_tilde_crunching && stats_.last_char_was_tilde && (word->word->space() == 0) &&
       !(word->word->flag(W_REP_CHAR) && tessedit_write_rep_codes) &&
-      (word->best_choice->unichar_id(0) == space)) {
+      (word->best_choice != nullptr && word->best_choice->unichar_id(0) == space)) {
     /* Prevent adjacent tilde across words - we know that adjacent tildes within
    words have been removed */
     word->MergeAdjacentBlobs(0);
@@ -157,7 +159,7 @@ void Tesseract::write_results(PAGE_RES_IT &page_res_it,
   if (newline_type || (word->word->flag(W_REP_CHAR) && tessedit_write_rep_codes)) {
     stats_.last_char_was_tilde = false;
   } else {
-    if (word->reject_map.length() > 0) {
+    if (word->reject_map.length() > 0 && word->best_choice != nullptr) {
       if (word->best_choice->unichar_id(word->reject_map.length() - 1) == space) {
         stats_.last_char_was_tilde = true;
       } else {
@@ -169,15 +171,17 @@ void Tesseract::write_results(PAGE_RES_IT &page_res_it,
     /* else it is unchanged as there are no output chars */
   }

-  ASSERT_HOST(word->best_choice->length() == word->reject_map.length());
+  if (word->best_choice != nullptr) {
+    ASSERT_HOST(word->best_choice->length() == word->reject_map.length());
+  }

   set_unlv_suspects(word);
   check_debug_pt(word, 120);
-  if (tessedit_rejection_debug) {
+  if (tessedit_rejection_debug && word->best_choice != nullptr) {
     tprintf("Dict word: \"%s\": %d\n", word->best_choice->debug_string().c_str(),
             dict_word(*(word->best_choice)));
   }
-  if (!word->word->flag(W_REP_CHAR) || !tessedit_write_rep_codes) {
+  if ((!word->word->flag(W_REP_CHAR) || !tessedit_write_rep_codes) && word->best_choice != nullptr) {
     if (tessedit_zero_rejection) {
       /* OVERRIDE ALL REJECTION MECHANISMS - ONLY REJECT TESS FAILURES */
       for (unsigned i = 0; i < word->best_choice->length(); ++i) {
@@ -250,7 +254,7 @@ UNICHAR_ID Tesseract::get_rep_char(WERD_RES *word) { // what char is repeated?
     ;
   }

-  if (i < word->reject_map.length()) {
+  if (i < word->reject_map.length() && word->best_choice != nullptr) {
     return word->best_choice->unichar_id(i);
   } else {
     return word->uch_set->unichar_to_id(unrecognised_char.c_str());
@@ -268,6 +272,9 @@ UNICHAR_ID Tesseract::get_rep_char(WERD_RES *word) { // what char is repeated?
  * tessedit_minimal_rejection.
  *************************************************************************/
 void Tesseract::set_unlv_suspects(WERD_RES *word_res) {
+  if (word_res->best_choice == nullptr) {
+    return; // No recognition results available
+  }
   int len = word_res->reject_map.length();
   const WERD_CHOICE &word = *(word_res->best_choice);
   const UNICHARSET &uchset = *word.unicharset();
diff --git a/src/ccmain/resultiterator.cpp b/src/ccmain/resultiterator.cpp
index d179666b..1f567496 100644
--- a/src/ccmain/resultiterator.cpp
+++ b/src/ccmain/resultiterator.cpp
@@ -673,14 +673,24 @@ char *ResultIterator::GetUTF8Text(PageIteratorLevel level) const {
       it.IterateAndAppendUTF8TextlineText(&text);
     } break;
     case RIL_WORD:
+      if (it_->word()->best_choice == nullptr) {
+        return nullptr;
+      }
       AppendUTF8WordText(&text);
       break;
     case RIL_SYMBOL: {
+      if (it_->word()->best_choice == nullptr) {
+        return nullptr;
+      }
       bool reading_direction_is_ltr = current_paragraph_is_ltr_ ^ in_minor_direction_;
       if (at_beginning_of_minor_run_) {
         text += reading_direction_is_ltr ? kLRM : kRLM;
       }
-      text = it_->word()->BestUTF8(blob_index_, false);
+      const char *utf8 = it_->word()->BestUTF8(blob_index_, false);
+      if (utf8 == nullptr) {
+        return nullptr;
+      }
+      text += utf8;
       if (IsAtFinalSymbolOfWord()) {
         AppendSuffixMarks(&text);
       }
@@ -710,7 +720,9 @@ void ResultIterator::AppendUTF8WordText(std::string *text) const {
   if (!it_->word()) {
     return;
   }
-  ASSERT_HOST(it_->word()->best_choice != nullptr);
+  if (it_->word()->best_choice == nullptr) {
+    return; // No recognition results available
+  }
   bool reading_direction_is_ltr = current_paragraph_is_ltr_ ^ in_minor_direction_;
   if (at_beginning_of_minor_run_) {
     *text += reading_direction_is_ltr ? kLRM : kRLM;
@@ -748,7 +760,12 @@ void ResultIterator::IterateAndAppendUTF8TextlineText(std::string *text) {

   int words_appended = 0;
   do {
-    int numSpaces = preserve_interword_spaces_ ? it_->word()->word->space() : (words_appended > 0);
+    const WERD_RES *word = it_->word();
+    if (word == nullptr || word->best_choice == nullptr) {
+      // Skip a word without recognition results. It gets neither text nor spaces.
+      continue;
+    }
+    int numSpaces = preserve_interword_spaces_ ? word->word->space() : (words_appended > 0);
     for (int i = 0; i < numSpaces; ++i) {
       *text += " ";
     }
diff --git a/unittest/resultiterator_test.cc b/unittest/resultiterator_test.cc
index 7eff5abd..fdbcb353 100644
--- a/unittest/resultiterator_test.cc
+++ b/unittest/resultiterator_test.cc
@@ -1,8 +1,10 @@

 #include <tesseract/baseapi.h>
 #include <tesseract/resultiterator.h>
+#include <memory>
 #include <string>
 #include "image.h"      // for Image
+#include "pageres.h"
 #include "scrollview.h"

 #include "include_gunit.h"
@@ -10,6 +12,38 @@

 namespace tesseract {

+class TestLTRResultIterator : public LTRResultIterator {
+ public:
+  using LTRResultIterator::LTRResultIterator;
+
+  explicit TestLTRResultIterator(const LTRResultIterator &src) : LTRResultIterator(src) {}
+
+  WERD_RES *CurrentWord() const { return it_->word(); }
+
+  void SetCurrentWordBestChoiceNull() {
+    if (it_ != nullptr && it_->word() != nullptr) {
+      it_->word()->best_choice = nullptr;
+    }
+  }
+};
+
+// Unlike TestLTRResultIterator, this keeps the concrete ResultIterator,
+// so the tests run the code of ResultIterator and not that of its base class.
+class TestResultIterator : public ResultIterator {
+ public:
+  explicit TestResultIterator(const ResultIterator &src) : ResultIterator(src) {}
+
+  WERD_RES *CurrentWord() const { return it_->word(); }
+};
+
+// Returns the text of the given iterator at the given level and frees the
+// buffer. A missing text gives an empty string.
+template <class Iterator>
+static std::string TextOf(const Iterator &it, PageIteratorLevel level) {
+  std::unique_ptr<char[]> text(it.GetUTF8Text(level));
+  return text ? std::string(text.get()) : std::string();
+}
+
 // DEFINE_string(tess_config, "", "config file for tesseract");
 // DEFINE_bool(visual_test, false, "Runs a visual test using scrollview");

@@ -642,6 +676,156 @@ TEST_F(ResultIteratorTest, TextlineOrderSanityCheck) {
   }
 }

+TEST_F(ResultIteratorTest, NullBestChoiceRegressionTest) {
+  SetImage("phototest.tif");
+  ASSERT_EQ(api_.Recognize(nullptr), 0);
+  std::unique_ptr<ResultIterator> r_it(api_.GetIterator());
+  ASSERT_NE(r_it, nullptr);
+
+  std::vector<std::pair<std::string, TestLTRResultIterator>> words;
+  for (TestLTRResultIterator it(*r_it); !it.Empty(RIL_WORD); it.Next(RIL_WORD)) {
+    if (it.CurrentWord() == nullptr || it.CurrentWord()->best_choice == nullptr) {
+      continue;
+    }
+    char *word = it.GetUTF8Text(RIL_WORD);
+    if (word == nullptr) {
+      continue;
+    }
+    words.emplace_back(word, TestLTRResultIterator(it));
+    delete[] word;
+  }
+  ASSERT_GE(words.size(), 3);
+
+  auto target = words[1];
+  target.second.SetCurrentWordBestChoiceNull();
+  EXPECT_EQ(nullptr, target.second.GetUTF8Text(RIL_WORD));
+  EXPECT_EQ(nullptr, target.second.GetUTF8Text(RIL_SYMBOL));
+
+  char *text = target.second.GetUTF8Text(RIL_TEXTLINE);
+  ASSERT_NE(text, nullptr);
+  std::string text_line(text);
+  delete[] text;
+  EXPECT_NE(text_line.find(words[2].first), std::string::npos)
+      << "A null best_choice should not stop textline traversal";
+
+  char *para_text = target.second.GetUTF8Text(RIL_PARA);
+  ASSERT_NE(para_text, nullptr);
+  std::string para(para_text);
+  delete[] para_text;
+  EXPECT_NE(para.find(words[2].first), std::string::npos)
+      << "A null best_choice should not stop paragraph traversal";
+}
+
+// A word without best_choice in the middle of a text line must be skipped
+// by ResultIterator, too: no text, no additional space, no confidence.
+TEST_F(ResultIteratorTest, NullBestChoiceInTextlineTest) {
+  SetImage("phototest.tif");
+  ASSERT_EQ(api_.Recognize(nullptr), 0);
+  std::unique_ptr<ResultIterator> r_it(api_.GetIterator());
+  ASSERT_NE(r_it, nullptr);
+
+  // Get the first three words of the first text line.
+  const TestResultIterator line(*r_it);
+  TestResultIterator second(line);
+  ASSERT_TRUE(second.Next(RIL_WORD));
+  TestResultIterator third(second);
+  ASSERT_TRUE(third.Next(RIL_WORD));
+  ASSERT_FALSE(third.IsAtBeginningOf(RIL_TEXTLINE));
+  const std::string prefix =
+      TextOf(line, RIL_WORD) + " " + TextOf(second, RIL_WORD) + " " + TextOf(third, RIL_WORD);
+  const std::string without_second = TextOf(line, RIL_WORD) + " " + TextOf(third, RIL_WORD);
+
+  // The text line without its second word, for both kinds of iterators.
+  const TestLTRResultIterator ltr_line(line);
+  const std::string text_line = TextOf(line, RIL_TEXTLINE);
+  const std::string ltr_text_line = TextOf(ltr_line, RIL_TEXTLINE);
+  ASSERT_EQ(0, text_line.compare(0, prefix.size(), prefix)) << text_line;
+  ASSERT_EQ(0, ltr_text_line.compare(0, prefix.size(), prefix)) << ltr_text_line;
+  const std::string expected = without_second + text_line.substr(prefix.size());
+  const std::string ltr_expected = without_second + ltr_text_line.substr(prefix.size());
+
+  WERD_RES *word = second.CurrentWord();
+  ASSERT_NE(word, nullptr);
+  WERD_CHOICE *best_choice = word->best_choice;
+  ASSERT_NE(best_choice, nullptr);
+  EXPECT_GT(second.Confidence(RIL_WORD), 0.0f);
+
+  // A best_choice without any character has no symbol.
+  WERD_CHOICE empty_choice(word->uch_set);
+  word->best_choice = &empty_choice;
+  EXPECT_EQ(nullptr, second.GetUTF8Text(RIL_SYMBOL));
+
+  word->best_choice = nullptr;
+  EXPECT_EQ(nullptr, second.GetUTF8Text(RIL_WORD));
+  EXPECT_EQ(nullptr, second.GetUTF8Text(RIL_SYMBOL));
+  EXPECT_EQ(0.0f, second.Confidence(RIL_WORD));
+  EXPECT_EQ(0.0f, second.Confidence(RIL_SYMBOL));
+  EXPECT_GT(line.Confidence(RIL_TEXTLINE), 0.0f);
+  EXPECT_EQ(expected, TextOf(line, RIL_TEXTLINE));
+  EXPECT_EQ(ltr_expected, TextOf(ltr_line, RIL_TEXTLINE));
+
+  word->best_choice = best_choice;
+  EXPECT_EQ(text_line, TextOf(line, RIL_TEXTLINE));
+}
+
+// A text line without any best_choice gives an empty line. It must neither
+// add spaces nor remove the line separator of the previous text line.
+TEST_F(ResultIteratorTest, NullBestChoiceTextlineTest) {
+  SetImage("phototest.tif");
+  ASSERT_EQ(api_.Recognize(nullptr), 0);
+  std::unique_ptr<ResultIterator> r_it(api_.GetIterator());
+  ASSERT_NE(r_it, nullptr);
+
+  // The second text line must be neither the first nor the last one
+  // of its paragraph.
+  const TestResultIterator first_line(*r_it);
+  TestResultIterator second_line(first_line);
+  ASSERT_TRUE(second_line.Next(RIL_TEXTLINE));
+  ASSERT_FALSE(second_line.IsAtBeginningOf(RIL_PARA));
+  TestResultIterator third_line(second_line);
+  ASSERT_TRUE(third_line.Next(RIL_TEXTLINE));
+  ASSERT_FALSE(third_line.IsAtBeginningOf(RIL_PARA));
+
+  // The paragraph with an empty second text line, for both kinds of iterators.
+  const TestLTRResultIterator ltr_first_line(first_line);
+  const TestLTRResultIterator ltr_second_line(second_line);
+  const std::string lines = TextOf(first_line, RIL_TEXTLINE) + TextOf(second_line, RIL_TEXTLINE);
+  const std::string ltr_lines =
+      TextOf(ltr_first_line, RIL_TEXTLINE) + TextOf(ltr_second_line, RIL_TEXTLINE);
+  const std::string para = TextOf(first_line, RIL_PARA);
+  const std::string ltr_para = TextOf(ltr_first_line, RIL_PARA);
+  ASSERT_EQ(0, para.compare(0, lines.size(), lines)) << para;
+  ASSERT_EQ(0, ltr_para.compare(0, ltr_lines.size(), ltr_lines)) << ltr_para;
+  const std::string expected =
+      TextOf(first_line, RIL_TEXTLINE) + "\n" + para.substr(lines.size());
+  const std::string ltr_expected =
+      TextOf(ltr_first_line, RIL_TEXTLINE) + "\n" + ltr_para.substr(ltr_lines.size());
+
+  // Remove the best_choice of all words of the second text line.
+  std::vector<std::pair<WERD_RES *, WERD_CHOICE *>> saved;
+  TestResultIterator it(second_line);
+  do {
+    WERD_RES *word = it.CurrentWord();
+    ASSERT_NE(word, nullptr);
+    saved.emplace_back(word, word->best_choice);
+  } while (it.Next(RIL_WORD) && !it.IsAtBeginningOf(RIL_TEXTLINE));
+  ASSERT_GE(saved.size(), 2u);
+  for (auto &entry : saved) {
+    entry.first->best_choice = nullptr;
+  }
+
+  EXPECT_EQ("\n", TextOf(second_line, RIL_TEXTLINE));
+  EXPECT_EQ("\n", TextOf(ltr_second_line, RIL_TEXTLINE));
+  EXPECT_EQ(0.0f, second_line.Confidence(RIL_TEXTLINE));
+  EXPECT_EQ(expected, TextOf(first_line, RIL_PARA));
+  EXPECT_EQ(ltr_expected, TextOf(ltr_first_line, RIL_PARA));
+
+  for (auto &entry : saved) {
+    entry.first->best_choice = entry.second;
+  }
+  EXPECT_EQ(para, TextOf(first_line, RIL_PARA));
+}
+
 // TODO: Missing image
 TEST_F(ResultIteratorTest, DISABLED_NonNullChoicesTest) {
   SetImage("5318c4b679264.jpg");