Wireshark mailing list archives

Re: Sprintf weirdness


From: Ian Schorr <ian.schorr () gmail com>
Date: Wed, 2 Jun 2010 15:05:48 +1000

Thanks as always, Guy.  I'm sure it's going to be something LIKE that,
and I'll do that from now on, but I don't think it's the problem in
THIS case.

It seems like sprintf really doesn't like guint64 for some reason.

So if instead I do:
last_fh_hash=8000;
sprintf (mystring+strlen(mystring), "FH: 0x%08x Offset: %lu Length:
%u, test4:%u, test5:%u, test6:%u", last_fh_hash, file_offset, 3, 4, 5,
6);

...Then I get:
LOCK FH: 0x7a974bdc Offset: 8000 Length: 8000 test4: 3 test5: 4 test6: 5

At first I thought the guint64 was spilling over, but it looks like
it's actually being write twice, and sprintf is getting confused about
what to write where.  g_sprintf has the same problem as well.
Bizarre.

Thanks,
Ian


On Wed, Jun 2, 2010 at 2:44 PM, Guy Harris <guy () alum mit edu> wrote:

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'.
___________________________________________________________________________
Sent via:    Wireshark-dev mailing list <wireshark-dev () wireshark org>
Archives:    http://www.wireshark.org/lists/wireshark-dev
Unsubscribe: https://wireshark.org/mailman/options/wireshark-dev
            mailto:wireshark-dev-request () wireshark org?subject=unsubscribe

___________________________________________________________________________
Sent via:    Wireshark-dev mailing list <wireshark-dev () wireshark org>
Archives:    http://www.wireshark.org/lists/wireshark-dev
Unsubscribe: https://wireshark.org/mailman/options/wireshark-dev
             mailto:wireshark-dev-request () wireshark org?subject=unsubscribe


Current thread: