text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE134_Uncontrolled_Format_String__char_file_vprintf_21.c
  Label Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml
  Template File: sources-vasinks-21.tmpl.c
  */
  /*
   * @description
   * CWE: 134 Uncontrolled Format String
   * BadSource: file Read input from a file
   * GoodSource: Copy a fixed string into data
   * Sinks: vprintf
   *    GoodSink: vprintf with a format string
   *    BadSink : vprintf without a format string
   * Flow Variant: 21 Control flow: Flow controlled by value of a static global variable. All functions contained in one file.
   *
   * */
  
  #include <stdarg.h>
  #include "std_testcase.h"
  
  #ifndef _WIN32
  #include <wchar.h>
  #endif
  
  #ifdef _WIN32
  #define FILENAME "C:\\temp\\file.txt"
  #else
  #define FILENAME "/tmp/file.txt"
  #endif
  
  #ifndef OMITBAD
  
  /* The static variable below is used to drive control flow in the sink function */
  static int badStatic = 0;
  
  static void badVaSink(char * data, ...)
  {
      if(badStatic)
      {
          {
              va_list args;
              va_start(args, data);
              /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */
              vprintf(data, args);
              va_end(args);
          }
      }
  }
  
  void CWE134_Uncontrolled_Format_String__char_file_vprintf_21_bad()
  {
      char * data;
      char dataBuffer[100] = "";
      data = dataBuffer;
      {
          /* Read input from a file */
          size_t dataLen = strlen(data);
          FILE * pFile;
          /* if there is room in data, attempt to read the input from a file */
          if (100-dataLen > 1)
          {
              pFile = fopen(FILENAME, "r");
              if (pFile != NULL)
              {
                  /* POTENTIAL FLAW: Read data from a file */
                  if (fgets(data+dataLen, (int)(100-dataLen), pFile) == NULL)
                  {
                      printLine("fgets() failed");
                      /* Restore NUL terminator if fgets fails */
                      data[dataLen] = '\0';
                  }
                  fclose(pFile);
              }
          }
      }
      badStatic = 1; /* true */
      badVaSink(data, data);
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* The static variables below are used to drive control flow in the sink functions. */
  static int goodB2G1Static = 0;
  static int goodB2G2Static = 0;
  static int goodG2BStatic = 0;
  
  /* goodB2G1() - use badsource and goodsink by setting the static variable to false instead of true */
  static void goodB2G1_vasink(char * data, ...)
  {
      if(goodB2G1Static)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          printLine("Benign, fixed string");
      }
      else
      {
          {
              va_list args;
              va_start(args, data);
              /* FIX: Specify the format disallowing a format string vulnerability */
              vprintf("%s", args);
              va_end(args);
          }
      }
  }
  
  static void goodB2G1()
  {
      char * data;
      char dataBuffer[100] = "";
      data = dataBuffer;
      {
          /* Read input from a file */
          size_t dataLen = strlen(data);
          FILE * pFile;
          /* if there is room in data, attempt to read the input from a file */
          if (100-dataLen > 1)
          {
              pFile = fopen(FILENAME, "r");
              if (pFile != NULL)
              {
                  /* POTENTIAL FLAW: Read data from a file */
                  if (fgets(data+dataLen, (int)(100-dataLen), pFile) == NULL)
                  {
                      printLine("fgets() failed");
                      /* Restore NUL terminator if fgets fails */
                      data[dataLen] = '\0';
                  }
                  fclose(pFile);
              }
          }
      }
      goodB2G1Static = 0; /* false */
      goodB2G1_vasink(data, data);
  }
  
  /* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */
  static void goodB2G2_vasink(char * data, ...)
  {
      if(goodB2G2Static)
      {
          {
              va_list args;
              va_start(args, data);
              /* FIX: Specify the format disallowing a format string vulnerability */
              vprintf("%s", args);
              va_end(args);
          }
      }
  }
  
  static void goodB2G2()
  {
      char * data;
      char dataBuffer[100] = "";
      data = dataBuffer;
      {
          /* Read input from a file */
          size_t dataLen = strlen(data);
          FILE * pFile;
          /* if there is room in data, attempt to read the input from a file */
          if (100-dataLen > 1)
          {
              pFile = fopen(FILENAME, "r");
              if (pFile != NULL)
              {
                  /* POTENTIAL FLAW: Read data from a file */
                  if (fgets(data+dataLen, (int)(100-dataLen), pFile) == NULL)
                  {
                      printLine("fgets() failed");
                      /* Restore NUL terminator if fgets fails */
                      data[dataLen] = '\0';
                  }
                  fclose(pFile);
              }
          }
      }
      goodB2G2Static = 1; /* true */
      goodB2G2_vasink(data, data);
  }
  
  /* goodG2B() - use goodsource and badsink */
  static void goodG2BVaSink(char * data, ...)
  {
      if(goodG2BStatic)
      {
          {
              va_list args;
              va_start(args, data);
              /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */
              vprintf(data, args);
              va_end(args);
          }
      }
  }
  
  static void goodG2B()
  {
      char * data;
      char dataBuffer[100] = "";
      data = dataBuffer;
      /* FIX: Use a fixed string that does not contain a format specifier */
      strcpy(data, "fixedstringtest");
      goodG2BStatic = 1; /* true */
      goodG2BVaSink(data, data);
  }
  
  void CWE134_Uncontrolled_Format_String__char_file_vprintf_21_good()
  {
      goodB2G1();
      goodB2G2();
      goodG2B();
  }
  
  #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_file_vprintf_21_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE134_Uncontrolled_Format_String__char_file_vprintf_21_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  