text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE134_Uncontrolled_Format_String__char_console_snprintf_11.c
  Label Definition File: CWE134_Uncontrolled_Format_String.label.xml
  Template File: sources-sinks-11.tmpl.c
  */
  /*
   * @description
   * CWE: 134 Uncontrolled Format String
   * BadSource: console Read input from the console
   * 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: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())
   *
   * */
  
  #include "std_testcase.h"
  
  #ifndef _WIN32
  #include <wchar.h>
  #endif
  
  #ifdef _WIN32
  #define SNPRINTF _snprintf
  #else
  #define SNPRINTF snprintf
  #endif
  
  #ifndef OMITBAD
  
  void CWE134_Uncontrolled_Format_String__char_console_snprintf_11_bad()
  {
      char * data;
      char dataBuffer[100] = "";
      data = dataBuffer;
      if(globalReturnsTrue())
      {
          {
              /* Read input from the console */
              size_t dataLen = strlen(data);
              /* if there is room in data, read into it from the console */
              if (100-dataLen > 1)
              {
                  /* POTENTIAL FLAW: Read data from the console */
                  if (fgets(data+dataLen, (int)(100-dataLen), stdin) != NULL)
                  {
                      /* The next few lines remove the carriage return from the string that is
                       * inserted by fgets() */
                      dataLen = strlen(data);
                      if (dataLen > 0 && data[dataLen-1] == '\n')
                      {
                          data[dataLen-1] = '\0';
                      }
                  }
                  else
                  {
                      printLine("fgets() failed");
                      /* Restore NUL terminator if fgets fails */
                      data[dataLen] = '\0';
                  }
              }
          }
      }
      if(globalReturnsTrue())
      {
          {
              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
  
  /* goodB2G1() - use badsource and goodsink by changing the second globalReturnsTrue() to globalReturnsFalse() */
  static void goodB2G1()
  {
      char * data;
      char dataBuffer[100] = "";
      data = dataBuffer;
      if(globalReturnsTrue())
      {
          {
              /* Read input from the console */
              size_t dataLen = strlen(data);
              /* if there is room in data, read into it from the console */
              if (100-dataLen > 1)
              {
                  /* POTENTIAL FLAW: Read data from the console */
                  if (fgets(data+dataLen, (int)(100-dataLen), stdin) != NULL)
                  {
                      /* The next few lines remove the carriage return from the string that is
                       * inserted by fgets() */
                      dataLen = strlen(data);
                      if (dataLen > 0 && data[dataLen-1] == '\n')
                      {
                          data[dataLen-1] = '\0';
                      }
                  }
                  else
                  {
                      printLine("fgets() failed");
                      /* Restore NUL terminator if fgets fails */
                      data[dataLen] = '\0';
                  }
              }
          }
      }
      if(globalReturnsFalse())
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          printLine("Benign, fixed string");
      }
      else
      {
          {
              char dest[100] = "";
              /* FIX: Specify the format disallowing a format string vulnerability */
              SNPRINTF(dest, 100-1, "%s", data);
              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(globalReturnsTrue())
      {
          {
              /* Read input from the console */
              size_t dataLen = strlen(data);
              /* if there is room in data, read into it from the console */
              if (100-dataLen > 1)
              {
                  /* POTENTIAL FLAW: Read data from the console */
                  if (fgets(data+dataLen, (int)(100-dataLen), stdin) != NULL)
                  {
                      /* The next few lines remove the carriage return from the string that is
                       * inserted by fgets() */
                      dataLen = strlen(data);
                      if (dataLen > 0 && data[dataLen-1] == '\n')
                      {
                          data[dataLen-1] = '\0';
                      }
                  }
                  else
                  {
                      printLine("fgets() failed");
                      /* Restore NUL terminator if fgets fails */
                      data[dataLen] = '\0';
                  }
              }
          }
      }
      if(globalReturnsTrue())
      {
          {
              char dest[100] = "";
              /* FIX: Specify the format disallowing a format string vulnerability */
              SNPRINTF(dest, 100-1, "%s", data);
              printLine(dest);
          }
      }
  }
  
  /* goodG2B1() - use goodsource and badsink by changing the first globalReturnsTrue() to globalReturnsFalse() */
  static void goodG2B1()
  {
      char * data;
      char dataBuffer[100] = "";
      data = dataBuffer;
      if(globalReturnsFalse())
      {
          /* 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(globalReturnsTrue())
      {
          {
              char dest[100] = "";
              /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */
              SNPRINTF(dest, 100-1, data);
              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(globalReturnsTrue())
      {
          /* FIX: Use a fixed string that does not contain a format specifier */
          strcpy(data, "fixedstringtest");
      }
      if(globalReturnsTrue())
      {
          {
              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_console_snprintf_11_good()
  {
      goodB2G1();
      goodB2G2();
      goodG2B1();
      goodG2B2();
  }
  
  #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_console_snprintf_11_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE134_Uncontrolled_Format_String__char_console_snprintf_11_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  