pvs-studio: (V557) Harden array boundary checks

Tighten array boundary checks to prevent possible overrun.
This commit is contained in:
Martin Duffy
2026-05-05 01:43:23 -04:00
parent 7217af55da
commit a77b7aa836
4 changed files with 9 additions and 6 deletions

View File

@@ -14,7 +14,7 @@
//-V::539
# Expression is always true/false.
//-V::547
# Possible array overrun.
# Array underrun/overrun is possible.
//-V::557
# Part of conditional expression is always true/false.
//-V::560

View File

@@ -391,7 +391,7 @@ static bool DumpFileWithLlvmNm(std::string const& nmPath, char const* filename,
line.c_str());
return false;
}
if (line.size() < sym_end + 1) {
if (line.size() < sym_end) {
fprintf(stderr, "Couldn't parse llvm-nm output line: %s\n",
line.c_str());
return false;

View File

@@ -93,7 +93,7 @@ void cmStringReplaceHelper::ParseReplaceExpression()
this->Replacements.emplace_back(
this->ReplaceExpression.substr(l, r - l));
} else {
if (r != l) {
if (r > l) {
this->Replacements.emplace_back(
this->ReplaceExpression.substr(l, r - l));
}

View File

@@ -81,14 +81,16 @@ bool cmTargetPropCommandBase::HandleArguments(
}
bool prepend = false;
if ((flags & PROCESS_BEFORE) && args[argIndex] == "BEFORE") {
if ((flags & PROCESS_BEFORE) && argIndex < args.size() &&
args[argIndex] == "BEFORE") {
if (args.size() < 3) {
this->SetError("called with incorrect number of arguments");
return false;
}
prepend = true;
++argIndex;
} else if ((flags & PROCESS_AFTER) && args[argIndex] == "AFTER") {
} else if ((flags & PROCESS_AFTER) && argIndex < args.size() &&
args[argIndex] == "AFTER") {
if (args.size() < 3) {
this->SetError("called with incorrect number of arguments");
return false;
@@ -97,7 +99,8 @@ bool cmTargetPropCommandBase::HandleArguments(
++argIndex;
}
if ((flags & PROCESS_REUSE_FROM) && args[argIndex] == "REUSE_FROM") {
if ((flags & PROCESS_REUSE_FROM) && argIndex < args.size() &&
args[argIndex] == "REUSE_FROM") {
if (args.size() != 3) {
this->SetError("called with incorrect number of arguments");
return false;