Skip to content

Commit 2a5a092

Browse files
committed
Fix CID 1393241 (Dereference null return value)
Add also some error handling if fopen fails. Signed-off-by: Stefan Weil <[email protected]>
1 parent 09976e6 commit 2a5a092

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/classify/mastertrainer.cpp

+17-9
Original file line numberDiff line numberDiff line change
@@ -578,8 +578,12 @@ void MasterTrainer::WriteInttempAndPFFMTable(const UNICHARSET& unicharset,
578578
INT_TEMPLATES int_templates = classify->CreateIntTemplates(float_classes,
579579
shape_set);
580580
FILE* fp = fopen(inttemp_file, "wb");
581-
classify->WriteIntTemplates(fp, int_templates, shape_set);
582-
fclose(fp);
581+
if (fp == nullptr) {
582+
tprintf("Error, failed to open file \"%s\"\n", inttemp_file);
583+
} else {
584+
classify->WriteIntTemplates(fp, int_templates, shape_set);
585+
fclose(fp);
586+
}
583587
// Now write pffmtable. This is complicated by the fact that the adaptive
584588
// classifier still wants one indexed by unichar-id, but the static
585589
// classifier needs one indexed by its shape class id.
@@ -612,15 +616,19 @@ void MasterTrainer::WriteInttempAndPFFMTable(const UNICHARSET& unicharset,
612616
shapetable_cutoffs.push_back(max_length);
613617
}
614618
fp = fopen(pffmtable_file, "wb");
615-
shapetable_cutoffs.Serialize(fp);
616-
for (int c = 0; c < unicharset.size(); ++c) {
617-
const char *unichar = unicharset.id_to_unichar(c);
618-
if (strcmp(unichar, " ") == 0) {
619-
unichar = "NULL";
619+
if (fp == nullptr) {
620+
tprintf("Error, failed to open file \"%s\"\n", pffmtable_file);
621+
} else {
622+
shapetable_cutoffs.Serialize(fp);
623+
for (int c = 0; c < unicharset.size(); ++c) {
624+
const char *unichar = unicharset.id_to_unichar(c);
625+
if (strcmp(unichar, " ") == 0) {
626+
unichar = "NULL";
627+
}
628+
fprintf(fp, "%s %d\n", unichar, unichar_cutoffs[c]);
620629
}
621-
fprintf(fp, "%s %d\n", unichar, unichar_cutoffs[c]);
630+
fclose(fp);
622631
}
623-
fclose(fp);
624632
free_int_templates(int_templates);
625633
delete classify;
626634
}

0 commit comments

Comments
 (0)