PQunescapeBytea code

From: "Jeroen T(dot) Vermeulen" <jtv(at)xs4all(dot)nl>
To: pgsql-hackers(at)postgresql(dot)org
Subject: PQunescapeBytea code
Date: 2003-10-30 19:24:13
Message-ID: 20031030192412.GF48224@xs4all.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Someething to consider for after the 7.4 release, perhaps...

As per today's CVS version, PQunescapeBytea() does the following when
it encounters an escaped character (i.e., a backslash) in the escaped
string strtext at offset i:

["if (strtext[i] == '\\')"]

i++;
if (strtext[i] == '\\')
buffer[j++] = strtext[i++];
else
{
if ((isdigit(strtext[i])) &&
(isdigit(strtext[i + 1])) &&
(isdigit(strtext[i + 2])))
{
byte = VAL(strtext[i++]);
byte = (byte << 3) + VAL(strtext[i++]);
buffer[j++] = (byte << 3) + VAL(strtext[i++]);
}
}

This code completely ignores any other usage of the backslash in the
escaped string, generating no output for unknown escape sequences. Is
that the desired behaviour? The code would be a little simpler if it
were to allow al characters to be escaped, which means ignoring the
backslash but not the following character:

i++;
if (isdigit(strtext[i]) && isdigit(strtext[i+1]) && isdigit(strtext[i+2]))
{
byte = VAL(strtext[i]);
i++;
byte = (byte<<3) + VAL(strtext[i]);
i++;
byte = (byte<<3) + VAL(strtext[i]);
buffer[j++] = byte;
}
else
{
buffer[j++] = strtext[i++];
}

In fact, the "else" part is identical to the normal (non-escaped) part of
the loop, so it could probably be merged--leaving only the octal parsing
part as a special case.

Then the whole loop could become something like this:

[unsigned char c;]

for (i=j=buflen=0; i<(int)strtextlen; ++i, buffer[j++]=c)
{
c = strtext[i];
if (c == '\\')
{
c = strtext[i++]; /* Skip backslash */
if (isdigit(c) && isdigit(strtext[i+1]) && isdigit(strtext[i+2]))
{
/* Parse octal number */
byte = VAL(strtext[i++]);
byte = (byte << 3) + VAL(strtext[i++]);
c = (byte << 3) + VAL(strtext[i]);
}
}
}

...Which saves 8 lines, reduces the number of special cases, adds some
comments, and permits arbitrary characters to be escaped.

Jeroen

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Jeroen T. Vermeulen 2003-10-30 20:31:31 Re: PQunescapeBytea code
Previous Message Jan Wieck 2003-10-30 19:13:01 7.4 and 7.3.5 showstopper (was: Re: Bug in Rule+Foreing key constrain?)