diff options
| author | Ralf.Wildenhues@gmx.de <Ralf.Wildenhues@gmx.de> | 2004-10-28 10:43:12 -0700 |
|---|---|---|
| committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-07 21:03:46 -0700 |
| commit | 1b872b55117762f1e3eb31320cb2539a58ecdfec (patch) | |
| tree | 571430fec8b4de1a83c4b682a0b05cc3b2bb07bd /validation | |
| parent | 561beaedd34cc3e23623890c635ef80580fdd18a (diff) | |
| download | sparse-dev-1b872b55117762f1e3eb31320cb2539a58ecdfec.tar.gz | |
[PATCH] More validation tests
badtype2 segfaults on buggy code (similar to last one, now badtype1),
badtype3 similarly,
builtin_safe1 uses __builtin_safe_p. sparse does not consider pure and
const attributes here yet (which might be considered worthwhile),
varargs1 gives a bogus warning on valid code.
Generally, it's a good idea to just throw a bunch of C files at sparse
to find more problems like this. The gcc testsuite is a good example
(where typical problem cases are small already), and, in fact, produces
twenty-some core files.
Diffstat (limited to 'validation')
| -rw-r--r-- | validation/badtype1.c | 1 | ||||
| -rw-r--r-- | validation/badtype2.c | 10 | ||||
| -rw-r--r-- | validation/badtype3.c | 10 | ||||
| -rw-r--r-- | validation/builtin_safe1.c | 26 | ||||
| -rw-r--r-- | validation/struct-size1.c | 17 | ||||
| -rw-r--r-- | validation/varargs1.c | 5 |
6 files changed, 69 insertions, 0 deletions
diff --git a/validation/badtype1.c b/validation/badtype1.c new file mode 100644 index 00000000..4366d8db --- /dev/null +++ b/validation/badtype1.c @@ -0,0 +1 @@ +static void foo(enum bar baz); diff --git a/validation/badtype2.c b/validation/badtype2.c new file mode 100644 index 00000000..aad725df --- /dev/null +++ b/validation/badtype2.c @@ -0,0 +1,10 @@ +//typedef int undef; +extern undef bar(void); +static undef foo(char *c) +{ + char p = *c; + switch (p) { + default: + return bar(); + } +} diff --git a/validation/badtype3.c b/validation/badtype3.c new file mode 100644 index 00000000..0aefe6a0 --- /dev/null +++ b/validation/badtype3.c @@ -0,0 +1,10 @@ +int +foo (int (*func) (undef, void*), void* data) +{ + int err = 0; + while (cur) { + if ((*func) (cur, data)) + break; + } + return err; +} diff --git a/validation/builtin_safe1.c b/validation/builtin_safe1.c new file mode 100644 index 00000000..322ebc39 --- /dev/null +++ b/validation/builtin_safe1.c @@ -0,0 +1,26 @@ +#define MY_MACRO(a) do { \ + __builtin_warning(!__builtin_safe_p(a), "Macro argument with side effects"); \ + a; \ + } while (0) + +int g(int); +int h(int) __attribute__((pure)); +int i(int) __attribute__((const)); + +int foo(int x, int y) +{ + /* unsafe: */ + MY_MACRO(x++); + MY_MACRO(x+=1); + MY_MACRO(x=x+1); + MY_MACRO(x%=y); + MY_MACRO(x=y); + MY_MACRO(g(x)); + MY_MACRO((y,g(x))); + /* safe: */ + MY_MACRO(x+1); + MY_MACRO(h(x)); + MY_MACRO(i(x)); + return x; +} + diff --git a/validation/struct-size1.c b/validation/struct-size1.c new file mode 100644 index 00000000..b2d5755b --- /dev/null +++ b/validation/struct-size1.c @@ -0,0 +1,17 @@ +struct A; +struct B { + struct A *pA; +}; +struct C; +struct E { + struct A **pA; + struct C *pC; +}; +void f(struct E *pE, struct B *pB) +{ + pB->pA = pE->pA[0]; +} +static const struct { int x; } foo[] = {{ 1 }}; +struct C { + int bar[(sizeof foo/sizeof foo[0])]; +}; diff --git a/validation/varargs1.c b/validation/varargs1.c new file mode 100644 index 00000000..6ae66be9 --- /dev/null +++ b/validation/varargs1.c @@ -0,0 +1,5 @@ +extern int foo (const char *, ...); +void error(const char err[]) +{ + foo("%s\n",err); +} |
