Skip to content

Commit

Permalink
Merge pull request #198 from tyler92/division-by-zero
Browse files Browse the repository at this point in the history
Prevent potential division by zero
  • Loading branch information
tristanpenman authored Oct 20, 2024
2 parents fc9ddf1 + 63c56dd commit 7fd212f
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions include/valijson/constraints/concrete_constraints.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <set>
#include <string>
#include <vector>
#include <cmath>

#include <valijson/constraints/basic_constraint.hpp>
#include <valijson/internal/custom_allocator.hpp>
Expand Down Expand Up @@ -784,6 +785,11 @@ class MultipleOfDoubleConstraint:

void setDivisor(double newValue)
{
if (!std::isfinite(newValue) || newValue <= 0.0) {
throwRuntimeError(
"Divisor for 'multipleOf' or 'divisibleBy' must be positive");
}

m_value = newValue;
}

Expand Down Expand Up @@ -813,6 +819,11 @@ class MultipleOfIntConstraint:

void setDivisor(int64_t newValue)
{
if (newValue <= 0) {
throwRuntimeError(
"Divisor for 'multipleOf' or 'divisibleBy' must be positive");
}

m_value = newValue;
}

Expand Down

0 comments on commit 7fd212f

Please sign in to comment.