Skip to content

Commit f11e54a

Browse files
IOBYTEdanmar
authored andcommitted
Fixed cppcheck-opensource#4902 (printf and scanf format checks don't support microsoft %I, %I32 and %I64 extensions)
1 parent 2ab8489 commit f11e54a

5 files changed

Lines changed: 167 additions & 92 deletions

File tree

‎lib/check64bit.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ void Check64BitPortability::pointerassignment()
6565
continue;
6666

6767
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
68-
if (Token::Match(tok, "return %var%|%num% [;+]") && !Token::simpleMatch(tok, "return 0 ;")) {
68+
if (Token::Match(tok, "return %var%|%num% [;+]") && !Token::simpleMatch(tok, "return 0 ;") &&
69+
(tok->scope() == scope)) { // #4919 - check if the 'return' doesn't belong to a lambda inside this 'scope'
6970
enum { NO, INT, PTR, PTRDIFF } type = NO;
7071
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) {
7172
if ((type == NO || type == INT) && Token::Match(tok2, "%var% [+;]") && isaddr(tok2->variable()))

‎lib/checkio.cpp‎

Lines changed: 125 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -543,86 +543,129 @@ void CheckIO::checkWrongPrintfScanfArguments()
543543
}
544544
}
545545
} else if (!scan && warning) {
546-
switch (*i) {
547-
case 's':
548-
if (variableInfo && argListTok->type() != Token::eString && isKnownType(variableInfo, varTypeTok) && (!variableInfo->isPointer() && !variableInfo->isArray()))
549-
invalidPrintfArgTypeError_s(tok, numFormat);
550-
break;
551-
case 'n':
552-
if ((varTypeTok && isKnownType(variableInfo, varTypeTok) && ((!variableInfo->isPointer() && !variableInfo->isArray()) || varTypeTok->strAt(-1) == "const")) || argListTok->type() == Token::eString)
553-
invalidPrintfArgTypeError_n(tok, numFormat);
554-
break;
555-
case 'c':
556-
case 'x':
557-
case 'X':
558-
case 'o':
559-
if (varTypeTok && isKnownType(variableInfo, varTypeTok) && !Token::Match(varTypeTok, "bool|short|long|int|char|size_t") && !variableInfo->isPointer() && !variableInfo->isArray())
560-
invalidPrintfArgTypeError_int(tok, numFormat, *i);
561-
else if (argListTok->type() == Token::eString)
562-
invalidPrintfArgTypeError_int(tok, numFormat, *i);
563-
break;
564-
case 'd':
565-
case 'i':
566-
if (varTypeTok && isKnownType(variableInfo, varTypeTok) && !variableInfo->isPointer() && !variableInfo->isArray()) {
567-
if ((varTypeTok->isUnsigned() || !Token::Match(varTypeTok, "bool|short|long|int")) && varTypeTok->str() != "char")
568-
invalidPrintfArgTypeError_sint(tok, numFormat, *i);
569-
} else if (argListTok->type() == Token::eString)
570-
invalidPrintfArgTypeError_sint(tok, numFormat, *i);
571-
break;
572-
case 'u':
573-
if (varTypeTok && isKnownType(variableInfo, varTypeTok) && !variableInfo->isPointer() && !variableInfo->isArray()) {
574-
if ((!varTypeTok->isUnsigned() || !Token::Match(varTypeTok, "char|short|long|int|size_t")) && varTypeTok->str() != "bool")
575-
invalidPrintfArgTypeError_uint(tok, numFormat, *i);
576-
} else if (argListTok->type() == Token::eString)
577-
invalidPrintfArgTypeError_uint(tok, numFormat, *i);
578-
break;
579-
case 'p':
580-
if (varTypeTok && isKnownType(variableInfo, varTypeTok) && !Token::Match(varTypeTok, "short|long|int|size_t") && !variableInfo->isPointer() && !variableInfo->isArray())
581-
invalidPrintfArgTypeError_p(tok, numFormat);
582-
else if (argListTok->type() == Token::eString)
583-
invalidPrintfArgTypeError_p(tok, numFormat);
584-
break;
585-
case 'e':
586-
case 'E':
587-
case 'f':
588-
case 'g':
589-
case 'G':
590-
if (varTypeTok && ((isKnownType(variableInfo, varTypeTok) && !Token::Match(varTypeTok, "float|double")) || variableInfo->isPointer() || variableInfo->isArray()))
591-
invalidPrintfArgTypeError_float(tok, numFormat, *i);
592-
else if (argListTok->type() == Token::eString)
593-
invalidPrintfArgTypeError_float(tok, numFormat, *i);
594-
break;
595-
case 'h': // Can be 'hh' (signed char or unsigned char) or 'h' (short int or unsigned short int)
596-
case 'l': // Can be 'll' (long long int or unsigned long long int) or 'l' (long int or unsigned long int)
597-
// If the next character is the same (which makes 'hh' or 'll') then expect another alphabetical character
598-
if (i != formatString.end() && *(i+1) == *i) {
599-
if (i+1 != formatString.end() && !isalpha(*(i+2))) {
600-
std::string modifier;
601-
modifier += *i;
602-
modifier += *(i+1);
603-
invalidLengthModifierError(tok, numFormat, modifier);
546+
std::string specifier;
547+
bool done = false;
548+
while (!done) {
549+
switch (*i) {
550+
case 's':
551+
if (variableInfo && argListTok->type() != Token::eString && isKnownType(variableInfo, varTypeTok) && (!variableInfo->isPointer() && !variableInfo->isArray()))
552+
invalidPrintfArgTypeError_s(tok, numFormat);
553+
done = true;
554+
break;
555+
case 'n':
556+
if ((varTypeTok && isKnownType(variableInfo, varTypeTok) && ((!variableInfo->isPointer() && !variableInfo->isArray()) || varTypeTok->strAt(-1) == "const")) || argListTok->type() == Token::eString)
557+
invalidPrintfArgTypeError_n(tok, numFormat);
558+
done = true;
559+
break;
560+
case 'c':
561+
case 'x':
562+
case 'X':
563+
case 'o':
564+
if (varTypeTok && isKnownType(variableInfo, varTypeTok) && !Token::Match(varTypeTok, "bool|short|long|int|char|size_t") && !variableInfo->isPointer() && !variableInfo->isArray()) {
565+
specifier += *i;
566+
invalidPrintfArgTypeError_int(tok, numFormat, specifier);
567+
} else if (argListTok->type() == Token::eString) {
568+
specifier += *i;
569+
invalidPrintfArgTypeError_int(tok, numFormat, specifier);
570+
}
571+
done = true;
572+
break;
573+
case 'd':
574+
case 'i':
575+
if (varTypeTok && isKnownType(variableInfo, varTypeTok) && !variableInfo->isPointer() && !variableInfo->isArray()) {
576+
if ((varTypeTok->isUnsigned() || !Token::Match(varTypeTok, "bool|short|long|int")) && varTypeTok->str() != "char") {
577+
specifier += *i;
578+
invalidPrintfArgTypeError_sint(tok, numFormat, specifier);
579+
}
580+
} else if (argListTok->type() == Token::eString) {
581+
specifier += *i;
582+
invalidPrintfArgTypeError_sint(tok, numFormat, specifier);
583+
}
584+
done = true;
585+
break;
586+
case 'u':
587+
if (varTypeTok && isKnownType(variableInfo, varTypeTok) && !variableInfo->isPointer() && !variableInfo->isArray()) {
588+
if ((!varTypeTok->isUnsigned() || !Token::Match(varTypeTok, "char|short|long|int|size_t")) && varTypeTok->str() != "bool") {
589+
specifier += *i;
590+
invalidPrintfArgTypeError_uint(tok, numFormat, specifier);
591+
}
592+
} else if (argListTok->type() == Token::eString) {
593+
specifier += *i;
594+
invalidPrintfArgTypeError_uint(tok, numFormat, specifier);
604595
}
605-
} else {
596+
done = true;
597+
break;
598+
case 'p':
599+
if (varTypeTok && isKnownType(variableInfo, varTypeTok) && !Token::Match(varTypeTok, "short|long|int|size_t") && !variableInfo->isPointer() && !variableInfo->isArray())
600+
invalidPrintfArgTypeError_p(tok, numFormat);
601+
else if (argListTok->type() == Token::eString)
602+
invalidPrintfArgTypeError_p(tok, numFormat);
603+
done = true;
604+
break;
605+
case 'e':
606+
case 'E':
607+
case 'f':
608+
case 'g':
609+
case 'G':
610+
if (varTypeTok && ((isKnownType(variableInfo, varTypeTok) && !Token::Match(varTypeTok, "float|double")) || variableInfo->isPointer() || variableInfo->isArray())) {
611+
specifier += *i;
612+
invalidPrintfArgTypeError_float(tok, numFormat, specifier);
613+
} else if (argListTok->type() == Token::eString) {
614+
specifier += *i;
615+
invalidPrintfArgTypeError_float(tok, numFormat, specifier);
616+
}
617+
done = true;
618+
break;
619+
case 'h': // Can be 'hh' (signed char or unsigned char) or 'h' (short int or unsigned short int)
620+
case 'l': // Can be 'll' (long long int or unsigned long long int) or 'l' (long int or unsigned long int)
621+
// If the next character is the same (which makes 'hh' or 'll') then expect another alphabetical character
622+
if (i != formatString.end() && *(i+1) == *i) {
623+
if (i+1 != formatString.end() && !isalpha(*(i+2))) {
624+
std::string modifier;
625+
modifier += *i;
626+
modifier += *(i+1);
627+
invalidLengthModifierError(tok, numFormat, modifier);
628+
}
629+
} else {
630+
if (i != formatString.end() && !isalpha(*(i+1))) {
631+
std::string modifier;
632+
modifier += *i;
633+
invalidLengthModifierError(tok, numFormat, modifier);
634+
}
635+
}
636+
done = true;
637+
break;
638+
case 'I': // Microsoft extension: I for size_t and ptrdiff_t, I32 for __int32, and I64 for __int64
639+
if (i != formatString.end()) {
640+
if (isalpha(*(i+1))) {
641+
specifier = *i++;
642+
} else if (*(i+1) == '3' && *(i+2) == '2' && isalpha(*(i+3))) {
643+
specifier = *i++;
644+
specifier += *i++;
645+
specifier += *i++;
646+
} else if (*(i+1) == '6' && *(i+2) == '4' && isalpha(*(i+3))) {
647+
specifier = *i++;
648+
specifier += *i++;
649+
specifier += *i++;
650+
}
651+
}
652+
break;
653+
case 'j': // intmax_t or uintmax_t
654+
case 'z': // size_t
655+
case 't': // ptrdiff_t
656+
case 'L': // long double
657+
// Expect an alphabetical character after these specifiers
606658
if (i != formatString.end() && !isalpha(*(i+1))) {
607659
std::string modifier;
608660
modifier += *i;
609661
invalidLengthModifierError(tok, numFormat, modifier);
610662
}
663+
done = true;
664+
break;
665+
default:
666+
done = true;
667+
break;
611668
}
612-
break;
613-
case 'j': // intmax_t or uintmax_t
614-
case 'z': // size_t
615-
case 't': // ptrdiff_t
616-
case 'L': // long double
617-
// Expect an alphabetical character after these specifiers
618-
if (i != formatString.end() && !isalpha(*(i+1))) {
619-
std::string modifier;
620-
modifier += *i;
621-
invalidLengthModifierError(tok, numFormat, modifier);
622-
}
623-
break;
624-
default:
625-
break;
626669
}
627670
}
628671
}
@@ -692,31 +735,31 @@ void CheckIO::invalidPrintfArgTypeError_p(const Token* tok, unsigned int numForm
692735
errmsg << "%p in format string (no. " << numFormat << ") requires an address given in the argument list.";
693736
reportError(tok, Severity::warning, "invalidPrintfArgType_p", errmsg.str());
694737
}
695-
void CheckIO::invalidPrintfArgTypeError_int(const Token* tok, unsigned int numFormat, char c)
738+
void CheckIO::invalidPrintfArgTypeError_int(const Token* tok, unsigned int numFormat, const std::string& specifier)
696739
{
697740
std::ostringstream errmsg;
698-
errmsg << "%" << c << " in format string (no. " << numFormat << ") requires an integer given in the argument list.";
741+
errmsg << "%" << specifier << " in format string (no. " << numFormat << ") requires an integer given in the argument list.";
699742
reportError(tok, Severity::warning, "invalidPrintfArgType_int", errmsg.str());
700743
}
701-
void CheckIO::invalidPrintfArgTypeError_uint(const Token* tok, unsigned int numFormat, char c)
744+
void CheckIO::invalidPrintfArgTypeError_uint(const Token* tok, unsigned int numFormat, const std::string& specifier)
702745
{
703746
std::ostringstream errmsg;
704-
errmsg << "%" << c << " in format string (no. " << numFormat << ") requires an unsigned integer given in the argument list.";
747+
errmsg << "%" << specifier << " in format string (no. " << numFormat << ") requires an unsigned integer given in the argument list.";
705748
reportError(tok, Severity::warning, "invalidPrintfArgType_uint", errmsg.str());
706749
}
707-
void CheckIO::invalidPrintfArgTypeError_sint(const Token* tok, unsigned int numFormat, char c)
750+
void CheckIO::invalidPrintfArgTypeError_sint(const Token* tok, unsigned int numFormat, const std::string& specifier)
708751
{
709752
std::ostringstream errmsg;
710-
errmsg << "%" << c << " in format string (no. " << numFormat << ") requires a signed integer given in the argument list.";
753+
errmsg << "%" << specifier << " in format string (no. " << numFormat << ") requires a signed integer given in the argument list.";
711754
reportError(tok, Severity::warning, "invalidPrintfArgType_sint", errmsg.str());
712755
}
713-
void CheckIO::invalidPrintfArgTypeError_float(const Token* tok, unsigned int numFormat, char c)
756+
void CheckIO::invalidPrintfArgTypeError_float(const Token* tok, unsigned int numFormat, const std::string& specifier)
714757
{
715758
std::ostringstream errmsg;
716-
errmsg << "%" << c << " in format string (no. " << numFormat << ") requires a floating point number given in the argument list.";
759+
errmsg << "%" << specifier << " in format string (no. " << numFormat << ") requires a floating point number given in the argument list.";
717760
reportError(tok, Severity::warning, "invalidPrintfArgType_float", errmsg.str());
718761
}
719-
void CheckIO::invalidLengthModifierError(const Token* tok, unsigned int numFormat, std::string& modifier)
762+
void CheckIO::invalidLengthModifierError(const Token* tok, unsigned int numFormat, const std::string& modifier)
720763
{
721764
std::ostringstream errmsg;
722765
errmsg << "'" << modifier << "' in format string (no. " << numFormat << ") is a length modifier and cannot be used without a conversion specifier.";

0 commit comments

Comments
 (0)