Fix false-positive leak detection report when ReferenceCountedOpenSslEngine constructor throws.

Motivation:

We need to ensure we only create the ResourceLeak when the constructor not throws.

Modifications:

Ensure ResourceLeakDetector.track(...) is only called if the constructor of ReferenceCoundedOpenSslEngine not throws.

Result:

No more false-positves.
This commit is contained in:
Norman Maurer 2017-07-28 13:37:39 +02:00
parent 60250f3795
commit c5b5d36360
2 changed files with 13 additions and 1 deletions

View File

@ -235,7 +235,6 @@ public class ReferenceCountedOpenSslEngine extends SSLEngine implements Referenc
int peerPort, boolean jdkCompatibilityMode, boolean leakDetection) { int peerPort, boolean jdkCompatibilityMode, boolean leakDetection) {
super(peerHost, peerPort); super(peerHost, peerPort);
OpenSsl.ensureAvailability(); OpenSsl.ensureAvailability();
leak = leakDetection ? leakDetector.track(this) : null;
this.alloc = checkNotNull(alloc, "alloc"); this.alloc = checkNotNull(alloc, "alloc");
apn = (OpenSslApplicationProtocolNegotiator) context.applicationProtocolNegotiator(); apn = (OpenSslApplicationProtocolNegotiator) context.applicationProtocolNegotiator();
session = new OpenSslSession(context.sessionContext()); session = new OpenSslSession(context.sessionContext());
@ -288,6 +287,7 @@ public class ReferenceCountedOpenSslEngine extends SSLEngine implements Referenc
PlatformDependent.throwException(cause); PlatformDependent.throwException(cause);
} }
} }
leak = leakDetection ? leakDetector.track(this) : null;
} }
/** /**

View File

@ -15,7 +15,9 @@
*/ */
package io.netty.handler.ssl; package io.netty.handler.ssl;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCountUtil;
import org.junit.Test;
import javax.net.ssl.SSLEngine; import javax.net.ssl.SSLEngine;
@ -54,4 +56,14 @@ public class ReferenceCountedOpenSslEngineTest extends OpenSslEngineTest {
protected void cleanupServerSslEngine(SSLEngine engine) { protected void cleanupServerSslEngine(SSLEngine engine) {
ReferenceCountUtil.release(engine); ReferenceCountUtil.release(engine);
} }
@Test(expected = NullPointerException.class)
public void testNotLeakOnException() throws Exception {
clientSslCtx = SslContextBuilder.forClient()
.trustManager(InsecureTrustManagerFactory.INSTANCE)
.sslProvider(sslClientProvider())
.build();
clientSslCtx.newEngine(null);
}
} }