Fixed false positive #4163

This commit is contained in:
PKEuS 2012-09-10 16:14:24 +02:00
parent 1e5d082251
commit e9f13e1547
3 changed files with 12 additions and 8 deletions

View File

@ -502,10 +502,10 @@ void CheckIO::checkWrongPrintfScanfArguments()
if ((!variableInfo->isPointer() && !variableInfo->isArray()) || varTypeTok->strAt(-1) == "const") if ((!variableInfo->isPointer() && !variableInfo->isArray()) || varTypeTok->strAt(-1) == "const")
invalidScanfArgTypeError(tok, tok->str(), numFormat); invalidScanfArgTypeError(tok, tok->str(), numFormat);
if (*i == 's' && variableInfo && isKnownType(variableInfo, varTypeTok) && variableInfo->isArray() && (variableInfo->dimensions().size() == 1)) { if (*i == 's' && variableInfo && isKnownType(variableInfo, varTypeTok) && variableInfo->isArray() && (variableInfo->dimensions().size() == 1) && variableInfo->dimensions()[0].known) {
if (!width.empty()) { if (!width.empty()) {
int numWidth = std::atoi(width.c_str()); int numWidth = std::atoi(width.c_str());
if (numWidth != (variableInfo->dimension(0) - 1)) if (numWidth != (variableInfo->dimension(0) - 1))
invalidScanfFormatWidthError(tok, numFormat, numWidth, variableInfo); invalidScanfFormatWidthError(tok, numFormat, numWidth, variableInfo);
} }
} }

View File

@ -748,11 +748,12 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti
if (_variableList[i] && _variableList[i]->isArray()) { if (_variableList[i] && _variableList[i]->isArray()) {
// check each array dimension // check each array dimension
for (std::size_t j = 0; j < _variableList[i]->dimensions().size(); j++) { for (std::size_t j = 0; j < _variableList[i]->dimensions().size(); j++) {
Dimension &dimension = const_cast<Dimension &>(_variableList[i]->dimensions()[j]);
// check for a single token dimension that is a variable // check for a single token dimension that is a variable
if (_variableList[i]->dimensions()[j].start && if (dimension.num == 0) {
(_variableList[i]->dimensions()[j].start == _variableList[i]->dimensions()[j].end) && dimension.known = false;
_variableList[i]->dimensions()[j].start->varId()) { if (!dimension.start || (dimension.start != dimension.end) || !dimension.start->varId())
Dimension &dimension = const_cast<Dimension &>(_variableList[i]->dimensions()[j]); continue;
// get maximum size from type // get maximum size from type
// find where this type is defined // find where this type is defined
@ -1372,6 +1373,8 @@ void SymbolDatabase::printVariable(const Variable *var, const char *indent) cons
std::cout << indent << "_dimensions:"; std::cout << indent << "_dimensions:";
for (std::size_t i = 0; i < var->dimensions().size(); i++) { for (std::size_t i = 0; i < var->dimensions().size(); i++) {
std::cout << " " << var->dimension(i); std::cout << " " << var->dimension(i);
if (!var->dimensions()[i].known)
std::cout << "?";
} }
std::cout << std::endl; std::cout << std::endl;
} }

View File

@ -46,11 +46,12 @@ enum AccessControl { Public, Protected, Private, Global, Namespace, Argument, Lo
* @brief Array dimension information. * @brief Array dimension information.
*/ */
struct Dimension { struct Dimension {
Dimension() : start(NULL), end(NULL), num(0) { } Dimension() : start(NULL), end(NULL), num(0), known(true) { }
const Token *start; // size start token const Token *start; // size start token
const Token *end; // size end token const Token *end; // size end token
MathLib::bigint num; // dimension length when size is a number, 0 if not known MathLib::bigint num; // (assumpted) dimension length when size is a number, 0 if not known
bool known; // Known size
}; };
/** @brief Information about a member variable. */ /** @brief Information about a member variable. */