text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE134_Uncontrolled_Format_String__char_environment_w32_vsnprintf_09.c
  Label Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml
  Template File: sources-vasinks-09.tmpl.c
  */
  /*
   * @description
   * CWE: 134 Uncontrolled Format String
   * BadSource: environment Read input from an environment variable
   * GoodSource: Copy a fixed string into data
   * Sinks: w32_vsnprintf
   *    GoodSink: vsnprintf with a format string
   *    BadSink : vsnprintf without a format string
   * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE)
   *
   * */
  
  #include <stdarg.h>
  #include "std_testcase.h"
  
  #ifndef _WIN32
  #include <wchar.h>
  #endif
  
  #define ENV_VARIABLE "ADD"
  
  #ifdef _WIN32
  #define GETENV getenv
  #else
  #define GETENV getenv
  #endif
  
  #ifndef OMITBAD
  
  static void badVaSinkB(char * data, ...)
  {
      {
          char dest[100] = "";
          va_list args;
          va_start(args, data);
          /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */
          vsnprintf(dest, 100-1, data, args);
          va_end(args);
          printLine(dest);
      }
  }
  
  void CWE134_Uncontrolled_Format_String__char_environment_w32_vsnprintf_09_bad()
  {
      char * data;
      char dataBuffer[100] = "";
      data = dataBuffer;
      if(GLOBAL_CONST_TRUE)
      {
          {
              /* 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);
              }
          }
      }
      if(GLOBAL_CONST_TRUE)
      {
          badVaSinkB(data, data);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  static void goodB2G1VaSinkG(char * data, ...)
  {
      {
          char dest[100] = "";
          va_list args;
          va_start(args, data);
          /* FIX: Specify the format disallowing a format string vulnerability */
          vsnprintf(dest, 100-1, "%s", args);
          va_end(args);
          printLine(dest);
      }
  }
  
  /* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */
  static void goodB2G1()
  {
      char * data;
      char dataBuffer[100] = "";
      data = dataBuffer;
      if(GLOBAL_CONST_TRUE)
      {
          {
              /* 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);
              }
          }
      }
      if(GLOBAL_CONST_FALSE)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          printLine("Benign, fixed string");
      }
      else
      {
          goodB2G1VaSinkG(data, data);
      }
  }
  
  static void goodB2G2VaSinkG(char * data, ...)
  {
      {
          char dest[100] = "";
          va_list args;
          va_start(args, data);
          /* FIX: Specify the format disallowing a format string vulnerability */
          vsnprintf(dest, 100-1, "%s", args);
          va_end(args);
          printLine(dest);
      }
  }
  
  /* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */
  static void goodB2G2()
  {
      char * data;
      char dataBuffer[100] = "";
      data = dataBuffer;
      if(GLOBAL_CONST_TRUE)
      {
          {
              /* 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);
              }
          }
      }
      if(GLOBAL_CONST_TRUE)
      {
          goodB2G2VaSinkG(data, data);
      }
  }
  
  static void goodG2B1VaSinkB(char * data, ...)
  {
      {
          char dest[100] = "";
          va_list args;
          va_start(args, data);
          /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */
          vsnprintf(dest, 100-1, data, args);
          va_end(args);
          printLine(dest);
      }
  }
  
  /* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */
  static void goodG2B1()
  {
      char * data;
      char dataBuffer[100] = "";
      data = dataBuffer;
      if(GLOBAL_CONST_FALSE)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          printLine("Benign, fixed string");
      }
      else
      {
          /* FIX: Use a fixed string that does not contain a format specifier */
          strcpy(data, "fixedstringtest");
      }
      if(GLOBAL_CONST_TRUE)
      {
          goodG2B1VaSinkB(data, data);
      }
  }
  
  static void goodG2B2VaSinkB(char * data, ...)
  {
      {
          char dest[100] = "";
          va_list args;
          va_start(args, data);
          /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */
          vsnprintf(dest, 100-1, data, args);
          va_end(args);
          printLine(dest);
      }
  }
  
  /* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */
  static void goodG2B2()
  {
      char * data;
      char dataBuffer[100] = "";
      data = dataBuffer;
      if(GLOBAL_CONST_TRUE)
      {
          /* FIX: Use a fixed string that does not contain a format specifier */
          strcpy(data, "fixedstringtest");
      }
      if(GLOBAL_CONST_TRUE)
      {
          goodG2B2VaSinkB(data, data);
      }
  }
  
  void CWE134_Uncontrolled_Format_String__char_environment_w32_vsnprintf_09_good()
  {
      goodG2B1();
      goodG2B2();
      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()...");
      CWE134_Uncontrolled_Format_String__char_environment_w32_vsnprintf_09_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE134_Uncontrolled_Format_String__char_environment_w32_vsnprintf_09_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  