Use division to detect unsigned wrap-around

Reviewers: mislav.bradac, buda

Reviewed By: mislav.bradac

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D550
This commit is contained in:
Teon Banek 2017-07-13 14:39:47 +02:00
parent 99e27cb7e9
commit 387c0621cb

View File

@ -171,13 +171,12 @@ std::uint64_t CartesianProductSize(const std::vector<std::vector<T>> &sets) {
if (set.empty()) {
return 0U;
}
std::uint64_t new_n = n * set.size();
if (new_n < n || new_n < set.size()) {
if (std::numeric_limits<std::uint64_t>::max() / n < set.size()) {
DLOG(WARNING) << "Unsigned wrap-around when calculating expected size of "
"Cartesian product.";
return std::numeric_limits<std::uint64_t>::max();
}
n = new_n;
n *= set.size();
}
return n;
}