[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [ProgSoc] sprintf
At 15:25 02 Feb 1999 +1100, Vik wrote:
| does anyone know what the consequences of using sprintf with the same
| string being used as the format string and as the buffer string might
| be(sorry if that sentence is grammatically incorrect - caffeine level is
| low atm:)
|
| as in:
|
| char s[20];
| strcpy(s, "%8.2f");
| sprintf(s, s, 78.33243);
It seems to me that this is a remarkably bad idea. I'm sure you'll get
slapped on the wrist for thinking about it by a few other people, too.
*wink*
As for the answer, 'undefined' does sort of leap to mind.
'unspecified', 'platform, library dependent' and 'segmentation fault' do
as well.
Under Linux 2.2, (looking at the source code) the implementation seems
what you would expect. As long as you don't have any % codes in there
anywhere, you might even be okay, with it being an inefficient no-op.
If you put in %'s, from that point on in the string, you're going to
start overwriting the format before it's been processed. If you were
real clever, I guess you could use this to insert another % code, and
let the routine catch up and do another subsitution on the newly
inserted code. That could be fun to play with, but on the whole, it
sounds ill-advised.
For the example you have above, you'll probably corrupt the data, as the
scanner copies data from the end over the beginning.
I'd leave it alone.
{ Can you tell I was bored tonight? }
Paul
PGP signature