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 (#1374… · netty/netty@ef10972

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • common/src
    • main/java/io/netty/util
      • AsciiString.java
    • test/java/io/netty/util
      • AsciiStringCharacterTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎common/src/main/java/io/netty/util/AsciiString.java‎

Copy file name to clipboardExpand all lines: common/src/main/java/io/netty/util/AsciiString.java
+15-2Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,14 +1386,27 @@ public static AsciiString of(CharSequence string) {
13861386
}
13871387

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

Collapse file

‎common/src/test/java/io/netty/util/AsciiStringCharacterTest.java‎

Copy file name to clipboardExpand all lines: common/src/test/java/io/netty/util/AsciiStringCharacterTest.java
+62Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,4 +552,66 @@ public void testRegionMatchesAsciiHandlesOutOfBounds() {
552552
assertFalse(AsciiString.regionMatchesAscii(str, false, -1, hello, 0, 5));
553553
assertFalse(AsciiString.regionMatchesAscii(str, false, 0, hello, -1, 5));
554554
}
555+
556+
@Test
557+
public void testCachedWithAsciiString() {
558+
// Pure ASCII strings should reuse the original string to preserve identity
559+
String ascii = "hello";
560+
AsciiString cached = AsciiString.cached(ascii);
561+
assertEquals(ascii, cached.toString());
562+
assertSame(ascii, cached.toString());
563+
assertEquals(ascii.length(), cached.length());
564+
assertTrue(cached.contentEquals(ascii));
565+
}
566+
567+
@Test
568+
public void testCachedWithAsciiLatin1String() {
569+
// Latin-1 strings (chars 128-255) should reuse the original string to preserve identity
570+
String latin1 = "h" + (char) 233 + "llo"; // héllo
571+
AsciiString cached = AsciiString.cached(latin1);
572+
assertEquals(latin1, cached.toString());
573+
assertSame(latin1, cached.toString());
574+
assertEquals(latin1.length(), cached.length());
575+
assertTrue(cached.contentEquals(latin1));
576+
}
577+
578+
@Test
579+
public void testCachedSanitizesNonLatin1String() {
580+
// Chars > 255 should be sanitized to '?' in the cached string to match the byte content
581+
String nonLatin1 = "test" + (char) 0x1234 + "ing";
582+
AsciiString cached = AsciiString.cached(nonLatin1);
583+
// The char 0x1234 gets converted to '?' by c2b, so toString should reflect that
584+
assertEquals("test?ing", cached.toString());
585+
}
586+
587+
@Test
588+
public void testCachedEmptyString() {
589+
AsciiString cached = AsciiString.cached("");
590+
assertEquals("", cached.toString());
591+
assertTrue(cached.isEmpty());
592+
}
593+
594+
@Test
595+
public void testCachedStringMatchesByteContent() {
596+
// The cached string should always match the byte content round-trip
597+
String nonLatin1 = "a" + (char) 0x4321 + "b";
598+
AsciiString cached = AsciiString.cached(nonLatin1);
599+
// Manually compute the expected sanitized string from the byte array
600+
StringBuilder expected = new StringBuilder();
601+
for (byte b : cached.toByteArray()) {
602+
expected.append((char) (b & 0xFF));
603+
}
604+
assertEquals(expected.toString(), cached.toString());
605+
}
606+
607+
@Test
608+
public void testCachedWithAllAsciiConstants() {
609+
// Constants used in the codebase should be unaffected
610+
AsciiString host = AsciiString.cached("host");
611+
assertEquals("host", host.toString());
612+
AsciiString method = AsciiString.cached(":method");
613+
assertEquals(":method", method.toString());
614+
AsciiString status = AsciiString.cached(":status");
615+
assertEquals(":status", status.toString());
616+
}
555617
}

0 commit comments

Comments
0 (0)
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.