-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added test to make sure
samples
produce the expected output / adjus…
…ted `samples` output and small cleanup (#6180)
- Loading branch information
Showing
20 changed files
with
110 additions
and
59 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
^ |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
^ |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 == {} |