Skip to content

Commit

Permalink
False Positive Potential Resource Leak Warnings With Inherited
Browse files Browse the repository at this point in the history
AutoCloseable Classes

fixes eclipse-jdt#2129

+ need to drill into cast expression in one more situation
  • Loading branch information
stephan-herrmann committed Apr 2, 2024
1 parent 151278c commit b851461
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,8 @@ public static FakedTrackingVariable preConnectTrackerAcrossAssignment(ASTNode lo
if (messageSend.binding != null && ((messageSend.binding.tagBits & TagBits.AnnotationNotOwning) == 0))
closeTracker.owningState = OWNED;
}
} else if (rhs instanceof CastExpression cast) {
preConnectTrackerAcrossAssignment(location, local, cast.expression, flowInfo, useAnnotations);
}
return closeTracker;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7175,4 +7175,50 @@ public ExecutorService t_supplier_lambda_returned(ExecutorService executor) {
"",
options);
}
public void testGH2129() {
if (this.complianceLevel < ClassFileConstants.JDK1_6) // override for implementing interface method
return;
Map options = getCompilerOptions();
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR);
runLeakTest(
new String[] {
"ExampleResourceLeakWarningInternalResource.java",
"""
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
public class ExampleResourceLeakWarningInternalResource implements AutoCloseable {
private SSLServerSocket sslServerSocket;
public ExampleResourceLeakWarningInternalResource(int aSecurePort, SSLContext aSSLContext) throws IOException {
sslServerSocket = initialise(aSSLContext, aSecurePort);
}
private SSLServerSocket initialise(SSLContext aSSLContext, int aPort) throws IOException {
SSLServerSocketFactory secure_server_socket_factory = aSSLContext.getServerSocketFactory();
// No warning here for Eclipse 2019.06 but warnings for Eclipse 2020.03 and later
SSLServerSocket server_secure_socket = (SSLServerSocket) secure_server_socket_factory.createServerSocket();
SocketAddress endpoint = new InetSocketAddress(aPort);
server_secure_socket.bind(endpoint, 1);
return server_secure_socket;
}
@Override
public void close() throws IOException {
sslServerSocket.close();
}
}
"""
},
"",
options);
}
}

0 comments on commit b851461

Please sign in to comment.