text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE134_Uncontrolled_Format_String__char_environment_snprintf_44.c
  Label Definition File: CWE134_Uncontrolled_Format_String.label.xml
  Template File: sources-sinks-44.tmpl.c
  */
  /*
   * @description
   * CWE: 134 Uncontrolled Format String
   * BadSource: environment Read input from an environment variable
   * GoodSource: Copy a fixed string into data
   * Sinks: snprintf
   *    GoodSink: snprintf with "%s" as the third argument and data as the fourth
   *    BadSink : snprintf with data as the third argument
   * Flow Variant: 44 Data/control flow: data passed as an argument from one function to a function in the same source file called via a function pointer
   *
   * */
  
  #include "std_testcase.h"
  
  #ifndef _WIN32
  #include <wchar.h>
  #endif
  
  #define ENV_VARIABLE "ADD"
  
  #ifdef _WIN32
  #define GETENV getenv
  #else
  #define GETENV getenv
  #endif
  
  #ifdef _WIN32
  #define SNPRINTF _snprintf
  #else
  #define SNPRINTF snprintf
  #endif
  
  #ifndef OMITBAD
  
  static void badSink(char * data)
  {
      {
          char dest[100] = "";
          /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */
          SNPRINTF(dest, 100-1, data);
          printLine(dest);
      }
  }
  
  void CWE134_Uncontrolled_Format_String__char_environment_snprintf_44_bad()
  {
      char * data;
      /* define a function pointer */
      void (*funcPtr) (char *) = badSink;
      char dataBuffer[100] = "";
      data = dataBuffer;
      {
          /* 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);
          }
      }
      /* use the function pointer */
      funcPtr(data);
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B() uses the GoodSource with the BadSink */
  static void goodG2BSink(char * data)
  {
      {
          char dest[100] = "";
          /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */
          SNPRINTF(dest, 100-1, data);
          printLine(dest);
      }
  }
  
  static void goodG2B()
  {
      char * data;
      void (*funcPtr) (char *) = goodG2BSink;
      char dataBuffer[100] = "";
      data = dataBuffer;
      /* FIX: Use a fixed string that does not contain a format specifier */
      strcpy(data, "fixedstringtest");
      funcPtr(data);
  }
  
  /* goodB2G() uses the BadSource with the GoodSink */
  static void goodB2GSink(char * data)
  {
      {
          char dest[100] = "";
          /* FIX: Specify the format disallowing a format string vulnerability */
          SNPRINTF(dest, 100-1, "%s", data);
          printLine(dest);
      }
  }
  
  static void goodB2G()
  {
      char * data;
      void (*funcPtr) (char *) = goodB2GSink;
      char dataBuffer[100] = "";
      data = dataBuffer;
      {
          /* 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);
          }
      }
      funcPtr(data);
  }
  
  void CWE134_Uncontrolled_Format_String__char_environment_snprintf_44_good()
  {
      goodG2B();
      goodB2G();
  }
  
  #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()...");
      CWE134_Uncontrolled_Format_String__char_environment_snprintf_44_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE134_Uncontrolled_Format_String__char_environment_snprintf_44_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  