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 — Make HTTP/2 frame hashCode consistent with equals (#16692) · netty/netty@13b6e99

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http2/src
    • main/java/io/netty/handler/codec/http2
      • AbstractHttp2StreamFrame.java
      • DefaultHttp2PingFrame.java
    • test/java/io/netty/handler/codec/http2
      • Http2DefaultFramesTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-http2/src/main/java/io/netty/handler/codec/http2/AbstractHttp2StreamFrame.java‎

Copy file name to clipboardExpand all lines: codec-http2/src/main/java/io/netty/handler/codec/http2/AbstractHttp2StreamFrame.java
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ public boolean equals(Object o) {
4848
@Override
4949
public int hashCode() {
5050
Http2FrameStream stream = this.stream;
51+
// Must be consistent with equals; super.hashCode() is Object's identity hash.
5152
if (stream == null) {
52-
return super.hashCode();
53+
return 0;
5354
}
5455
return stream.hashCode();
5556
}
Collapse file

‎codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2PingFrame.java‎

Copy file name to clipboardExpand all lines: codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2PingFrame.java
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ public boolean equals(Object o) {
6161

6262
@Override
6363
public int hashCode() {
64-
int hash = super.hashCode();
64+
// Must be consistent with equals; super.hashCode() is Object's identity hash.
65+
int hash = (int) (content ^ content >>> 32);
6566
hash = hash * 31 + (ack ? 1 : 0);
6667
return hash;
6768
}
Collapse file

‎codec-http2/src/test/java/io/netty/handler/codec/http2/Http2DefaultFramesTest.java‎

Copy file name to clipboardExpand all lines: codec-http2/src/test/java/io/netty/handler/codec/http2/Http2DefaultFramesTest.java
+46Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import io.netty.buffer.Unpooled;
2020
import org.junit.jupiter.api.Test;
2121

22+
import static org.junit.jupiter.api.Assertions.assertEquals;
2223
import static org.junit.jupiter.api.Assertions.assertFalse;
24+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
2325

2426
public class Http2DefaultFramesTest {
2527

@@ -41,4 +43,48 @@ public void testEqualOperation() {
4143
dflt.release();
4244
}
4345
}
46+
47+
// Reproduces https://github.com/netty/netty/issues/13659
48+
// AbstractHttp2StreamFrame.equals() treats two frames with a null stream as equal, but
49+
// AbstractHttp2StreamFrame.hashCode() previously returned super.hashCode() (identity hash)
50+
// in that case, producing different hashes for equal instances and violating the
51+
// Object.hashCode contract.
52+
@Test
53+
public void testAbstractHttp2StreamFrameEqualInstancesHaveEqualHashCodes() {
54+
AbstractHttp2StreamFrame a = new TestStreamFrame();
55+
AbstractHttp2StreamFrame b = new TestStreamFrame();
56+
assertEquals(a, b);
57+
assertEquals(a.hashCode(), b.hashCode());
58+
}
59+
60+
// Reproduces https://github.com/netty/netty/issues/13659 for DefaultHttp2PingFrame.
61+
// equals() compares ack and content; before the fix hashCode() folded in the identity
62+
// hash of Object, so two equal pings had different hash codes.
63+
@Test
64+
public void testDefaultHttp2PingFrameEqualInstancesHaveEqualHashCodes() {
65+
DefaultHttp2PingFrame a = new DefaultHttp2PingFrame(42L, true);
66+
DefaultHttp2PingFrame b = new DefaultHttp2PingFrame(42L, true);
67+
assertEquals(a, b);
68+
assertEquals(a.hashCode(), b.hashCode());
69+
}
70+
71+
// Smoke test that the hash actually folds in content and ack. This is a regression guard
72+
// against the pre-fix implementation, which seeded the hash with Object identity and never
73+
// folded content into the result at all. Specific hashCode values are an implementation
74+
// detail; we only assert that the chosen widely-spread inputs do not collide.
75+
@Test
76+
public void testDefaultHttp2PingFrameHashCodeDistinguishesDifferentValues() {
77+
DefaultHttp2PingFrame a = new DefaultHttp2PingFrame(0L, false);
78+
DefaultHttp2PingFrame differentContent = new DefaultHttp2PingFrame(Long.MAX_VALUE, false);
79+
DefaultHttp2PingFrame differentAck = new DefaultHttp2PingFrame(0L, true);
80+
assertNotEquals(a.hashCode(), differentContent.hashCode());
81+
assertNotEquals(a.hashCode(), differentAck.hashCode());
82+
}
83+
84+
private static final class TestStreamFrame extends AbstractHttp2StreamFrame {
85+
@Override
86+
public String name() {
87+
return "TEST";
88+
}
89+
}
4490
}

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.