You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

CWE122_Heap_Based_Buffer_Overflow__CWE131_loop_01.c 2.5 kB

3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* TEMPLATE GENERATED TESTCASE FILE
  2. Filename: CWE122_Heap_Based_Buffer_Overflow__CWE131_loop_01.c
  3. Label Definition File: CWE122_Heap_Based_Buffer_Overflow__CWE131.label.xml
  4. Template File: sources-sink-01.tmpl.c
  5. */
  6. /*
  7. * @description
  8. * CWE: 122 Heap Based Buffer Overflow
  9. * BadSource: Allocate memory without using sizeof(int)
  10. * GoodSource: Allocate memory using sizeof(int)
  11. * Sink: loop
  12. * BadSink : Copy array to data using a loop
  13. * Flow Variant: 01 Baseline
  14. *
  15. * */
  16. #include "std_testcase.h"
  17. #ifndef OMITBAD
  18. void CWE122_Heap_Based_Buffer_Overflow__CWE131_loop_01_bad()
  19. {
  20. int * data;
  21. data = NULL;
  22. /* FLAW: Allocate memory without using sizeof(int) */
  23. data = (int *)malloc(10);
  24. if (data == NULL) {exit(-1);}
  25. {
  26. int source[10] = {0};
  27. size_t i;
  28. /* POTENTIAL FLAW: Possible buffer overflow if data was not allocated correctly in the source */
  29. for (i = 0; i < 10; i++)
  30. {
  31. data[i] = source[i];
  32. }
  33. printIntLine(data[0]);
  34. free(data);
  35. }
  36. }
  37. #endif /* OMITBAD */
  38. #ifndef OMITGOOD
  39. /* goodG2B uses the GoodSource with the BadSink */
  40. static void goodG2B()
  41. {
  42. int * data;
  43. data = NULL;
  44. /* FIX: Allocate memory using sizeof(int) */
  45. data = (int *)malloc(10*sizeof(int));
  46. if (data == NULL) {exit(-1);}
  47. {
  48. int source[10] = {0};
  49. size_t i;
  50. /* POTENTIAL FLAW: Possible buffer overflow if data was not allocated correctly in the source */
  51. for (i = 0; i < 10; i++)
  52. {
  53. data[i] = source[i];
  54. }
  55. printIntLine(data[0]);
  56. free(data);
  57. }
  58. }
  59. void CWE122_Heap_Based_Buffer_Overflow__CWE131_loop_01_good()
  60. {
  61. goodG2B();
  62. }
  63. #endif /* OMITGOOD */
  64. /* Below is the main(). It is only used when building this testcase on
  65. * its own for testing or for building a binary to use in testing binary
  66. * analysis tools. It is not used when compiling all the testcases as one
  67. * application, which is how source code analysis tools are tested.
  68. */
  69. #ifdef INCLUDEMAIN
  70. int main(int argc, char * argv[])
  71. {
  72. /* seed randomness */
  73. srand( (unsigned)time(NULL) );
  74. #ifndef OMITGOOD
  75. printLine("Calling good()...");
  76. CWE122_Heap_Based_Buffer_Overflow__CWE131_loop_01_good();
  77. printLine("Finished good()");
  78. #endif /* OMITGOOD */
  79. #ifndef OMITBAD
  80. printLine("Calling bad()...");
  81. CWE122_Heap_Based_Buffer_Overflow__CWE131_loop_01_bad();
  82. printLine("Finished bad()");
  83. #endif /* OMITBAD */
  84. return 0;
  85. }
  86. #endif

No Description

Contributors (1)