diff --git a/lib/checkio.cpp b/lib/checkio.cpp index a68ded0e2..a45dc26b9 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -1226,14 +1226,15 @@ void CheckIO::checkFormatString(const Token * const tok, done = true; break; case 'h': // Can be 'hh' (signed char or unsigned char) or 'h' (short int or unsigned short int) - case 'l': // Can be 'll' (long long int or unsigned long long int) or 'l' (long int or unsigned long int) + case 'l': { // Can be 'll' (long long int or unsigned long long int) or 'l' (long int or unsigned long int) // If the next character is the same (which makes 'hh' or 'll') then expect another alphabetical character - if (i != formatString.end() && *(i+1) == *i) { - if (i+1 != formatString.end()) { - if (!isalpha(*(i+2))) { + const bool isSecondCharAvailable = ((i + 1) != formatString.end()); + if (i != formatString.end() && isSecondCharAvailable && *(i + 1) == *i) { + if (isSecondCharAvailable) { + if (!isalpha(*(i + 2))) { std::string modifier; modifier += *i; - modifier += *(i+1); + modifier += *(i + 1); invalidLengthModifierError(tok, numFormat, modifier); done = true; } else { @@ -1245,7 +1246,7 @@ void CheckIO::checkFormatString(const Token * const tok, } } else { if (i != formatString.end()) { - if (!isalpha(*(i+1))) { + if ((i + 1) != formatString.end() && !isalpha(*(i + 1))) { std::string modifier; modifier += *i; invalidLengthModifierError(tok, numFormat, modifier); @@ -1257,7 +1258,8 @@ void CheckIO::checkFormatString(const Token * const tok, done = true; } } - break; + } + break; case 'I': // Microsoft extension: I for size_t and ptrdiff_t, I32 for __int32, and I64 for __int64 if ((*(i+1) == '3' && *(i+2) == '2') || (*(i+1) == '6' && *(i+2) == '4')) { diff --git a/test/cfg/posix.c b/test/cfg/posix.c index 77df74a73..22df7c1ff 100644 --- a/test/cfg/posix.c +++ b/test/cfg/posix.c @@ -92,7 +92,8 @@ void memleak_mmap(int fd) // cppcheck-suppress memleak } -void resourceLeak_fdopen(int fd) { +void resourceLeak_fdopen(int fd) +{ // cppcheck-suppress unreadVariable FILE *f = fdopen(fd, "r"); // cppcheck-suppress resourceLeak