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_01.c 2.2 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* TEMPLATE GENERATED TESTCASE FILE
  2. Filename: CWE476_NULL_Pointer_Dereference__binary_if_01.c
  3. Label Definition File: CWE476_NULL_Pointer_Dereference.pointflaw.label.xml
  4. Template File: point-flaw-01.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: 01 Baseline
  13. *
  14. * */
  15. #include "std_testcase.h"
  16. #ifndef OMITBAD
  17. void CWE476_NULL_Pointer_Dereference__binary_if_01_bad()
  18. {
  19. {
  20. twoIntsStruct *twoIntsStructPointer = NULL;
  21. /* FLAW: Using a single & in the if statement will cause both sides of the expression to be evaluated
  22. * thus causing a NPD */
  23. if ((twoIntsStructPointer != NULL) & (twoIntsStructPointer->intOne == 5))
  24. {
  25. printLine("intOne == 5");
  26. }
  27. }
  28. }
  29. #endif /* OMITBAD */
  30. #ifndef OMITGOOD
  31. static void good1()
  32. {
  33. {
  34. twoIntsStruct *twoIntsStructPointer = NULL;
  35. /* FIX: Use && in the if statement so that if the left side of the expression fails then
  36. * the right side will not be evaluated */
  37. if ((twoIntsStructPointer != NULL) && (twoIntsStructPointer->intOne == 5))
  38. {
  39. printLine("intOne == 5");
  40. }
  41. }
  42. }
  43. void CWE476_NULL_Pointer_Dereference__binary_if_01_good()
  44. {
  45. good1();
  46. }
  47. #endif /* OMITGOOD */
  48. /* Below is the main(). It is only used when building this testcase on
  49. its own for testing or for building a binary to use in testing binary
  50. analysis tools. It is not used when compiling all the testcases as one
  51. application, which is how source code analysis tools are tested. */
  52. #ifdef INCLUDEMAIN
  53. int main(int argc, char * argv[])
  54. {
  55. /* seed randomness */
  56. srand( (unsigned)time(NULL) );
  57. #ifndef OMITGOOD
  58. printLine("Calling good()...");
  59. CWE476_NULL_Pointer_Dereference__binary_if_01_good();
  60. printLine("Finished good()");
  61. #endif /* OMITGOOD */
  62. #ifndef OMITBAD
  63. printLine("Calling bad()...");
  64. CWE476_NULL_Pointer_Dereference__binary_if_01_bad();
  65. printLine("Finished bad()");
  66. #endif /* OMITBAD */
  67. return 0;
  68. }
  69. #endif

No Description

Contributors (1)