From 9e0da72818eb62c29029bab0051a68bffbd1362e Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Mon, 28 Nov 2016 12:43:30 +0100 Subject: [PATCH] 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(char_errors) / truth_size, division by expression truth_size which may be zero has undefined behavior. Signed-off-by: Stefan Weil --- lstm/lstmtrainer.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lstm/lstmtrainer.cpp b/lstm/lstmtrainer.cpp index a44125b2e2..cffde1c97d 100644 --- a/lstm/lstmtrainer.cpp +++ b/lstm/lstmtrainer.cpp @@ -1201,6 +1201,9 @@ double LSTMTrainer::ComputeCharError(const GenericVector& 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(char_errors) / truth_size; }