Huge thanks to our Platinum Members Endace and LiveAction,
and our Silver Member Veeam, for supporting the Wireshark Foundation and project.

Wireshark-dev: Re: [Wireshark-dev] Sprintf weirdness

From: Guy Harris <guy@xxxxxxxxxxxx>
Date: Tue, 1 Jun 2010 21:44:08 -0700
On Jun 1, 2010, at 9:01 PM, Ian Schorr wrote:

> 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);

From ANSI(R) X3.159-1989, American National Standard for Information Systems -- Programming Language -- C:

	4.9.6.5  The sprintf function

		...

	... If copying takes place between objects that overlap, the behavior is undefined.

Translation: don't do that.

> 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);

Don't do that, either, even if you happen to be lucky enough, on a particular platform, that it happens to do what you want to do; that's an accident of the sprintf() implementation on the OS/compiler combination you're using.

If you want to append to a string, make the target of the sprintf the location of the trailing '\0'.