text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE761_Free_Pointer_Not_At_Start_Of_Buffer__wchar_t_fromConsole_15.c
  Label Definition File: CWE761_Free_Pointer_Not_At_Start_Of_Buffer.label.xml
  Template File: source-sinks-15.tmpl.c
  */
  /*
   * @description
   * CWE: 761 Free Pointer Not At Start of Buffer
   * BadSource: fromConsole Read input from the console
   * Sinks:
   *    GoodSink: free() memory correctly at the start of the buffer
   *    BadSink : free() memory not at the start of the buffer
   * Flow Variant: 15 Control flow: switch(6)
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  #define SEARCH_CHAR L'S'
  #define BAD_SRC_FIXED L"Fixed String" /* MAINTENANCE NOTE: This string must contain the SEARCH_CHAR */
  
  #ifndef OMITBAD
  
  void CWE761_Free_Pointer_Not_At_Start_Of_Buffer__wchar_t_fromConsole_15_bad()
  {
      wchar_t * data;
      data = (wchar_t *)malloc(100*sizeof(wchar_t));
      data[0] = L'\0';
      {
          /* Read input from the console */
          size_t data_len = wcslen(data);
          /* if there is room in data, read into it from the console */
          if(100-data_len > 1)
          {
              fgetws(data+data_len, (int)(100-data_len), stdin);
              /* The next 3 lines remove the carriage return from the string that is
               * inserted by fgetws() */
              data_len = wcslen(data);
              if (data_len > 0)
              {
                  data[data_len-1] = L'\0';
              }
          }
      }
      switch(6)
      {
      case 6:
          /* 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);
          break;
      default:
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
      {
          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);
      }
      break;
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodB2G1() - use badsource and goodsink by changing the switch to switch(5) */
  static void goodB2G1()
  {
      wchar_t * data;
      data = (wchar_t *)malloc(100*sizeof(wchar_t));
      data[0] = L'\0';
      {
          /* Read input from the console */
          size_t data_len = wcslen(data);
          /* if there is room in data, read into it from the console */
          if(100-data_len > 1)
          {
              fgetws(data+data_len, (int)(100-data_len), stdin);
              /* The next 3 lines remove the carriage return from the string that is
               * inserted by fgetws() */
              data_len = wcslen(data);
              if (data_len > 0)
              {
                  data[data_len-1] = L'\0';
              }
          }
      }
      switch(5)
      {
      case 6:
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* 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);
          break;
      default:
      {
          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);
      }
      break;
      }
  }
  
  /* goodB2G2() - use badsource and goodsink by reversing the blocks in the switch */
  static void goodB2G2()
  {
      wchar_t * data;
      data = (wchar_t *)malloc(100*sizeof(wchar_t));
      data[0] = L'\0';
      {
          /* Read input from the console */
          size_t data_len = wcslen(data);
          /* if there is room in data, read into it from the console */
          if(100-data_len > 1)
          {
              fgetws(data+data_len, (int)(100-data_len), stdin);
              /* The next 3 lines remove the carriage return from the string that is
               * inserted by fgetws() */
              data_len = wcslen(data);
              if (data_len > 0)
              {
                  data[data_len-1] = L'\0';
              }
          }
      }
      switch(6)
      {
      case 6:
      {
          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);
      }
      break;
      default:
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* 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);
          break;
      }
  }
  
  void CWE761_Free_Pointer_Not_At_Start_Of_Buffer__wchar_t_fromConsole_15_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_fromConsole_15_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE761_Free_Pointer_Not_At_Start_Of_Buffer__wchar_t_fromConsole_15_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  