Signedness of an Enum is undefined in standard C++ #6017

issue sinetek opened this issue on April 15, 2015
  1. sinetek commented at 5:34 PM on April 15, 2015: contributor

    I am reviewing code, and came accross the following in chain.h.

    //! Check whether this block index entry is valid up to the passed validity level.
    bool IsValid(enum BlockStatus nUpTo = BLOCK_VALID_TRANSACTIONS) const
    {
        assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
        if (nStatus & BLOCK_FAILED_MASK)
            return false;
        return ((nStatus & BLOCK_VALID_MASK) >= nUpTo);
    }
    
    //! Raise the validity level of this block index entry.
    //! Returns true if the validity was changed.
    bool RaiseValidity(enum BlockStatus nUpTo)
    {
        assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed.
        if (nStatus & BLOCK_FAILED_MASK)
            return false;
        if ((nStatus & BLOCK_VALID_MASK) < nUpTo) {
            nStatus = (nStatus & ~BLOCK_VALID_MASK) | nUpTo;
            return true;
        }
        return false;
    }
    

    Now I don't think there is a problem per se, but from what I can see the standard says that nUpTo may be either signed or unsigned. Is this a problem when combined with the various bitwise operations, or even comparison operator < ? My compiler complains.

    Edit: Here's what the C++03 standard (ISO/IEC 14882:2003) document says in 7.2-5 (Enumeration declarations):

    The underlying type of an enumeration is an integral type that can represent all the enumerator values defined in the enumeration. It is implementation-defined which integral type is used as the underlying type for an enumeration except that the underlying type shall not be larger than int unless the value of an enumerator cannot fit in an int or unsigned int.

  2. sinetek closed this on Apr 15, 2015

  3. sinetek reopened this on Apr 15, 2015

  4. laanwj commented at 12:12 PM on April 20, 2015: member

    Right. May make sense to change the passed-in type to unsigned int instead. On the other hand, with C++11 it is possible to define the type of an enumeration and this problem goes away, so as this doesn't cause any immediate problems may be better to wait for that.

  5. laanwj added the label Refactoring on Feb 16, 2016
  6. laanwj commented at 11:33 AM on April 28, 2016: member

    This could be addressed now that we've started using c++11.

  7. laanwj referenced this in commit 073225cb01 on Apr 28, 2016
  8. sinetek commented at 12:47 PM on April 28, 2016: contributor

    Awesome. Thanks.

  9. laanwj closed this on May 2, 2016

  10. deadalnix referenced this in commit bc883470e5 on Jan 11, 2017
  11. deadalnix referenced this in commit a8d7581e40 on Jan 11, 2017
  12. deadalnix referenced this in commit a3315b653b on Jan 15, 2017
  13. deadalnix referenced this in commit 17cfc6082d on Jan 16, 2017
  14. deadalnix referenced this in commit 40674f6950 on Jan 17, 2017
  15. deadalnix referenced this in commit c303674d6a on Jan 19, 2017
  16. deadalnix referenced this in commit e5d38f2aba on Jan 19, 2017
  17. 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 18:15 UTC

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