Re: PQunescapeBytea - Mailing list pgsql-general

From Joe Conway
Subject Re: PQunescapeBytea
Date
Msg-id 402980D9.8070505@joeconway.com
Whole thread Raw
In response to PQunescapeBytea  (Iker Arizmendi <iker@research.att.com>)
List pgsql-general
Iker Arizmendi wrote:
> Am I using the escape/unescape functions incorrectly? I was
> expecting the unescape function to produce an exact
> duplicate of a[].

The two functions are not, and should not be, precise reciprocals.
PQescapeBytea() prepares a string to be used in an SQL statement,
meaning it must produce something in the form "\\ooo" (without the
quotes) -- notice *two* backslashes before the octal number. This is
needed because the SQL string literal parser eats one backslash, and the
byteain() function needs the other to recognize that it is receiving an
octal sequence.

PQunescapeBytea(), on the other hand, takes a string in the form "\ooo"
which is the output format produced by byteaout() function. Notice that
only *one* backslash is expected before the octal.

In your example, "aaaa\0" is fed into PQescapeBytea(), producing
"aaaa\\000". When that is fed into PQunescapeBytea(), the two
backslashes are seen as a single backslash that has been escaped.
Therefore the two backslashes are transformed into a single unescaped
backslash, and the octal number is taken as a literal part of the
string, producing "aaaa\000".

HTH,

Joe



pgsql-general by date:

Previous
From: Jenny Zhang
Date:
Subject: Re: DB cache size strategies
Next
From: Jan Wieck
Date:
Subject: Re: I want to use postresql for this app, but...