text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE761_Free_Pointer_Not_at_Start_of_Buffer__wchar_t_fixed_string_04.c
  Label Definition File: CWE761_Free_Pointer_Not_at_Start_of_Buffer.label.xml
  Template File: source-sinks-04.tmpl.c
  */
  /*
   * @description
   * CWE: 761 Free Pointer not at Start of Buffer
   * BadSource: fixed_string Initialize data to be a fixed string
   * Sinks:
   *    GoodSink: free() memory correctly at the start of the buffer
   *    BadSink : free() memory not at the start of the buffer
   * Flow Variant: 04 Control flow: if(STATIC_CONST_TRUE) and if(STATIC_CONST_FALSE)
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  #define BAD_SOURCE_FIXED_STRING L"Fixed String" /* MAINTENANCE NOTE: This string must contain the SEARCH_CHAR */
  
  #define SEARCH_CHAR L'S'
  
  /* The two variables below are declared "const", so a tool should
     be able to identify that reads of these will always return their
     initialized values. */
  static const int STATIC_CONST_TRUE = 1; /* true */
  static const int STATIC_CONST_FALSE = 0; /* false */
  
  #ifndef OMITBAD
  
  void CWE761_Free_Pointer_Not_at_Start_of_Buffer__wchar_t_fixed_string_04_bad()
  {
      wchar_t * data;
      data = (wchar_t *)malloc(100*sizeof(wchar_t));
      if (data == NULL) {exit(-1);}
      data[0] = L'\0';
      /* POTENTIAL FLAW: Initialize data to be a fixed string that contains the search character in the sinks */
      wcscpy(data, BAD_SOURCE_FIXED_STRING);
      if(STATIC_CONST_TRUE)
      {
          /* FLAW: We are incrementing the pointer in the loop - this will cause us to free the
           * memory block not at the start of the buffer */
          for (; *data != L'\0'; data++)
          {
              if (*data == SEARCH_CHAR)
              {
                  printLine("We have a match!");
                  break;
              }
          }
          free(data);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodB2G1() - use badsource and goodsink by changing STATIC_CONST_TRUE to STATIC_CONST_FALSE */
  static void goodB2G1()
  {
      wchar_t * data;
      data = (wchar_t *)malloc(100*sizeof(wchar_t));
      if (data == NULL) {exit(-1);}
      data[0] = L'\0';
      /* POTENTIAL FLAW: Initialize data to be a fixed string that contains the search character in the sinks */
      wcscpy(data, BAD_SOURCE_FIXED_STRING);
      if(STATIC_CONST_FALSE)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          printLine("Benign, fixed string");
      }
      else
      {
          {
              size_t i;
              /* FIX: Use a loop variable to traverse through the string pointed to by data */
              for (i=0; i < wcslen(data); i++)
              {
                  if (data[i] == SEARCH_CHAR)
                  {
                      printLine("We have a match!");
                      break;
                  }
              }
              free(data);
          }
      }
  }
  
  /* goodB2G2() - use badsource and goodsink by reversing statements in if  */
  static void goodB2G2()
  {
      wchar_t * data;
      data = (wchar_t *)malloc(100*sizeof(wchar_t));
      if (data == NULL) {exit(-1);}
      data[0] = L'\0';
      /* POTENTIAL FLAW: Initialize data to be a fixed string that contains the search character in the sinks */
      wcscpy(data, BAD_SOURCE_FIXED_STRING);
      if(STATIC_CONST_TRUE)
      {
          {
              size_t i;
              /* FIX: Use a loop variable to traverse through the string pointed to by data */
              for (i=0; i < wcslen(data); i++)
              {
                  if (data[i] == SEARCH_CHAR)
                  {
                      printLine("We have a match!");
                      break;
                  }
              }
              free(data);
          }
      }
  }
  
  void CWE761_Free_Pointer_Not_at_Start_of_Buffer__wchar_t_fixed_string_04_good()
  {
      goodB2G1();
      goodB2G2();
  }
  
  #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()...");
      CWE761_Free_Pointer_Not_at_Start_of_Buffer__wchar_t_fixed_string_04_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE761_Free_Pointer_Not_at_Start_of_Buffer__wchar_t_fixed_string_04_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  