From 27a5908a5585108283f1af2e3ce7abbd77e7d429 Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Wed, 20 Jun 2018 19:43:00 +0200 Subject: [PATCH 1/3] Fix CID 1393239 (Dereference null return value) Add also some error handling if fopen fails. Signed-off-by: Stefan Weil --- src/api/baseapi.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/api/baseapi.cpp b/src/api/baseapi.cpp index b17fe2d2c..850de33b6 100644 --- a/src/api/baseapi.cpp +++ b/src/api/baseapi.cpp @@ -1195,8 +1195,12 @@ bool TessBaseAPI::ProcessPage(Pix* pix, int page_index, const char* filename, if (failed && retry_config != nullptr && retry_config[0] != '\0') { // Save current config variables before switching modes. FILE* fp = fopen(kOldVarsFile, "wb"); - PrintVariables(fp); - fclose(fp); + if (fp == nullptr) { + tprintf("Error, failed to open file \"%s\"\n", kOldVarsFile); + } else { + PrintVariables(fp); + fclose(fp); + } // Switch to alternate mode for retry. ReadConfigFile(retry_config); SetImage(pix); From 09976e6125ea05e28bd9a9df9c240024157df28c Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Wed, 20 Jun 2018 19:47:38 +0200 Subject: [PATCH 2/3] Fix CID 1393238 (Dereference null return value) Add also some error handling if fopen fails. Signed-off-by: Stefan Weil --- src/ccmain/control.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ccmain/control.cpp b/src/ccmain/control.cpp index 5523313c7..5ce8c0b4f 100644 --- a/src/ccmain/control.cpp +++ b/src/ccmain/control.cpp @@ -128,8 +128,12 @@ bool Tesseract::ProcessTargetWord(const TBOX& word_box, if (backup_config_file_ == nullptr) { backup_config_file_ = kBackUpConfigFile; FILE* config_fp = fopen(backup_config_file_, "wb"); - ParamUtils::PrintParams(config_fp, params()); - fclose(config_fp); + if (config_fp == nullptr) { + tprintf("Error, failed to open file \"%s\"\n", backup_config_file_); + } else { + ParamUtils::PrintParams(config_fp, params()); + fclose(config_fp); + } ParamUtils::ReadParamsFile(word_config, SET_PARAM_CONSTRAINT_DEBUG_ONLY, params()); From 2a5a092469bfa712bb0e8163d13554172f3da2b8 Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Wed, 20 Jun 2018 19:56:15 +0200 Subject: [PATCH 3/3] Fix CID 1393241 (Dereference null return value) Add also some error handling if fopen fails. Signed-off-by: Stefan Weil --- src/classify/mastertrainer.cpp | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/classify/mastertrainer.cpp b/src/classify/mastertrainer.cpp index b90db311e..682d25bb6 100644 --- a/src/classify/mastertrainer.cpp +++ b/src/classify/mastertrainer.cpp @@ -578,8 +578,12 @@ void MasterTrainer::WriteInttempAndPFFMTable(const UNICHARSET& unicharset, INT_TEMPLATES int_templates = classify->CreateIntTemplates(float_classes, shape_set); FILE* fp = fopen(inttemp_file, "wb"); - classify->WriteIntTemplates(fp, int_templates, shape_set); - fclose(fp); + if (fp == nullptr) { + tprintf("Error, failed to open file \"%s\"\n", inttemp_file); + } else { + classify->WriteIntTemplates(fp, int_templates, shape_set); + fclose(fp); + } // Now write pffmtable. This is complicated by the fact that the adaptive // classifier still wants one indexed by unichar-id, but the static // classifier needs one indexed by its shape class id. @@ -612,15 +616,19 @@ void MasterTrainer::WriteInttempAndPFFMTable(const UNICHARSET& unicharset, shapetable_cutoffs.push_back(max_length); } fp = fopen(pffmtable_file, "wb"); - shapetable_cutoffs.Serialize(fp); - for (int c = 0; c < unicharset.size(); ++c) { - const char *unichar = unicharset.id_to_unichar(c); - if (strcmp(unichar, " ") == 0) { - unichar = "NULL"; + if (fp == nullptr) { + tprintf("Error, failed to open file \"%s\"\n", pffmtable_file); + } else { + shapetable_cutoffs.Serialize(fp); + for (int c = 0; c < unicharset.size(); ++c) { + const char *unichar = unicharset.id_to_unichar(c); + if (strcmp(unichar, " ") == 0) { + unichar = "NULL"; + } + fprintf(fp, "%s %d\n", unichar, unichar_cutoffs[c]); } - fprintf(fp, "%s %d\n", unichar, unichar_cutoffs[c]); + fclose(fp); } - fclose(fp); free_int_templates(int_templates); delete classify; }