> I found a problem with PQescapeString (I think). Since it escapes
authorBruce Momjian
Thu, 13 Sep 2001 17:00:34 +0000 (17:00 +0000)
committerBruce Momjian
Thu, 13 Sep 2001 17:00:34 +0000 (17:00 +0000)
> null bytes to be literally '\0', the following can happen:
> 1. User inputs string value as "##" where ## are digits in the
> range of 0 to 7.
> 2. PQescapeString converts this to "\0##"
> 3. Escaped string is used in a context that causes "\0##" to be evaluated as
> an octal escape sequence.

I agree that this is a problem, though it is not possible to do
anything harmful with it.  In addition, it only occurs if there are
any NUL characters in its input, which is very unlikely if you are
using C strings.

The patch below addresses the issue by removing escaping of \0
characters entirely.

> If the goal is to "safely" encode null bytes, and preserve the rest of the
> string as it was entered, I think the null bytes should be escaped as \\000
> (note that if you simply use \000 the same string truncation problem
> occurs).

We can't do that, this would require 4n + 1 bytes of storage for the
result, breaking the interface.

Florian Weimer

src/interfaces/jdbc/org/postgresql/jdbc2/ResultSet.java
src/interfaces/libpq/fe-exec.c

index bf412571115c4311070a19eb2f521eb76d3aab4a..9df669910daf71c2f507f3541d6707b65d41fc0e 100644 (file)
@@ -1396,7 +1396,7 @@ public class ResultSet extends org.postgresql.ResultSet implements java.sql.Resu
        if (s != null)
        {
            int c = s.charAt(0);
-           return ((c == 't') || (c == 'T'));
+           return ((c == 't') || (c == 'T') || (c == '1'));
        }
        return false;       // SQL NULL
    }
index bdff56fe0804b50c1c521879eecaafd3a8ca34bd..e7495dabf7f38bcc79e28305ec1ec0f79139d60c 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *   $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.110 2001/09/07 22:02:32 momjian Exp $
+ *   $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.111 2001/09/13 17:00:34 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -59,7 +59,7 @@ static int    getNotice(PGconn *conn);
 /* ---------------
  * Escaping arbitrary strings to get valid SQL strings/identifiers.
  *
- * Replaces "\\" with "\\\\", "\0" with "\\0", and "'" with "''".
+ * Replaces "\\" with "\\\\" and "'" with "''".
  * length is the length of the buffer pointed to by
  * from.  The buffer at to must be at least 2*length + 1 characters
  * long.  A terminating NUL character is written.
@@ -75,13 +75,6 @@ PQescapeString (char *to, const char *from, size_t length)
 
    while (remaining > 0) {
        switch (*source) {
-       case '\0':
-           *target = '\\';
-           target++;
-           *target = '0';
-           /* target and remaining are updated below. */
-           break;
-           
        case '\\':
            *target = '\\';
            target++;