From a49b8f1d211cba3daca4435b3606376b393cae8c Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Sat, 30 Jun 2018 16:51:15 +0200 Subject: [PATCH] Fix CID 1297960 (Dereference after null check) Signed-off-by: Stefan Weil --- src/api/baseapi.cpp | 60 ++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/src/api/baseapi.cpp b/src/api/baseapi.cpp index e84ebd621c..41059ab450 100644 --- a/src/api/baseapi.cpp +++ b/src/api/baseapi.cpp @@ -1998,39 +1998,39 @@ bool TessBaseAPI::AdaptToWordStr(PageSegMode mode, const char* wordstr) { WERD_RES* word_res = it.word(); if (word_res != nullptr) { word_res->word->set_text(wordstr); + // Check to see if text matches wordstr. + int w = 0; + int t; + for (t = 0; text[t] != '\0'; ++t) { + if (text[t] == '\n' || text[t] == ' ') + continue; + while (wordstr[w] == ' ') ++w; + if (text[t] != wordstr[w]) + break; + ++w; + } + if (text[t] != '\0' || wordstr[w] != '\0') { + // No match. + delete page_res_; + GenericVector boxes; + page_res_ = tesseract_->SetupApplyBoxes(boxes, block_list_); + tesseract_->ReSegmentByClassification(page_res_); + tesseract_->TidyUp(page_res_); + PAGE_RES_IT pr_it(page_res_); + if (pr_it.word() == nullptr) + success = false; + else + word_res = pr_it.word(); + } else { + word_res->BestChoiceToCorrectText(); + } + if (success) { + tesseract_->EnableLearning = true; + tesseract_->LearnWord(nullptr, word_res); + } } else { success = false; } - // Check to see if text matches wordstr. - int w = 0; - int t = 0; - for (t = 0; text[t] != '\0'; ++t) { - if (text[t] == '\n' || text[t] == ' ') - continue; - while (wordstr[w] == ' ') ++w; - if (text[t] != wordstr[w]) - break; - ++w; - } - if (text[t] != '\0' || wordstr[w] != '\0') { - // No match. - delete page_res_; - GenericVector boxes; - page_res_ = tesseract_->SetupApplyBoxes(boxes, block_list_); - tesseract_->ReSegmentByClassification(page_res_); - tesseract_->TidyUp(page_res_); - PAGE_RES_IT pr_it(page_res_); - if (pr_it.word() == nullptr) - success = false; - else - word_res = pr_it.word(); - } else { - word_res->BestChoiceToCorrectText(); - } - if (success) { - tesseract_->EnableLearning = true; - tesseract_->LearnWord(nullptr, word_res); - } } else { success = false; }