Re: [HACKERS] snprintf causes regression tests to fail - Mailing list pgsql-hackers-win32

From Tom Lane
Subject Re: [HACKERS] snprintf causes regression tests to fail
Date
Msg-id 2719.1109721223@sss.pgh.pa.us
Whole thread Raw
In response to Re: [HACKERS] snprintf causes regression tests to fail  (Nicolai Tufar <ntufar@gmail.com>)
List pgsql-hackers-win32
Nicolai Tufar <ntufar@gmail.com> writes:
> On Tue, 01 Mar 2005 18:15:49 -0500, Tom Lane <tgl@sss.pgh.pa.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

pgsql-hackers-win32 by date:

Previous
From: Nicolai Tufar
Date:
Subject: Re: [HACKERS] snprintf causes regression tests to fail
Next
From: Tom Lane
Date:
Subject: Re: [HACKERS] snprintf causes regression tests to fail