Menu

Types of error

Felipe
2022-05-06
2022-05-09
  • Felipe

    Felipe - 2022-05-06

    Hello there!

    I am using Cppcheck in combination with Cxx SonarQube plugin to make C/C++ code analysis. After executing Cppcheck I get the report in XML format and send it to SonarQube so I can check more in a graphical interface all the issues of my code.
    After checking the results I see that Cppcheck does not differenciate between Bugs, Vulnerabilities or Security Hotspots as SonarQube does, there are only Bugs.

    This is the Cppcheck report file of my code:

    <?xml version="1.0" encoding="UTF-8"?>
    <results version="2">
        <cppcheck version="2.7"/>
        <errors>
            <error id="memleakOnRealloc" severity="error" msg="Common realloc mistake: &apos;buf&apos; nulled but not freed upon failure" verbose="Common realloc mistake: &apos;buf&apos; nulled but not freed upon failure" cwe="401" file0="src/C.c">
                <location file="src/C.c" line="391" column="12"/>
                <symbol>buf</symbol>
            </error>
            <error id="deallocuse" severity="error" msg="Dereferencing &apos;ip&apos; after it is deallocated / released" verbose="Dereferencing &apos;ip&apos; after it is deallocated / released" cwe="416" file0="src/component1.cc">
                <location file="src/component1.cc" line="37" column="14"/>
                <symbol>ip</symbol>
            </error>
            <error id="deallocuse" severity="error" msg="Dereferencing &apos;ip&apos; after it is deallocated / released" verbose="Dereferencing &apos;ip&apos; after it is deallocated / released" cwe="416" file0="src/component1.cc">
                <location file="src/component1.cc" line="42" column="6"/>
                <symbol>ip</symbol>
            </error>
            <error id="doubleFree" severity="error" msg="Memory pointed to by &apos;ip&apos; is freed twice." verbose="Memory pointed to by &apos;ip&apos; is freed twice." cwe="415" file0="src/component1.cc">
                <location file="src/component1.cc" line="47" column="12"/>
                <location file="src/component1.cc" line="46" column="5"/>
                <symbol>ip</symbol>
            </error>
            <error id="mismatchAllocDealloc" severity="error" msg="Mismatching allocation and deallocation: ip" verbose="Mismatching allocation and deallocation: ip" cwe="762" file0="src/component1.cc">
                <location file="src/component1.cc" line="51" column="15"/>
                <location file="src/component1.cc" line="50" column="10"/>
                <symbol>ip</symbol>
            </error>
            <error id="uninitvar" severity="error" msg="Uninitialized variable: a" verbose="Uninitialized variable: a" cwe="457" file0="src/component1.cc">
                <location file="src/component1.cc" line="32" column="8"/>
                <symbol>a</symbol>
            </error>
            <error id="deallocuse" severity="error" msg="Dereferencing &apos;ip&apos; after it is deallocated / released" verbose="Dereferencing &apos;ip&apos; after it is deallocated / released" cwe="416" file0="src/component2.cc">
                <location file="src/component2.cc" line="36" column="14"/>
                <symbol>ip</symbol>
            </error>
            <error id="deallocuse" severity="error" msg="Dereferencing &apos;ip&apos; after it is deallocated / released" verbose="Dereferencing &apos;ip&apos; after it is deallocated / released" cwe="416" file0="src/component2.cc">
                <location file="src/component2.cc" line="41" column="6"/>
                <symbol>ip</symbol>
            </error>
            <error id="doubleFree" severity="error" msg="Memory pointed to by &apos;ip&apos; is freed twice." verbose="Memory pointed to by &apos;ip&apos; is freed twice." cwe="415" file0="src/component2.cc">
                <location file="src/component2.cc" line="46" column="12"/>
                <location file="src/component2.cc" line="45" column="5"/>
                <symbol>ip</symbol>
            </error>
            <error id="mismatchAllocDealloc" severity="error" msg="Mismatching allocation and deallocation: ip" verbose="Mismatching allocation and deallocation: ip" cwe="762" file0="src/component2.cc">
                <location file="src/component2.cc" line="50" column="15"/>
                <location file="src/component2.cc" line="49" column="10"/>
                <symbol>ip</symbol>
            </error>
            <error id="uninitvar" severity="error" msg="Uninitialized variable: a" verbose="Uninitialized variable: a" cwe="457" file0="src/component2.cc">
                <location file="src/component2.cc" line="31" column="8"/>
                <symbol>a</symbol>
            </error>
            <error id="zerodiv" severity="error" msg="Division by zero." verbose="Division by zero." cwe="369" file0="src/main.cc">
                <location file="src/main.cc" line="9" column="15" info="Division by zero"/>
                <location file="src/main.cc" line="8" column="13" info="Assignment &apos;x=0&apos;, assigned value is 0"/>
            </error>
        </errors>
    </results>
    

    I see only errors and I think SonarQube take them all as Bugs, I don't know if there is something that I'm missing or what.

    Thanks in advance!

     

    Last edit: Felipe 2022-05-06
  • Daniel Marjamäki

    if you use --enable=style you should see other severities also.

     
    👍
    1
    • Felipe

      Felipe - 2022-05-09

      Hi @danielmarjamaki,

      If I use --enable=style I get the following results:

      <?xml version="1.0" encoding="UTF-8"?>
      <results version="2">
          <cppcheck version="2.7"/>
          <errors>
              <error id="memleakOnRealloc" severity="error" msg="Common realloc mistake: &apos;buf&apos; nulled but not freed upon failure" verbose="Common realloc mistake: &apos;buf&apos; nulled but not freed upon failure" cwe="401" file0="src/C.c">
                  <location file="src/C.c" line="391" column="12"/>
                  <symbol>buf</symbol>
              </error>
              <error id="variableScope" severity="style" msg="The scope of the variable &apos;e&apos; can be reduced." verbose="The scope of the variable &apos;e&apos; can be reduced. Warning: Be careful when fixingthis message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for &apos;i&apos; can be reduced:\012void f(int x)\012{\012    int i = 0;\012    if (x) {\012    // it&apos;s safe to move &apos;int i = 0;&apos; here\012        for (int n = 0; n &lt; 10; ++n) {\012            // it is possible but not safe to move &apos;int i = 0;&apos; here\012            do_something(&amp;i);\012        }\012    }\012}\012When you see this message it is always safe to reduce the variable scope 1 level." cwe="398" file0="src/C.c">
                  <location file="src/C.c" line="247" column="18"/>
                  <symbol>e</symbol>
              </error>
              <error id="variableScope" severity="style" msg="The scope of the variable &apos;filename&apos; can be reduced." verbose="The scope of the variable &apos;filename&apos; can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for &apos;i&apos; can be reduced:\012void f(int x)\012{\012    int i = 0;\012    if (x) {\012        // it&apos;s safe to move &apos;int i = 0;&apos; here\012        for (int n = 0; n &lt; 10; ++n) {\012            // it is possible but not safe to move &apos;int i = 0;&apos; here\012    do_something(&amp;i);\012        }\012    }\012}\012When you see this message it is always safe to reduce the variable scope 1 level." cwe="398" file0="src/C.c">
                  <location file="src/C.c" line="248" column="44"/>
                  <symbol>filename</symbol>
              </error>
              <error id="variableScope" severity="style" msg="The scope of the variable &apos;oldest_mtime&apos; can be reduced." verbose="The scope of the variable &apos;oldest_mtime&apos; can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for &apos;i&apos; can be reduced:\012void f(int x)\012{\012    int i = 0;\012    if (x) {\012        // it&apos;s safe to move &apos;int i = 0;&apos; here\012        for (int n = 0; n &lt; 10; ++n) {\012            // it is possible but not safe to move &apos;int i = 0;&apos; here\012            do_something(&amp;i);\012        }\012    }\012}\012When you see this message it is always safe to reduce the variable scope 1 level." cwe="398" file0="src/C.c">
                  <location file="src/C.c" line="249" column="10"/>
                  <symbol>oldest_mtime</symbol>
              </error>
              <error id="variableScope" severity="style" msg="The scope of the variable &apos;num_files&apos; can be reduced." verbose="The scope of the variable &apos;num_files&apos; can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for &apos;i&apos; can be reduced:\012void f(int x)\012{\012    int i = 0;\012if (x) {\012        // it&apos;s safe to move &apos;int i = 0;&apos; here\012        for (int n = 0; n &lt; 10; ++n) {\012            // it is possible but not safe to move &apos;int i = 0;&apos; here\012      do_something(&amp;i);\012        }\012    }\012}\012When you see this message it is always safe to reduce the variable scope 1 level." cwe="398" file0="src/C.c">
                  <location file="src/C.c" line="250" column="7"/>
                  <symbol>num_files</symbol>
              </error>
              <error id="variableScope" severity="style" msg="The scope of the variable &apos;comp_spec&apos; can be reduced." verbose="The scope of the variable &apos;comp_spec&apos; can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for &apos;i&apos; can be reduced:\012void f(int x)\012{\012    int i = 0;\012if (x) {\012        // it&apos;s safe to move &apos;int i = 0;&apos; here\012        for (int n = 0; n &lt; 10; ++n) {\012            // it is possible but not safe to move &apos;int i = 0;&apos; here\012      do_something(&amp;i);\012        }\012    }\012}\012When you see this message it is always safe to reduce the variable scope 1 level." cwe="398" file0="src/C.c">
                  <location file="src/C.c" line="294" column="8"/>
                  <symbol>comp_spec</symbol>
              </error>
              <error id="variableScope" severity="style" msg="The scope of the variable &apos;buf&apos; can be reduced." verbose="The scope of the variable &apos;buf&apos; can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for &apos;i&apos; can be reduced:\012void f(int x)\012{\012    int i = 0;\012    if (x) {\012        // it&apos;s safe to move &apos;int i = 0;&apos; here\012        for (int n = 0; n &lt; 10; ++n) {\012            // it is possible but not safe to move &apos;int i = 0;&apos; here\012            do_something(&amp;i);\012        }\012    }\012}\012When you see this message it is always safe to reduce the variable scope 1 level." cwe="398" file0="src/C.c">
                  <location file="src/C.c" line="503" column="18"/>
                  <symbol>buf</symbol>
              </error>
              <error id="deallocuse" severity="error" msg="Dereferencing &apos;ip&apos; after it is deallocated / released" verbose="Dereferencing &apos;ip&apos; after it is deallocated / released" cwe="416" file0="src/component1.cc">
                  <location file="src/component1.cc" line="37" column="14"/>
                  <symbol>ip</symbol>
              </error>
              <error id="deallocuse" severity="error" msg="Dereferencing &apos;ip&apos; after it is deallocated / released" verbose="Dereferencing &apos;ip&apos; after it is deallocated / released" cwe="416" file0="src/component1.cc">
                  <location file="src/component1.cc" line="42" column="6"/>
                  <symbol>ip</symbol>
              </error>
              <error id="doubleFree" severity="error" msg="Memory pointed to by &apos;ip&apos; is freed twice." verbose="Memory pointed to by &apos;ip&apos; is freed twice." cwe="415" file0="src/component1.cc">
                  <location file="src/component1.cc" line="47" column="12"/>
                  <location file="src/component1.cc" line="46" column="5"/>
                  <symbol>ip</symbol>
              </error>
              <error id="mismatchAllocDealloc" severity="error" msg="Mismatching allocation and deallocation: ip" verbose="Mismatching allocation and deallocation: ip" cwe="762" file0="src/component1.cc">
                  <location file="src/component1.cc" line="51" column="15"/>
                  <location file="src/component1.cc" line="50" column="10"/>
                  <symbol>ip</symbol>
              </error>
              <error id="uninitvar" severity="error" msg="Uninitialized variable: a" verbose="Uninitialized variable: a" cwe="457" file0="src/component1.cc">
                  <location file="src/component1.cc" line="32" column="8"/>
                  <symbol>a</symbol>
              </error>
              <error id="unusedVariable" severity="style" msg="Unused variable: x" verbose="Unused variable: x" cwe="563" file0="src/component1.cc">
                  <location file="src/component1.cc" line="17" column="18"/>
                  <symbol>x</symbol>
              </error>
              <error id="unreadVariable" severity="style" msg="Variable &apos;i&apos; is assigned a value that is never used." verbose="Variable &apos;i&apos; is assigned a value that is never used." cwe="563" file0="src/component1.cc">
                  <location file="src/component1.cc" line="37" column="11"/>
                  <symbol>i</symbol>
              </error>
              <error id="deallocuse" severity="error" msg="Dereferencing &apos;ip&apos; after it is deallocated / released" verbose="Dereferencing &apos;ip&apos; after it is deallocated / released" cwe="416" file0="src/component2.cc">
                  <location file="src/component2.cc" line="36" column="14"/>
                  <symbol>ip</symbol>
              </error>
              <error id="deallocuse" severity="error" msg="Dereferencing &apos;ip&apos; after it is deallocated / released" verbose="Dereferencing &apos;ip&apos; after it is deallocated / released" cwe="416" file0="src/component2.cc">
                  <location file="src/component2.cc" line="41" column="6"/>
                  <symbol>ip</symbol>
              </error>
              <error id="doubleFree" severity="error" msg="Memory pointed to by &apos;ip&apos; is freed twice." verbose="Memory pointed to by &apos;ip&apos; is freed twice." cwe="415" file0="src/component2.cc">
                  <location file="src/component2.cc" line="46" column="12"/>
                  <location file="src/component2.cc" line="45" column="5"/>
                  <symbol>ip</symbol>
              </error>
              <error id="mismatchAllocDealloc" severity="error" msg="Mismatching allocation and deallocation: ip" verbose="Mismatching allocation and deallocation: ip" cwe="762" file0="src/component2.cc">
                  <location file="src/component2.cc" line="50" column="15"/>
                  <location file="src/component2.cc" line="49" column="10"/>
                  <symbol>ip</symbol>
              </error>
              <error id="uninitvar" severity="error" msg="Uninitialized variable: a" verbose="Uninitialized variable: a" cwe="457" file0="src/component2.cc">
                  <location file="src/component2.cc" line="31" column="8"/>
                  <symbol>a</symbol>
              </error>
              <error id="unusedVariable" severity="style" msg="Unused variable: x" verbose="Unused variable: x" cwe="563" file0="src/component2.cc">
                  <location file="src/component2.cc" line="16" column="18"/>
                  <symbol>x</symbol>
              </error>
              <error id="unreadVariable" severity="style" msg="Variable &apos;i&apos; is assigned a value that is never used." verbose="Variable &apos;i&apos; is assigned a value that is never used." cwe="563" file0="src/component2.cc">
                  <location file="src/component2.cc" line="36" column="11"/>
                  <symbol>i</symbol>
              </error>
              <error id="zerodiv" severity="error" msg="Division by zero." verbose="Division by zero." cwe="369" file0="src/main.cc">
                  <location file="src/main.cc" line="9" column="15" info="Division by zero"/>
                  <location file="src/main.cc" line="8" column="13" info="Assignment &apos;x=0&apos;, assigned value is 0"/>
              </error>
              <error id="unreadVariable" severity="style" msg="Variable &apos;y&apos; is assigned a value that is never used." verbose="Variable &apos;y&apos; is assigned a value that is never used." cwe="563" file0="src/main.cc">
                  <location file="src/main.cc" line="9" column="11"/>
                  <symbol>y</symbol>
              </error>
          </errors>
      </results>
      

      It is true that there are two types of error severities: Error and Style, that does not seem to work with the same types of SonarQube (Bugs, New Vulnerabilities and New Security Hotspots).

       
    • Felipe

      Felipe - 2022-05-09

      Hi again @danielmarjamaki,

      Maybe I'm wrong, the generated report is fine and Sonar is the one that has to classify the errors with the given report.

       
      • Daniel Marjamäki

        the generated report is fine and Sonar is the one that has to classify the errors with the given report.

        Yes

         
        👍
        1

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.