(perhaps) strange bug in our printf function!? #1290

issue Diapolo opened this issue on May 13, 2012
  1. Diapolo commented at 8:08 PM on May 13, 2012: none

    <pre>printf("CBlock::WriteToDisk() : Truncated block file %s (new size: %u bytes)\n", std::string.c_str(), uint64 &);</pre>

    Result: "CBlock::WriteToDisk() : Preallocated C:\Bitcoin\testnet\blk0001.dat bytes for block file 2130706432" -> good

    <pre>printf("CBlock::WriteToDisk() : Truncated block file %u (new size: %s bytes)\n", uint64 &, std::string.c_str());</pre>

    Result: "CBlock::WriteToDisk() : Preallocated 2130706432 bytes for block file (null)" -> bad

    I can't explain, but the uint64 before the reference to a std::string works, reverse order and the std::string displays as (null). Can a dev try to reproduce this or has an idea? This is an occurence in an own-build, so perhaps not a global problem!

  2. laanwj commented at 8:13 PM on May 13, 2012: member

    Is %u the right format character for an int64? I don't think so -- we have these:

    util.h:

    #if defined(_MSC_VER) || defined(__MSVCRT__)
    #define PRI64d  "I64d"
    #define PRI64u  "I64u"
    #define PRI64x  "I64x"
    #else
    #define PRI64d  "lld"
    #define PRI64u  "llu"
    #define PRI64x  "llx"
    #endif
    
  3. sipa commented at 8:16 PM on May 13, 2012: member

    %u is unsigned integer, usually 32 bits. uint64 obviously is 64. So you push a 64-bit value on the stack,, but %u only consumes the first 32 bits of it (which, if the the number is less than 2^32, is the right value). It leaves the higher part to be interpreted as a string pointer, though.

  4. Diapolo commented at 8:22 PM on May 13, 2012: none

    I knew it's a 64 bit value, what I didn't know was that we have a %"PRI64u" define. So %llu should be fine. Will try and report back, thanks!

  5. laanwj commented at 8:24 PM on May 13, 2012: member

    @sipa is right, what you see is consistent with little-endian. This is, BTW, why people concerned with type-safety hate printf. Some compilers will warn you for these mistakes with -Wformat.

  6. Diapolo commented at 8:32 PM on May 13, 2012: none

    Great, now it works and thanks for that quick help -> closed!

  7. Diapolo closed this on May 13, 2012

  8. sipa commented at 9:28 PM on May 13, 2012: member

    No, %llu is not fine. uint64_t is not guaranteed to be the same as unsigned long long. Just use PRI64u.

  9. Diapolo commented at 9:31 PM on May 13, 2012: none

    @sipa: That's what I did, thanks again.

  10. suprnurd referenced this in commit 04ed5db62c on Dec 5, 2017
  11. lateminer referenced this in commit d2f9d8df0e on Jan 22, 2019
  12. lateminer referenced this in commit 2ceeb2cca0 on May 6, 2020
  13. lateminer referenced this in commit 16d7dac5f7 on May 6, 2020
  14. DrahtBot locked this on Sep 8, 2021
Contributors

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-21 18:16 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me