Re: [HACKERS] snprintf causes regression tests to fail

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Nicolai Tufar <ntufar(at)gmail(dot)com>
Cc: Joerg Hessdoerfer <Joerg(dot)Hessdoerfer(at)sea-gmbh(dot)com>, Magnus Hagander <mha(at)sollentuna(dot)net>, Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>, pgsql-hackers-win32(at)postgresql(dot)org
Subject: Re: [HACKERS] snprintf causes regression tests to fail
Date: 2005-03-01 23:53:43
Message-ID: 2719.1109721223@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-hackers-win32

Nicolai Tufar <ntufar(at)gmail(dot)com> writes:
> On Tue, 01 Mar 2005 18:15:49 -0500, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> Did you try combinations of parameters of different sizes? For instance
>>
>> snprintf(..., "%g %d", doubleval, intval);
>> and
>> snprintf(..., "%2$d %1$g", doubleval, intval);

> It works perfectly fine. Just checked.

Well, whatever architecture you're checking on may happen to make it
work, but that does *not* mean it's portable. On my machine it fails:

int main()
{
char buf[1000];
double d = 3.14159265358979;
int i = 42;

snprintf(buf, sizeof buf, "%.15g %d", d, i);
printf("result = '%s'\n", buf);
snprintf(buf, sizeof buf, "%2$d %1$.15g", d, i);
printf("result = '%s'\n", buf);
return 0;
}

"Correct" output with the system snprintf is

result = '3.14159265358979 42'
result = '42 3.14159265358979'

With CVS-tip snprintf I get

result = '3 42'
result = '3 3505'

The second value in the last line varies erratically from run to run,
presumably because it's picking up garbage. This output appears to
indicate some problems in passing around precision specs as well as the
values themselves...

regards, tom lane

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 2005-03-02 00:00:48 Re: [pgsql-hackers-win32] snprintf causes regression tests to fail
Previous Message Nicolai Tufar 2005-03-01 23:37:56 Re: [HACKERS] snprintf causes regression tests to fail

Browse pgsql-hackers-win32 by date

  From Date Subject
Next Message Bruce Momjian 2005-03-02 00:00:48 Re: [pgsql-hackers-win32] snprintf causes regression tests to fail
Previous Message Nicolai Tufar 2005-03-01 23:37:56 Re: [HACKERS] snprintf causes regression tests to fail