Re: Problem with using snprintf in C-function - Mailing list pgsql-general

From Tom Lane
Subject Re: Problem with using snprintf in C-function
Date
Msg-id 26492.1255797972@sss.pgh.pa.us
Whole thread Raw
In response to Problem with using snprintf in C-function  (Ilya Urikh <ilya.urikh@gmail.com>)
List pgsql-general
Ilya Urikh <ilya.urikh@gmail.com> writes:
> This code I wrote for PostgreSQL 8.3.7 on Fedora 10 and it worked fine. But
> now I use CentOS 5.3 and PostgreSQL 8.3.8.

It looks to me like you're assuming that %ld is the appropriate format
string for an int64 value.  This might be true on a 64-bit machine but
it would not be on 32-bit.  It's not obvious why that would lead to
exactly the results you show, but it could certainly account for
snprintf fetching the wrong bits for some of its arguments.

The portable way (within Postgres code) is to use INT64_FORMAT, which
will be set up correctly by configure.  So instead of

>              WHERE  accountId = %ld AND\

you need

               WHERE  accountId = " INT64_FORMAT " AND\

(relying on concatenation of literals in C).

BTW, if your compiler didn't warn you about this, you need to get a
new compiler, or at least start using the switches that would make it
warn you.  If you did get a warning, and ignored it, you need to
learn better habits ;-)

            regards, tom lane

pgsql-general by date:

Previous
From: Tim Landscheidt
Date:
Subject: Re: XPath PostgreSQL 8.4
Next
From: Christophe Pettus
Date:
Subject: Re: 10/13 SFPUG meeting, "The Mighty GUCS," video now available