Repository navigation
format loaders: raise ParseError for input that cannot be loaded - #830
Poseidonas wants to merge 6 commits into
Conversation
fallenmi
left a comment
There was a problem hiding this comment.
The remaining unwrapped numeric conversions mean this does not yet establish the ParseError-only loader contract that #827 depends on.
On the current head (5002715), this syntactically valid KCD with an invalid input attribute still leaks a builtin exception:
from cantools.database.can.formats import kcd
text = '''<NetworkDefinition xmlns="http://kayak.2codeornot2code.org/1.0">
<Bus name="B"><Message id="0x1" name="M">
<Signal name="S" offset="0" length=""/>
</Message></Bus></NetworkDefinition>'''
kcd.load_string(text, strict=False)The result is ValueError: invalid literal for int() with base 10: '', not cantools.database.errors.ParseError. The same path has other direct int() conversions for signal offsets, label values, mux counts, message IDs/intervals, lengths, and baud rates. A syntactically valid SYM that references an undefined signal can likewise still escape as KeyError during _load_message.
Today the broad probe in load_string() happens to catch these. Once #827 is rebased to catch only ParseError, they will escape rather than be retained in UnsupportedDatabaseFormatError. Please normalize the remaining input-derived failures (with regression coverage for a post-tokenization/attribute-conversion case), or narrow the promised contract before #827 relies on it.
AI assistance disclosure: OpenAI Codex was used to inspect the diff and run the adversarial reproducer; the technical finding and review submission were authorized by the account owner.
|
Confirmed — reproduced the KCD case on 5002715 ( |
|
Done in 17ce3af. Every loader's |
fallenmi
left a comment
There was a problem hiding this comment.
Thanks — the original KCD and SYM post-tokenization cases are fixed on exact head 17ce3af, and the complete tests/test_database.py file passes here (201/201). One non-input boundary is still being converted by the new loader-wide decorator: sort_signals is a documented user callback, but its exceptions are now relabeled as parse failures.
Using tests/files/dbc/vehicle.dbc and a callback that raises a sentinel TypeError, direct dbc.load_string(text, sort_signals=callback) raises a new ParseError whose cause is the sentinel on this head. On previous head 5002715, the identical call raises the original sentinel object unchanged. I reproduced the same regression for callback-raised AttributeError, ValueError, and KeyError.
This means the narrowed probe planned in #827 would still classify unrelated user-code failures as unsupported database content, which is the same error-boundary problem #814 is trying to remove. Please limit conversion to input-derived loader failures (or otherwise let user callback failures escape unchanged) and add a sentinel callback regression alongside the new malformed-input matrix.
Follow-up review performed with OpenAI Codex assistance under the account owner authorization.
| ) | ||
|
|
||
|
|
||
| def raises_parse_error(func: Callable[_P, _T]) -> Callable[_P, _T]: |
There was a problem hiding this comment.
instead of introducing this decorator which does basically the same as load_string() (what's the point?), I would like to see load_string() to be modified: if it catches a ParseError, no message is produced and the next format is tried, if it catches a NotImplementedError, it prints the error message belonging to the exception and refrains from probing further formats, and on any other exception it should print the error message belonging to the exception (plus adding that this is a buglet in cantools that ought to be fixed) and continue probing.
There was a problem hiding this comment.
the task for this PR is then to ensure that no exceptions other than ParseError and NotImplementedError are raised by load_string() for the whole test suite.
17ce3af to
93c5477
Compare
|
Done as described: |
|
On the callback point: under the scheme andlaus asked for, an exception raised by a user callback such as |
93c5477 to
d817ac1
Compare
|
with #827 merged, I feel more confident about this. Since the PR has been opened, there has been a pretty extensive cleanup of the DBC-related code so it needs to be rebased on the latest |
d817ac1 to
80276a4
Compare
|
Rebased on master. Three things it needed, the first one worth noting: The cleanup added two checks for leftover
449 tests pass, ruff and mypy clean. Since |
80276a4 to
70366aa
Compare
|
Rebased on current master. Two things needed adapting, and one of them is worth a look before you spend time on the rest. The commit that adapted to the
I updated it to expect Given #872, the thing you probably want to know is whether this rejects anything that used to load. Loading every file under
|
Replace the export-only option with a database-owned comment, preserve scoped comments, and invalidate legacy cached database objects.
|
@Poseidonas: this one now has conflicts which need to be resolved. |
70366aa to
0e671ed
Compare
|
Rebased on current master. The one real conflict was in Testing #887 turned up two more cases that escaped as builtin exceptions, so I added them here:
Both now raise All files in |
0e671ed to
13421b2
Compare
|
Rebased on master with #887. If it makes the review easier, I can split this, for example the DBC changes from the other formats. |
Prerequisite for #827, split out as requested there.
The dbc, kcd, sym, arxml and cdd loaders now raise
cantools.database.errors.ParseErrorwhenever the input cannot be loaded, instead of a mix ofValueError,KeyError,textparser.ParseErrorandxml.etree.ElementTree.ParseError. Message text is unchanged, with two exceptions:num()informats/utils.pyhad an unreachable fallback, so a bad number now gets its intended message rather than Python'sfloat()one; and the three cddKeyErrors lose the quotesstr(KeyError)adds.NotImplementedErrorfor the unsupported ARXML direction inecu_extract_loader.pyis kept as is pending the question on #827; two converted sites insystem_loader.py("only AUTOSAR 3 and 4", "non-canonical references not yet supported") read as unsupported rather than malformed and can flip with it.load_string()indatabase/__init__.pyis not touched; #827 will be rebased on top of this.