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

Rule (C & C++): sizeof(sizeof(...)) should not be used

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.5
    • Component/s: Rules
    • Labels:
      None

      Description

      Title

      sizeof(sizeof(...)) should not be used

      Description

      <p>
      A call to <code>sizeof(sizeof(...))</code> is equivalent to <code>sizeof(size_t)</code>, and therefore indicates a misuse of the <code>sizeof</code> construct.
      </p>
      
      <p>The following code:</p>
      
      <pre>
      #include &lt;string.h&gt;
      
      int main(int argc, char* argv[])
      {
        char buffer[42];
        char buffer2[sizeof(sizeof(buffer))]; /* Non-Compliant - a single sizeof() was intended */
      
        memcpy(buffer, "Hello, world!", strlen("Hello, world!")+1);
        memcpy(buffer2, buffer, sizeof(buffer)); /* Buffer overflow */
      
        return 0;
      }
      </pre>
      
      <p>should be refactored into:</p>
      
      <pre>
      #include &lt;string.h&gt;
      
      int main(int argc, char* argv[])
      {
        char buffer[42];
        char buffer2[sizeof(buffer)]; /* Compliant */
      
        memcpy(buffer, "Hello, world!", strlen("Hello, world!")+1);
        memcpy(buffer2, buffer, sizeof(buffer));
      
        return 0;
      }
      </pre>
      

      Message

      Remove the inner sizeof.

      Severity

      Critical

      In Sonar way?

      Yes

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                dinesh.bolkensteyn Dinesh Bolkensteyn (Inactive)
                Reporter:
                dinesh.bolkensteyn Dinesh Bolkensteyn (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: