Fix page fault when file size exceeds MaxFileSize - #9
Merged
Conversation
ReadBuffer was allocated at exactly IN.CFG.MaxFileSize bytes but its
contents are consumed as a C string by strtok_r in csvProcessData().
FIOWrap passes IN.PAR.len straight through to FileRead with no clamp,
so a file at or above MaxFileSize filled the buffer completely, leaving
no terminator. Parsing then ran off the end of the allocation and the
target took a page fault.
- csvInitBuffer(): allocate one extra byte so a completely full buffer
is still a valid string. MaxLength still holds the usable payload
length, so all existing overflow checks are unchanged.
- csvClearBuffer(): clear the guard byte as well.
- CSVFn_Cyclic(), CSV_ST_OPEN: compare FIOWrap.OUT.STAT.FileLen against
ReadBuffer.MaxLength before touching the data. An oversized file now
reports CSV_ERR_BUFFERFULL ("Maximum file size reached. Check
IN.CFG.MaxFileSize.") instead of parsing truncated data. On success,
record CurrentLength and terminate the buffer explicitly rather than
relying on leftover zeros from the last clear.
Verified by compiling the example project's CSVFileLib against AS6.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
Problem
Loading a CSV file at or above
IN.CFG.MaxFileSizepage-faults the target.ReadBufferis allocated at exactlyMaxFileSizebytes (csvInitBufferviaCSVFn_Init), but its contents are consumed as a C string —csvProcessData()runsstrtok_rover it. FIOWrap passesIN.PAR.lenstraight toFileReadwith no clamp and no truncation check:FileReadhas no "bytes actually read" output, so for a file >=MaxFileSizeit returnsDonewith the buffer 100% full and unterminated. Parsing then walks past the end of theTMP_allocblock.Smaller files worked only by accident — the tail of the buffer was still zeroed by
csvClearBuffer, so the bug stayed silent right up to the limit.The same one-byte hole existed on the write side:
csvAddToBufferaccepts data up to exactlyMaxLength, so acsvExpandVarfill landing on the limit leftReadBufferunterminated too.Fix
csvInitBuffer()— allocateBufferLength + 1.MaxLengthstill holds the usable payload size, socsvAddToBuffer's overflow check and everyMaxFileSizesemantic are unchanged; the extra byte can only ever hold the terminator. This also closes thecsvExpandVarvariant.csvClearBuffer()— clear the guard byte as well.CSVFn_Cyclic(),CSV_ST_OPEN— checkFIOWrap.OUT.STAT.FileLenagainstReadBuffer.MaxLengthbefore touching the data. Oversized files now reportCSV_ERR_BUFFERFULL— "Maximum file size reached. Check IN.CFG.MaxFileSize." — withErrorState = CSV_ST_OPEN, instead of parsing truncated data. On the success path,CurrentLengthis set from the real file length and the buffer is terminated explicitly rather than relying on leftover zeros.Behavior change
A file larger than
MaxFileSizeused to crash the target; it now raises a normal, actionable library error and the state machine goes toCSV_ST_ERROR. Files that fit are unaffected.Verification
Compiled
CSVFileLibthrough the example project with the AS6 toolchain — the library compiles and links clean. The threecsvOpenVar.c-Woverflow/-Wmaybe-uninitializedwarnings in the build log are pre-existing and untouched by this change.Note: the example project pins AR 6.6.2 / OpcUaCs 6.6.1, neither of which is installed on this machine, so the build was run against 6.7.6 / 6.7.0 locally. Those retargets were reverted and are not part of this PR.
🤖 Generated with Claude Code