malloc-overflow.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // RUN: %clang_cc1 -triple x86_64-apple-macosx10.7.0 -analyze -analyzer-checker=security.experimental.MallocOverflow -verify %s
  2. typedef __typeof__(sizeof(int)) size_t;
  3. extern void * malloc(size_t);
  4. void * f1(int n)
  5. {
  6. return malloc(n * sizeof(int)); // expected-warning {{the computation of the size of the memory allocation may overflow}}
  7. }
  8. void * f2(int n)
  9. {
  10. return malloc(sizeof(int) * n); // // expected-warning {{the computation of the size of the memory allocation may overflow}}
  11. }
  12. void * f3()
  13. {
  14. return malloc(4 * sizeof(int)); // no-warning
  15. }
  16. struct s4
  17. {
  18. int n;
  19. };
  20. void * f4(struct s4 *s)
  21. {
  22. return malloc(s->n * sizeof(int)); // expected-warning {{the computation of the size of the memory allocation may overflow}}
  23. }
  24. void * f5(struct s4 *s)
  25. {
  26. struct s4 s2 = *s;
  27. return malloc(s2.n * sizeof(int)); // expected-warning {{the computation of the size of the memory allocation may overflow}}
  28. }
  29. void * f6(int n)
  30. {
  31. return malloc((n + 1) * sizeof(int)); // expected-warning {{the computation of the size of the memory allocation may overflow}}
  32. }
  33. #include <stddef.h>
  34. extern void * malloc (size_t);
  35. void * f7(int n)
  36. {
  37. if (n > 10)
  38. return NULL;
  39. return malloc(n * sizeof(int)); // no-warning
  40. }
  41. void * f8(int n)
  42. {
  43. if (n < 10)
  44. return malloc(n * sizeof(int)); // no-warning
  45. else
  46. return NULL;
  47. }
  48. void * f9(int n)
  49. {
  50. int * x = malloc(n * sizeof(int)); // expected-warning {{the computation of the size of the memory allocation may overflow}}
  51. for (int i = 0; i < n; i++)
  52. x[i] = i;
  53. return x;
  54. }
  55. void * f10(int n)
  56. {
  57. int * x = malloc(n * sizeof(int)); // expected-warning {{the computation of the size of the memory allocation may overflow}}
  58. int i = 0;
  59. while (i < n)
  60. x[i++] = 0;
  61. return x;
  62. }
  63. void * f11(int n)
  64. {
  65. int * x = malloc(n * sizeof(int)); // expected-warning {{the computation of the size of the memory allocation may overflow}}
  66. int i = 0;
  67. do {
  68. x[i++] = 0;
  69. } while (i < n);
  70. return x;
  71. }
  72. void * f12(int n)
  73. {
  74. n = (n > 10 ? 10 : n);
  75. int * x = malloc(n * sizeof(int)); // no-warning
  76. for (int i = 0; i < n; i++)
  77. x[i] = i;
  78. return x;
  79. }
  80. struct s13
  81. {
  82. int n;
  83. };
  84. void * f13(struct s13 *s)
  85. {
  86. if (s->n > 10)
  87. return NULL;
  88. return malloc(s->n * sizeof(int)); // no warning
  89. }
  90. void * f14(int n)
  91. {
  92. if (n < 0)
  93. return NULL;
  94. return malloc(n * sizeof(int)); // expected-warning {{the computation of the size of the memory allocation may overflow}}
  95. }