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) · netty/netty@cdbb241

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
+6-3Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,14 +1386,17 @@ 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+
* string reconstructed from the Latin-1 byte content for later use in {@link #toString()}.
1391+
* This means the cached string is guaranteed to match the Latin-1 byte content exactly,
1392+
* even for inputs containing non-Latin-1 characters (the constructor's {@link #c2b(char)}
1393+
* converts those to {@code '?'}).
13911394
* Used for the constants (which already stored in the JVM's string table) and in cases
13921395
* where the guaranteed use of the {@link #toString()} method.
13931396
*/
13941397
public static AsciiString cached(String string) {
13951398
AsciiString asciiString = new AsciiString(string);
1396-
asciiString.string = string;
1399+
asciiString.string = asciiString.toString(0);
13971400
return asciiString;
13981401
}
13991402

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
+59Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,4 +552,63 @@ 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 work unchanged
559+
String ascii = "hello";
560+
AsciiString cached = AsciiString.cached(ascii);
561+
assertEquals(ascii, cached.toString());
562+
// String is re-derived from byte array during sanitization, so identity may differ
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 work unchanged
570+
String latin1 = "h" + (char) 233 + "llo"; // héllo
571+
AsciiString cached = AsciiString.cached(latin1);
572+
assertEquals(latin1, cached.toString());
573+
}
574+
575+
@Test
576+
public void testCachedSanitizesNonLatin1String() {
577+
// Chars > 255 should be sanitized to '?' in the cached string to match the byte content
578+
String nonLatin1 = "test" + (char) 0x1234 + "ing";
579+
AsciiString cached = AsciiString.cached(nonLatin1);
580+
// The char 0x1234 gets converted to '?' by c2b, so toString should reflect that
581+
assertEquals("test?ing", cached.toString());
582+
}
583+
584+
@Test
585+
public void testCachedEmptyString() {
586+
AsciiString cached = AsciiString.cached("");
587+
assertEquals("", cached.toString());
588+
assertTrue(cached.isEmpty());
589+
}
590+
591+
@Test
592+
public void testCachedStringMatchesByteContent() {
593+
// The cached string should always match the byte content round-trip
594+
String nonLatin1 = "a" + (char) 0x4321 + "b";
595+
AsciiString cached = AsciiString.cached(nonLatin1);
596+
// Manually compute the expected sanitized string from the byte array
597+
StringBuilder expected = new StringBuilder();
598+
for (byte b : cached.toByteArray()) {
599+
expected.append((char) (b & 0xFF));
600+
}
601+
assertEquals(expected.toString(), cached.toString());
602+
}
603+
604+
@Test
605+
public void testCachedWithAllAsciiConstants() {
606+
// Constants used in the codebase should be unaffected
607+
AsciiString host = AsciiString.cached("host");
608+
assertEquals("host", host.toString());
609+
AsciiString method = AsciiString.cached(":method");
610+
assertEquals(":method", method.toString());
611+
AsciiString status = AsciiString.cached(":status");
612+
assertEquals(":status", status.toString());
613+
}
555614
}

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.