This patch adds a collection of test sources for use by the testsuites of the various checker harnesses.
checkers/ChangeLog: * test-sources/conditional-leak.c: New file. * test-sources/cpychecker-demo.c: New file. * test-sources/divide-by-zero.c: New file. * test-sources/harmless.c: New file. * test-sources/multiple-1.c: New file. * test-sources/multiple-2.c: New file. * test-sources/out-of-bounds.c: New file. * test-sources/read-through-null.c: New file. * test-sources/return-of-stack-address.c: New file. * test-sources/unconditional-file-leak.c: New file. --- checkers/test-sources/conditional-leak.c | 17 ++++ checkers/test-sources/cpychecker-demo.c | 110 ++++++++++++++++++++++++ checkers/test-sources/divide-by-zero.c | 4 + checkers/test-sources/harmless.c | 9 ++ checkers/test-sources/multiple-1.c | 6 ++ checkers/test-sources/multiple-2.c | 9 ++ checkers/test-sources/out-of-bounds.c | 6 ++ checkers/test-sources/read-through-null.c | 4 + checkers/test-sources/return-of-stack-address.c | 6 ++ checkers/test-sources/unconditional-file-leak.c | 10 +++ 10 files changed, 181 insertions(+) create mode 100644 checkers/test-sources/conditional-leak.c create mode 100644 checkers/test-sources/cpychecker-demo.c create mode 100644 checkers/test-sources/divide-by-zero.c create mode 100644 checkers/test-sources/harmless.c create mode 100644 checkers/test-sources/multiple-1.c create mode 100644 checkers/test-sources/multiple-2.c create mode 100644 checkers/test-sources/out-of-bounds.c create mode 100644 checkers/test-sources/read-through-null.c create mode 100644 checkers/test-sources/return-of-stack-address.c create mode 100644 checkers/test-sources/unconditional-file-leak.c diff --git a/checkers/test-sources/conditional-leak.c b/checkers/test-sources/conditional-leak.c new file mode 100644 index 0000000..2ab46f5 --- /dev/null +++ b/checkers/test-sources/conditional-leak.c @@ -0,0 +1,17 @@ +#include <stdlib.h> + +void test () +{ + void *ptr_1; + void *ptr_2; + + ptr_1 = malloc (64); + if (!ptr_1) + return; + ptr_2 = malloc (64); + if (!ptr_2) + return; + + free (ptr_2); + free (ptr_1); +} diff --git a/checkers/test-sources/cpychecker-demo.c b/checkers/test-sources/cpychecker-demo.c new file mode 100644 index 0000000..b379729 --- /dev/null +++ b/checkers/test-sources/cpychecker-demo.c @@ -0,0 +1,110 @@ +/* + Copyright 2011 David Malcolm <dmalc...@redhat.com> + Copyright 2011 Red Hat, Inc. + + This is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see + <http://www.gnu.org/licenses/>. +*/ + +/* Examples of mistakes made using the Python API */ +#include <Python.h> + +extern uint16_t htons(uint16_t hostshort); + +PyObject * +socket_htons(PyObject *self, PyObject *args) +{ + unsigned long x1, x2; + + if (!PyArg_ParseTuple(args, "i:htons", &x1)) { + return NULL; + } + x2 = (int)htons((short)x1); + return PyInt_FromLong(x2); +} + +PyObject * +not_enough_varargs(PyObject *self, PyObject *args) +{ + if (!PyArg_ParseTuple(args, "i")) { + return NULL; + } + Py_RETURN_NONE; +} + +PyObject * +too_many_varargs(PyObject *self, PyObject *args) +{ + int i, j; + if (!PyArg_ParseTuple(args, "i", &i, &j)) { + return NULL; + } + Py_RETURN_NONE; +} + +PyObject * +kwargs_example(PyObject *self, PyObject *args, PyObject *kwargs) +{ + double x, y; + char *keywords[] = {"x", "y"}; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "(ff):kwargs_example", keywords, &x, &y)) { + return NULL; + } + Py_RETURN_NONE; +} + + +extern int convert_to_ssize(PyObject *, Py_ssize_t *); + +PyObject * +buggy_converter(PyObject *self, PyObject *args) +{ + int i; + + if (!PyArg_ParseTuple(args, "O&", convert_to_ssize, &i)) { + return NULL; + } + + Py_RETURN_NONE; +} + +PyObject * +make_a_list_of_random_ints_badly(PyObject *self, + PyObject *args) +{ + PyObject *list, *item; + long count, i; + + if (!PyArg_ParseTuple(args, "i", &count)) { + return NULL; + } + + list = PyList_New(0); + + for (i = 0; i < count; i++) { + item = PyLong_FromLong(random()); + PyList_Append(list, item); + } + + return list; +} + +/* + PEP-7 +Local variables: +c-basic-offset: 4 +indent-tabs-mode: nil +End: +*/ diff --git a/checkers/test-sources/divide-by-zero.c b/checkers/test-sources/divide-by-zero.c new file mode 100644 index 0000000..f40692d --- /dev/null +++ b/checkers/test-sources/divide-by-zero.c @@ -0,0 +1,4 @@ +int divide_by_zero (int i) +{ + return i / 0; +} diff --git a/checkers/test-sources/harmless.c b/checkers/test-sources/harmless.c new file mode 100644 index 0000000..c29f0cc --- /dev/null +++ b/checkers/test-sources/harmless.c @@ -0,0 +1,9 @@ +#include <stdio.h> + +int main (int argc, const char **argv) +{ + int i; + for (i = 0; i < argc; i++) + printf ("argv[%i]: %s\n", i, argv[i]); + return 0; +} diff --git a/checkers/test-sources/multiple-1.c b/checkers/test-sources/multiple-1.c new file mode 100644 index 0000000..8d26c69 --- /dev/null +++ b/checkers/test-sources/multiple-1.c @@ -0,0 +1,6 @@ +extern int helper (int argc, const char **argv); + +int main (int argc, const char **argv) +{ + return helper (argc, argv); +} diff --git a/checkers/test-sources/multiple-2.c b/checkers/test-sources/multiple-2.c new file mode 100644 index 0000000..a7a20ac --- /dev/null +++ b/checkers/test-sources/multiple-2.c @@ -0,0 +1,9 @@ +#include <stdio.h> + +int helper (int argc, const char **argv) +{ + int i; + for (i = 0; i < argc; i++) + printf ("argv[%i]: %s\n", i, argv[i]); + return 0; +} diff --git a/checkers/test-sources/out-of-bounds.c b/checkers/test-sources/out-of-bounds.c new file mode 100644 index 0000000..4137389 --- /dev/null +++ b/checkers/test-sources/out-of-bounds.c @@ -0,0 +1,6 @@ +int out_of_bounds (void) +{ + int arr[10]; + + return arr[15]; +} diff --git a/checkers/test-sources/read-through-null.c b/checkers/test-sources/read-through-null.c new file mode 100644 index 0000000..2f0450c --- /dev/null +++ b/checkers/test-sources/read-through-null.c @@ -0,0 +1,4 @@ +int read_through_null (void) +{ + return *(int *)0; +} diff --git a/checkers/test-sources/return-of-stack-address.c b/checkers/test-sources/return-of-stack-address.c new file mode 100644 index 0000000..66c8893 --- /dev/null +++ b/checkers/test-sources/return-of-stack-address.c @@ -0,0 +1,6 @@ +void *test (void) +{ + char tmp[16]; + + return tmp; +} diff --git a/checkers/test-sources/unconditional-file-leak.c b/checkers/test-sources/unconditional-file-leak.c new file mode 100644 index 0000000..3c6655c --- /dev/null +++ b/checkers/test-sources/unconditional-file-leak.c @@ -0,0 +1,10 @@ +#include <stdio.h> + +void test (const char *filename) +{ + int i; + FILE *f; + f = fopen (filename, "w"); + for (i = 0; i < 10; i++) + fprintf (f, "%i: %i", i, i * i); +} -- 1.8.5.3