Skip to content

Commit 99b11c9

Browse files
committed
[netty#4327] Ensure toString() will not throw IllegalReferenceCountException
Motivation: As toString() is often used while logging we need to ensure this produces no exception. Modifications: Ensure we never throw an IllegalReferenceCountException. Result: Be able to log without produce exceptions.
1 parent 2d7e957 commit 99b11c9

4 files changed

Lines changed: 45 additions & 12 deletions

File tree

buffer/src/main/java/io/netty/buffer/DefaultByteBufHolder.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,16 @@ public boolean release(int decrement) {
9090
return data.release(decrement);
9191
}
9292

93+
/**
94+
* Return {@link ByteBuf#toString()} without checking the reference count first. This is useful to implemement
95+
* {@link #toString()}.
96+
*/
97+
protected final String contentToString() {
98+
return data.toString();
99+
}
100+
93101
@Override
94102
public String toString() {
95-
return StringUtil.simpleClassName(this) + '(' + content().toString() + ')';
103+
return StringUtil.simpleClassName(this) + '(' + contentToString() + ')';
96104
}
97105
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2015 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.buffer;
17+
18+
import org.junit.Test;
19+
20+
import static org.junit.Assert.*;
21+
22+
public class DefaultByteBufHolderTest {
23+
24+
@Test
25+
public void testToString() {
26+
ByteBufHolder holder = new DefaultByteBufHolder(Unpooled.buffer());
27+
assertEquals(1, holder.refCnt());
28+
assertNotNull(holder.toString());
29+
assertTrue(holder.release());
30+
assertNotNull(holder.toString());
31+
}
32+
}

codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketFrame.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public int rsv() {
6868

6969
@Override
7070
public String toString() {
71-
return StringUtil.simpleClassName(this) + "(data: " + content() + ')';
71+
return StringUtil.simpleClassName(this) + "(data: " + contentToString() + ')';
7272
}
7373

7474
@Override

transport-sctp/src/main/java/io/netty/channel/sctp/SctpMessage.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import com.sun.nio.sctp.MessageInfo;
1919
import io.netty.buffer.ByteBuf;
20-
import io.netty.buffer.ByteBufUtil;
2120
import io.netty.buffer.DefaultByteBufHolder;
2221

2322
/**
@@ -191,15 +190,9 @@ public SctpMessage touch(Object hint) {
191190

192191
@Override
193192
public String toString() {
194-
if (refCnt() == 0) {
195-
return "SctpFrame{" +
196-
"streamIdentifier=" + streamIdentifier + ", protocolIdentifier=" + protocolIdentifier +
197-
", unordered=" + unordered +
198-
", data=(FREED)}";
199-
}
200193
return "SctpFrame{" +
201-
"streamIdentifier=" + streamIdentifier + ", protocolIdentifier=" + protocolIdentifier +
202-
", unordered=" + unordered +
203-
", data=" + ByteBufUtil.hexDump(content()) + '}';
194+
"streamIdentifier=" + streamIdentifier + ", protocolIdentifier=" + protocolIdentifier +
195+
", unordered=" + unordered +
196+
", data=" + contentToString() + '}';
204197
}
205198
}

0 commit comments

Comments
 (0)