Repository navigation
Conversation
Replace unbounded strtoll calls with length-bounded integer parsing and reject overflow and missing terminators. Check remaining packet length before reading list and dictionary terminators, reject malformed strings, and safely clean up partial containers. Add guard-page regression tests for truncated packets and integer limits.
There was a problem hiding this comment.
I think this fix is inappropriate: the proposed code re-implements parts of strtoll, ignoring some documented rules (arbitrary number of spaces before the digits, possible "+" sign, etc) which makes it misleading. Besides it should test the terminating 'e' character which when missing is a clue that something's wrong.
Instead of re-implementing strtoll with these additional checks, I suggest you still rely on strtoll with additional checks since the bug report is right in saying that it's possible to craft a packet without a proper terminating character (missing 'e' basically):
0 - call ret=strtoll() as originally done
1 - if endp==*data return false (detects empty numbers) See "man strtol"
2 - check errno (detects overflow/underflow)
3 - if ok, check that *endp == 'e' (this is specific to the encoding of ints in BitDHT). This has to be true no matter what. See bencode.c:458
4 - return ret.
Copy numeric fields into NUL-terminated buffers before calling strtoll. Reject missing digits, overflow, trailing junk, and missing delimiters while preserving whitespace and optional sign handling. Extend regression tests for parsing compatibility and packet bounds.
|
Please don't copy-paste what I'm saying to AI and copy-back the result. It's insulting for the person who's reviewing. |
Replace unbounded strtoll calls with length-bounded integer parsing and reject overflow and missing terminators.
Check remaining packet length before reading list and dictionary terminators, reject malformed strings, and safely clean up partial containers.
Add guard-page regression tests for truncated packets and integer limits.
code by: GPT 6 Astra light