Skip to content

Commit 4776432

Browse files
committed
Several fixes to invalidScanfFormatWidthError():
- Different IDs for different messages (cppcheck-opensource#5809) - severity is warning, not style, so check _settings->isEnabled() properly - Removed never shown message text
1 parent d1d3c24 commit 4776432

1 file changed

Lines changed: 17 additions & 19 deletions

File tree

lib/checkio.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,26 +1905,24 @@ void CheckIO::invalidLengthModifierError(const Token* tok, unsigned int numForma
19051905

19061906
void CheckIO::invalidScanfFormatWidthError(const Token* tok, unsigned int numFormat, int width, const Variable *var)
19071907
{
1908-
std::ostringstream errmsg;
1909-
Severity::SeverityType severity = Severity::warning;
1910-
bool inconclusive = false;
1908+
MathLib::bigint arrlen = 0;
1909+
std::string varname;
19111910

19121911
if (var) {
1913-
if (var->dimension(0) > width) {
1914-
if (!_settings->inconclusive)
1915-
return;
1916-
inconclusive = true;
1917-
errmsg << "Width " << width << " given in format string (no. " << numFormat << ") is smaller than destination buffer"
1918-
<< " '" << var->name() << "[" << var->dimension(0) << "]'.";
1919-
} else {
1920-
errmsg << "Width " << width << " given in format string (no. " << numFormat << ") is larger than destination buffer '"
1921-
<< var->name() << "[" << var->dimension(0) << "]', use %" << (var->dimension(0) - 1) << "s to prevent overflowing it.";
1922-
severity = Severity::error;
1923-
}
1924-
1925-
} else
1926-
errmsg << "Width " << width << " given in format string (no. " << numFormat << ") doesn't match destination buffer.";
1912+
arrlen = var->dimension(0);
1913+
varname = var->name();
1914+
}
19271915

1928-
if (severity == Severity::error || _settings->isEnabled("style"))
1929-
reportError(tok, severity, "invalidScanfFormatWidth", errmsg.str(), inconclusive);
1916+
std::ostringstream errmsg;
1917+
if (arrlen > width) {
1918+
if (!_settings->inconclusive || !_settings->isEnabled("warning"))
1919+
return;
1920+
errmsg << "Width " << width << " given in format string (no. " << numFormat << ") is smaller than destination buffer"
1921+
<< " '" << varname << "[" << arrlen << "]'.";
1922+
reportError(tok, Severity::warning, "invalidScanfFormatWidth_smaller", errmsg.str(), true);
1923+
} else {
1924+
errmsg << "Width " << width << " given in format string (no. " << numFormat << ") is larger than destination buffer '"
1925+
<< varname << "[" << arrlen << "]', use %" << (arrlen - 1) << "s to prevent overflowing it.";
1926+
reportError(tok, Severity::error, "invalidScanfFormatWidth", errmsg.str(), false);
1927+
}
19301928
}

0 commit comments

Comments
 (0)