text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE588_Attempt_to_Access_Child_of_Non_Structure_Pointer__struct_11.c
  Label Definition File: CWE588_Attempt_to_Access_Child_of_Non_Structure_Pointer__struct.label.xml
  Template File: sources-sink-11.tmpl.c
  */
  /*
   * @description
   * CWE: 588 Attempt to Access Child of Non Structure Pointer
   * BadSource:  Void pointer to an int
   * GoodSource: Void pointer to a twoints struct
   * Sink:
   *    BadSink : Print data
   * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())
   *
   * */
  
  #include "std_testcase.h"
  
  #ifndef OMITBAD
  
  void CWE588_Attempt_to_Access_Child_of_Non_Structure_Pointer__struct_11_bad()
  {
      void * data;
      twoIntsStruct dataGoodBuffer;
      int dataBadBuffer = 100;
      dataGoodBuffer.intOne = 0;
      dataGoodBuffer.intTwo = 0;
      if(globalReturnsTrue())
      {
          /* FLAW: Set data to point to an int */
          data = &dataBadBuffer;
      }
      /* POTENTIAL FLAW: Attempt to print a struct when data may be a non-struct data type */
      printStructLine((twoIntsStruct *)data);
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B1() - use goodsource and badsink by changing the globalReturnsTrue() to globalReturnsFalse() */
  static void goodG2B1()
  {
      void * data;
      twoIntsStruct dataGoodBuffer;
      int dataBadBuffer = 100;
      dataGoodBuffer.intOne = 0;
      dataGoodBuffer.intTwo = 0;
      if(globalReturnsFalse())
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          printLine("Benign, fixed string");
      }
      else
      {
          /* FIX: Set data to point to a twoIntsStruct struct */
          data = &dataGoodBuffer;
      }
      /* POTENTIAL FLAW: Attempt to print a struct when data may be a non-struct data type */
      printStructLine((twoIntsStruct *)data);
  }
  
  /* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */
  static void goodG2B2()
  {
      void * data;
      twoIntsStruct dataGoodBuffer;
      int dataBadBuffer = 100;
      dataGoodBuffer.intOne = 0;
      dataGoodBuffer.intTwo = 0;
      if(globalReturnsTrue())
      {
          /* FIX: Set data to point to a twoIntsStruct struct */
          data = &dataGoodBuffer;
      }
      /* POTENTIAL FLAW: Attempt to print a struct when data may be a non-struct data type */
      printStructLine((twoIntsStruct *)data);
  }
  
  void CWE588_Attempt_to_Access_Child_of_Non_Structure_Pointer__struct_11_good()
  {
      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()...");
      CWE588_Attempt_to_Access_Child_of_Non_Structure_Pointer__struct_11_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE588_Attempt_to_Access_Child_of_Non_Structure_Pointer__struct_11_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  