Skip to content

Instantly share code, notes, and snippets.

@anatolyborodin
Last active August 28, 2019 12:42
Show Gist options
  • Save anatolyborodin/0efc6e58536721603930de89d60325ee to your computer and use it in GitHub Desktop.
Save anatolyborodin/0efc6e58536721603930de89d60325ee to your computer and use it in GitHub Desktop.
cppcheck bug
#include <stdio.h>
#define ARR_SIZE 10
int arr[ARR_SIZE];
static int f(int v)
{
int i;
int found = 0;
for (i = 0; i < ARR_SIZE; ++i) {
if (arr[i] == v) {
found = 1;
break;
}
}
if (!found) {
return -1;
}
arr[i] = 0;
return i;
}
int main()
{
int i;
for (i = 0; i < ARR_SIZE; ++i) {
arr[i] = i * i * i;
}
printf("f(125) = %d\n", f(125));
printf("Try again...\n");
printf("f(125) = %d\n", f(125));
}
/* cppcheck 1.86-1 (Ubuntu 19.04) says:
# cppcheck test.c
Checking test.c ...
[test.c:20]: (error) Array 'arr[10]' accessed at index 10, which is out of bounds.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment