Skip to content

Commit 9e0da72

Browse files
committed
lstm: Fix possible float division by zero
Coverity report: CID 1366441 (#1 of 1): Division or modulo by float zero (DIVIDE_BY_ZERO) 5. divide_by_zero: In expression static_cast<double>(char_errors) / truth_size, division by expression truth_size which may be zero has undefined behavior. Signed-off-by: Stefan Weil <[email protected]>
1 parent dfd7082 commit 9e0da72

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

lstm/lstmtrainer.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,9 @@ double LSTMTrainer::ComputeCharError(const GenericVector<int>& truth_str,
12011201
for (int i = 0; i < label_counts.size(); ++i) {
12021202
char_errors += abs(label_counts[i]);
12031203
}
1204+
if (truth_size == 0) {
1205+
return (char_errors == 0) ? 0.0 : 1.0;
1206+
}
12041207
return static_cast<double>(char_errors) / truth_size;
12051208
}
12061209

0 commit comments

Comments
 (0)