Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent crash when comparing ill-typed numeric types. #2949

Merged
merged 1 commit into from
Oct 23, 2024

Conversation

ashleysommer
Copy link
Contributor

This fixes comparisons that look like:

if Literal("1234", datatype="xsd:integer") > Literal("hello", datatype="xsd:integer")

Both of the dataypes are in the _NUMERIC_LITERAL_TYPES list, so they enter the fast-path comparison.
The problem is the second literal is not a valid integer, so it has the value None.
That throws a comparison error that is uncaught by RDFLib, causing the application to crash.

If this seems like something that is pretty common and should be picked up sooner, I think its because this error only occurs when rdflib.NORMALIZE_LITERALS is False. When rdflib normalizes the literals, it fixes the value, so this does not happen. I found this when testing some PySHACL code sections which run with rdflib.NORMALIZE_LITERALS=False.

Luckily since rdflib introduced the concept of ill_typed literals, we can use that to restrict the fast-path to only those numeric literals that are not ill-typed.

@coveralls
Copy link

Coverage Status

coverage: 90.302%. remained the same
when pulling caba1c8 on compare_numeric
into 82c2421 on main.

@ashleysommer ashleysommer merged commit 14e8d7f into main Oct 23, 2024
22 checks passed
@ashleysommer ashleysommer deleted the compare_numeric branch October 23, 2024 21:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants