From 085ebe1e96cd8dbbcf425d400fea9d7510f377aa Mon Sep 17 00:00:00 2001 From: Tiago Gomes Date: Fri, 20 Apr 2012 17:16:10 +0100 Subject: [PATCH] check for overflows when performing expansion case the factor size after expansion be bigger than an unsigned integer, abort the program --- packages/CLPBN/clpbn/bp/Parfactor.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/CLPBN/clpbn/bp/Parfactor.cpp b/packages/CLPBN/clpbn/bp/Parfactor.cpp index d8b7b00c8..60e7c1648 100644 --- a/packages/CLPBN/clpbn/bp/Parfactor.cpp +++ b/packages/CLPBN/clpbn/bp/Parfactor.cpp @@ -583,7 +583,14 @@ Parfactor::expandPotential ( unsigned newRange, const vector& sumIndexes) { - unsigned newSize = (params_.size() / ranges_[fIdx]) * newRange; + Params::size_type newSize = (params_.size() / ranges_[fIdx]) * newRange; + if (newSize > Util::maxUnsigned()) { + // factor will become to bigger, is not worth to continue + cerr << "error: an overflow occurred when performing expansion" ; + cerr << endl; + abort(); + } + Params copy = params_; params_.clear(); params_.reserve (newSize); @@ -599,6 +606,9 @@ Parfactor::expandPotential ( ranges_[fIdx] = newRange; vector indices (ranges_.size(), 0); for (unsigned k = 0; k < newSize; k++) { + if (index >= copy.size()) { + abort(); + } assert (index < copy.size()); params_.push_back (copy[index]); for (int i = ranges_.size() - 1; i >= 0; i--) {