Menu

outOfBounds flags datatype from .cfg in 2.2

Ronny Soak
2020-10-05
2020-10-06
  • Ronny Soak

    Ronny Soak - 2020-10-05

    With the new outOfBounds check I get a lot of new errors because a datatype I use is declared as
    an array-like container in a .cfg file. (see below).

    The real datatype is initialized in it's constructor and has a fixed size.

    Is there a way to make the size known in the .cfg file so the outOfBounds check can make sense of it and not flag every use of the [] operator as an error?
    Or can I selectively suppress the check for specific datatypes?

    On another note: Are there any improvements for defining operator overloads in the .cfg file in the new 2.2?

    Thanks

    <?xml version="1.0"?>
    <def format="2">
    <define name="MyTypeFloat" value="MyTempVar &lt;float&gt;"/>
    <define name="MyTypeInt" value="MyTempVar &lt;int&gt;"/>
    
    <container id="MyBaseVar" endPattern="&gt; !!::" opLessAllowed="false" itEndPattern="&gt; ::" hasInitializerListConstructor="true">
        <type templateParameter="0"/>
        <access indexOperator="array-like">
          <function name="get_data" yields="at_index"/>
        </access>
      </container>
    
    <container id="MyTempVar" startPattern="MyTempVar &lt;" inherits="MyBaseVar"> 
      </container>
      </def>
    
     

    Last edit: Ronny Soak 2020-10-06
  • Daniel Marjamäki

    could you show a short c++ code also that generates the FP.

    Is there a way to make the size known in the .cfg file

    what should the size be?

     

    Last edit: Daniel Marjamäki 2020-10-05
  • Ronny Soak

    Ronny Soak - 2020-10-05

    Example code would be

    MyTypeBool myFunction (const MyTypeFloat &value,
                                      const char *testParameterName)
    
    {
      MyTypeBool result;
    
      for (int index = 1; index < DEMO_CONST + 1; ++index) {
        if (checkFunction (index)) {
          float valueFloat = value.is_valid (index) ? value[index].value () : numeric_limits<float>::max ();
          result[index] = externalFunction (valueFloat, testParameterName, index - 1); //<--- Access out of bounds
        }
      }
    
      return result;
    }
    

    For which in the .cfg posted above the line

    <define name="MyTypeBool" value="MyTempVar &lt;bool&gt;"/>
    

    needs to be added as well.

    The full error message is

    Out of bounds access in expression 'result[index]' because 'result' is empty.

    CppCheck is started with

    cppcheck --enable=warning,style,performance,information,portability --suppress=missingInclude --force --dump --platform=unix64 --std=c++11 --library=custom --xml-version=2
    

    Both .cfg and the example are extracted out of more complex codebase. I'll try to verify that it gives the error with this minmal extract too, but it may take me a moment. I'll comment here once I can verify.

    PS.: The size to which the custom array-like type is initialized is of course DEMO_CONST in the example. But if you want to try with a real world number, lets just assume its 64.

     

    Last edit: Ronny Soak 2020-10-06
  • Ronny Soak

    Ronny Soak - 2020-10-06

    I can now verify that the above code examples indeed produce the outOfBounds error.

    <?xml version="1.0" encoding="UTF-8"?>
    <results version="2">
        <cppcheck version="2.2"/>
        <errors>
            <error id="containerOutOfBounds" severity="error" msg="Out of bounds access in expression &apos;result[index]&apos; because &apos;result&apos; is empty." verbose="Out of bounds access in expression &apos;result[index]&apos; because &apos;result&apos; is empty." cwe="398" hash="7410409346732110854">
                <location file="test.cpp" line="10" column="13" info="Access out of bounds"/>
                <symbol>result</symbol>
            </error>
            <error id="unmatchedSuppression" severity="information" msg="Unmatched suppression: missingInclude" verbose="Unmatched suppression: missingInclude"/>
        </errors>
    </results>
    

    I've edited the above examples to be complete (missing <def> in .cfg) and compilable (put cppcheck marker after //).</def>

     

    Last edit: Ronny Soak 2020-10-06

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.