fix: bound strtod() scan to the input length - #36
Open
SweetSophia wants to merge 2 commits into
Open
SweetSophia wants to merge 2 commits into
SweetSophia wants to merge 2 commits into
Conversation
The scan loop had no upper bound. Once i exceeds s.length, substring
clamps to the full string, so a complete number never becomes NaN and
the loop never exits. Empty and whitespace-only inputs hang the same
way because Number('') is 0.
Keep the existing parse behavior; only terminate the prefix walk.
strtod() remainder is empty when a number reaches end-of-input.
buffer.indexOf('') always returns 0, so parseValue rewound to the
start of the buffer and could loop. Derive outEndPos from the
slice length minus remainder length instead.
There was a problem hiding this comment.
🟢 Approval recommended
The changes directly address confirmed infinite-loop and parse-position rewind bugs with minimal, localized risk and consistent behavior for existing callers.
Pull request overview
Fixes a JSON parsing hang by ensuring numeric scanning and parse position advancement are correctly bounded at end-of-input. This improves reliability of CubismJson.parseValue when parsing numbers at EOF or with trailing whitespace.
Changes:
- Bound
strtod()’s incremental scan loop tos.lengthto prevent non-terminating scans on valid EOF numbers or empty input. - Updated
CubismJson.parseValueto computeoutEndPosfrom the consumed numeric length (instead ofindexOf), preventing parse-position rewind when the remainder is''.
File summaries
| File | Description |
|---|---|
| src/live2dcubismframework.ts | Prevents infinite loops in strtod() by bounding the scan to the input length. |
| src/utils/cubismjson.ts | Fixes numeric outEndPos calculation to advance correctly through end-of-input cases. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
Bug
strtod()insrc/live2dcubismframework.tsscanned with an unbounded loop:Termination depends on
Number(s.substring(0, i))becomingNaN. Onceiexceedss.length,substringclamps to the full string. A complete valid number never producesNaN, so the loop never exits.Number("") === 0, so empty / whitespace-only input hangs the same way.Caller:
CubismJson.parseValueinsrc/utils/cubismjson.ts. Typical JSON with a trailing}often survives; a number at EOF, a numeric document, or trailing whitespace after a number does not.A second hang remained after bounding the scan:
parseValuederivedoutEndPoswithbuffer.indexOf(afterString[0]). Whenstrtodconsumes through end-of-input the remainder is'', andindexOf('')is always0, so parse position rewound.Change
for (let i = 1; i <= s.length; i++).outEndPosfrom consumed length instead ofindexOf:Parse /
endPtrbehavior is otherwise unchanged.Repro (Node)
Unbounded
strtod('123', [''])never returns. Same for"1.5","1e10","-3.2","1E-4",""," 5 ". Inputs with a terminator ("123}","123,","123abc") already returned.After the bound-only change, EOF numbers still rewound via
indexOf(''). Consumed-lengthoutEndPosadvances to the end of the slice.Checks
tsc --noEmit: pre-existingLive2DCubismCoreerrors ondevelop(Core is not in this repo). No new errors from this change.Ikati tracking: https://github.com/SweetSophia/ikati/issues/27
Fork review: SweetSophia#1