text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE761_Free_Pointer_Not_at_Start_of_Buffer__wchar_t_file_08.c
  Label Definition File: CWE761_Free_Pointer_Not_at_Start_of_Buffer.label.xml
  Template File: source-sinks-08.tmpl.c
  */
  /*
   * @description
   * CWE: 761 Free Pointer not at Start of Buffer
   * BadSource: file Read input from a file
   * Sinks:
   *    GoodSink: free() memory correctly at the start of the buffer
   *    BadSink : free() memory not at the start of the buffer
   * Flow Variant: 08 Control flow: if(staticReturnsTrue()) and if(staticReturnsFalse())
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  #ifdef _WIN32
  #define FILENAME "C:\\temp\\file.txt"
  #else
  #define FILENAME "/tmp/file.txt"
  #endif
  
  #define SEARCH_CHAR L'S'
  
  /* The two function below always return the same value, so a tool
     should be able to identify that calls to the functions will always
     return a fixed value. */
  static int staticReturnsTrue()
  {
      return 1;
  }
  
  static int staticReturnsFalse()
  {
      return 0;
  }
  
  #ifndef OMITBAD
  
  void CWE761_Free_Pointer_Not_at_Start_of_Buffer__wchar_t_file_08_bad()
  {
      wchar_t * data;
      data = (wchar_t *)malloc(100*sizeof(wchar_t));
      if (data == NULL) {exit(-1);}
      data[0] = L'\0';
      {
          /* Read input from a file */
          size_t dataLen = wcslen(data);
          FILE * pFile;
          /* if there is room in data, attempt to read the input from a file */
          if (100-dataLen > 1)
          {
              pFile = fopen(FILENAME, "r");
              if (pFile != NULL)
              {
                  /* POTENTIAL FLAW: Read data from a file */
                  if (fgetws(data+dataLen, (int)(100-dataLen), pFile) == NULL)
                  {
                      printLine("fgetws() failed");
                      /* Restore NUL terminator if fgetws fails */
                      data[dataLen] = L'\0';
                  }
                  fclose(pFile);
              }
          }
      }
      if(staticReturnsTrue())
      {
          /* 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 staticReturnsTrue() to staticReturnsFalse() */
  static void goodB2G1()
  {
      wchar_t * data;
      data = (wchar_t *)malloc(100*sizeof(wchar_t));
      if (data == NULL) {exit(-1);}
      data[0] = L'\0';
      {
          /* Read input from a file */
          size_t dataLen = wcslen(data);
          FILE * pFile;
          /* if there is room in data, attempt to read the input from a file */
          if (100-dataLen > 1)
          {
              pFile = fopen(FILENAME, "r");
              if (pFile != NULL)
              {
                  /* POTENTIAL FLAW: Read data from a file */
                  if (fgetws(data+dataLen, (int)(100-dataLen), pFile) == NULL)
                  {
                      printLine("fgetws() failed");
                      /* Restore NUL terminator if fgetws fails */
                      data[dataLen] = L'\0';
                  }
                  fclose(pFile);
              }
          }
      }
      if(staticReturnsFalse())
      {
          /* 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';
      {
          /* Read input from a file */
          size_t dataLen = wcslen(data);
          FILE * pFile;
          /* if there is room in data, attempt to read the input from a file */
          if (100-dataLen > 1)
          {
              pFile = fopen(FILENAME, "r");
              if (pFile != NULL)
              {
                  /* POTENTIAL FLAW: Read data from a file */
                  if (fgetws(data+dataLen, (int)(100-dataLen), pFile) == NULL)
                  {
                      printLine("fgetws() failed");
                      /* Restore NUL terminator if fgetws fails */
                      data[dataLen] = L'\0';
                  }
                  fclose(pFile);
              }
          }
      }
      if(staticReturnsTrue())
      {
          {
              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_file_08_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_file_08_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE761_Free_Pointer_Not_at_Start_of_Buffer__wchar_t_file_08_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  