text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE476_NULL_Pointer_Dereference__int_10.c
  Label Definition File: CWE476_NULL_Pointer_Dereference.label.xml
  Template File: sources-sinks-10.tmpl.c
  */
  /*
   * @description
   * CWE: 476 NULL Pointer Dereference
   * BadSource:  Set data to NULL
   * GoodSource: Initialize data
   * Sinks:
   *    GoodSink: Check for NULL before attempting to print data
   *    BadSink : Print data
   * Flow Variant: 10 Control flow: if(globalTrue) and if(globalFalse)
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  #ifndef OMITBAD
  
  void CWE476_NULL_Pointer_Dereference__int_10_bad()
  {
      int * data;
      if(globalTrue)
      {
          /* POTENTIAL FLAW: Set data to NULL */
          data = NULL;
      }
      if(globalTrue)
      {
          /* POTENTIAL FLAW: Attempt to use data, which may be NULL */
          printIntLine(*data);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodB2G1() - use badsource and goodsink by changing the second globalTrue to globalFalse */
  static void goodB2G1()
  {
      int * data;
      if(globalTrue)
      {
          /* POTENTIAL FLAW: Set data to NULL */
          data = NULL;
      }
      if(globalFalse)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          printLine("Benign, fixed string");
      }
      else
      {
          /* FIX: Check for NULL before attempting to print data */
          if (data != NULL)
          {
              printIntLine(*data);
          }
          else
          {
              printLine("data is NULL");
          }
      }
  }
  
  /* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */
  static void goodB2G2()
  {
      int * data;
      if(globalTrue)
      {
          /* POTENTIAL FLAW: Set data to NULL */
          data = NULL;
      }
      if(globalTrue)
      {
          /* FIX: Check for NULL before attempting to print data */
          if (data != NULL)
          {
              printIntLine(*data);
          }
          else
          {
              printLine("data is NULL");
          }
      }
  }
  
  /* goodG2B1() - use goodsource and badsink by changing the first globalTrue to globalFalse */
  static void goodG2B1()
  {
      int * data;
      int tmpData = 5;
      if(globalFalse)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          printLine("Benign, fixed string");
      }
      else
      {
          /* FIX: Initialize data */
          {
              data = &tmpData;
          }
      }
      if(globalTrue)
      {
          /* POTENTIAL FLAW: Attempt to use data, which may be NULL */
          printIntLine(*data);
      }
  }
  
  /* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */
  static void goodG2B2()
  {
      int * data;
      int tmpData = 5;
      if(globalTrue)
      {
          /* FIX: Initialize data */
          {
              data = &tmpData;
          }
      }
      if(globalTrue)
      {
          /* POTENTIAL FLAW: Attempt to use data, which may be NULL */
          printIntLine(*data);
      }
  }
  
  void CWE476_NULL_Pointer_Dereference__int_10_good()
  {
      goodB2G1();
      goodB2G2();
      goodG2B1();
      goodG2B2();
  }
  
  #endif /* OMITGOOD */
  
  /* Below is the main(). It is only used when building this testcase on
     its own for testing or for building a binary to use in testing binary
     analysis tools. It is not used when compiling all the testcases as one
     application, which is how source code analysis tools are tested. */
  
  #ifdef INCLUDEMAIN
  
  int main(int argc, char * argv[])
  {
      /* seed randomness */
      srand( (unsigned)time(NULL) );
  #ifndef OMITGOOD
      printLine("Calling good()...");
      CWE476_NULL_Pointer_Dereference__int_10_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE476_NULL_Pointer_Dereference__int_10_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  