Skip to content

Commit

Permalink
lstm: Fix possible float division by zero
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
stweil committed Dec 1, 2016
1 parent dfd7082 commit 9e0da72
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lstm/lstmtrainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,9 @@ double LSTMTrainer::ComputeCharError(const GenericVector<int>& truth_str,
for (int i = 0; i < label_counts.size(); ++i) {
char_errors += abs(label_counts[i]);
}
if (truth_size == 0) {
return (char_errors == 0) ? 0.0 : 1.0;
}
return static_cast<double>(char_errors) / truth_size;
}

Expand Down

0 comments on commit 9e0da72

Please sign in to comment.