Menu

Library definition for boost::array

2018-12-10
2019-11-10
  • Steven Cook

    Steven Cook - 2018-12-10

    In my library file I have a definition for boost::array like this:

        <container id="boostArray" startPattern="boost :: array &lt;" inherits="stdArray" />
    

    However this doesn't seem to work correctly. I have a method that returns a boost::array by value, and this results in the warning "error (returnDanglingLifetime): Returning pointer to local variable 'coords' that will be invalid when returning". If I change the boost::array to a std::array, no warning is generated.

    Can anyone tell me the correct way to define that boost::array behaves like std::array?

    Thanks!

     
  • Paul Fultz

    Paul Fultz - 2018-12-10

    Do you have a code snippet for this?

     
  • Steven Cook

    Steven Cook - 2018-12-11

    Library file:

    <?xml version="1.0"?>
    <def format="2">
        <container id="boostArray" startPattern="boost :: array &lt;" inherits="stdArray" />
    </def>
    

    Code snippet:

    #include <boost/array.hpp>
    
    typedef boost::array< int, 4 > Coords;
    
    Coords GetCoords()
    {
        Coords c;
        return c;
    }
    

    Cppcheck 1.86 output:

    $ cppcheck --library=std --library=library.xml test.cpp
    Checking test.cpp ...
    [test.cpp:8] -> [test.cpp:7] -> [test.cpp:8]: (error) Returning pointer to local variable 'c' that will be invalid when returning.
    

    If I change the boost::array to a std::array, cppcheck doesn't report a warning.

     
  • Daniel Marjamäki

    I am not sure if you can inherit from stdArray. because it comes from a different file. maybe it works. I guess std.cfg is loaded first but I am not sure about that neither.

    Does it work otherwise? For example does this code generate a warning:

    void f() {
        boost::array<int,5> a;
        a[10] = 0; // <- a error should be written
    }
    

    I would be highly interested in publishing a boost.cfg. At my work we use some boost utility functions also so I would probably add those.

     
    • versat

      versat - 2018-12-12

      I have tested if the std.cfg is loaded before boost.cfg that i created locally. When only --library=boost is specified an error message is printed that stdArray is not known. But when --library=std --library=boost is specified the std.cfg is loaded before boost.cfg and stdArray is found. That corresponds with earlier observations i made. The std.cfg is normally one of the last library files that is loaded.

       
  • Steven Cook

    Steven Cook - 2018-12-12

    Yes, that code generates the expected warning.

    I am also interested in boost.cfg as we use boost extensively at my work (though we are slowly transitioning to C++11 equivalents where possible). I would be willing to help with this.

     
  • Daniel Marjamäki

    I am also interested in boost.cfg as we use boost extensively at my work (though we are slowly transitioning to C++11 equivalents where possible). I would be willing to help with this.

    I created a small boost.cfg file. please fill in some information. I will add some boost algorithm functions and maybe the boost property_tree somehow. that will cover most stuff that we have at my work.

     
  • Steven Cook

    Steven Cook - 2018-12-14

    I made a start this morning. I'm unsure what type of function definitions provide the most value (ie are most likely to improve the checking) so it's hard to know where to begin. I couldn't come up with anything meaningful for the boost features we use the most (bind, shared_ptr, mutex, lock_guard, ptime, make_shared).

    I've defined some math functions and constants, macros for the BOOST_TEST framework, and a couple of containers. This does introduce a dependency on std though.
    https://github.com/StevePER/cppcheck/blob/master/cfg/boost.cfg

     
  • Daniel Marjamäki

    I'm unsure what type of function definitions provide the most value (ie are most likely to improve the checking) so it's hard to know where to begin

    Ideally you should be able to use --check-library.

    I couldn't come up with anything meaningful for the boost features we use the most (bind, shared_ptr, mutex, lock_guard, ptime, make_shared).

    yeah as far as I know you can't configure those meaningfully. That would be useful.

     

    Last edit: Daniel Marjamäki 2018-12-14
    • Steven Cook

      Steven Cook - 2018-12-17

      Ideally you should be able to use --check-library.

      Only 7000 warnings when run on our code base from work! But it does give some ideas for where to start. I'll take a look at boost::circular_buffer, unordered_set, and filesystem next. The string algorithms and ptree will be useful too.

       
  • Daniel Marjamäki

    I've defined some math functions and constants, macros for the BOOST_TEST framework, and a couple of containers. This does introduce a dependency on std though.

    Could you create a PR?

    I guess the std dependency is ok.

     
  • Steven Cook

    Steven Cook - 2018-12-18
     
  • Steven Cook

    Steven Cook - 2018-12-18

    I've added all the boost::constants, more containers, and functions from boost::filesystem.

     
  • Steven Cook

    Steven Cook - 2018-12-21

    Added boost::lexical_cast and endian conversion functions. I can't see anything else that I can add.

     
  • Steven Cook

    Steven Cook - 2019-02-13

    Thanks for adding the initial version of this file to 1.87. Given that I haven't contributed to cppcheck before, what is the process for getting my library definitions added?

     
  • versat

    versat - 2019-02-13

    Not sure which information you exactly need.
    Here is how i contribute:
    I forked cppcheck and cloned it locally.
    Regularly update the local repository with git pull upstream master.
    Change some code / configuration file / ...
    Create a branch for the Pull Request i want to make.
    Commit the corresponding changes to this branch.
    Push the branch to my forked repository at GitHub.
    Create a Pull Request on GitHub based on the branch, it is automatically suggested for some time after the branch was pushed.

     
  • Daniel Marjamäki

    Given that I haven't contributed to cppcheck before, what is the process for getting my library definitions added?

    thanks!

    as versat said.. best is to create some github pull requests.

     

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.