text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE194_Unexpected_Sign_Extension__rand_malloc_62a.cpp
  Label Definition File: CWE194_Unexpected_Sign_Extension.label.xml
  Template File: sources-sink-62a.tmpl.cpp
  */
  /*
   * @description
   * CWE: 194 Unexpected Sign Extension
   * BadSource: rand Set data to result of RAND32(), which could be negative
   * GoodSource: Positive integer
   * Sinks: malloc
   *    BadSink : Allocate memory using malloc() with the size of data
   * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  namespace CWE194_Unexpected_Sign_Extension__rand_malloc_62
  {
  
  #ifndef OMITBAD
  
  /* bad function declaration */
  void badSource(short &data);
  
  void bad()
  {
      short data;
      /* Initialize data */
      data = 0;
      badSource(data);
      /* Assume we want to allocate a relatively small buffer */
      if (data < 100)
      {
          /* POTENTIAL FLAW: malloc() takes a size_t (unsigned int) as input and therefore if it is negative,
           * the conversion will cause malloc() to allocate a very large amount of data or fail */
          char * dataBuffer = (char *)malloc(data);
          if (dataBuffer == NULL) {exit(-1);}
          /* Do something with dataBuffer */
          memset(dataBuffer, 'A', data-1);
          dataBuffer[data-1] = '\0';
          printLine(dataBuffer);
          free(dataBuffer);
      }
  }
  
  #endif /* OMITBAD */
  
  #ifndef OMITGOOD
  
  /* goodG2B uses the GoodSource with the BadSink */
  void goodG2BSource(short &data);
  
  static void goodG2B()
  {
      short data;
      /* Initialize data */
      data = 0;
      goodG2BSource(data);
      /* Assume we want to allocate a relatively small buffer */
      if (data < 100)
      {
          /* POTENTIAL FLAW: malloc() takes a size_t (unsigned int) as input and therefore if it is negative,
           * the conversion will cause malloc() to allocate a very large amount of data or fail */
          char * dataBuffer = (char *)malloc(data);
          if (dataBuffer == NULL) {exit(-1);}
          /* Do something with dataBuffer */
          memset(dataBuffer, 'A', data-1);
          dataBuffer[data-1] = '\0';
          printLine(dataBuffer);
          free(dataBuffer);
      }
  }
  
  void good()
  {
      goodG2B();
  }
  
  #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 CWE194_Unexpected_Sign_Extension__rand_malloc_62; /* 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
  