text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE761_Free_Pointer_Not_At_Start_Of_Buffer__char_Environment_54e.c
  Label Definition File: CWE761_Free_Pointer_Not_At_Start_Of_Buffer.label.xml
  Template File: source-sinks-54e.tmpl.c
  */
  /*
   * @description
   * CWE: 761 Free Pointer Not At Start of Buffer
   * BadSource: Environment Read input from an environment variable
   * Sinks:
   *    GoodSink: free() memory correctly at the start of the buffer
   *    BadSink : free() memory not at the start of the buffer
   * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files
   *
   * */
  
  #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 */
  
  #define ENV_VARIABLE "ADD"
  
  #ifdef _WIN32
  # define GETENV getenv
  #else
  # define GETENV getenv
  #endif
  
  #ifndef OMITBAD
  
  void CWE761_Free_Pointer_Not_At_Start_Of_Buffer__char_Environment_54e_bad_sink(char * data)
  {
      /* 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);
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  void CWE761_Free_Pointer_Not_At_Start_Of_Buffer__char_Environment_54e_goodB2G_sink(char * data)
  {
      {
          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 /* OMITGOOD */
  