PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/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. #include <stdio.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. typedef double Real;
  11. typedef Real Float;
  12. char buffer[256];
  13. /* A simple segmentation fault on an uninitialized pointer */
  14. int seg_crash() {
  15. int *a = 0;
  16. *a = 3;
  17. return 1;
  18. }
  19. /* Blow the process heap */
  20. int blowheap_crash() {
  21. int i;
  22. int *a = (int *) malloc(sizeof(int));
  23. for (i = 0;; i++) {
  24. a[i] = i;
  25. }
  26. }
  27. /* Buffer overflow crash on the stack */
  28. int overflow_crash() {
  29. int a[512];
  30. int i;
  31. for (i = 0; i < 1024; i++) {
  32. a[i] = i;
  33. }
  34. }
  35. /* A simple bus error. */
  36. int bus_crash() {
  37. double *a = (double *) (buffer+1);
  38. *a = 3.4;
  39. return 1;
  40. }
  41. /* An assertion */
  42. int abort_crash(int n) {
  43. assert(n > 0);
  44. return 1;
  45. }
  46. /* A math error (maybe) */
  47. int math_crash(int x, int y) {
  48. return x/y;
  49. }
  50. void type_crash(int a, short b, char c, unsigned long d, float f, double g) {
  51. int la;
  52. short lb;
  53. char lc;
  54. long ld;
  55. float lf;
  56. double lg;
  57. long ll;
  58. la = a;
  59. lb = b;
  60. lc = c;
  61. ld = ld;
  62. lf = lf;
  63. lg = lg;
  64. assert(0);
  65. }
  66. #ifdef NEED_MAIN
  67. static const char *usage="\n\
  68. Usage: debug type\n\
  69. seg - Fail with an uninitialized pointer.\n\
  70. bus - Fail with a bus error.\n\
  71. abort - Fail with an assertion error.\n\
  72. math - Fail with a math error.\n\
  73. heap - Blow the process heap.\n\
  74. overflow - Buffer overflow on the stack.\n\
  75. ";
  76. int main(int argc, char **argv) {
  77. int n;
  78. printf("WAD debug program.\n");
  79. if (argc < 2) {
  80. printf("%s\n", usage);
  81. exit(0);
  82. }
  83. if (strcmp(argv[1],"abort") == 0) {
  84. abort_crash(-4);
  85. } else if (strcmp(argv[1],"seg") ==0) {
  86. seg_crash();
  87. } else if (strcmp(argv[1],"bus") == 0) {
  88. bus_crash();
  89. } else if (strcmp(argv[1],"math") == 0) {
  90. math_crash(3,0);
  91. } else if (strcmp(argv[1],"heap") == 0) {
  92. blowheap_crash();
  93. } else if (strcmp(argv[1],"overflow") == 0) {
  94. overflow_crash();
  95. } else if (strcmp(argv[1],"type") == 0) {
  96. type_crash(0,2,'x',420000,3.14159,2.1828);
  97. }
  98. }
  99. #endif