Skip to content

fix: bound error message serialization - #309

Open
RealAdii wants to merge 1 commit into
vercel:mainfrom
RealAdii:fix/306-bound-error-serialization
Open

RealAdii wants to merge 1 commit into
vercel:mainfrom
RealAdii:fix/306-bound-error-serialization

Conversation

@RealAdii

Copy link
Copy Markdown

Fixes #306

Summary

When ms() or parse() rejects an invalid argument, building error messages directly via JSON.stringify(val) caused two issues:

  1. Arguments like sparse arrays (arr[20000000] = 0) or huge objects caused unbounded iteration and allocation, blocking the calling thread for seconds and creating massive error strings.
  2. Arguments with unsupported types such as BigInt or circular objects caused JSON.stringify to throw a TypeError first, masking the validation error.

Changes

  • Added a serializeValue helper that safely and deterministically formats values with bounds:
    • Caps serialized string length at 100 characters.
    • Limits array element traversal (up to 10 elements) and object keys (up to 10 keys).
    • Handles BigInt values (10n) without throwing.
    • Detects circular references ([Circular]) and bounds deep structures.
    • Catches any throwing accessors safely.
  • Added comprehensive unit tests in src/index.test.ts and src/parse.test.ts verifying fast non-blocking execution on sparse arrays, BigInt handling, circular structures, and bounds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unbounded error-message serialization in ms() allows a sparse-array argument to block the calling thread

1 participant