Skip to content

Commit

Permalink
fix: NPE in PGXAConnection$ConnectionHandler.invoke() of .equals(null) (
Browse files Browse the repository at this point in the history
#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 cea5231
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pgjdbc/src/main/java/org/postgresql/xa/PGXAConnection.java
Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand Down

0 comments on commit cea5231

Please sign in to comment.