sCalc: fix out-of-bounds read in lrc() on an empty operand#39
Open
physwkim wants to merge 1 commit into
Open
Conversation
lrc() looped with `i < strlen(rawInput)-1`. strlen() returns size_t,
so for an empty operand strlen(rawInput)-1 wraps to SIZE_MAX and the
loop condition is always true, reading rawInput[] far out of bounds.
The LRC and AMODBUS operators reach lrc() with any string on the stack
(sCalcPerform.c case LRC/AMODBUS), so an scalcout expression such as
LRC('') or LRC applied to an empty field crashes the IOC with SIGSEGV.
Compute the length once into a signed int and loop while `i+1 < len`,
so an empty operand yields an empty loop (LRC "00") and non-empty
operands are unchanged. The sibling xor8() already guards len==0.
Add scalcTest cases: LRC('') -> "00" and LRC('F7031389000A') -> "60"
(the LRC 0x60 from the AMODBUS example in the source comment). Without
the fix scalcTest terminates with SIGSEGV on the empty-operand case.
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
lrc()insCalcPerform.cloops withstrlen()returnssize_t(unsigned). For an empty operand,strlen(rawInput)-1wraps toSIZE_MAX, soi < SIZE_MAXis alwaystrue and the loop reads
rawInput[]far out of bounds.The
LRCandAMODBUSoperators calllrc()with whatever string ison the stack (
case LRC:/case AMODBUS:insCalcPerform.c), so anscalcout expression like
LRC('')— orLRCapplied to an emptystring field — crashes the IOC with SIGSEGV.
Fix
Compute the length once into a signed
intand loop whilei+1 < len.An empty operand now yields an empty loop (LRC
"00"); non-emptyoperands are unchanged. The sibling
xor8()already guardslen == 0;this brings
lrc()in line.Test
Adds two
scalcTestcases:LRC('')→"00"LRC('F7031389000A')→"60"(the LRC0x60from the AMODBUSworked example in the source comment)
Without the fix
scalcTestterminates with SIGSEGV on the empty-operandcase; with the fix the full suite passes (145/145).