False positive for array index out-of-bounds when dealing with postincremented index on an array of functions, e.g.
#include<iostream>#include<array>#include<functional>intmain(intargc,constchar*argv[]){unsignedroiElementNumber=0;//std::array<std::function<int(constint)>,2>fillResultFunctors={[&](constintn){returnn;},[&](constintn){return100+n;}};constexprstd::array<int,3>a{1,2,3};//cppcheck gives false containerOutOfBounds errorfor(constautoi:a){if(roiElementNumber>1){roiElementNumber=1;}std::cout<<fillResultFunctors[roiElementNumber++](i)<<'\n';}std::cout<<'\n';//cppcheck is ok with thisroiElementNumber=0;std::array<int,2>b{100,200};for(constautoi:a){if(roiElementNumber>1){roiElementNumber=1;}std::cout<<b[roiElementNumber++]<<'\n';}return0;}
Last edit: penguish 2023-02-01
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
False positive for array index out-of-bounds when dealing with postincremented index on an array of functions, e.g.
Last edit: penguish 2023-02-01
Thanks for reporting, see https://trac.cppcheck.net/ticket/11530