codeql-coding-standards
codeql-coding-standards copied to clipboard
`CTR50-CPP`: Failed to compute the proper range for a resized vector.
Affected rules
- cpp/cert/container-access-without-range-check-cert
Description
The rule warns about the vector container which is resized after delectation.
Access of container of type Payload does not ensure that the index is smaller than the bounds.
Example
#include <iostream>
#include <string>
#include <vector>
#include <filesystem>
using namespace std;
namespace fs = std::filesystem;
int main() {
typedef vector<uint8_t> Payload;
wstring file(L"This is a wstring");
uint64_t attributes;
Payload serialized_data; serialized_data.resize(file.size() * sizeof(wchar_t) + sizeof(attributes));
*(uint64_t*)&serialized_data.front() = attributes;
/*
* Append the path.
*/
file.copy((wchar_t*)&serialized_data[sizeof(attributes)], file.size());
return 0;
}
The current analysis does not track the size of the wstring in your example. When we analyze file.size() * sizeof(wchar_t) we therefore consider the possibility that the multiplication wraps around, which could lead to the size being set to 0.
We could analyze the size of constant strings to contribute to our analysis, which would address this case.