/tags/ttn-post-libtool-1-4-3-upgrade/SWIG/Tools/WAD/Test/debug.c
C | 119 lines | 87 code | 20 blank | 12 comment | 22 complexity | 2db5e84d11316dda46715cc6cd68ef76 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
1/* -----------------------------------------------------------------------------
2 * debug.c
3 *
4 * This file contains a variety of different programming errors to test with
5 * WAD.
6 * ----------------------------------------------------------------------------- */
7
8#include <stdio.h>
9#include <string.h>
10#include <assert.h>
11
12typedef double Real;
13typedef Real Float;
14
15char buffer[256];
16
17/* A simple segmentation fault on an uninitialized pointer */
18int seg_crash() {
19 int *a = 0;
20 *a = 3;
21 return 1;
22}
23
24/* Blow the process heap */
25
26int blowheap_crash() {
27 int i;
28 int *a = (int *) malloc(sizeof(int));
29
30 for (i = 0;; i++) {
31 a[i] = i;
32 }
33}
34
35/* Buffer overflow crash on the stack */
36int overflow_crash() {
37 int a[512];
38 int i;
39
40 for (i = 0; i < 1024; i++) {
41 a[i] = i;
42 }
43}
44
45/* A simple bus error. */
46int bus_crash() {
47 double *a = (double *) (buffer+1);
48 *a = 3.4;
49 return 1;
50}
51
52/* An assertion */
53int abort_crash(int n) {
54 assert(n > 0);
55 return 1;
56}
57
58/* A math error (maybe) */
59int math_crash(int x, int y) {
60 return x/y;
61}
62
63void type_crash(int a, short b, char c, unsigned long d, float f, double g) {
64 int la;
65 short lb;
66 char lc;
67 long ld;
68 float lf;
69 double lg;
70 long ll;
71
72 la = a;
73 lb = b;
74 lc = c;
75 ld = ld;
76 lf = lf;
77 lg = lg;
78 assert(0);
79}
80
81#ifdef NEED_MAIN
82
83static const char *usage="\n\
84Usage: debug type\n\
85 seg - Fail with an uninitialized pointer.\n\
86 bus - Fail with a bus error.\n\
87 abort - Fail with an assertion error.\n\
88 math - Fail with a math error.\n\
89 heap - Blow the process heap.\n\
90 overflow - Buffer overflow on the stack.\n\
91";
92
93int main(int argc, char **argv) {
94 int n;
95
96 printf("WAD debug program.\n");
97
98 if (argc < 2) {
99 printf("%s\n", usage);
100 exit(0);
101 }
102 if (strcmp(argv[1],"abort") == 0) {
103 abort_crash(-4);
104 } else if (strcmp(argv[1],"seg") ==0) {
105 seg_crash();
106 } else if (strcmp(argv[1],"bus") == 0) {
107 bus_crash();
108 } else if (strcmp(argv[1],"math") == 0) {
109 math_crash(3,0);
110 } else if (strcmp(argv[1],"heap") == 0) {
111 blowheap_crash();
112 } else if (strcmp(argv[1],"overflow") == 0) {
113 overflow_crash();
114 } else if (strcmp(argv[1],"type") == 0) {
115 type_crash(0,2,'x',420000,3.14159,2.1828);
116 }
117}
118
119#endif