Uploaded image for project: 'SonarCFamily'
  1. SonarCFamily
  2. CPP-3133

S874: False positive when combining operations due to integer promotion

    XMLWordPrintable

    Details

    • Type: False-Positive
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 6.20.1
    • Fix Version/s: 6.26
    • Component/s: Rules
    • Labels:
      None

      Description

      https://peach.sonarsource.com/project/issues?id=c-family%3Acmake&issues=AWPdaCMW1M8o-ilJzwyE&open=AWPdaCMW1M8o-ilJzwyE
      RSPEC-874

      The rule gets triggered 3 times in the following code, with the & operator...

      void Base32Encode5(const unsigned char src[5], char dst[8])
      {
        // [0]:5 bits
        dst[0] = Base32EncodeChar((src[0] >> 3) & 0x1F);
      
        // [0]:3 bits + [1]:2 bits
        dst[1] = Base32EncodeChar(((src[0] << 2) & 0x1C) + ((src[1] >> 6) & 0x03));
      }
      

      -According to the rules of the standard, 0x1F is a signed constant, yet it does not differ from its unsigned equivalent, and maybe we should exclude positive signed constants from this rule.- Positive constants are already handled

       

      src[0] is unsigned, 3 is a positive number, but src[0] >> 3 is a signed integer according to the language rules. Therefore, (src[0] >> 3) & 0x1F is considered as an issue.

      To remove this FP, we should not only look at the type of the operands of a bitwise operation, but go to the leafs of a full bitwise expression, and if all those leafs have the right type, not report the issue. (this is similar to what the essential type model of MISRA is trying to achieve).

      Additionally, it looks like the rules about bitwise operations have changed in C++20, so we should revisit if this rule still makes sense...
       

        Attachments

          Activity

            People

            Assignee:
            loic.joly Loïc Joly
            Reporter:
            loic.joly Loïc Joly
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: