chatformat: reject a non-positive split limit instead of hanging - #64
Merged
Merged
Conversation
split() with limit<=0 cut at 0 (or -1), appended text[:0] forever and never shortened text, growing a list until the process died. A two-line guard raises ValueError at the door; regression tests cover limit=0, limit=-5 and the limit=1 boundary.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What & why
Fixes #60.
chatformat.split(text, limit)never finishes whenlimit <= 0: withlimit=0every cut is0, sotext[:0]is appended andtextnever gets shorter — an infinite loop growing a list for ever. With a negative limit the same arithmetic lands oncut = -1,text[-1:]is re-appended forever.Today's callers pass sensible limits, so the bug is latent — but
splitis a pure function and the fix is a two-line guard. I chose to raiseValueErrorrather than clamp to 1: a non-positive limit is a bug in the caller, and silently treating0as "one message, unbounded" would hide it while still producing wrong output.Checklist
SplitLimitTestsintests/test_chatformat.py:limit=0andlimit=-5raise,limit=1still splits)taskuary/chatformat.py(+5 lines) and its testpython -m pytest -qpasses (offline, no credentials)Test evidence
split('abc', 0)andsplit('abc', -5)(killed by timeout); new code raisesValueErrorimmediately in both cases, andsplit('abc', 5)still returns['abc'].tests/test_chatformat.py: 26 passed (including the 2 new tests).test_audit_fixescharset decode,test_coretwo-digit-fraction stamp,test_imapmailSentAt) fail identically on cleanHEADwithout this change (verified by stashing); they are Python 3.14 codec/fromisoformatbehavior differences. CI runs Python 3.10/3.12 where these pass.Risk
None expected: the guard only rejects
limit < 1; the only caller (blocks) passesmax(200, ...) >= 200. No formatting changes, no unrelated edits.