Fix float division by zero

That runtime error is normally not visible because it does not abort
the program, but is detected when the code was compiled with sanitizers.

It can be triggered with this OSS-Fuzz testcase:
https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=13662

Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
Stefan Weil 2019-03-12 09:28:16 +01:00
parent 4c0b98bd12
commit 56a39bda77

View File

@ -1174,19 +1174,22 @@ void LanguageModel::FillConsistencyInfo(
} }
} }
if (expected_gap_found) { if (expected_gap_found) {
float actual_gap = int actual_gap = word_res->GetBlobsGap(curr_col-1);
static_cast<float>(word_res->GetBlobsGap(curr_col-1)); if (actual_gap == 0) {
float gap_ratio = expected_gap / actual_gap;
// TODO(rays) The gaps seem to be way off most of the time, saved by
// the error here that the ratio was compared to 1/2, when it should
// have been 0.5f. Find the source of the gaps discrepancy and put
// the 0.5f here in place of 0.0f.
// Test on 2476595.sj, pages 0 to 6. (In French.)
if (gap_ratio < 0.0f || gap_ratio > 2.0f) {
consistency_info->num_inconsistent_spaces++; consistency_info->num_inconsistent_spaces++;
} else {
float gap_ratio = expected_gap / actual_gap;
// TODO(rays) The gaps seem to be way off most of the time, saved by
// the error here that the ratio was compared to 1/2, when it should
// have been 0.5f. Find the source of the gaps discrepancy and put
// the 0.5f here in place of 0.0f.
// Test on 2476595.sj, pages 0 to 6. (In French.)
if (gap_ratio < 0.0f || gap_ratio > 2.0f) {
consistency_info->num_inconsistent_spaces++;
}
} }
if (language_model_debug_level > 1) { if (language_model_debug_level > 1) {
tprintf("spacing for %s(%d) %s(%d) col %d: expected %g actual %g\n", tprintf("spacing for %s(%d) %s(%d) col %d: expected %g actual %d\n",
unicharset.id_to_unichar(parent_b->unichar_id()), unicharset.id_to_unichar(parent_b->unichar_id()),
parent_b->unichar_id(), unicharset.id_to_unichar(unichar_id), parent_b->unichar_id(), unicharset.id_to_unichar(unichar_id),
unichar_id, curr_col, expected_gap, actual_gap); unichar_id, curr_col, expected_gap, actual_gap);