Skip to content

Commit cfbc13f

Browse files
committed
Add validity check around check eval
so the script doesn't crash on undefined tlm
1 parent f2cdd8a commit cfbc13f

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

openc3/lib/openc3/script/api_shared.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,18 @@ def _check_eval(target_name, packet_name, item_name, comparison_to_eval, value)
864864
# Note: We have to preserve the original 'value' variable because we're going to eval against it
865865
value_str = value.is_a?(String) ? "'#{value}'" : value
866866
with_value = "with value == #{value_str}"
867-
if eval(string)
867+
868+
eval_is_valid = _check_eval_validity(value, comparison_to_eval)
869+
unless eval_is_valid
870+
message = "Invalid comparison for types"
871+
if $disconnect
872+
puts "ERROR: #{message}"
873+
else
874+
raise CheckError, message
875+
end
876+
end
877+
878+
if eval_is_valid && eval(string)
868879
puts "#{check_str} success #{with_value}"
869880
else
870881
message = "#{check_str} failed #{with_value}"
@@ -883,5 +894,17 @@ def _check_eval(target_name, packet_name, item_name, comparison_to_eval, value)
883894
raise e
884895
end
885896
end
897+
898+
def _check_eval_validity(value, comparison)
899+
return true if comparison.nil? || comparison.empty?
900+
901+
operator, operand = extract_operator_and_operand_from_comparison(comparison)
902+
903+
if [">=", "<=", ">", "<"].include?(operator)
904+
return false if value.nil? || operand.nil? || value.is_a?(Array) || operand.is_a?(Array)
905+
end
906+
907+
return true
908+
end
886909
end
887910
end

openc3/python/openc3/api/api_shared.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from openc3.utilities.extract import (
2020
extract_fields_from_check_text,
2121
extract_fields_from_tlm_text,
22+
extract_operator_and_operand_from_comparison,
2223
)
2324

2425

@@ -965,8 +966,12 @@ def _check_eval(target_name, packet_name, item_name, comparison_to_eval, value):
965966
else:
966967
value_str = value
967968
with_value = f"with value == {value_str}"
969+
970+
eval_is_valid = _check_eval_validity(value, comaparison_to_eval)
971+
if not eval_is_valid:
972+
raise CheckError("ERROR: Invalid comparison for types")
968973
try:
969-
if eval(string):
974+
if eval_is_valid and eval(string):
970975
print(f"{check_str} success {with_value}")
971976
else:
972977
message = f"{check_str} failed {with_value}"
@@ -986,6 +991,25 @@ def _frange(value):
986991
return value
987992

988993

994+
def _check_eval_validity(value, comparison):
995+
if not comparison:
996+
return True
997+
998+
operator, operand = extract_operator_and_operand_from_comparison(comparison)
999+
1000+
if operator in [">=", "<=", ">", "<"]:
1001+
if value is None or operand is None or isinstance(value, list) or isinstance(operand, list):
1002+
return False
1003+
1004+
if operator == "in": # Ruby doesn't have this operator
1005+
if isinstance(operand, str) and not isinstance(value, str):
1006+
return False
1007+
elif not isinstance(operand, list):
1008+
return False
1009+
1010+
return True
1011+
1012+
9891013
# Interesting formatter to a specific number of significant digits:
9901014
# https://stackoverflow.com/questions/3410976/how-to-round-a-number-to-significant-figures-in-python?rq=3
9911015
# def format(value, sigfigs=9):

openc3/python/openc3/script/api_shared.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from openc3.utilities.extract import (
2020
extract_fields_from_check_text,
2121
extract_fields_from_tlm_text,
22+
extract_operator_and_operand_from_comparison,
2223
)
2324

2425
from .exceptions import CheckError
@@ -1046,8 +1047,16 @@ def _check_eval(target_name, packet_name, item_name, comparison_to_eval, value):
10461047
else:
10471048
value_str = value
10481049
with_value = f"with value == {value_str}"
1050+
1051+
eval_is_valid = _check_eval_validity(value, comparison_to_eval)
1052+
if not eval_is_valid:
1053+
message = "Invalid comparison for types"
1054+
if openc3.script.DISCONNECT:
1055+
print(f"ERROR: {message}")
1056+
else:
1057+
raise CheckError(message)
10491058
try:
1050-
if eval(string):
1059+
if eval_is_valid and eval(string):
10511060
print(f"{check_str} success {with_value}")
10521061
else:
10531062
message = f"{check_str} failed {with_value}"
@@ -1070,6 +1079,25 @@ def _frange(value):
10701079
return value
10711080

10721081

1082+
def _check_eval_validity(value, comparison):
1083+
if not comparison:
1084+
return True
1085+
1086+
operator, operand = extract_operator_and_operand_from_comparison(comparison)
1087+
1088+
if operator in [">=", "<=", ">", "<"]:
1089+
if value is None or operand is None or isinstance(value, list) or isinstance(operand, list):
1090+
return False
1091+
1092+
if operator == "in": # Ruby doesn't have this operator
1093+
if isinstance(operand, str) and not isinstance(value, str):
1094+
return False
1095+
elif not isinstance(operand, list):
1096+
return False
1097+
1098+
return True
1099+
1100+
10731101
# Interesting formatter to a specific number of significant digits:
10741102
# https://stackoverflow.com/questions/3410976/how-to-round-a-number-to-significant-figures-in-python?rq=3
10751103
# def format(value, sigfigs=9):

0 commit comments

Comments
 (0)