error: containerOutOfBounds - Out of bounds access in 'data[7]', if 'data' size is 2 and '7' is 7
However a pointer to the data type of a vector can be implicitly converted to an iterator. Therefore this constructor is used:
template< class InputIt >
vector( InputIt first, InputIt last, const Allocator& alloc = Allocator() );
#include<stdio.h>#include<cstdint>#include<vector>structSizedData{constintlength;constvoid*data;};boolExampleOfAnIncorrectlyDeterminedArraySize(constSizedData&sizedData){if(8<=sizedData.length){std::vector<uint8_t>data{static_cast<constuint8_t*>(sizedData.data),static_cast<constuint8_t*>(sizedData.data)+sizedData.length};printf("Size of constructed array is: %d NOT 2.\n",static_cast<int>(data.size()));return0==(data[7]&0x4);}returnfalse;}intmain(){constuint8_tdata[]={1,2,3,4,5,6,7,8};SizedDatasizedData{sizeof(data),data};printf("Result: %d\n",ExampleOfAnIncorrectlyDeterminedArraySize(sizedData));}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I don't think the workaround accounts for variable sized arrays being passed around very easily and if it is valid C++ then hopefully eventually you come up with a better solution.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
There seems to be another problem: getArguments returns two tokens, data and +.
Although + is considered to be a pointer (?), no valid size can be extracted from these arguments.
Last edit: CHR 2021-09-28
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
error: containerOutOfBounds - Out of bounds access in 'data[7]', if 'data' size is 2 and '7' is 7
However a pointer to the data type of a vector can be implicitly converted to an iterator. Therefore this constructor is used:
template< class InputIt >
vector( InputIt first, InputIt last, const Allocator& alloc = Allocator() );
Reduced example:
A workaround is to use parentheses instead of braces to initialize
v
.Maybe
getInitListSize()
should be amended like this:Last edit: CHR 2021-09-27
I don't think the workaround accounts for variable sized arrays being passed around very easily and if it is valid C++ then hopefully eventually you come up with a better solution.
I agree that it's a bug. Perhaps @pfultz2 has an opinion on this?
There seems to be another problem:
getArguments
returns two tokens,data
and+
.Although
+
is considered to be a pointer (?), no valid size can be extracted from these arguments.Last edit: CHR 2021-09-28
Ticket is here: https://trac.cppcheck.net/ticket/10513