Skip to content

AsciiString.cached(String) should sanitize the provided String (#13749) (#17007) - #17075

Merged
normanmaurer merged 1 commit into
4.1from
ascii41
Jul 9, 2026
Merged

normanmaurer merged 1 commit into
4.1from
ascii41

Conversation

@normanmaurer

Copy link
Copy Markdown
Member

Motivation

AsciiString.cached(String) stored the original String reference
directly in asciiString.string, bypassing the c2b() Latin-1 byte
conversion performed in the constructor. When strings contained
non-Latin-1 characters (>255), the byte array would get '?' in those
positions (via c2b truncation), but toString() still returned the
unsanitized original. This broke the invariant that the cached string
matches the Latin-1 byte content exactly, preventing use of the cached
string for fast equality comparisons (e.g., String::contentEquals with
vectorized JVM intrinsics).

Modification

In AsciiString.cached(String), the method now first sets the cached
string to the original input (preserving identity for fast equals()
checks), then scans the input for non-Latin-1 characters. If any are
found, the cached string is reconstructed from the byte array via
toString(0) to ensure consistency with the Latin-1 byte content. The
Javadoc was updated to document this behavior.

Added 6 test methods (AsciiStringCharacterTest.java) covering pure
ASCII, Latin-1, non-Latin-1 sanitization, empty strings, byte-content
round-trip, and real-world constant preservation. Tests for pure ASCII
and Latin-1 strings assert that the original String identity is
preserved (assertSame).

Result

For pure ASCII/Latin-1 inputs, the original String identity is preserved
(enabling faster equality checks for interned constants).
For non-Latin-1 inputs, the cached string is sanitized to match the
Latin-1 byte content.

Closes #13749

Co-authored-by: Norman Maurer [email protected]

… (#17007)

## Motivation

`AsciiString.cached(String)` stored the original String reference
directly in `asciiString.string`, bypassing the `c2b()` Latin-1 byte
conversion performed in the constructor. When strings contained
non-Latin-1 characters (>255), the byte array would get `'?'` in those
positions (via `c2b` truncation), but `toString()` still returned the
unsanitized original. This broke the invariant that the cached string
matches the Latin-1 byte content exactly, preventing use of the cached
string for fast equality comparisons (e.g., `String::contentEquals` with
vectorized JVM intrinsics).

## Modification

In `AsciiString.cached(String)`, the method now first sets the cached
string to the original input (preserving identity for fast `equals()`
checks), then scans the input for non-Latin-1 characters. If any are
found, the cached string is reconstructed from the byte array via
`toString(0)` to ensure consistency with the Latin-1 byte content. The
Javadoc was updated to document this behavior.

Added 6 test methods (`AsciiStringCharacterTest.java`) covering pure
ASCII, Latin-1, non-Latin-1 sanitization, empty strings, byte-content
round-trip, and real-world constant preservation. Tests for pure ASCII
and Latin-1 strings assert that the original String identity is
preserved (`assertSame`).

## Result

For pure ASCII/Latin-1 inputs, the original String identity is preserved
(enabling faster equality checks for interned constants).
For non-Latin-1 inputs, the cached string is sanitized to match the
Latin-1 byte content.

Closes #13749

Co-authored-by: Norman Maurer <[email protected]>
@normanmaurer
normanmaurer merged commit 24c2e38 into 4.1 Jul 9, 2026
19 checks passed
@normanmaurer
normanmaurer deleted the ascii41 branch July 9, 2026 19:42
lltopk pushed a commit to lltopk/netty that referenced this pull request Oct 24, 2026
…#13749) (netty#17007) (netty#17075)

## Motivation

`AsciiString.cached(String)` stored the original String reference
directly in `asciiString.string`, bypassing the `c2b()` Latin-1 byte
conversion performed in the constructor. When strings contained
non-Latin-1 characters (>255), the byte array would get `'?'` in those
positions (via `c2b` truncation), but `toString()` still returned the
unsanitized original. This broke the invariant that the cached string
matches the Latin-1 byte content exactly, preventing use of the cached
string for fast equality comparisons (e.g., `String::contentEquals` with
vectorized JVM intrinsics).

## Modification

In `AsciiString.cached(String)`, the method now first sets the cached
string to the original input (preserving identity for fast `equals()`
checks), then scans the input for non-Latin-1 characters. If any are
found, the cached string is reconstructed from the byte array via
`toString(0)` to ensure consistency with the Latin-1 byte content. The
Javadoc was updated to document this behavior.

Added 6 test methods (`AsciiStringCharacterTest.java`) covering pure
ASCII, Latin-1, non-Latin-1 sanitization, empty strings, byte-content
round-trip, and real-world constant preservation. Tests for pure ASCII
and Latin-1 strings assert that the original String identity is
preserved (`assertSame`).

## Result

For pure ASCII/Latin-1 inputs, the original String identity is preserved
(enabling faster equality checks for interned constants).
For non-Latin-1 inputs, the cached string is sanitized to match the
Latin-1 byte content.

Closes netty#13749

Co-authored-by: Norman Maurer <[email protected]>

Co-authored-by: Vasily Pelikh <[email protected]>
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.

2 participants