Re: [HACKERS] snprintf causes regression tests to fail

Tom Lane <[email protected]>
Newsgroups gmane.comp.db.postgresql.devel.win32
Message-ID <[email protected]>
Nicolai Tufar <[email protected]> writes:
>> Having looked at the current snprintf.c, I don't actually believe that
>> it works at all in the presence of positional parameter specs.

> It picks up arguments in order of appearance, places them in
> array then shuffles them according to %n$ positional parameter.
> I checked it with in many different combinations, it works!

Did you try combinations of parameters of different sizes?  For instance

	snprintf(..., "%g %d", doubleval, intval);
and
	snprintf(..., "%2$d %1$g", doubleval, intval);

You cannot pick this up in order of appearance and expect it to work.
It might fail to fail on some machine architectures, but it's not going
to be portable.

>> On the other side of the
>> coin, the hardwired 4K limit in printf() is certainly *not* enough.

> How would one solve this issue. Calling malloc() from a print function
> would be rather expensive.

printf shouldn't use a buffer at all.  I was thinking in terms of
passing around a state struct like

	typedef struct {
		char	*output;
		char	*end;
		FILE	*outfile;
	} printf_target;

and making dopr_outch look like

static void
dopr_outch(int c, printf_target *state)
{
	if (state->output)
	{
		if (state->end == NULL || state->output < state->end)
			*(state->output++) = c;
	}
	else
		fputc(c, state->outfile);
}

			regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
      joining column's datatypes do not match
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.