Skip to content

Commit 27e0f52

Browse files
authored
Guard usage with null check to prevent possible NPE during finalization. (#12434)
Motivation: We need to add a null check to ensure we not throw a NPE if the finalizer of ReferenceCountedOpenSslEngine runs because of an OOME before we assign the field. Modifications: - Add null check - Assign fields first Result: No more NPE in finalizer possible
1 parent 871dd2e commit 27e0f52

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

handler/src/main/java/io/netty/handler/ssl/ReferenceCountedOpenSslEngine.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ protected void deallocate() {
229229
int peerPort, boolean jdkCompatibilityMode, boolean leakDetection) {
230230
super(peerHost, peerPort);
231231
OpenSsl.ensureAvailability();
232+
engineMap = context.engineMap;
233+
enableOcsp = context.enableOcsp;
234+
this.jdkCompatibilityMode = jdkCompatibilityMode;
232235
this.alloc = checkNotNull(alloc, "alloc");
233236
apn = (OpenSslApplicationProtocolNegotiator) context.applicationProtocolNegotiator();
234237
clientMode = context.isClient();
@@ -309,13 +312,11 @@ public List<byte[]> getStatusResponses() {
309312
} else {
310313
session = new DefaultOpenSslSession(context.sessionContext());
311314
}
312-
engineMap = context.engineMap;
313-
enableOcsp = context.enableOcsp;
315+
314316
if (!context.sessionContext().useKeyManager()) {
315317
session.setLocalCertificate(context.keyCertChain);
316318
}
317319

318-
this.jdkCompatibilityMode = jdkCompatibilityMode;
319320
Lock readerLock = context.ctxLock.readLock();
320321
readerLock.lock();
321322
final long finalSsl;
@@ -561,7 +562,12 @@ public final synchronized long sslPointer() {
561562
public final synchronized void shutdown() {
562563
if (!destroyed) {
563564
destroyed = true;
564-
engineMap.remove(ssl);
565+
// Let's check if engineMap is null as it could be in theory if we throw an OOME during the construction of
566+
// ReferenceCountedOpenSslEngine (before we assign the field). This is needed as shutdown() is called from
567+
// the finalizer as well.
568+
if (engineMap != null) {
569+
engineMap.remove(ssl);
570+
}
565571
SSL.freeSSL(ssl);
566572
ssl = networkBIO = 0;
567573

0 commit comments

Comments
 (0)