Re: Bug report: NullPointerException from Driver.connect when passed a Properties with non-string values

From: Nathaniel Waisbrot <waisbrot(at)highfleet(dot)com>
To: Dave Cramer <pg(at)fastcrypt(dot)com>
Cc: dmp <danap(at)ttc-cmc(dot)net>, List <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: Bug report: NullPointerException from Driver.connect when passed a Properties with non-string values
Date: 2013-01-28 19:43:32
Message-ID: E67FF73D-0FCA-406A-A03B-BFB1398B9936@highfleet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Thanks for the responses, dmp and Dave.
I agree that it's rather surprising to be getting non-String values for the Driver, but I don't think it's actually *required* by any spec that they be Strings. The references I'm looking at are:

The connect method of Driver, which takes a Properties:
http://docs.oracle.com/javase/6/docs/api/java/sql/Driver.html#connect%28java.lang.String,%20java.util.Properties%29

The Properties class:
http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html

Which warns that:
"Because Properties inherits from Hashtable, the put and putAll methods can be applied to a Properties object. Their use is strongly discouraged as they allow the caller to insert entries whose keys or values are not Strings. The setProperty method should be used instead. If the store or save method is called on a "compromised" Properties object that contains a non-String key or value, the call will fail. Similarly, the call to the propertyNames or list method will fail if it is called on a "compromised" Properties object that contains a non-String key."

Frustratingly, you need to actually look at the source code of Properties to see some undocumented behavior of getProperty:

public String getProperty(String key) {
Object oval = super.get(key);
String sval = (oval instanceof String) ? (String)oval : null;
return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval;
}

Note that middle line--the reason that my example program fails is because an Object is not of type String, and so getProperty is substituting a null value.

I've attached a candidate patch for you: waisbrot-exception.patch checks the result of getProperty() and throws a PSQLException if it is null. This allows drivers that want to deal with non-string Properties to get a turn with the URL, but will cause DriverManager to report the error if no Driver can perform the connection.

Attachment Content-Type Size
waisbrot-exception.patch application/octet-stream 879 bytes
unknown_filename text/plain 3.1 KB

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Kevin Grittner 2013-01-28 20:02:28 Re: Bug report: NullPointerException from Driver.connect when passed a Properties with non-string values
Previous Message Dave Cramer 2013-01-28 18:40:51 Re: Bug report: NullPointerException from Driver.connect when passed a Properties with non-string values