Harden strings unit and low-level memory operations against edge cases.
strings.pas:
Guard all loop bounds against uint32 underflow (0-1 wrapping)
Add nil check in stringSize
Clamp out-of-bounds parameters in stringTrim and stringSub
Fix stringReplace: use new stringMatchAt helper for substring matching instead of stringEquals (which compared full remaining string), rewrite as prefix/replacement/suffix memcpy
Fix stringIndexOf/stringContains: use stringMatchAt, handle empty-string and length edge cases
Rewrite intToString to build digits right-to-left without temp buffer
Guard hexStringToInt against empty input
Add stringMatchAt helper function
Add comprehensive UnitTest procedure covering all string functions with edge cases (empty strings, boundary values, uint32 max)
Assert/PrintSummary test harness with syslog output
util.pas:
Guard memset and memcpy against size=0 (prevents uint32 underflow in 0-1 loop bound causing 4GB iteration on bare metal)
kernel.pas:
Call strings.UnitTest on boot
3 files changed, ~400 insertions, ~105 deletions
Harden strings unit and low-level memory operations against edge cases.
**strings.pas:**
- Guard all loop bounds against uint32 underflow (0-1 wrapping)
- Add nil check in stringSize
- Clamp out-of-bounds parameters in stringTrim and stringSub
- Fix stringReplace: use new stringMatchAt helper for substring matching instead of stringEquals (which compared full remaining string), rewrite as prefix/replacement/suffix memcpy
- Fix stringIndexOf/stringContains: use stringMatchAt, handle empty-string and length edge cases
- Rewrite intToString to build digits right-to-left without temp buffer
- Guard hexStringToInt against empty input
- Add stringMatchAt helper function
- Add comprehensive UnitTest procedure covering all string functions with edge cases (empty strings, boundary values, uint32 max)
- Assert/PrintSummary test harness with syslog output
**util.pas:**
- Guard memset and memcpy against size=0 (prevents uint32 underflow in 0-1 loop bound causing 4GB iteration on bare metal)
**kernel.pas:**
- Call strings.UnitTest on boot
3 files changed, ~400 insertions, ~105 deletions
When size=0 was passed to memset() or memcpy(), the loop bound
`0 to size-1` underflowed to $FFFFFFFF, iterating ~4 billion times
and causing an immediate page fault.
Added `if size = 0 then exit` guards to both memset() and memcpy()
in util.pas.
Also guarded all `for i:=0 to X-1` loops in strings.pas where X
could be 0 on empty string inputs:
- stringToUpper: wrap loop in `if stringSize > 0`
- stringToLower: wrap loop in `if stringSize > 0`
- stringEquals: early exit when both sizes are 0
- stringToInt: early exit when string is empty
Additionally fixed all previously identified bugs in strings.pas:
- hexStringToInt: guard against empty string underflow
- stringConcat: cache stringSize calls to avoid redundant O(n) scans
- stringTrim: clamp length to source string size
- stringSub: bounds check start/size against source length
- stringReplace: complete rewrite - was using stringEquals() for
substring matching (only matched at string end), dropped prefix
chars, had dangling assignment overwriting valid result with
uninitialized pointer, and had unused variable
- stringIndexOf: rewritten to use new stringMatchAt() helper for
correct substring matching
- stringContains: simplified to delegate to fixed stringIndexOf(),
also fixing uint32 underflow when sub was empty
- stringToInt: removed always-true `v >= 0` check on uint32
- intToString: implemented (was a stub returning ' ')
- Added stringMatchAt() helper for prefix comparison
- Added comprehensive UnitTest procedure with 77 assertions
including adversarial empty/nil/boundary inputs
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Harden strings unit and low-level memory operations against edge cases.
strings.pas:
util.pas:
kernel.pas:
3 files changed, ~400 insertions, ~105 deletions