Merge pull request #1688 from stweil/cov

Check return values from fopen (fixes several Coverity Scan reports)
This commit is contained in:
zdenop 2018-06-20 21:59:10 +02:00 committed by GitHub
commit 2bb4c4ed44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 13 deletions

View File

@ -1195,8 +1195,12 @@ bool TessBaseAPI::ProcessPage(Pix* pix, int page_index, const char* filename,
if (failed && retry_config != nullptr && retry_config[0] != '\0') { if (failed && retry_config != nullptr && retry_config[0] != '\0') {
// Save current config variables before switching modes. // Save current config variables before switching modes.
FILE* fp = fopen(kOldVarsFile, "wb"); FILE* fp = fopen(kOldVarsFile, "wb");
PrintVariables(fp); if (fp == nullptr) {
fclose(fp); tprintf("Error, failed to open file \"%s\"\n", kOldVarsFile);
} else {
PrintVariables(fp);
fclose(fp);
}
// Switch to alternate mode for retry. // Switch to alternate mode for retry.
ReadConfigFile(retry_config); ReadConfigFile(retry_config);
SetImage(pix); SetImage(pix);

View File

@ -128,8 +128,12 @@ bool Tesseract::ProcessTargetWord(const TBOX& word_box,
if (backup_config_file_ == nullptr) { if (backup_config_file_ == nullptr) {
backup_config_file_ = kBackUpConfigFile; backup_config_file_ = kBackUpConfigFile;
FILE* config_fp = fopen(backup_config_file_, "wb"); FILE* config_fp = fopen(backup_config_file_, "wb");
ParamUtils::PrintParams(config_fp, params()); if (config_fp == nullptr) {
fclose(config_fp); tprintf("Error, failed to open file \"%s\"\n", backup_config_file_);
} else {
ParamUtils::PrintParams(config_fp, params());
fclose(config_fp);
}
ParamUtils::ReadParamsFile(word_config, ParamUtils::ReadParamsFile(word_config,
SET_PARAM_CONSTRAINT_DEBUG_ONLY, SET_PARAM_CONSTRAINT_DEBUG_ONLY,
params()); params());

View File

@ -578,8 +578,12 @@ void MasterTrainer::WriteInttempAndPFFMTable(const UNICHARSET& unicharset,
INT_TEMPLATES int_templates = classify->CreateIntTemplates(float_classes, INT_TEMPLATES int_templates = classify->CreateIntTemplates(float_classes,
shape_set); shape_set);
FILE* fp = fopen(inttemp_file, "wb"); FILE* fp = fopen(inttemp_file, "wb");
classify->WriteIntTemplates(fp, int_templates, shape_set); if (fp == nullptr) {
fclose(fp); 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 // Now write pffmtable. This is complicated by the fact that the adaptive
// classifier still wants one indexed by unichar-id, but the static // classifier still wants one indexed by unichar-id, but the static
// classifier needs one indexed by its shape class id. // 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); shapetable_cutoffs.push_back(max_length);
} }
fp = fopen(pffmtable_file, "wb"); fp = fopen(pffmtable_file, "wb");
shapetable_cutoffs.Serialize(fp); if (fp == nullptr) {
for (int c = 0; c < unicharset.size(); ++c) { tprintf("Error, failed to open file \"%s\"\n", pffmtable_file);
const char *unichar = unicharset.id_to_unichar(c); } else {
if (strcmp(unichar, " ") == 0) { shapetable_cutoffs.Serialize(fp);
unichar = "NULL"; 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); free_int_templates(int_templates);
delete classify; delete classify;
} }