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.

CWE476_NULL_Pointer_Dereference__binary_if_04.c 3.4 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* TEMPLATE GENERATED TESTCASE FILE
  2. Filename: CWE476_NULL_Pointer_Dereference__binary_if_04.c
  3. Label Definition File: CWE476_NULL_Pointer_Dereference.pointflaw.label.xml
  4. Template File: point-flaw-04.tmpl.c
  5. */
  6. /*
  7. * @description
  8. * CWE: 476 NULL Pointer Dereference
  9. * Sinks: binary_if
  10. * GoodSink: Do not check for NULL after the pointer has been dereferenced
  11. * BadSink : Check for NULL after a pointer has already been dereferenced
  12. * Flow Variant: 04 Control flow: if(STATIC_CONST_TRUE) and if(STATIC_CONST_FALSE)
  13. *
  14. * */
  15. #include "std_testcase.h"
  16. /* The two variables below are declared "const", so a tool should
  17. be able to identify that reads of these will always return their
  18. initialized values. */
  19. static const int STATIC_CONST_TRUE = 1; /* true */
  20. static const int STATIC_CONST_FALSE = 0; /* false */
  21. #ifndef OMITBAD
  22. void CWE476_NULL_Pointer_Dereference__binary_if_04_bad()
  23. {
  24. if(STATIC_CONST_TRUE)
  25. {
  26. {
  27. twoIntsStruct *twoIntsStructPointer = NULL;
  28. /* FLAW: Using a single & in the if statement will cause both sides of the expression to be evaluated
  29. * thus causing a NPD */
  30. if ((twoIntsStructPointer != NULL) & (twoIntsStructPointer->intOne == 5))
  31. {
  32. printLine("intOne == 5");
  33. }
  34. }
  35. }
  36. }
  37. #endif /* OMITBAD */
  38. #ifndef OMITGOOD
  39. /* good1() uses if(STATIC_CONST_FALSE) instead of if(STATIC_CONST_TRUE) */
  40. static void good1()
  41. {
  42. if(STATIC_CONST_FALSE)
  43. {
  44. /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
  45. printLine("Benign, fixed string");
  46. }
  47. else
  48. {
  49. {
  50. twoIntsStruct *twoIntsStructPointer = NULL;
  51. /* FIX: Use && in the if statement so that if the left side of the expression fails then
  52. * the right side will not be evaluated */
  53. if ((twoIntsStructPointer != NULL) && (twoIntsStructPointer->intOne == 5))
  54. {
  55. printLine("intOne == 5");
  56. }
  57. }
  58. }
  59. }
  60. /* good2() reverses the bodies in the if statement */
  61. static void good2()
  62. {
  63. if(STATIC_CONST_TRUE)
  64. {
  65. {
  66. twoIntsStruct *twoIntsStructPointer = NULL;
  67. /* FIX: Use && in the if statement so that if the left side of the expression fails then
  68. * the right side will not be evaluated */
  69. if ((twoIntsStructPointer != NULL) && (twoIntsStructPointer->intOne == 5))
  70. {
  71. printLine("intOne == 5");
  72. }
  73. }
  74. }
  75. }
  76. void CWE476_NULL_Pointer_Dereference__binary_if_04_good()
  77. {
  78. good1();
  79. good2();
  80. }
  81. #endif /* OMITGOOD */
  82. /* Below is the main(). It is only used when building this testcase on
  83. its own for testing or for building a binary to use in testing binary
  84. analysis tools. It is not used when compiling all the testcases as one
  85. application, which is how source code analysis tools are tested. */
  86. #ifdef INCLUDEMAIN
  87. int main(int argc, char * argv[])
  88. {
  89. /* seed randomness */
  90. srand( (unsigned)time(NULL) );
  91. #ifndef OMITGOOD
  92. printLine("Calling good()...");
  93. CWE476_NULL_Pointer_Dereference__binary_if_04_good();
  94. printLine("Finished good()");
  95. #endif /* OMITGOOD */
  96. #ifndef OMITBAD
  97. printLine("Calling bad()...");
  98. CWE476_NULL_Pointer_Dereference__binary_if_04_bad();
  99. printLine("Finished bad()");
  100. #endif /* OMITBAD */
  101. return 0;
  102. }
  103. #endif

No Description

Contributors (1)