Test case (re: A JDBC bug or problem relating to string length in Java) - Mailing list pgsql-jdbc

From joe user
Subject Test case (re: A JDBC bug or problem relating to string length in Java)
Date
Msg-id 20030902032836.40676.qmail@web20421.mail.yahoo.com
Whole thread Raw
In response to Re: A JDBC bug or problem relating to string length in Java  (joe user <palehaole@yahoo.com>)
List pgsql-jdbc
Hello, I have created a reproducible test case for the
behavior I was describing, where Java thinks the
string length is one thing but PG thinks it is
something else (longer) throwing an SQLException.  I'm
not sure where the bug is, but it's a real bug and
it's somewhere.  I have attached the test case as a
file.  I would appreciate it if someone else could
verify that he sees this behavior and that it is a
bug.

Test details:

Tested on both Redhat 9, and SuSE 8.2
Sun JDK 1.4.2
Postgres 7.3.4, with the JDBC that comes with it

Linux was installed "plain vanilla".  If anyone has
questions about my Linux environment I can answer
them.  Postgres was compiled also "plain vanilla",
like this:

./configure --with-java
make

and that is all.

The output that I get from my test program is this:

% java SqlClientTest
The length of the evil string is: 99
Exception in thread "main" java.sql.SQLException:
ERROR:  value too long for type character varying(100)

Clearly, Java thinks the string is 99 chars, and
Postgres thinks it is longer than 100 chars.  Any
feedback or information about this would be most
welcome, especially any suggestions of fixes or
workarounds.

Thanks!


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.comimport java.sql.*;

public class SqlClientTest {

    /** Truncates a String s to length i or less */
    public static String truncate(String s, int i) {
    if(s.length() > i) return s.substring(0, i);
    else return s;
    }

    public static byte[] evilBytes = { (byte) 0150, (byte) 0164, (byte) 0164, (byte) 0160,
(byte) 0072, (byte) 0057, (byte) 0057, (byte) 0163, (byte) 0145,
(byte) 0145, (byte) 0153, (byte) 0056, (byte) 0063, (byte) 0067,
(byte) 0062, (byte) 0061, (byte) 0056, (byte) 0143, (byte) 0157, (byte) 0155,
(byte) 0057, (byte) 0063, (byte) 0067, (byte) 0062, (byte) 0061,
(byte) 0057, (byte) 0141, (byte) 0144, (byte) 0166, (byte) 0137,
(byte) 0162, (byte) 0145, (byte) 0156, (byte) 0144, (byte) 0145, (byte) 0162,
(byte) 0056, (byte) 0150, (byte) 0164, (byte) 0155,
(byte) 0077, (byte) 0141, (byte) 0144, (byte) 0166,
(byte) 0151, (byte) 0075, (byte) 0141, (byte) 0144,
(byte) 0166, (byte) 0137, (byte) 0162, (byte) 0151, (byte) 0147,
(byte) 0150, (byte) 0164, (byte) 0046, (byte) 0156, (byte) 0141,
(byte) 0155, (byte) 0145, (byte) 0075, (byte) 0302, (byte) 0270, (byte) 0303,
(byte) 0237, (byte) 0303, (byte) 0220, (byte) 0302, (byte) 0275, (byte) 0303,
(byte) 0226, (byte) 0302, (byte) 0260, (byte) 0303, (byte) 0216, (byte) 0302,
(byte) 0273, (byte) 0303, (byte) 0225, (byte) 0303, (byte) 0220, (byte) 0303,
(byte) 0206, (byte) 0302, (byte) 0270, (byte) 0046, (byte) 0160, (byte) 0141,
(byte) 0147, (byte) 0145, (byte) 0075, (byte) 0061, (byte) 0046, (byte) 0167,
(byte) 0151, (byte) 0144, (byte) 0164, (byte) 0150, (byte) 0075, (byte) 0061,
(byte) 0063, (byte) 0060, (byte) 0012 };

    /** This exposes a bug in the Postgres JDBC.  It relies on a database with
     * this table:
     * <pre>
     * CREATE TABLE foo (bar VARCHAR(100));
     * </pre> */
    public static void main(String args[]) throws Exception {

    String driver = "org.postgresql.Driver";
    String connectionUrl = "jdbc:postgresql://localhost:5432/testing";
    Class.forName(driver).newInstance();
    Connection db = DriverManager.getConnection(connectionUrl, "me", "");

    String qstring = "INSERT INTO foo (bar) VALUES (?)";

    PreparedStatement ps = db.prepareStatement(qstring);

    String evilString = new String(evilBytes);

    evilString = truncate(evilString, 99);

    System.out.println("The length of the evil string is: " + evilString.length());

    ps.setString(1, evilString);
    ps.executeUpdate();
    ps.close();
    db.close();
    System.out.println("Done.");
    }
}

pgsql-jdbc by date:

Previous
From: Jean-Christian Imbeault
Date:
Subject: Re: A JDBC bug or problem relating to string length in Java
Next
From: Michael Stephenson
Date:
Subject: Re: A JDBC bug or problem relating to string length in Java