Wireshark-dev: [Wireshark-dev] Sprintf weirdness
From: Ian Schorr <ian.schorr@xxxxxxxxx>
Date: Wed, 2 Jun 2010 14:01:13 +1000
Hello list,

This isn't exactly a Wireshark-specific question, but it's coming up
while I'm working on a dissector, and I'm sure someone here will know
the answer, so...  =)

I'm trying to use sprintf() to append to an existing string with some
formatted text.  Obviously there's several ways to do this, but
sprintf() seemed to be most efficient for the way I'm doing things.  I
end up appending this string to the Info Column later, but that seems
irrelevant.

For example, I have 4 variables:
- string mystring, with value "LOCK"
- guint32 last_fh_hash, with value "2056735708"
- guint64 file_offset, with value 0
- guint64 lock_length, with value 10

The weird thing is that when I do this:

sprintf (mystring, "%s FH: 0x%08x Offset: %lu Length: %lu",
mystring,last_fh_hash,file_offset,lock_length);

...then "mystring" becomes "LOCK FH: 0x7a974bdc Offset: 0 Length: 0".
Length is WRONG.  It is wrong in a very consistent way.

But if I do this:

sprintf (mystring, "%s FH: 0x%08x", mystring,last_fh_hash);
sprintf (mystring, "%s Offset: %u", mystring,file_offset);
sprintf (mystring, "%s Length: %u", mystring,lock_length);

Then the resulting value of mystring is correct.  "LOCK FH: 0x7a974bdc
Offset: 0 Length: 10".  In fact, if I flip the positions of
"file_offset" and "lock_length" then things are fine, regardless of
their values.

It's difficult to reproduce or debug.  I have a number of similar
statements scattered throughout code and each has varying degrees of
strangeness.  Some work properly.  In some cases the values are
actually flipped (one variable printed one place, the other in
another).  In some cases the values are actually empty.  I can't
imagine it has anything to do with the way the string was declared or
memory allocated in the first place, it looks like sprintf() is simply
writing out the wrong values to memory for some reason.

Anybody have any thoughts on why that might be?  I'm assuming I've
done something silly, though having a tough time guessing where.

I haven't tested yet to see if this is something specific to the dev
platform I'm using.  At the moment that's Windows.

Thanks,
Ian