GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — AsciiString.cached(String) should sanitize the provided String (#13749) by vpelikh · Pull Request #17007 · netty/netty

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
normanmaurer merged 2 commits into netty:4.2 from vpelikh:GH-13749
Jul 8, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

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

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
7e8e98c
AsciiString.cached(String) should sanitize the provided String (#13749)
vpelikh Jun 28, 2026
ebf8ef0
Merge branch '4.2' into GH-13749
normanmaurer Jul 2, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

17 changes: 15 additions & 2 deletions 17 common/src/main/java/io/netty/util/AsciiString.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1386,14 +1386,27 @@ public static AsciiString of(CharSequence string) {
}

/**
* Returns an {@link AsciiString} containing the given string and retains/caches the input
* string for later use in {@link #toString()}.
* Returns an {@link AsciiString} containing the given string and retains/caches the
* input string for later use in {@link #toString()}.
* If the input contains only Latin-1 characters (0-255), the original string is reused
* to preserve identity for faster equality checks. Otherwise, the string is reconstructed
* from the Latin-1 byte content to guarantee consistency (the constructor's {@link #c2b(char)}
* converts non-Latin-1 characters to {@code '?'}).
* Used for the constants (which already stored in the JVM's string table) and in cases
* where the guaranteed use of the {@link #toString()} method.
*/
public static AsciiString cached(String string) {
AsciiString asciiString = new AsciiString(string);
asciiString.string = string;
// Check if the input contains any non-Latin-1 characters that would have been
// truncated to '?' by c2b() during construction. If so, reconstruct the cached
// string from the byte array to ensure consistency.
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) > MAX_CHAR_VALUE) {
asciiString.string = asciiString.toString(0);
break;
}
}
return asciiString;
}

Expand Down
62 changes: 62 additions & 0 deletions 62 common/src/test/java/io/netty/util/AsciiStringCharacterTest.java
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -552,4 +552,66 @@ public void testRegionMatchesAsciiHandlesOutOfBounds() {
assertFalse(AsciiString.regionMatchesAscii(str, false, -1, hello, 0, 5));
assertFalse(AsciiString.regionMatchesAscii(str, false, 0, hello, -1, 5));
}

@Test
public void testCachedWithAsciiString() {
// Pure ASCII strings should reuse the original string to preserve identity
String ascii = "hello";
AsciiString cached = AsciiString.cached(ascii);
assertEquals(ascii, cached.toString());
assertSame(ascii, cached.toString());
assertEquals(ascii.length(), cached.length());
assertTrue(cached.contentEquals(ascii));
}

@Test
public void testCachedWithAsciiLatin1String() {
// Latin-1 strings (chars 128-255) should reuse the original string to preserve identity
String latin1 = "h" + (char) 233 + "llo"; // héllo
AsciiString cached = AsciiString.cached(latin1);
assertEquals(latin1, cached.toString());
assertSame(latin1, cached.toString());
assertEquals(latin1.length(), cached.length());
assertTrue(cached.contentEquals(latin1));
}

@Test
public void testCachedSanitizesNonLatin1String() {
// Chars > 255 should be sanitized to '?' in the cached string to match the byte content
String nonLatin1 = "test" + (char) 0x1234 + "ing";
AsciiString cached = AsciiString.cached(nonLatin1);
// The char 0x1234 gets converted to '?' by c2b, so toString should reflect that
assertEquals("test?ing", cached.toString());
}

@Test
public void testCachedEmptyString() {
AsciiString cached = AsciiString.cached("");
assertEquals("", cached.toString());
assertTrue(cached.isEmpty());
}

@Test
public void testCachedStringMatchesByteContent() {
// The cached string should always match the byte content round-trip
String nonLatin1 = "a" + (char) 0x4321 + "b";
AsciiString cached = AsciiString.cached(nonLatin1);
// Manually compute the expected sanitized string from the byte array
StringBuilder expected = new StringBuilder();
for (byte b : cached.toByteArray()) {
expected.append((char) (b & 0xFF));
}
assertEquals(expected.toString(), cached.toString());
}

@Test
public void testCachedWithAllAsciiConstants() {
// Constants used in the codebase should be unaffected
AsciiString host = AsciiString.cached("host");
assertEquals("host", host.toString());
AsciiString method = AsciiString.cached(":method");
assertEquals(":method", method.toString());
AsciiString status = AsciiString.cached(":status");
assertEquals(":status", status.toString());
}
}
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.