text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE134_Uncontrolled_Format_String__char_file_snprintf_43.cpp
  Label Definition File: CWE134_Uncontrolled_Format_String.label.xml
  Template File: sources-sinks-43.tmpl.cpp
  */
  /*
   * @description
   * CWE: 134 Uncontrolled Format String
   * BadSource: file Read input from a file
   * 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: 43 Data flow: data flows using a C++ reference from one function to another in the same source file
   *
   * */
  
  #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
  
  #ifdef _WIN32
  #define SNPRINTF _snprintf
  #else
  #define SNPRINTF snprintf
  #endif
  
  namespace CWE134_Uncontrolled_Format_String__char_file_snprintf_43
  {
  
  #ifndef OMITBAD
  
  static void badSource(char * &data)
  {
      {
          /* 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);
              }
          }
      }
  }
  
  void bad()
  {
      char * data;
      char dataBuffer[100] = "";
      data = dataBuffer;
      badSource(data);
      {
          char dest[100] = "";
          /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */
          SNPRINTF(dest, 100-1, data);
          printLine(dest);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B() uses the GoodSource with the BadSink */
  static void goodG2BSource(char * &data)
  {
      /* FIX: Use a fixed string that does not contain a format specifier */
      strcpy(data, "fixedstringtest");
  }
  
  static void goodG2B()
  {
      char * data;
      char dataBuffer[100] = "";
      data = dataBuffer;
      goodG2BSource(data);
      {
          char dest[100] = "";
          /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */
          SNPRINTF(dest, 100-1, data);
          printLine(dest);
      }
  }
  
  /* goodB2G() uses the BadSource with the GoodSink */
  static void goodB2GSource(char * &data)
  {
      {
          /* 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);
              }
          }
      }
  }
  
  static void goodB2G()
  {
      char * data;
      char dataBuffer[100] = "";
      data = dataBuffer;
      goodB2GSource(data);
      {
          char dest[100] = "";
          /* FIX: Specify the format disallowing a format string vulnerability */
          SNPRINTF(dest, 100-1, "%s", data);
          printLine(dest);
      }
  }
  
  void good()
  {
      goodG2B();
      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 CWE134_Uncontrolled_Format_String__char_file_snprintf_43; /* 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
  