checkio: Fixed potential usage of invalid iterator. (#1066)

* checkio: Fixed potential usage of invalid iterator.

* formatted the code.

A ticket about FN (invalidIterator1) is created at https://trac.cppcheck.net/ticket/8373
This commit is contained in:
orbitcowboy 2018-01-30 08:43:15 +01:00 committed by GitHub
parent 4b5e4f989a
commit f5e6ef9fd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 8 deletions

View File

@ -1226,14 +1226,15 @@ void CheckIO::checkFormatString(const Token * const tok,
done = true; done = true;
break; break;
case 'h': // Can be 'hh' (signed char or unsigned char) or 'h' (short int or unsigned short int) 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 the next character is the same (which makes 'hh' or 'll') then expect another alphabetical character
if (i != formatString.end() && *(i+1) == *i) { const bool isSecondCharAvailable = ((i + 1) != formatString.end());
if (i+1 != formatString.end()) { if (i != formatString.end() && isSecondCharAvailable && *(i + 1) == *i) {
if (!isalpha(*(i+2))) { if (isSecondCharAvailable) {
if (!isalpha(*(i + 2))) {
std::string modifier; std::string modifier;
modifier += *i; modifier += *i;
modifier += *(i+1); modifier += *(i + 1);
invalidLengthModifierError(tok, numFormat, modifier); invalidLengthModifierError(tok, numFormat, modifier);
done = true; done = true;
} else { } else {
@ -1245,7 +1246,7 @@ void CheckIO::checkFormatString(const Token * const tok,
} }
} else { } else {
if (i != formatString.end()) { if (i != formatString.end()) {
if (!isalpha(*(i+1))) { if ((i + 1) != formatString.end() && !isalpha(*(i + 1))) {
std::string modifier; std::string modifier;
modifier += *i; modifier += *i;
invalidLengthModifierError(tok, numFormat, modifier); invalidLengthModifierError(tok, numFormat, modifier);
@ -1257,7 +1258,8 @@ void CheckIO::checkFormatString(const Token * const tok,
done = true; done = true;
} }
} }
break; }
break;
case 'I': // Microsoft extension: I for size_t and ptrdiff_t, I32 for __int32, and I64 for __int64 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') || if ((*(i+1) == '3' && *(i+2) == '2') ||
(*(i+1) == '6' && *(i+2) == '4')) { (*(i+1) == '6' && *(i+2) == '4')) {

View File

@ -92,7 +92,8 @@ void memleak_mmap(int fd)
// cppcheck-suppress memleak // cppcheck-suppress memleak
} }
void resourceLeak_fdopen(int fd) { void resourceLeak_fdopen(int fd)
{
// cppcheck-suppress unreadVariable // cppcheck-suppress unreadVariable
FILE *f = fdopen(fd, "r"); FILE *f = fdopen(fd, "r");
// cppcheck-suppress resourceLeak // cppcheck-suppress resourceLeak