util: Add ParseUInt32 and ParseUInt64 #8168

pull laanwj wants to merge 1 commits into bitcoin:master from laanwj:2016_06_parseuint changing 4 files +112 −1
  1. laanwj commented at 8:26 AM on June 8, 2016: member

    Add error and range-checking parsers for unsigned 32 and 64 bit numbers. The 32-bit variant is required for parsing sequence numbers from the command line in bitcoin-tx (see #8164 for discussion). I've thrown in the 64-bit variant as a bonus, as I'm sure it will be needed at some point.

    Also adds tests, and updates developer-notes.md.

  2. laanwj added the label Utils and libraries on Jun 8, 2016
  3. laanwj force-pushed on Jun 8, 2016
  4. util: Add ParseUInt32 and ParseUInt64
    Add error and range-checking parsers for unsigned 32 and 64 bit numbers.
    The 32-bit variant is required for parsing sequence numbers from the
    command line in `bitcoin-tx` (see #8164 for discussion). I've thrown in
    the 64-bit variant as a bonus, as I'm sure it will be needed at some
    point.
    
    Also adds tests, and updates `developer-notes.md`.
    e012f3cea0
  5. laanwj force-pushed on Jun 8, 2016
  6. MarcoFalke commented at 9:31 AM on June 8, 2016: member

    utACK e012f3c. I think we also need them in init.cpp

  7. jonasschnelli commented at 10:57 AM on June 8, 2016: contributor

    utACK e012f3cea0ca4096dd4dd59a356a973c43651912

  8. in src/utilstrencodings.cpp:None in e012f3cea0
     472 | +    unsigned long int n = strtoul(str.c_str(), &endp, 10);
     473 | +    if(out) *out = (uint32_t)n;
     474 | +    // Note that strtoul returns a *unsigned long int*, so even if it doesn't report a over/underflow
     475 | +    // we still have to check that the returned value is within the range of an *uint32_t*. On 64-bit
     476 | +    // platforms the size of these types may be different.
     477 | +    return endp && *endp == 0 && !errno &&
    


    sipa commented at 11:58 AM on June 8, 2016:

    Instead of checking whether *endp == 0, shouldn't you check whether endp == str.c_str() + str.size() (otherwise you'd accept strings that have a 0 inside)


    sipa commented at 12:00 PM on June 8, 2016:

    Ah, ParsePrechecks catches this.


    laanwj commented at 12:30 PM on June 8, 2016:

    Yes, that solution would be somewhat more elegant. Indeed, ParsePrechecks checks some number-parsing preconditions such as that one.

    I'm thinking about (at some point) implementing these functions from scratch instead of calling out to the C API. It wouldn't even be much longer, as there are just too many exceptions. This still doesn't exclude strto*l accepting locale-specific separators, for example. Not a big deal for current uses, but I hate such surprises.

    In any case introducing them here (with tests) allows swapping out the implementation later.


    sipa commented at 12:33 PM on June 8, 2016:

    Yes, I was about to suggest that a from scratch implementation might be easier than trying to put a harness around existing libc calls. But let's not do that now. It works and seems sufficiently tested.

  9. sipa commented at 12:27 PM on June 8, 2016: member

    utACK e012f3cea0ca4096dd4dd59a356a973c43651912

  10. theuni commented at 2:20 PM on June 8, 2016: member

    @laanwj Thanks for grabbing this so quickly! A generic utility function is the way to go for sure.

    utACK e012f3cea0ca4096dd4dd59a356a973c43651912

  11. laanwj merged this on Jun 9, 2016
  12. laanwj closed this on Jun 9, 2016

  13. laanwj referenced this in commit d36618585d on Jun 9, 2016
  14. codablock referenced this in commit bc34b1d9a7 on Sep 16, 2017
  15. codablock referenced this in commit 72f00137a7 on Sep 19, 2017
  16. codablock referenced this in commit ca228f09ff on Dec 22, 2017
  17. andvgal referenced this in commit 0550a104a6 on Jan 6, 2019
  18. MarcoFalke locked this on Sep 8, 2021

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-13 15:15 UTC

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