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;
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,6 +1258,7 @@ void CheckIO::checkFormatString(const Token * const tok,
done = true;
}
}
}
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') ||

View File

@ -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