text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE190_Integer_Overflow__int_fscanf_multiply_05.c
  Label Definition File: CWE190_Integer_Overflow__int.label.xml
  Template File: sources-sinks-05.tmpl.c
  */
  /*
   * @description
   * CWE: 190 Integer Overflow
   * BadSource: fscanf Read data from the console using fscanf()
   * GoodSource: Small, non-zero
   * Sinks: multiply
   *    GoodSink: Ensure there is no overflow before performing the multiplication
   *    BadSink : Multiply data by 2
   * Flow Variant: 05 Control flow: if(static_t) and if(static_f)
   *
   * */
  
  #include "std_testcase.h"
  
  /* The two variables below are not defined as "const", but are never
     assigned any other value, so a tool should be able to identify that
     reads of these will always return their initialized values. */
  static int static_t = 1; /* true */
  static int static_f = 0; /* false */
  
  #ifndef OMITBAD
  
  void CWE190_Integer_Overflow__int_fscanf_multiply_05_bad()
  {
      int data;
      /* Initialize data */
      data = -1;
      if(static_t)
      {
          fscanf (stdin, "%d", &data);
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */
          data = 5;
      }
      if(static_t)
      {
          {
              /* POTENTIAL FLAW: Multiplying data by 2 could cause an integer overflow */
              int result = data * 2;
              printIntLine(result);
          }
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
              int result = -1;
              /* FIX: Add a check to prevent an overflow from occurring */
              if (data <= (INT_MAX/2))
              {
                  result = data * 2;
                  printIntLine(result);
              }
              else
              {
                  printLine("Input value is too large to perform arithmetic safely.");
              }
          }
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodB2G1() - use badsource and goodsink by changing the second static_t to static_f */
  static void goodB2G1()
  {
      int data;
      /* Initialize data */
      data = -1;
      if(static_t)
      {
          fscanf (stdin, "%d", &data);
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */
          data = 5;
      }
      if(static_f)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
              /* POTENTIAL FLAW: Multiplying data by 2 could cause an integer overflow */
              int result = data * 2;
              printIntLine(result);
          }
      }
      else
      {
          {
              int result = -1;
              /* FIX: Add a check to prevent an overflow from occurring */
              if (data <= (INT_MAX/2))
              {
                  result = data * 2;
                  printIntLine(result);
              }
              else
              {
                  printLine("Input value is too large to perform arithmetic safely.");
              }
          }
      }
  }
  
  /* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */
  static void goodB2G2()
  {
      int data;
      /* Initialize data */
      data = -1;
      if(static_t)
      {
          fscanf (stdin, "%d", &data);
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */
          data = 5;
      }
      if(static_t)
      {
          {
              int result = -1;
              /* FIX: Add a check to prevent an overflow from occurring */
              if (data <= (INT_MAX/2))
              {
                  result = data * 2;
                  printIntLine(result);
              }
              else
              {
                  printLine("Input value is too large to perform arithmetic safely.");
              }
          }
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
              /* POTENTIAL FLAW: Multiplying data by 2 could cause an integer overflow */
              int result = data * 2;
              printIntLine(result);
          }
      }
  }
  
  /* goodG2B1() - use goodsource and badsink by changing the first static_t to static_f */
  static void goodG2B1()
  {
      int data;
      /* Initialize data */
      data = -1;
      if(static_f)
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          fscanf (stdin, "%d", &data);
      }
      else
      {
          /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */
          data = 5;
      }
      if(static_t)
      {
          {
              /* POTENTIAL FLAW: Multiplying data by 2 could cause an integer overflow */
              int result = data * 2;
              printIntLine(result);
          }
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
              int result = -1;
              /* FIX: Add a check to prevent an overflow from occurring */
              if (data <= (INT_MAX/2))
              {
                  result = data * 2;
                  printIntLine(result);
              }
              else
              {
                  printLine("Input value is too large to perform arithmetic safely.");
              }
          }
      }
  }
  
  /* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */
  static void goodG2B2()
  {
      int data;
      /* Initialize data */
      data = -1;
      if(static_t)
      {
          /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */
          data = 5;
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          fscanf (stdin, "%d", &data);
      }
      if(static_t)
      {
          {
              /* POTENTIAL FLAW: Multiplying data by 2 could cause an integer overflow */
              int result = data * 2;
              printIntLine(result);
          }
      }
      else
      {
          /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */
          {
              int result = -1;
              /* FIX: Add a check to prevent an overflow from occurring */
              if (data <= (INT_MAX/2))
              {
                  result = data * 2;
                  printIntLine(result);
              }
              else
              {
                  printLine("Input value is too large to perform arithmetic safely.");
              }
          }
      }
  }
  
  void CWE190_Integer_Overflow__int_fscanf_multiply_05_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()...");
      CWE190_Integer_Overflow__int_fscanf_multiply_05_good();
      printLine("Finished good()");
  #endif /* OMITGOOD */
  #ifndef OMITBAD
      printLine("Calling bad()...");
      CWE190_Integer_Overflow__int_fscanf_multiply_05_bad();
      printLine("Finished bad()");
  #endif /* OMITBAD */
      return 0;
  }
  
  #endif
  