Skip to content

Commit

Permalink
Fix possible NPE in DefaultHttp2UnknownFrame#equals (netty#9625)
Browse files Browse the repository at this point in the history
Motivation:
`DefaultHttp2UnknownFrame#equals` may produce NPE due to
incorrect comparison of `stream` field.

Modification:
- Fix the `stream` field compare.
- Cleanup usage of class fields: use direct access instead of getters
(because the class is final).

Result:
No NPE in `equals` method.
  • Loading branch information
fenik17 authored and normanmaurer committed Oct 7, 2019
1 parent 4dc1ecc commit 1fb5ff1
Showing 1 changed file with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public DefaultHttp2UnknownFrame retainedDuplicate() {

@Override
public DefaultHttp2UnknownFrame replace(ByteBuf content) {
return new DefaultHttp2UnknownFrame(frameType, flags, content).stream(stream());
return new DefaultHttp2UnknownFrame(frameType, flags, content).stream(stream);
}

@Override
Expand All @@ -97,8 +97,8 @@ public DefaultHttp2UnknownFrame retain(int increment) {

@Override
public String toString() {
return StringUtil.simpleClassName(this) + "(frameType=" + frameType() + ", stream=" + stream() +
", flags=" + flags() + ", content=" + contentToString() + ')';
return StringUtil.simpleClassName(this) + "(frameType=" + frameType + ", stream=" + stream +
", flags=" + flags + ", content=" + contentToString() + ')';
}

@Override
Expand All @@ -119,18 +119,20 @@ public boolean equals(Object o) {
return false;
}
DefaultHttp2UnknownFrame other = (DefaultHttp2UnknownFrame) o;
return super.equals(other) && flags().equals(other.flags())
&& frameType() == other.frameType() && (stream() == null && other.stream() == null) ||
stream().equals(other.stream());
Http2FrameStream otherStream = other.stream();
return (stream == otherStream || otherStream != null && otherStream.equals(stream))
&& flags.equals(other.flags())
&& frameType == other.frameType()
&& super.equals(other);
}

@Override
public int hashCode() {
int hash = super.hashCode();
hash = hash * 31 + frameType();
hash = hash * 31 + flags().hashCode();
if (stream() != null) {
hash = hash * 31 + stream().hashCode();
hash = hash * 31 + frameType;
hash = hash * 31 + flags.hashCode();
if (stream != null) {
hash = hash * 31 + stream.hashCode();
}

return hash;
Expand Down

0 comments on commit 1fb5ff1

Please sign in to comment.