text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE761_Free_Pointer_Not_at_Start_of_Buffer__char_environment_74a.cpp
  Label Definition File: CWE761_Free_Pointer_Not_at_Start_of_Buffer.label.xml
  Template File: source-sinks-74a.tmpl.cpp
  */
  /*
   * @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: 74 Data flow: data passed in a map from one function to another in different source files
   *
   * */
  
  #include "std_testcase.h"
  #include <map>
  
  #include <wchar.h>
  
  #define ENV_VARIABLE "ADD"
  
  #ifdef _WIN32
  #define GETENV getenv
  #else
  #define GETENV getenv
  #endif
  
  using namespace std;
  
  namespace CWE761_Free_Pointer_Not_at_Start_of_Buffer__char_environment_74
  {
  
  #ifndef OMITBAD
  
  /* bad function declaration */
  void badSink(map<int, char *> dataMap);
  
  void bad()
  {
      char * data;
      map<int, char *> dataMap;
      data = (char *)malloc(100*sizeof(char));
      data[0] = '\0';
      {
          /* Append input from an environment variable to data */
          size_t dataLen = strlen(data);
          char * environment = GETENV(ENV_VARIABLE);
          /* If there is data in the environment variable */
          if (environment != NULL)
          {
              /* POTENTIAL FLAW: Read data from an environment variable */
              strncat(data+dataLen, environment, 100-dataLen-1);
          }
      }
      /* Put data in a map */
      dataMap[0] = data;
      dataMap[1] = data;
      dataMap[2] = data;
      badSink(dataMap);
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodB2G uses the BadSource with the GoodSink */
  void goodB2GSink(map<int, char *> dataMap);
  
  static void goodB2G()
  {
      char * data;
      map<int, char *> dataMap;
      data = (char *)malloc(100*sizeof(char));
      data[0] = '\0';
      {
          /* Append input from an environment variable to data */
          size_t dataLen = strlen(data);
          char * environment = GETENV(ENV_VARIABLE);
          /* If there is data in the environment variable */
          if (environment != NULL)
          {
              /* POTENTIAL FLAW: Read data from an environment variable */
              strncat(data+dataLen, environment, 100-dataLen-1);
          }
      }
      dataMap[0] = data;
      dataMap[1] = data;
      dataMap[2] = data;
      goodB2GSink(dataMap);
  }
  
  void good()
  {
      goodB2G();
  }
  
  #endif /* OMITGOOD */
  
  } /* close namespace */
  
  /* 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
  
  using namespace CWE761_Free_Pointer_Not_at_Start_of_Buffer__char_environment_74; /* so that we can use good and bad easily */
  
  int main(int argc, char * argv[])
  {
      /* seed randomness */
      srand( (unsigned)time(NULL) );
  #ifndef OMITGOOD
      printLine("Calling good()...");
      good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  