text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE761_Free_Pointer_Not_At_Start_Of_Buffer__char_fromConsole_10.c
  Label Definition File: CWE761_Free_Pointer_Not_At_Start_Of_Buffer.label.xml
  Template File: source-sinks-10.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: 10 Control flow: if(global_t) and if(global_f)
   *
   * */
  
  #include "std_testcase.h"
  
  #include <wchar.h>
  
  #define SEARCH_CHAR 'S'
  #define BAD_SRC_FIXED "Fixed String" /* MAINTENANCE NOTE: This string must contain the SEARCH_CHAR */
  
  #ifndef OMITBAD
  
  void CWE761_Free_Pointer_Not_At_Start_Of_Buffer__char_fromConsole_10_bad()
  {
      char * data;
      data = (char *)malloc(100*sizeof(char));
      data[0] = '\0';
      {
          /* Read input from the console */
          size_t data_len = strlen(data);
          /* if there is room in data, read into it from the console */
          if(100-data_len > 1)
          {
              fgets(data+data_len, (int)(100-data_len), stdin);
              /* The next 3 lines remove the carriage return from the string that is
               * inserted by fgets() */
              data_len = strlen(data);
              if (data_len > 0)
              {
                  data[data_len-1] = '\0';
              }
          }
      }
      if(global_t)
      {
          /* 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 != '\0'; data++)
          {
              if (*data == SEARCH_CHAR)
              {
                  printLine("We have a match!");
                  break;
              }
          }
          free(data);
      }
      else
      {
          /* 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 < strlen(data); i++)
              {
                  if (data[i] == SEARCH_CHAR)
                  {
                      printLine("We have a match!");
                      break;
                  }
              }
              free(data);
          }
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodB2G1() - use badsource and goodsink by changing global_t to global_f */
  static void goodB2G1()
  {
      char * data;
      data = (char *)malloc(100*sizeof(char));
      data[0] = '\0';
      {
          /* Read input from the console */
          size_t data_len = strlen(data);
          /* if there is room in data, read into it from the console */
          if(100-data_len > 1)
          {
              fgets(data+data_len, (int)(100-data_len), stdin);
              /* The next 3 lines remove the carriage return from the string that is
               * inserted by fgets() */
              data_len = strlen(data);
              if (data_len > 0)
              {
                  data[data_len-1] = '\0';
              }
          }
      }
      if(global_f)
      {
          /* 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 != '\0'; data++)
          {
              if (*data == SEARCH_CHAR)
              {
                  printLine("We have a match!");
                  break;
              }
          }
          free(data);
      }
      else
      {
          {
              size_t i;
              /* FIX: Use a loop variable to traverse through the string pointed to by data */
              for (i=0; i < strlen(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()
  {
      char * data;
      data = (char *)malloc(100*sizeof(char));
      data[0] = '\0';
      {
          /* Read input from the console */
          size_t data_len = strlen(data);
          /* if there is room in data, read into it from the console */
          if(100-data_len > 1)
          {
              fgets(data+data_len, (int)(100-data_len), stdin);
              /* The next 3 lines remove the carriage return from the string that is
               * inserted by fgets() */
              data_len = strlen(data);
              if (data_len > 0)
              {
                  data[data_len-1] = '\0';
              }
          }
      }
      if(global_t)
      {
          {
              size_t i;
              /* FIX: Use a loop variable to traverse through the string pointed to by data */
              for (i=0; i < strlen(data); i++)
              {
                  if (data[i] == SEARCH_CHAR)
                  {
                      printLine("We have a match!");
                      break;
                  }
              }
              free(data);
          }
      }
      else
      {
          /* 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 != '\0'; data++)
          {
              if (*data == SEARCH_CHAR)
              {
                  printLine("We have a match!");
                  break;
              }
          }
          free(data);
      }
  }
  
  void CWE761_Free_Pointer_Not_At_Start_Of_Buffer__char_fromConsole_10_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__char_fromConsole_10_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE761_Free_Pointer_Not_At_Start_Of_Buffer__char_fromConsole_10_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  