Skip to content

Centralize overflow-safe layout sizing and reserve validation#688

Open
jmestwa-coder wants to merge 1 commit into
apache:masterfrom
jmestwa-coder:layout-size-overflow-guards
Open

Centralize overflow-safe layout sizing and reserve validation#688
jmestwa-coder wants to merge 1 commit into
apache:masterfrom
jmestwa-coder:layout-size-overflow-guards

Conversation

@jmestwa-coder
Copy link
Copy Markdown
Contributor

Summary

Centralize overflow-safe layout sizing and reservation logic across layout implementations.

Changes

  • Added shared internal helpers in layout_priv.h for:

    • saturating layout size doubling
    • guarded reserve sizing validation
  • Replaced unchecked sizing arithmetic in:

    • FMTLayout
    • HTMLLayout
    • JSONLayout
    • PatternLayout
    • XMLLayout
  • Replaced direct sizing operations such as:

expectedPatternLength * 2
output.reserve(a + b)

with centralized checked helpers.

Why

The previous layout sizing logic used unchecked size_t arithmetic during:

  • expected buffer growth calculation
  • reserve size computation

This change avoids invalid reserve calculations and overflow-prone sizing behavior while preserving existing formatting semantics.

Behavior

  • No formatting or output changes
  • No parser or layout behavior changes
  • Only affects preallocation sizing paths
  • Falls back to normal append growth when reservation would exceed valid bounds

Tests

Added deterministic coverage for:

  • saturation behavior
  • boundary arithmetic cases
  • successful reserve paths
  • overflow/skip reserve paths

Validated with focused jsonlayouttest coverage.

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.

1 participant