Re: (yet) more buffer paranoia

From: Neil Conway <neilc(at)samurai(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: PostgreSQL Patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: (yet) more buffer paranoia
Date: 2002-08-24 05:04:12
Message-ID: 878z2walv7.fsf@mailbox.samurai.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-patches

Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> writes:
> Neil Conway <neilc(at)samurai(dot)com> writes:
> > This patches replaces a few more usages of strcpy() and sprintf() when
> > copying into a fixed-size buffer (in this case, a buffer of
> > NAMEDATALEN bytes). AFAICT nothing to worry about here, but worth
> > fixing anyway...
>
> I'm not that eager to replace every single sprintf with snprintf.
> Most of these are obviously safe and do not need the clutter and
> extra cycles...

Hmm, can't seem to see much performance difference:

#include <stdio.h>

int
main(void)
{
char buf[128];
int i;

memset(buf, 0, sizeof(buf));

for (i = 0; i < 1000000; i++)
{
snprintf(buf, sizeof(buf), "%d %d %d %d %d %d %d %d %d %d %d",
i, i + i, i, i + i, i, i + i, i, i + 5, i, i, i);
}
}

With snprintf():

./a.out 7.76s user 0.04s system 99% cpu 7.848 total
./a.out 7.77s user 0.03s system 99% cpu 7.846 total
./a.out 7.70s user 0.09s system 99% cpu 7.867 total
./a.out 7.76s user 0.02s system 99% cpu 7.841 total
./a.out 7.74s user 0.03s system 98% cpu 7.852 total

With sprintf():

./a.out 7.83s user 0.09s system 98% cpu 8.042 total
./a.out 7.89s user 0.04s system 96% cpu 8.247 total
./a.out 7.83s user 0.02s system 98% cpu 7.937 total
./a.out 7.87s user 0.01s system 98% cpu 7.972 total
./a.out 7.80s user 0.01s system 98% cpu 7.918 total

If you're going to make a case for a performance hit, I'd agree that's
a concern -- but I'd prefer some actual data over vague claims of
"extra cycles".

Cheers,

Neil

--
Neil Conway <neilc(at)samurai(dot)com> || PGP Key ID: DB3C29FC

In response to

Browse pgsql-patches by date

  From Date Subject
Next Message Bruce Momjian 2002-08-24 11:31:47 Re: (yet) more buffer paranoia
Previous Message Tom Lane 2002-08-24 04:52:55 Re: (yet) more buffer paranoia