Skip to content

Commit

Permalink
Check input string before double conversion in non negative checks (#37)
Browse files Browse the repository at this point in the history
Signed-off-by: romanodanilo <[email protected]>
  • Loading branch information
romanodanilo authored Jul 29, 2024
1 parent fa6bf8d commit 9a7d059
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ def check_rule(checker_data: models.CheckerData) -> None:
continue

logging.debug(f"current_transition_time: {current_transition_time}")
if not utils.is_xsd_double(current_transition_time):
logging.error(
f"Cannot convert '{current_transition_time}' to double as it does not match xsd:double pattern. Skipping check..."
)
return

current_numeric_value = float(current_transition_time)
has_issue = current_numeric_value < 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ def check_rule(checker_data: models.CheckerData) -> None:
continue
current_duration = current_duration_param_value

if not utils.is_xsd_double(current_duration):
logging.error(
f"Cannot convert '{current_duration}' to double as it does not match xsd:double pattern. Skipping check..."
)
return

current_numeric_value = float(current_duration)
has_issue = current_numeric_value < 0

Expand Down
14 changes: 14 additions & 0 deletions qc_openscenario/checks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,17 @@ def get_attribute_type(attribute_value: str) -> models.AttributeType:
return models.AttributeType.PARAMETER

return models.AttributeType.VALUE


def is_xsd_double(input_str: str) -> bool:
"""Checks if input string follows xsd double specification
The pattern is built following xsd double definition from http://www.datypic.com/sc/xsd/t-xsd_double.html
Args:
input_str (str): The input string to check
Returns:
bool: True if the input string represent a valid xsd:double value. False otherwise
"""
pattern = re.compile(r"^([+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?|INF|-INF|NaN)$")
return pattern.match(input_str) is not None

0 comments on commit 9a7d059

Please sign in to comment.