univalue: Avoid brittle, narrowing and verbose integral type confusions #25611

pull MarcoFalke wants to merge 2 commits into bitcoin:master from MarcoFalke:2207-univalue-types-🙉 changing 3 files +24 −26
  1. MarcoFalke commented at 9:06 AM on July 14, 2022: member

    As UniValue provides several constructors for integral types, the compiler is unable to select one if the passed type does not exactly match. This is unintuitive for developers and forces them to write verbose and brittle code. (Refer to -Wnarrowing compiler warning)

    For example, there are many places where an unsigned int is cast to a signed int. While the cast is safe in practice, it is still needlessly verbose and confusing as the value can never be negative. In fact it might even be unsafe if the unsigned value is large enough to map to a negative signed one.

    Fix this issue and other (minor) type issues.

  2. MarcoFalke added the label Refactoring on Jul 14, 2022
  3. rpc: Select int-UniValue constructor for enum value in upgradewallet RPC
    UniValue does not have a constructor for enum values, however the
    compiler will decay the enum into an int and select that constructor.
    Avoid this compiler magic and clarify the code by explicitly selecting
    the int-constructor.
    
    This is needed for the next commit.
    fa3a9a1e8d
  4. MarcoFalke force-pushed on Jul 14, 2022
  5. MarcoFalke force-pushed on Jul 14, 2022
  6. univalue: Avoid narrowing and verbose int constructors
    As UniValue provides several constructors for integral types, the
    compiler is unable to select one if the passed type does not exactly
    match. This is unintuitive for developers and forces them to write
    verbose and brittle code.
    
    For example, there are many places where an unsigned int is cast to a
    signed int. While the cast is safe in practice, it is still needlessly
    verbose and confusing as the value can never be negative. In fact it
    might even be unsafe if the unsigned value is large enough to map to a
    negative signed one.
    fa23c19750
  7. MarcoFalke force-pushed on Jul 14, 2022
  8. MarcoFalke force-pushed on Jul 14, 2022
  9. MarcoFalke force-pushed on Jul 14, 2022
  10. aureleoules commented at 2:39 PM on July 18, 2022: member

    ACK fa23c197509f692a815193acc1b50bad2fcbedfe. I verified that this change prevents type narrowing when not casting explicitly.

    nit: shouldn't the implementation be moved to univalue.cpp for consistency? https://github.com/bitcoin/bitcoin/blob/fa23c197509f692a815193acc1b50bad2fcbedfe/src/univalue/include/univalue.h#L55

  11. MarcoFalke commented at 2:46 PM on July 18, 2022: member

    nit: shouldn't the implementation be moved to univalue.cpp for consistency?

    I think oneline stuff is fine to keep inlined. Also, seems unrelated to the changes here?

  12. fanquake merged this on Jul 25, 2022
  13. fanquake closed this on Jul 25, 2022

  14. MarcoFalke deleted the branch on Jul 25, 2022
  15. sidhujag referenced this in commit 3cccd7835f on Jul 25, 2022
  16. bitcoin locked this on Jul 25, 2023

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-17 06:13 UTC

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