text: /* TEMPLATE GENERATED TESTCASE FILE
  Filename: CWE195_Signed_to_Unsigned_Conversion_Error__rand_malloc_51b.c
  Label Definition File: CWE195_Signed_to_Unsigned_Conversion_Error.label.xml
  Template File: sources-sink-51b.tmpl.c
  */
  /*
   * @description
   * CWE: 195 Signed to Unsigned Conversion Error
   * BadSource: rand Set data to result of rand(), which may be zero
   * GoodSource: Positive integer
   * Sink: malloc
   *    BadSink : Allocate memory using malloc() with the size of data
   * Flow Variant: 51 Data flow: data passed as an argument from one function to another in different source files
   *
   * */
  
  #include "std_testcase.h"
  
  /* all the sinks are the same, we just want to know where the hit originated if a tool flags one */
  
  #ifndef OMITBAD
  
  void CWE195_Signed_to_Unsigned_Conversion_Error__rand_malloc_51b_badSink(int 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 CWE195_Signed_to_Unsigned_Conversion_Error__rand_malloc_51b_goodG2BSink(int 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 /* OMITGOOD */
  