question on some code.

Started by Chris Bowlbyover 20 years ago5 messages
#1Chris Bowlby
excalibur@accesswave.ca

Hi All,

I've been doing a code audit (review) for my own personal use of the
7.4.x series code base and have come across something that I'd like to get
more details on, if it is possible to do so.

I've been going over the communications section and am curious how this
segment of code is actually working:

/* --------------------------------
* pq_sendint - append a binary integer to a
StringInfo buffer
* --------------------------------
*/
void
pq_sendint(StringInfo buf, int i, int b)
{
unsigned char n8;
uint16 n16;
uint32 n32;

switch (b)
{
case 1:
n8 = (unsigned char) i;
appendBinaryStringInfo(buf, (char *) &n8, 1);
break;
case 2:
n16 = htons((uint16) i);
appendBinaryStringInfo(buf, (char *) &n16, 2);
break;
case 4:
n32 = htonl((uint32) i);
appendBinaryStringInfo(buf, (char *) &n32, 4);
break;
default:
elog(ERROR, "unsupported integer size %d", b);
break;
}
}

I understand the concept of the code, to append binary values to a string
buffer (char *), but, under my compiler on FreeBSD 5.4.x (gcc (GCC) 3.4.2
[FreeBSD] 20040728) I see a few issues that have cropped up.

If I understand the code right, your trying to pass in to
appendBinaryStringInfo an "address" or reference to the n8, n16, or n32
variables and cast them so that a char * pointer can access that address
space. Through some testing that I've been doing (outputting the values in
the appendBinaryStringInfo function), I never seem to see any data, The
variable pointer that references the "n8, n16, or n32" value is not holding
any data in the appendBinaryStringInfo function.

This might be my miss-understanding of the function and was hoping
someone would be willing to clearify it for me?

#2Korry
korry@starband.net
In reply to: Chris Bowlby (#1)
Re: question on some code.

If I understand the code right, your trying to pass in to
appendBinaryStringInfo an "address" or reference to the n8, n16, or n32
variables and cast them so that a char * pointer can access that address
space. Through some testing that I've been doing (outputting the values in
the appendBinaryStringInfo function), I never seem to see any data, The
variable pointer that references the "n8, n16, or n32" value is not holding
any data in the appendBinaryStringInfo function.

How are you looking at the data? If you're treating the pointer as if
it were a null-terminated string, it's likely that the first byte is a
null character much of the time. Since this is binary data (not
character data), you'll need to look at the individual bytes. If you're
using gdb, use 'x' command instead of the 'p' command. If you've thrown
a printf() into the code, you can't use a "%s" format specifier, you'll
have to look at the bytes one at a time using something like "%02X".

-- Korry

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Chris Bowlby (#1)
Re: question on some code.

Chris Bowlby <excalibur@accesswave.ca> writes:

I understand the concept of the code, to append binary values to a string
buffer (char *), but, under my compiler on FreeBSD 5.4.x (gcc (GCC) 3.4.2
[FreeBSD] 20040728) I see a few issues that have cropped up.

You've got a broken compiler then, because it certainly works for
everyone else. That function's been substantially unchanged since
... hmm ... version 1.1 of pqformat.c dated 25 Apr 1999. And I'm
fairly sure that I got it from some older code rather than writing
it from scratch when I made that file.

Through some testing that I've been doing (outputting the values in
the appendBinaryStringInfo function), I never seem to see any data, The
variable pointer that references the "n8, n16, or n32" value is not holding
any data in the appendBinaryStringInfo function.

Define "never seem to see any data"...

regards, tom lane

#4Chris Bowlby
excalibur@accesswave.ca
In reply to: Korry (#2)
Re: question on some code.

Ahh, so it's not attempting to turn it into an "acsii" string, just storing
raw binary data... Ok that makes sense now, thanks for the help.

At 02:54 PM 7/19/2005, Korry wrote:

Show quoted text

If I understand the code right, your trying to pass in to
appendBinaryStringInfo an "address" or reference to the n8, n16, or n32
variables and cast them so that a char * pointer can access that address
space. Through some testing that I've been doing (outputting the values in
the appendBinaryStringInfo function), I never seem to see any data, The
variable pointer that references the "n8, n16, or n32" value is not holding
any data in the appendBinaryStringInfo function.

How are you looking at the data? If you're treating the pointer as if it
were a null-terminated string, it's likely that the first byte is a null
character much of the time. Since this is binary data (not character
data), you'll need to look at the individual bytes. If you're using gdb,
use 'x' command instead of the 'p' command. If you've thrown a printf()
into the code, you can't use a "%s" format specifier, you'll have to look
at the bytes one at a time using something like "%02X".

-- Korry

#5Chris Bowlby
excalibur@accesswave.ca
In reply to: Tom Lane (#3)
Re: question on some code.

Hi Tom,

good to hear from you. It was my miss-understanding of what the code was
doing, not the issue with the compiler :>

At 03:22 PM 7/19/2005, Tom Lane wrote:

Show quoted text

Chris Bowlby <excalibur@accesswave.ca> writes:

I understand the concept of the code, to append binary values to a string
buffer (char *), but, under my compiler on FreeBSD 5.4.x (gcc (GCC) 3.4.2
[FreeBSD] 20040728) I see a few issues that have cropped up.

You've got a broken compiler then, because it certainly works for
everyone else. That function's been substantially unchanged since
... hmm ... version 1.1 of pqformat.c dated 25 Apr 1999. And I'm
fairly sure that I got it from some older code rather than writing
it from scratch when I made that file.

Through some testing that I've been doing (outputting the values in
the appendBinaryStringInfo function), I never seem to see any data, The
variable pointer that references the "n8, n16, or n32" value is not

holding

any data in the appendBinaryStringInfo function.

Define "never seem to see any data"...

regards, tom lane