Skip to content
Permalink
Browse files

fix: NPE in PGXAConnection$ConnectionHandler.invoke() of .equals(null) (

#1365)

The PGXAConnection inner class call-filtering proxy ConnectionHandler
special cases calls to equals() by testing if the argument is itself
a proxy that should be unwrapped.

However it fails to test if the argument is null before calling getClass()
on it, resulting in an NPE like

    java.lang.NullPointerException: null
    at org.postgresql.xa.PGXAConnection$ConnectionHandler.invoke(PGXAConnection.java:139)

Fix by testing for null. While we're at it, also defensively check to ensure
there's exactly one argument to .equals. It's not sensible to pass more and
will error later anyway.
  • Loading branch information...
ringerc authored and davecramer committed Dec 8, 2018
1 parent cdfd49c commit cea5231ba794c362cbd5285b299ba3f82ad29e73
@@ -134,9 +134,9 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
* If the argument to equals-method is also a wrapper, present the original unwrapped
* connection to the underlying equals method.
*/
if (method.getName().equals("equals")) {
if (method.getName().equals("equals") && args.length == 1) {
Object arg = args[0];
if (Proxy.isProxyClass(arg.getClass())) {
if (arg != null && Proxy.isProxyClass(arg.getClass())) {
InvocationHandler h = Proxy.getInvocationHandler(arg);
if (h instanceof ConnectionHandler) {
// unwrap argument
@@ -191,6 +191,8 @@ public int hashCode() {
@Test
public void testWrapperEquals() throws Exception {
assertTrue("Wrappers should be equal", conn.equals(conn));
assertFalse("Wrapper should be unequal to null", conn.equals(null));
assertFalse("Wrapper should be unequal to unrelated object", conn.equals("dummy string object"));
}

@Test

0 comments on commit cea5231

Please sign in to comment.
You can’t perform that action at this time.