pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedStrings with null characters produce exceptions when selected or inserted. Attempts to select messages with null bytes produces "ERROR: insufficient data left in message". And inserting produces "ERROR: invalid byte sequence for encoding \"UTF8\": 0x00". Since a null character is a valid UTF code point why is it rejected by the JDBC driver? The attached test can work with Mysql and their JDBC driver.
6+ messages / 6 participants
[nested] [flat]
* Strings with null characters produce exceptions when selected or inserted. Attempts to select messages with null bytes produces "ERROR: insufficient data left in message". And inserting produces "ERROR: invalid byte sequence for encoding \"UTF8\": 0x00". Since a null character is a valid UTF code point why is it rejected by the JDBC driver? The attached test can work with Mysql and their JDBC driver.
@ 2009-06-03 19:12 user2037@ymail.com
2009-06-03 20:41 ` Re: Strings with null characters produce exceptions when selected or inserted. Attempts to select messages with null bytes produces "ERROR: insufficient data left in message". And inserting produces "ERROR: invalid byte sequence for encoding \"UTF8\": 0x00". Since a null character is a valid UTF code point why is it rejected by the JDBC driver? The attached test can work with Mysql and their JDBC driver. Kris Jurka <books@ejurka.com>
0 siblings, 1 reply; 6+ messages in thread
From: user2037@ymail.com @ 2009-06-03 19:12 UTC (permalink / raw)
To: pgsql-jdbc
Strings with null characters produce exceptions when selected or inserted. Attempts to select messages with null bytes produces "ERROR: insufficient data left in message". And inserting produces "ERROR: invalid byte sequence for encoding \"UTF8\": 0x00".
Since a null character is a valid UTF code point why is it rejected by the JDBC driver?
It can work with Mysql and their JDBC driver.
import java.sql.*;
public class TestStringsWithNullBytes {
public static void main(String[] args) {
String noNulls = "No null bytes";
String nulls = "Null bytes \000\000";
try {
Class.forName("org.postgresql.Driver");
Connection db = DriverManager.getConnection(
"jdbc:postgresql://localhost/test?user=test&password=secret");
/*
Class.forName("com.mysql.jdbc.Driver");
Connection db = DriverManager.getConnection(
"jdbc:mysql://localhost/test?user=test&password=secret");
*/
db.setAutoCommit(true);
tryQuery(db, "SELECT * FROM test");
tryQuery(db, "DROP TABLE IF EXISTS test");
tryQuery(db, "CREATE TABLE test (name TEXT)");
tryQuery(db, "SELECT '" + noNulls + "'");
tryQuery(db, "SELECT '" + nulls + "'");
tryQuery(db, "INSERT INTO test (name) VALUES('" + noNulls + "')");
tryQuery(db, "INSERT INTO test (name) VALUES('" + nulls + "')");
tryPreparedQuery(db, "SELECT ?", noNulls);
tryPreparedQuery(db, "SELECT ?", nulls);
tryPreparedQuery(db, "INSERT INTO test (name) VALUES(?)", noNulls);
tryPreparedQuery(db, "INSERT INTO test (name) VALUES(?)", nulls);
tryQuery(db, "SELECT * FROM test");
}
catch(Exception e) {
e.printStackTrace();
}
}
private static void tryQuery(Connection db, String query) {
try {
Statement s = db.createStatement();
if (s.execute(query)) {
ResultSet rs = s.getResultSet();
while(rs.next()) {
System.out.println(escape(query) + ": " + escape(rs.getString(1)));
}
}
}
catch(Exception e) {
System.err.println(escape(query) + ": " + e.getMessage());
}
}
private static void tryPreparedQuery(Connection db, String query, String s) {
try {
PreparedStatement ps = db.prepareStatement(query);
ps.setString(1, s);
if (ps.execute()) {
ResultSet rs = ps.getResultSet();
while(rs.next()) {
System.out.println(escape(query) + ": " + escape(rs.getString(1)));
}
}
}
catch(Exception e) {
System.err.println(escape(query) + ": " + e.getMessage());
}
}
private static StringBuilder escape(String s) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < s.length(); i++) {
if ((32 > (int)s.charAt(i)) || (126 < (int)s.charAt(i))) {
sb.append("<" + (int)s.charAt(i) + ">");
}
else {
sb.append(s.charAt(i));
}
}
return(sb);
}
}
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Strings with null characters produce exceptions when selected or inserted. Attempts to select messages with null bytes produces "ERROR: insufficient data left in message". And inserting produces "ERROR: invalid byte sequence for encoding \"UTF8\": 0x00". Since a null character is a valid UTF code point why is it rejected by the JDBC driver? The attached test can work with Mysql and their JDBC driver.
2009-06-03 19:12 Strings with null characters produce exceptions when selected or inserted. Attempts to select messages with null bytes produces "ERROR: insufficient data left in message". And inserting produces "ERROR: invalid byte sequence for encoding \"UTF8\": 0x00". Since a null character is a valid UTF code point why is it rejected by the JDBC driver? The attached test can work with Mysql and their JDBC driver. user2037@ymail.com
@ 2009-06-03 20:41 ` Kris Jurka <books@ejurka.com>
2009-06-04 00:34 ` Re: Strings with null characters produce exceptions when selected or inserted. Attempts to select messages with null bytes produces "ERROR: insufficient data left in message". And inserting produces "ERROR: invalid byte sequence for encoding \"UTF8\": 0x00". Since a null character is a valid UTF code point why is it rejected by the JDBC driver? The attached test can work with Mysql and their JDBC driver. Craig Ringer <craig@postnewspapers.com.au>
0 siblings, 1 reply; 6+ messages in thread
From: Kris Jurka @ 2009-06-03 20:41 UTC (permalink / raw)
To: user2037@ymail.com; +Cc: pgsql-jdbc
On Wed, 3 Jun 2009, user2037@ymail.com wrote:
> Strings with null characters produce exceptions when selected or
> inserted. Attempts to select messages with null bytes produces "ERROR:
> insufficient data left in message". And inserting produces "ERROR:
> invalid byte sequence for encoding \"UTF8\": 0x00".
>
> Since a null character is a valid UTF code point why is it rejected by
> the JDBC driver?
Because the server can't handle it. The server is written in C and tracks
all textual data as C strings which are null terminated. It cannot handle
intermediate null bytes, so the driver is just providing that message as
early as possible to you.
Kris Jurka
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Strings with null characters produce exceptions when selected or inserted. Attempts to select messages with null bytes produces "ERROR: insufficient data left in message". And inserting produces "ERROR: invalid byte sequence for encoding \"UTF8\": 0x00". Since a null character is a valid UTF code point why is it rejected by the JDBC driver? The attached test can work with Mysql and their JDBC driver.
2009-06-03 19:12 Strings with null characters produce exceptions when selected or inserted. Attempts to select messages with null bytes produces "ERROR: insufficient data left in message". And inserting produces "ERROR: invalid byte sequence for encoding \"UTF8\": 0x00". Since a null character is a valid UTF code point why is it rejected by the JDBC driver? The attached test can work with Mysql and their JDBC driver. user2037@ymail.com
2009-06-03 20:41 ` Re: Strings with null characters produce exceptions when selected or inserted. Attempts to select messages with null bytes produces "ERROR: insufficient data left in message". And inserting produces "ERROR: invalid byte sequence for encoding \"UTF8\": 0x00". Since a null character is a valid UTF code point why is it rejected by the JDBC driver? The attached test can work with Mysql and their JDBC driver. Kris Jurka <books@ejurka.com>
@ 2009-06-04 00:34 ` Craig Ringer <craig@postnewspapers.com.au>
0 siblings, 0 replies; 6+ messages in thread
From: Craig Ringer @ 2009-06-04 00:34 UTC (permalink / raw)
To: Kris Jurka <books@ejurka.com>; +Cc: user2037@ymail.com; pgsql-jdbc
Kris Jurka wrote:
>
>
> On Wed, 3 Jun 2009, user2037@ymail.com wrote:
>
>> Strings with null characters produce exceptions when selected or
>> inserted. Attempts to select messages with null bytes produces "ERROR:
>> insufficient data left in message". And inserting produces "ERROR:
>> invalid byte sequence for encoding \"UTF8\": 0x00".
>>
>> Since a null character is a valid UTF code point why is it rejected by
>> the JDBC driver?
>
> Because the server can't handle it. The server is written in C and
> tracks all textual data as C strings which are null terminated. It
> cannot handle intermediate null bytes, so the driver is just providing
> that message as early as possible to you.
Note that the `bytea' type _does_ store null bytes fine.
It's interesting that \0x00 is in fact valid utf-8, since it raises the
question of whether Pg should in fact support null bytes in `text' and
`varchar' strings.
--
Craig Ringer
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Strings with null characters produce exceptions when selectedor inserted. Attempts to select messages with null bytes produces "ERROR:insufficient data left in message". And inserting produces "ERROR: invalidbyte sequence for encoding \"UTF8\":
@ 2009-06-04 11:58 John Lister <john.lister@kickstone.com>
2009-06-04 12:21 ` Re: Strings with null characters produce exceptions when selectedor inserted. Attempts to select messages with null bytes produces "ERROR:insufficient data left in message". And inserting produces "ERROR: invalidbyte sequence for encoding \"UTF8\": Hannu Krosing <hannu@2ndQuadrant.com>
2009-06-04 13:22 ` Re: Strings with null characters produce exceptions when selectedor inserted. Attempts to select messages with null bytes produces "ERROR:insufficient data left in message". And inserting produces "ERROR: invalidbyte sequence for encoding \"UTF8\": Oliver Jowett <oliver@opencloud.com>
0 siblings, 2 replies; 6+ messages in thread
From: John Lister @ 2009-06-04 11:58 UTC (permalink / raw)
To: Craig Ringer <craig@postnewspapers.com.au>; pgsql-jdbc-owner@postgresql.org; Kris Jurka <books@ejurka.com>; +Cc: user2037@ymail.com; pgsql-jdbc
Apologies for top post using a blackberry. The binary mode should be able to cope fine as it passes the length before sending the data. Should be straightforward to add strings to patch.
John
------Original Message------
From: Craig Ringer
Sender: pgsql-jdbc-owner@postgresql.org
To: Kris Jurka
Cc: user2037@ymail.com
Cc: pgsql-jdbc@postgresql.org
Subject: Re: [JDBC] Strings with null characters produce exceptions when selectedor inserted. Attempts to select messages with null bytes produces "ERROR:insufficient data left in message". And inserting produces "ERROR: invalidbyte sequence for encoding \"UTF8\": 0x00". Since a null character is avalid UTF code point why is it rejected by the JDBC driver? The attachedtest can work with Mysql and their JDBC driver.
Sent: 4 Jun 2009 01:34
Kris Jurka wrote:
>
>
> On Wed, 3 Jun 2009, user2037@ymail.com wrote:
>
>> Strings with null characters produce exceptions when selected or
>> inserted. Attempts to select messages with null bytes produces "ERROR:
>> insufficient data left in message". And inserting produces "ERROR:
>> invalid byte sequence for encoding \"UTF8\": 0x00".
>>
>> Since a null character is a valid UTF code point why is it rejected by
>> the JDBC driver?
>
> Because the server can't handle it. The server is written in C and
> tracks all textual data as C strings which are null terminated. It
> cannot handle intermediate null bytes, so the driver is just providing
> that message as early as possible to you.
Note that the `bytea' type _does_ store null bytes fine.
It's interesting that \0x00 is in fact valid utf-8, since it raises the
question of whether Pg should in fact support null bytes in `text' and
`varchar' strings.
--
Craig Ringer
--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc
Sent using BlackBerry® from Orange
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Strings with null characters produce exceptions when selectedor inserted. Attempts to select messages with null bytes produces "ERROR:insufficient data left in message". And inserting produces "ERROR: invalidbyte sequence for encoding \"UTF8\":
2009-06-04 11:58 Re: Strings with null characters produce exceptions when selectedor inserted. Attempts to select messages with null bytes produces "ERROR:insufficient data left in message". And inserting produces "ERROR: invalidbyte sequence for encoding \"UTF8\": John Lister <john.lister@kickstone.com>
@ 2009-06-04 12:21 ` Hannu Krosing <hannu@2ndQuadrant.com>
1 sibling, 0 replies; 6+ messages in thread
From: Hannu Krosing @ 2009-06-04 12:21 UTC (permalink / raw)
To: john.lister@kickstone.com; +Cc: Craig Ringer <craig@postnewspapers.com.au>; pgsql-jdbc-owner@postgresql.org; Kris Jurka <books@ejurka.com>; user2037@ymail.com; pgsql-jdbc
On Thu, 2009-06-04 at 11:58 +0000, John Lister wrote:
> Apologies for top post using a blackberry. The binary mode should be able to cope fine as it passes the length before sending the data. Should be straightforward to add strings to patch.
PostgreSQL itself does not consider byte sequences with embedded nulls
to be strings, as lot of its string-handling expects C strings
Use bytea type for "strings" with embedded nulls.
--
Hannu Krosing http://www.2ndQuadrant.com
PostgreSQL Scalability and Availability
Services, Consulting and Training
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: Strings with null characters produce exceptions when selectedor inserted. Attempts to select messages with null bytes produces "ERROR:insufficient data left in message". And inserting produces "ERROR: invalidbyte sequence for encoding \"UTF8\":
2009-06-04 11:58 Re: Strings with null characters produce exceptions when selectedor inserted. Attempts to select messages with null bytes produces "ERROR:insufficient data left in message". And inserting produces "ERROR: invalidbyte sequence for encoding \"UTF8\": John Lister <john.lister@kickstone.com>
@ 2009-06-04 13:22 ` Oliver Jowett <oliver@opencloud.com>
1 sibling, 0 replies; 6+ messages in thread
From: Oliver Jowett @ 2009-06-04 13:22 UTC (permalink / raw)
To: john.lister@kickstone.com; +Cc: Craig Ringer <craig@postnewspapers.com.au>; pgsql-jdbc-owner@postgresql.org; Kris Jurka <books@ejurka.com>; user2037@ymail.com; pgsql-jdbc
John Lister wrote:
> Apologies for top post using a blackberry. The binary mode should be able to cope fine as it passes the length before sending the data. Should be straightforward to add strings to patch.
It's not that we can't transfer strings with embedded zero bytes to the
server (we can), it's that the server does not handle the resulting
string. From memory it just truncates the string at that point. So we
disallow it in the driver rather than causing strange effects on the
server side.
So adding support for these strings to the binary patch would actually
be a regression.
-O
^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2009-06-04 13:22 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2009-06-03 19:12 Strings with null characters produce exceptions when selected or inserted. Attempts to select messages with null bytes produces "ERROR: insufficient data left in message". And inserting produces "ERROR: invalid byte sequence for encoding \"UTF8\": 0x00". Since a null character is a valid UTF code point why is it rejected by the JDBC driver? The attached test can work with Mysql and their JDBC driver. user2037@ymail.com
2009-06-03 20:41 ` Kris Jurka <books@ejurka.com>
2009-06-04 00:34 ` Craig Ringer <craig@postnewspapers.com.au>
2009-06-04 11:58 Re: Strings with null characters produce exceptions when selectedor inserted. Attempts to select messages with null bytes produces "ERROR:insufficient data left in message". And inserting produces "ERROR: invalidbyte sequence for encoding \"UTF8\": John Lister <john.lister@kickstone.com>
2009-06-04 12:21 ` Re: Strings with null characters produce exceptions when selectedor inserted. Attempts to select messages with null bytes produces "ERROR:insufficient data left in message". And inserting produces "ERROR: invalidbyte sequence for encoding \"UTF8\": Hannu Krosing <hannu@2ndQuadrant.com>
2009-06-04 13:22 ` Re: Strings with null characters produce exceptions when selectedor inserted. Attempts to select messages with null bytes produces "ERROR:insufficient data left in message". And inserting produces "ERROR: invalidbyte sequence for encoding \"UTF8\": Oliver Jowett <oliver@opencloud.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox