Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Tzvel-0005d4-DZ for pgsql-jdbc@arkaria.postgresql.org; Mon, 28 Jan 2013 20:50:19 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1Tzvek-0003pH-AI for pgsql-jdbc@arkaria.postgresql.org; Mon, 28 Jan 2013 20:50:18 +0000 Received: from magus.postgresql.org ([87.238.57.229]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Tzvej-0003pB-6w for pgsql-jdbc@postgresql.org; Mon, 28 Jan 2013 20:50:17 +0000 Received: from mxout-07.mxes.net ([216.86.168.182]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Tzved-000482-Ei for pgsql-jdbc@postgresql.org; Mon, 28 Jan 2013 20:50:16 +0000 Received: from [10.0.1.13] (unknown [69.255.36.139]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) by smtp.mxes.net (Postfix) with ESMTPSA id 7382A22E253; Mon, 28 Jan 2013 15:50:09 -0500 (EST) Content-Type: multipart/alternative; boundary="Apple-Mail=_3FEC300D-CD95-422B-97E8-C977ADC0879C" Mime-Version: 1.0 (Mac OS X Mail 6.2 \(1499\)) Subject: Re: Bug report: NullPointerException from Driver.connect when passed a Properties with non-string values From: Nathaniel Waisbrot In-Reply-To: Date: Mon, 28 Jan 2013 15:50:08 -0500 Cc: dmp , List Message-Id: References: <74B305A4-D481-4D4C-8E0E-8EBAB50CF584@highfleet.com> <5106C07B.8020800@ttc-cmc.net> To: Dave Cramer X-Mailer: Apple Mail (2.1499) X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-jdbc Precedence: bulk Sender: pgsql-jdbc-owner@postgresql.org --Apple-Mail=_3FEC300D-CD95-422B-97E8-C977ADC0879C Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=iso-8859-1 That's a good point. At least in my case, the URL I'm passing in isn't = even for PG so it shouldn't need to care that the Properties are set up = with weird non-String values. =20 I think this is a good patch. =20 You could add mine in, too, just to conform a little more rigidly to the = JDBC spec (which says to throw SQLException if the driver wants to = handle the URL but can't because of an error). But once the driver is = only throwing exceptions for its own URLs, the problem becomes quite = minor, I think. On Jan 28, 2013, at 3:29 PM, Dave Cramer wrote: > My proposed patch is this >=20 > if (url.startsWith("jdbc:postgresql:")) { > return null; > } > =20 > Right at the top of getConnection, before we do any real work. >=20 > Thoughts ? > =20 >=20 > Dave Cramer >=20 > dave.cramer(at)credativ(dot)ca > http://www.credativ.ca >=20 >=20 > On Mon, Jan 28, 2013 at 3:23 PM, Dave Cramer wrote: > Nathaniel, >=20 > It's ironic to note that Properties.getProperty() returns null even if = there is a value in the hashtable for the key. >=20 > It suggests that it will only return a string. >=20 > Either way the solution is to check to make sure the url is for us = before loading up the properties. >=20 >=20 > Dave Cramer >=20 > dave.cramer(at)credativ(dot)ca > http://www.credativ.ca >=20 >=20 > On Mon, Jan 28, 2013 at 2:43 PM, Nathaniel Waisbrot = wrote: > 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: >=20 > The connect method of Driver, which takes a Properties: > = http://docs.oracle.com/javase/6/docs/api/java/sql/Driver.html#connect%28ja= va.lang.String,%20java.util.Properties%29 >=20 > The Properties class: > http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html >=20 > 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." >=20 >=20 > Frustratingly, you need to actually look at the source code of = Properties to see some undocumented behavior of getProperty: >=20 > public String getProperty(String key) { > Object oval =3D super.get(key); > String sval =3D (oval instanceof String) ? (String)oval : = null; > return ((sval =3D=3D null) && (defaults !=3D null)) ? = defaults.getProperty(key) : sval; > } >=20 >=20 > 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. >=20 > 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. >=20 >=20 >=20 >=20 >=20 >=20 >=20 >=20 > On Jan 28, 2013, at 1:40 PM, Dave Cramer wrote: >=20 > > Well ... another interesting stretching of the spec. > > > > Driver PropertyInfo values are required to be Strings as per the = spec. = http://docs.oracle.com/javase/1.3/docs/api/java/sql/DriverPropertyInfo.htm= l > > > > Your test case is actually a bit misleading as getProperty returns = null because getString on the empty object returns null. > > > > while I'm inclined to accept a patch to fix this, this is clearly = 'out of spec' > > > > Dave Cramer > > > > dave.cramer(at)credativ(dot)ca > > http://www.credativ.ca > > > > > > On Mon, Jan 28, 2013 at 1:16 PM, dmp wrote: > > Hello Nathaniel, > > > > Any very robust program will try to handle anything the user throws = at it. > > > > In this case you have passed a null value as an argument to the = method > > and it as acted according by giving you a NUllPointerException. = Perhaps > > wrapping this as SQLException may be more easily caught by your = app., but > > would be deceptive in response. > > > > Perhaps you could submit a patch for consideration, to the solution = you > > desire. The code may be obtained at GitHub. > > > > git clone git://github.com/pgjdbc/pgjdbc.git > > > > danap. > > > > > > > > Nathaniel Waisbrot wrote: > > I found this while working with another JDBC driver (Stels XML = driver). The Postgresql driver's connect() method is expecting the = passed Properties object to have only string values, but this is not = actually guaranteed to be the case. I think that the PG driver should = wrap the NullPointerException in a SQLException, which would allow the = DriverManager to attempt to use other JDBC drivers to make the = connection. > > > > Here is a simple Java program which will reproduce the problem: > > > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > import java.sql.DriverManager; > > import java.util.Properties; > > > > public class PGTest { > > public static void main(String[] args) throws Exception { > > Class.forName("org.postgresql.Driver"); > > Properties info =3D new Properties(); > > info.put("foo", new Object()); // = info.getPropert("foo") will return null > > DriverManager.getConnection("foo:bar//baz", info); > > } > > } > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > > > and here is the stack trace produced by running that program: > > > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > Exception in thread "main" java.lang.NullPointerException > > at java.util.Hashtable.put(Hashtable.java:542) > > at java.util.Properties.setProperty(Properties.java:161) > > at org.postgresql.Driver.connect(Driver.java:244) > > at = java.sql.DriverManager.getConnection(DriverManager.java:579) > > at = java.sql.DriverManager.getConnection(DriverManager.java:190) > > at PGTest.main(PGTest.java:9) > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > > > I'm using postgresql-9.2-1002.jdbc4.jar, with Java 7, running on Mac = OS 10.8.2. Output of java -version: > > java version "1.7.0_07" > > Java(TM) SE Runtime Environment (build 1.7.0_07-b10) > > Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode) > > > > > > > > > > > > > > -- > > Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) > > To make changes to your subscription: > > http://www.postgresql.org/mailpref/pgsql-jdbc > > >=20 >=20 >=20 >=20 --Apple-Mail=_3FEC300D-CD95-422B-97E8-C977ADC0879C Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=iso-8859-1 pg@fastcrypt.com> = wrote:
My proposed patch is = this

if (url.startsWith("jdbc:postgresql:")) = {
            return = null;
        }
    =    
Right at the top of getConnection, = before we do any real work.

Thoughts ?
 

Dave = Cramer

dave.cramer(at)credativ(dot)ca
http://www.credativ.ca


On Mon, Jan 28, 2013 at 3:23 PM, Dave = Cramer <pg@fastcrypt.com> wrote:
Nathaniel,

It's ironic to note that = Properties.getProperty() returns null even if there is a value in the = hashtable for the key.

It suggests that it will = only return a string.

Either way the solution is to check to make sure the = url is for us before loading up the = properties.


Dave Cramer

dave.cramer(at)credativ(dot)ca
http://www.credativ.ca


On Mon, = Jan 28, 2013 at 2:43 PM, Nathaniel Waisbrot <waisbrot@highfleet.com> wrote:
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/Prope= rties.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 =3D super.get(key);
        String sval =3D (oval instanceof String) ? = (String)oval : null;
        return ((sval =3D=3D null) && = (defaults !=3D 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.








On Jan 28, 2013, at 1:40 PM, Dave Cramer <pg@fastcrypt.com> wrote:

> Well ... another interesting stretching of the spec.
>
> Driver PropertyInfo values are required to be Strings as per the = spec. http://docs.oracle.com/javase/1.3/docs/api/java/sql/Driv= erPropertyInfo.html
>
> Your test case is actually a bit misleading as getProperty returns = null because getString on the empty object returns null.
>
> while I'm inclined to accept a patch to fix this, this is clearly = 'out of spec'
>
> Dave Cramer
>
> dave.cramer(at)credativ(dot)ca
> http://www.credativ.ca
>
>
> On Mon, Jan 28, 2013 at 1:16 PM, dmp <danap@ttc-cmc.net> wrote:
> Hello Nathaniel,
>
> Any very robust program will try to handle anything the user throws = at it.
>
> In this case you have passed a null value as an argument to the = method
> and it as acted according by giving you a NUllPointerException. = Perhaps
> wrapping this as SQLException may be more easily caught by your = app., but
> would be deceptive in response.
>
> Perhaps you could submit a patch for consideration, to the solution = you
> desire. The code may be obtained at GitHub.
>
> git clone git://github.com/pgjdbc/pgjdbc.git
>
> danap.
>
>
>
> Nathaniel Waisbrot wrote:
> I found this while working with another JDBC driver (Stels XML = driver).  The Postgresql driver's connect() method is expecting the = passed Properties object to have only string values, but this is not = actually guaranteed to be the case.  I think that the PG driver = should wrap the NullPointerException in a SQLException, which would = allow the DriverManager to attempt to use other JDBC drivers to make the = connection.
>
> Here is a simple Java program which will reproduce the problem:
>
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> import java.sql.DriverManager;
> import java.util.Properties;
>
> public class PGTest {
>         public static void main(String[] args) = throws Exception {
>                 = Class.forName("org.postgresql.Driver");
>                 Properties = info =3D new Properties();
>                 = info.put("foo", new Object());      // = info.getPropert("foo") will return null
>                 = DriverManager.getConnection("foo:bar//baz", info);
>         }
> }
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
>
> and here is the stack trace produced by running that program:
>
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> Exception in thread "main" java.lang.NullPointerException
>         at = java.util.Hashtable.put(Hashtable.java:542)
>         at = java.util.Properties.setProperty(Properties.java:161)
>         at = org.postgresql.Driver.connect(Driver.java:244)
>         at = java.sql.DriverManager.getConnection(DriverManager.java:579)
>         at = java.sql.DriverManager.getConnection(DriverManager.java:190)
>         at PGTest.main(PGTest.java:9)
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
>
> I'm using postgresql-9.2-1002.jdbc4.jar, with Java 7, running on = Mac OS 10.8.2.  Output of java -version:
> java version "1.7.0_07"
> Java(TM) SE Runtime Environment (build 1.7.0_07-b10)
> Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)
>
>
>
>
>
>
> --
> Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-jdbc
>





= --Apple-Mail=_3FEC300D-CD95-422B-97E8-C977ADC0879C--