text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE134_Uncontrolled_Format_String__char_fromConsole_vprintf_44.c
  Label Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml
  Template File: sources-vasinks-44.tmpl.c
  */
  /*
   * @description
   * CWE: 134 Uncontrolled Format String
   * BadSource: fromConsole Read input from the console
   * GoodSource: Copy a fixed string into data
   * Sinks: vprintf
   *    GoodSink: vprintf with a format string
   *    BadSink : vprintf without a format string
   * Flow Variant: 44 Data/control flow: data passed as an argument from one function to a function in the same source file called via a function pointer
   *
   * */
  
  #include <stdarg.h>
  #include "std_testcase.h"
  
  #ifndef OMITBAD
  
  static void bad_vasink(char * data, ...)
  {
      {
          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_fromConsole_vprintf_44_bad()
  {
      char * data;
      /* define a function pointer */
      void (*func_ptr) (char *, ...) = bad_vasink;
      char data_buf[100] = "";
      data = data_buf;
      {
          /* Read input from the console */
          size_t data_len = strlen(data);
          /* if there is room in data, read into it from the console */
          if(100-data_len > 1)
          {
              fgets(data+data_len, (int)(100-data_len), stdin);
              /* The next 3 lines remove the carriage return from the string that is
               * inserted by fgets() */
              data_len = strlen(data);
              if (data_len > 0)
              {
                  data[data_len-1] = '\0';
              }
          }
      }
      /* use the function pointer */
      func_ptr(data, data);
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  static void goodG2B_vasink(char * data, ...)
  {
      {
          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;
      void (*func_ptr) (char *, ...) = goodG2B_vasink;
      char data_buf[100] = "";
      data = data_buf;
      /* FIX: Use a fixed string that does not contain a format specifier */
      strcpy(data, "fixedstringtest");
      func_ptr(data);
  }
  
  /* goodB2G uses the BadSource with the GoodSink */
  static void goodB2G_vasink(char * data, ...)
  {
      {
          va_list args;
          va_start(args, data);
          /* FIX: Specify the format disallowing a format string vulnerability */
          vprintf("%s", args);
          va_end(args);
      }
  }
  
  static void goodB2G()
  {
      char * data;
      void (*func_ptr) (char *, ...) = goodB2G_vasink;
      char data_buf[100] = "";
      data = data_buf;
      {
          /* Read input from the console */
          size_t data_len = strlen(data);
          /* if there is room in data, read into it from the console */
          if(100-data_len > 1)
          {
              fgets(data+data_len, (int)(100-data_len), stdin);
              /* The next 3 lines remove the carriage return from the string that is
               * inserted by fgets() */
              data_len = strlen(data);
              if (data_len > 0)
              {
                  data[data_len-1] = '\0';
              }
          }
      }
      func_ptr(data);
  }
  
  void CWE134_Uncontrolled_Format_String__char_fromConsole_vprintf_44_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_fromConsole_vprintf_44_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE134_Uncontrolled_Format_String__char_fromConsole_vprintf_44_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  