Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added test to make sure samples produce the expected output / adjusted samples output and small cleanup #6180

Merged
merged 6 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions samples/arrayIndexOutOfBounds/out.txt

This file was deleted.

3 changes: 3 additions & 0 deletions samples/arrayIndexOutOfBounds_1/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
samples\arrayIndexOutOfBounds_1\bad.c:7:6: error: Array 'a[2]' accessed at index 2, which is out of bounds. [arrayIndexOutOfBounds]
a[2] = 0;
^
8 changes: 8 additions & 0 deletions samples/arrayIndexOutOfBounds_2/bad.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
int main()
{
int a[2];
int i;
for (i = 0; i < 3; i++)
a[i] = 0;
return a[0];
}
8 changes: 8 additions & 0 deletions samples/arrayIndexOutOfBounds_2/good.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
int main()
{
int a[3];
int i;
for (i = 0; i < 3; i++)
a[i] = 0;
return a[0];
}
9 changes: 9 additions & 0 deletions samples/arrayIndexOutOfBounds_2/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
samples\arrayIndexOutOfBounds_2\bad.c:6:10: error: Array 'a[2]' accessed at index 2, which is out of bounds. [arrayIndexOutOfBounds]
a[i] = 0;
^
samples\arrayIndexOutOfBounds_2\bad.c:5:19: note: Assuming that condition 'i<3' is not redundant
for (i = 0; i < 3; i++)
^
samples\arrayIndexOutOfBounds_2\bad.c:6:10: note: Array index out of bounds
a[i] = 0;
^
9 changes: 4 additions & 5 deletions samples/bufferAccessOutOfBounds/bad.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include <stdio.h>
int main()
{
int a[2];
int i;
for (i = 0; i < 3; i++)
a[i] = 0;
return a[0];
char str[5];
strcpy(str, "0123456789abcdef");
return 0;
}
9 changes: 4 additions & 5 deletions samples/bufferAccessOutOfBounds/good.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include <stdio.h>
int main()
{
int a[3];
int i;
for (i = 0; i < 3; i++)
a[i] = 0;
return a[0];
char str[10];
snprintf(str, 10, "%s", "abc");
return 0;
}
6 changes: 3 additions & 3 deletions samples/bufferAccessOutOfBounds/out.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
samples\bufferAccessOutOfBounds\bad.c:6:10: error: Array 'a[2]' accessed at index 2, which is out of bounds. [arrayIndexOutOfBounds]
a[i] = 0;
^
samples\bufferAccessOutOfBounds\bad.c:5:12: error: Buffer is accessed out of bounds: str [bufferAccessOutOfBounds]
strcpy(str, "0123456789abcdef");
^
24 changes: 0 additions & 24 deletions samples/erase/out.txt

This file was deleted.

File renamed without changes.
File renamed without changes.
24 changes: 24 additions & 0 deletions samples/invalidContainer/out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
samples\invalidContainer\bad.cpp:9:32: error: inconclusive: Using iterator to local container 'items' that may be invalid. [invalidContainer]
for (iter = items.begin(); iter != items.end(); ++iter) {
^
samples\invalidContainer\bad.cpp:9:28: note: Iterator to container is created here.
for (iter = items.begin(); iter != items.end(); ++iter) {
^
samples\invalidContainer\bad.cpp:10:19: note: Assuming condition is true.
if (*iter == 2) {
^
samples\invalidContainer\bad.cpp:10:19: note: Assuming condition is true.
if (*iter == 2) {
^
samples\invalidContainer\bad.cpp:9:37: note: Assuming condition is true.
for (iter = items.begin(); iter != items.end(); ++iter) {
^
samples\invalidContainer\bad.cpp:11:19: note: After calling 'erase', iterators or references to the container's data may be invalid .
items.erase(iter);
^
samples\invalidContainer\bad.cpp:4:22: note: Variable created here.
std::vector<int> items;
^
samples\invalidContainer\bad.cpp:9:32: note: Using iterator to local container 'items' that may be invalid.
for (iter = items.begin(); iter != items.end(); ++iter) {
^
2 changes: 1 addition & 1 deletion samples/memleak/out.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
samples/memleak/bad.c:8:5: error: Memory leak: a [memleak]
samples\memleak\bad.c:8:5: error: Memory leak: a [memleak]
return result;
^
7 changes: 0 additions & 7 deletions samples/outOfBounds/bad.c

This file was deleted.

7 changes: 0 additions & 7 deletions samples/outOfBounds/good.c

This file was deleted.

3 changes: 0 additions & 3 deletions samples/outOfBounds/out.txt

This file was deleted.

2 changes: 1 addition & 1 deletion samples/resourceLeak/bad.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stdio.h>
int main()
{
FILE *a = fopen("good.c", "r");
const FILE *a = fopen("good.c", "r");
if (!a)
return 0;

Expand Down
45 changes: 45 additions & 0 deletions test/cli/samples_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import os
import sys

from testutils import cppcheck

__script_dir = os.path.dirname(os.path.abspath(__file__))
__root_dir = os.path.abspath(os.path.join(__script_dir, '..', '..'))


def test_samples():
failures = {}

samples_dir = os.path.join(__root_dir, 'samples')
for entry in os.listdir(samples_dir):
sample_dir = os.path.join(samples_dir, entry)
if not os.path.isdir(sample_dir):
continue

with open(os.path.join(sample_dir, 'out.txt')) as out_in:
out_txt = out_in.read()
if not sys.platform == 'win32':
out_txt = out_txt.replace('\\', '/')

if not os.path.exists(os.path.join(sample_dir, 'good.c')):
good_src = os.path.join('samples', entry, 'good.cpp')
bad_src = os.path.join('samples', entry, 'bad.cpp')
else:
good_src = os.path.join('samples', entry, 'good.c')
bad_src = os.path.join('samples', entry, 'bad.c')

# check that good input does not produce any warnings
ret, stdout, stderr = cppcheck(['-q', '--enable=all', '--disable=missingInclude', '--inconclusive', '--check-level=exhaustive', '--error-exitcode=1', good_src], cwd=__root_dir)
if not ret == 0:
failures[good_src] = stderr

# check that the bad inout produces a warning
ret, stdout, stderr = cppcheck(['-q', '--enable=all', '--disable=missingInclude', '--inconclusive', '--check-level=exhaustive', '--error-exitcode=1', bad_src], cwd=__root_dir)
if not ret == 1:
failures[bad_src] = stderr

# check that the bad input procudes the expected output
if not stderr == out_txt:
failures[bad_src] = stderr

assert failures == {}
Loading