mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-01 10:54:32 +02:00
Make sure that operator % throws an exception when the ride-hand-side argument is 0
fixes #8089
This commit is contained in:
parent
8ef8316ca6
commit
23a556c7ce
@ -336,11 +336,9 @@ Value icinga::operator*(int lhs, const Value& rhs)
|
|||||||
|
|
||||||
Value icinga::operator/(const Value& lhs, const Value& rhs)
|
Value icinga::operator/(const Value& lhs, const Value& rhs)
|
||||||
{
|
{
|
||||||
if (lhs.IsEmpty())
|
if (rhs.IsEmpty())
|
||||||
return 0;
|
|
||||||
else if (rhs.IsEmpty())
|
|
||||||
BOOST_THROW_EXCEPTION(std::invalid_argument("Right-hand side argument for operator / is Empty."));
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Right-hand side argument for operator / is Empty."));
|
||||||
else if (lhs.IsNumber() && rhs.IsNumber()) {
|
else if ((lhs.IsEmpty() || lhs.IsNumber()) && rhs.IsNumber()) {
|
||||||
if (static_cast<double>(rhs) == 0)
|
if (static_cast<double>(rhs) == 0)
|
||||||
BOOST_THROW_EXCEPTION(std::invalid_argument("Right-hand side argument for operator / is 0."));
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Right-hand side argument for operator / is 0."));
|
||||||
|
|
||||||
@ -369,6 +367,67 @@ Value icinga::operator/(int lhs, const Value& rhs)
|
|||||||
return Value(lhs) / rhs;
|
return Value(lhs) / rhs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Value icinga::operator%(const Value& lhs, const Value& rhs)
|
||||||
|
{
|
||||||
|
if (rhs.IsEmpty())
|
||||||
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Right-hand side argument for operator % is Empty."));
|
||||||
|
else if ((rhs.IsNumber() || lhs.IsNumber()) && rhs.IsNumber()) {
|
||||||
|
if (static_cast<double>(rhs) == 0)
|
||||||
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Right-hand side argument for operator % is 0."));
|
||||||
|
|
||||||
|
return static_cast<int>(lhs) % static_cast<int>(rhs);
|
||||||
|
} else
|
||||||
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Operator % cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
|
||||||
|
}
|
||||||
|
|
||||||
|
Value icinga::operator%(const Value& lhs, double rhs)
|
||||||
|
{
|
||||||
|
return lhs % Value(rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
Value icinga::operator%(double lhs, const Value& rhs)
|
||||||
|
{
|
||||||
|
return Value(lhs) % rhs;
|
||||||
|
}
|
||||||
|
|
||||||
|
Value icinga::operator%(const Value& lhs, int rhs)
|
||||||
|
{
|
||||||
|
return lhs % Value(rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
Value icinga::operator%(int lhs, const Value& rhs)
|
||||||
|
{
|
||||||
|
return Value(lhs) % rhs;
|
||||||
|
}
|
||||||
|
|
||||||
|
Value icinga::operator^(const Value& lhs, const Value& rhs)
|
||||||
|
{
|
||||||
|
if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
|
||||||
|
return static_cast<int>(lhs) ^ static_cast<int>(rhs);
|
||||||
|
else
|
||||||
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Operator & cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
|
||||||
|
}
|
||||||
|
|
||||||
|
Value icinga::operator^(const Value& lhs, double rhs)
|
||||||
|
{
|
||||||
|
return lhs & Value(rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
Value icinga::operator^(double lhs, const Value& rhs)
|
||||||
|
{
|
||||||
|
return Value(lhs) & rhs;
|
||||||
|
}
|
||||||
|
|
||||||
|
Value icinga::operator^(const Value& lhs, int rhs)
|
||||||
|
{
|
||||||
|
return lhs & Value(rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
Value icinga::operator^(int lhs, const Value& rhs)
|
||||||
|
{
|
||||||
|
return Value(lhs) & rhs;
|
||||||
|
}
|
||||||
|
|
||||||
Value icinga::operator&(const Value& lhs, const Value& rhs)
|
Value icinga::operator&(const Value& lhs, const Value& rhs)
|
||||||
{
|
{
|
||||||
if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
|
if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
|
||||||
|
@ -278,6 +278,18 @@ I2_BASE_API Value operator/(double lhs, const Value& rhs);
|
|||||||
I2_BASE_API Value operator/(const Value& lhs, int rhs);
|
I2_BASE_API Value operator/(const Value& lhs, int rhs);
|
||||||
I2_BASE_API Value operator/(int lhs, const Value& rhs);
|
I2_BASE_API Value operator/(int lhs, const Value& rhs);
|
||||||
|
|
||||||
|
I2_BASE_API Value operator%(const Value& lhs, const Value& rhs);
|
||||||
|
I2_BASE_API Value operator%(const Value& lhs, double rhs);
|
||||||
|
I2_BASE_API Value operator%(double lhs, const Value& rhs);
|
||||||
|
I2_BASE_API Value operator%(const Value& lhs, int rhs);
|
||||||
|
I2_BASE_API Value operator%(int lhs, const Value& rhs);
|
||||||
|
|
||||||
|
I2_BASE_API Value operator^(const Value& lhs, const Value& rhs);
|
||||||
|
I2_BASE_API Value operator^(const Value& lhs, double rhs);
|
||||||
|
I2_BASE_API Value operator^(double lhs, const Value& rhs);
|
||||||
|
I2_BASE_API Value operator^(const Value& lhs, int rhs);
|
||||||
|
I2_BASE_API Value operator^(int lhs, const Value& rhs);
|
||||||
|
|
||||||
I2_BASE_API Value operator&(const Value& lhs, const Value& rhs);
|
I2_BASE_API Value operator&(const Value& lhs, const Value& rhs);
|
||||||
I2_BASE_API Value operator&(const Value& lhs, double rhs);
|
I2_BASE_API Value operator&(const Value& lhs, double rhs);
|
||||||
I2_BASE_API Value operator&(double lhs, const Value& rhs);
|
I2_BASE_API Value operator&(double lhs, const Value& rhs);
|
||||||
|
@ -159,12 +159,12 @@ Value DivideExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
|||||||
|
|
||||||
Value ModuloExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
Value ModuloExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
||||||
{
|
{
|
||||||
return (long)m_Operand1->Evaluate(frame) % (long)m_Operand2->Evaluate(frame);
|
return m_Operand1->Evaluate(frame) % m_Operand2->Evaluate(frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value XorExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
Value XorExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
||||||
{
|
{
|
||||||
return (long)m_Operand1->Evaluate(frame) ^ (long)m_Operand2->Evaluate(frame);
|
return m_Operand1->Evaluate(frame) ^ m_Operand2->Evaluate(frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value BinaryAndExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
Value BinaryAndExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
|
||||||
|
Loading…
x
Reference in New Issue
Block a user