Re: Problem with using snprintf in C-function

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Ilya Urikh <ilya(dot)urikh(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Problem with using snprintf in C-function
Date: 2009-10-17 16:46:12
Message-ID: 26492.1255797972@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Ilya Urikh <ilya(dot)urikh(at)gmail(dot)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Christophe Pettus 2009-10-17 22:55:17 Re: 10/13 SFPUG meeting, "The Mighty GUCS," video now available
Previous Message Tim Landscheidt 2009-10-17 16:02:00 Re: XPath PostgreSQL 8.4