text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE134_Uncontrolled_Format_String__char_console_printf_32.c
  Label Definition File: CWE134_Uncontrolled_Format_String.label.xml
  Template File: sources-sinks-32.tmpl.c
  */
  /*
   * @description
   * CWE: 134 Uncontrolled Format String
   * BadSource: console Read input from the console
   * GoodSource: Copy a fixed string into data
   * Sinks: printf
   *    GoodSink: printf with "%s" as the first argument and data as the second
   *    BadSink : printf with only data as an argument
   * Flow Variant: 32 Data flow using two pointers to the same value within the same function
   *
   * */
  
  #include "std_testcase.h"
  
  #ifndef _WIN32
  #include <wchar.h>
  #endif
  
  #ifndef OMITBAD
  
  void CWE134_Uncontrolled_Format_String__char_console_printf_32_bad()
  {
      char * data;
      char * *dataPtr1 = &data;
      char * *dataPtr2 = &data;
      char dataBuffer[100] = "";
      data = dataBuffer;
      {
          char * data = *dataPtr1;
          {
              /* 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';
                  }
              }
          }
          *dataPtr1 = data;
      }
      {
          char * data = *dataPtr2;
          /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */
          printf(data);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B() uses the GoodSource with the BadSink */
  static void goodG2B()
  {
      char * data;
      char * *dataPtr1 = &data;
      char * *dataPtr2 = &data;
      char dataBuffer[100] = "";
      data = dataBuffer;
      {
          char * data = *dataPtr1;
          /* FIX: Use a fixed string that does not contain a format specifier */
          strcpy(data, "fixedstringtest");
          *dataPtr1 = data;
      }
      {
          char * data = *dataPtr2;
          /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */
          printf(data);
      }
  }
  
  /* goodB2G() uses the BadSource with the GoodSink */
  static void goodB2G()
  {
      char * data;
      char * *dataPtr1 = &data;
      char * *dataPtr2 = &data;
      char dataBuffer[100] = "";
      data = dataBuffer;
      {
          char * data = *dataPtr1;
          {
              /* 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';
                  }
              }
          }
          *dataPtr1 = data;
      }
      {
          char * data = *dataPtr2;
          /* FIX: Specify the format disallowing a format string vulnerability */
          printf("%s\n", data);
      }
  }
  
  void CWE134_Uncontrolled_Format_String__char_console_printf_32_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_console_printf_32_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE134_Uncontrolled_Format_String__char_console_printf_32_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  