Skip to content

Commit fada3ef

Browse files
committed
prevent useless exception catcking
1 parent be4ba1f commit fada3ef

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

include/behaviortree_cpp/scripting/operators.hpp

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -157,40 +157,49 @@ struct ExprBinaryArithmetic : ExprBase
157157
return Any(lv / rv);
158158
default: {}
159159
}
160-
try {
161-
int64_t li = lhs_v.cast<int64_t>();
162-
int64_t ri = rhs_v.cast<int64_t>();
163-
switch (op)
164-
{
165-
case bit_and:
160+
161+
if(op == bit_and || op == bit_or || op == bit_xor)
162+
{
163+
try {
164+
int64_t li = lhs_v.cast<int64_t>();
165+
int64_t ri = rhs_v.cast<int64_t>();
166+
switch (op)
167+
{
168+
case bit_and:
166169
return Any(static_cast<double>(li & ri));
167-
case bit_or:
170+
case bit_or:
168171
return Any(static_cast<double>(li | ri));
169-
case bit_xor:
172+
case bit_xor:
170173
return Any(static_cast<double>(li ^ ri));
171-
default: {}
174+
default: {}
175+
}
176+
}
177+
catch(...)
178+
{
179+
throw std::runtime_error("Binary operators are not allowed if "
180+
"one of the operands is not an integer");
172181
}
173-
}
174-
catch(...)
175-
{
176-
throw std::runtime_error("Binary operators are not allowed if one of the operands is an integer");
177182
}
178183

179-
try {
180-
auto lb = lhs_v.cast<bool>();
181-
auto rb = rhs_v.cast<bool>();
182-
switch (op)
183-
{
184-
case logic_or:
184+
if(op == logic_or || op == logic_and)
185+
{
186+
try {
187+
auto lb = lhs_v.cast<bool>();
188+
auto rb = rhs_v.cast<bool>();
189+
switch (op)
190+
{
191+
case logic_or:
185192
return Any(static_cast<double>(lb || rb));
186-
case logic_and:
193+
case logic_and:
187194
return Any(static_cast<double>(lb && rb));
188-
default: {}
195+
default: {}
196+
}
197+
}
198+
catch(...)
199+
{
200+
throw std::runtime_error("Logic operators are not allowed if "
201+
"one of the operands is not castable to bool");
189202
}
190-
}
191-
catch(...)
192-
{
193-
throw std::runtime_error("Logic operators are not allowed if one of the operands is not castable to bool");
194203
}
195204
}
196205
else if(rhs_v.isType<SimpleString>() && lhs_v.isType<SimpleString>() && op == plus)

0 commit comments

Comments
 (0)