{"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_11.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_11.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":4604,"source_sha256":"5586b6bda304e844fb753ff742ccc2c5e07a99cb70c7ea69fa2211a95b3bb842","line_count":150,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_11.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE193.label.xml\r\nTemplate File: sources-sink-11.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Point data to a buffer that does not have space for a NULL terminator\r\n * GoodSource: Point data to a buffer that includes space for a NULL terminator\r\n * Sink: loop\r\n * BadSink : Copy array to data using a loop\r\n * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n/* MAINTENANCE NOTE: The length of this string should equal the 10 */\r\n#define SRC_STRING L\"AAAAAAAAAA\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_11_bad()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBadBuffer[10];\r\n wchar_t dataGoodBuffer[10+1];\r\n if(globalReturnsTrue())\r\n {\r\n /* FLAW: Set a pointer to a buffer that does not leave room for a NULL terminator when performing\r\n * string copies in the sinks */\r\n data = dataBadBuffer;\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n wchar_t source[10+1] = SRC_STRING;\r\n size_t i, sourceLen;\r\n sourceLen = wcslen(source);\r\n /* Copy length + 1 to include NUL terminator from source */\r\n /* POTENTIAL FLAW: data may not have enough space to hold source */\r\n for (i = 0; i < sourceLen + 1; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printWLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBadBuffer[10];\r\n wchar_t dataGoodBuffer[10+1];\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Set a pointer to a buffer that leaves room for a NULL terminator when performing\r\n * string copies in the sinks */\r\n data = dataGoodBuffer;\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n wchar_t source[10+1] = SRC_STRING;\r\n size_t i, sourceLen;\r\n sourceLen = wcslen(source);\r\n /* Copy length + 1 to include NUL terminator from source */\r\n /* POTENTIAL FLAW: data may not have enough space to hold source */\r\n for (i = 0; i < sourceLen + 1; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printWLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBadBuffer[10];\r\n wchar_t dataGoodBuffer[10+1];\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Set a pointer to a buffer that leaves room for a NULL terminator when performing\r\n * string copies in the sinks */\r\n data = dataGoodBuffer;\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n wchar_t source[10+1] = SRC_STRING;\r\n size_t i, sourceLen;\r\n sourceLen = wcslen(source);\r\n /* Copy length + 1 to include NUL terminator from source */\r\n /* POTENTIAL FLAW: data may not have enough space to hold source */\r\n for (i = 0; i < sourceLen + 1; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printWLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_11_good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_11_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_11_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_34.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_34.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":4051,"source_sha256":"d3d668feddccbba4e6c2c165621ae2027a711ac761187752743d877e2b877496","line_count":126,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_34.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE193.label.xml\r\nTemplate File: sources-sink-34.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Point data to a buffer that does not have space for a NULL terminator\r\n * GoodSource: Point data to a buffer that includes space for a NULL terminator\r\n * Sinks: loop\r\n * BadSink : Copy array to data using a loop\r\n * Flow Variant: 34 Data flow: use of a union containing two methods of accessing the same data (within the same function)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n/* MAINTENANCE NOTE: The length of this string should equal the 10 */\r\n#define SRC_STRING L\"AAAAAAAAAA\"\r\n\r\ntypedef union\r\n{\r\n wchar_t * unionFirst;\r\n wchar_t * unionSecond;\r\n} CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_34_unionType;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_34_bad()\r\n{\r\n wchar_t * data;\r\n CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_34_unionType myUnion;\r\n wchar_t dataBadBuffer[10];\r\n wchar_t dataGoodBuffer[10+1];\r\n /* FLAW: Set a pointer to a buffer that does not leave room for a NULL terminator when performing\r\n * string copies in the sinks */\r\n data = dataBadBuffer;\r\n data[0] = L'\\0'; /* null terminate */\r\n myUnion.unionFirst = data;\r\n {\r\n wchar_t * data = myUnion.unionSecond;\r\n {\r\n wchar_t source[10+1] = SRC_STRING;\r\n size_t i, sourceLen;\r\n sourceLen = wcslen(source);\r\n /* Copy length + 1 to include NUL terminator from source */\r\n /* POTENTIAL FLAW: data may not have enough space to hold source */\r\n for (i = 0; i < sourceLen + 1; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printWLine(data);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_34_unionType myUnion;\r\n wchar_t dataBadBuffer[10];\r\n wchar_t dataGoodBuffer[10+1];\r\n /* FIX: Set a pointer to a buffer that leaves room for a NULL terminator when performing\r\n * string copies in the sinks */\r\n data = dataGoodBuffer;\r\n data[0] = L'\\0'; /* null terminate */\r\n myUnion.unionFirst = data;\r\n {\r\n wchar_t * data = myUnion.unionSecond;\r\n {\r\n wchar_t source[10+1] = SRC_STRING;\r\n size_t i, sourceLen;\r\n sourceLen = wcslen(source);\r\n /* Copy length + 1 to include NUL terminator from source */\r\n /* POTENTIAL FLAW: data may not have enough space to hold source */\r\n for (i = 0; i < sourceLen + 1; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printWLine(data);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_34_good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_34_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_34_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_63b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_63b.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":2064,"source_sha256":"e33977f8d32964b7aea17f37f2fdc7f2a9028566ea3deaae03d535e68278da26","line_count":67,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_63b.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE193.label.xml\r\nTemplate File: sources-sink-63b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Point data to a buffer that does not have space for a NULL terminator\r\n * GoodSource: Point data to a buffer that includes space for a NULL terminator\r\n * Sinks: loop\r\n * BadSink : Copy array to data using a loop\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n/* MAINTENANCE NOTE: The length of this string should equal the 10 */\r\n#define SRC_STRING L\"AAAAAAAAAA\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_63b_badSink(wchar_t * * dataPtr)\r\n{\r\n wchar_t * data = *dataPtr;\r\n {\r\n wchar_t source[10+1] = SRC_STRING;\r\n size_t i, sourceLen;\r\n sourceLen = wcslen(source);\r\n /* Copy length + 1 to include NUL terminator from source */\r\n /* POTENTIAL FLAW: data may not have enough space to hold source */\r\n for (i = 0; i < sourceLen + 1; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printWLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_63b_goodG2BSink(wchar_t * * dataPtr)\r\n{\r\n wchar_t * data = *dataPtr;\r\n {\r\n wchar_t source[10+1] = SRC_STRING;\r\n size_t i, sourceLen;\r\n sourceLen = wcslen(source);\r\n /* Copy length + 1 to include NUL terminator from source */\r\n /* POTENTIAL FLAW: data may not have enough space to hold source */\r\n for (i = 0; i < sourceLen + 1; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printWLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_68a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_68a.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":3406,"source_sha256":"6083b4e0084c4caf34d5f6e8b328dbf1915ad415e097a8b2b4da577ea8fbcf87","line_count":100,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_68a.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE193.label.xml\r\nTemplate File: sources-sink-68a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Point data to a buffer that does not have space for a NULL terminator\r\n * GoodSource: Point data to a buffer that includes space for a NULL terminator\r\n * Sink: loop\r\n * BadSink : Copy array to data using a loop\r\n * Flow Variant: 68 Data flow: data passed as a global variable from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n/* MAINTENANCE NOTE: The length of this string should equal the 10 */\r\n#define SRC_STRING L\"AAAAAAAAAA\"\r\n\r\nwchar_t * CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_68_badData;\r\nwchar_t * CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_68_goodG2BData;\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_68b_badSink();\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_68_bad()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBadBuffer[10];\r\n wchar_t dataGoodBuffer[10+1];\r\n /* FLAW: Set a pointer to a buffer that does not leave room for a NULL terminator when performing\r\n * string copies in the sinks */\r\n data = dataBadBuffer;\r\n data[0] = L'\\0'; /* null terminate */\r\n CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_68_badData = data;\r\n CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_68b_badSink();\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_68b_goodG2BSink();\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBadBuffer[10];\r\n wchar_t dataGoodBuffer[10+1];\r\n /* FIX: Set a pointer to a buffer that leaves room for a NULL terminator when performing\r\n * string copies in the sinks */\r\n data = dataGoodBuffer;\r\n data[0] = L'\\0'; /* null terminate */\r\n CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_68_goodG2BData = data;\r\n CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_68b_goodG2BSink();\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_68_good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_68_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_68_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_82_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_82_bad.cpp","cwe_id":"CWE121","file_extension":".cpp","language_hint":"C++","source_size_bytes":1400,"source_sha256":"b00bfce810f040269906562ebda1482906c7d863d580a61d33f6cec0fc00eab3","line_count":41,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_82_bad.cpp\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE193.label.xml\r\nTemplate File: sources-sink-82_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Point data to a buffer that does not have space for a NULL terminator\r\n * GoodSource: Point data to a buffer that includes space for a NULL terminator\r\n * Sinks: loop\r\n * BadSink : Copy array to data using a loop\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_82.h\"\r\n\r\nnamespace CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_82\r\n{\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_loop_82_bad::action(wchar_t * data)\r\n{\r\n {\r\n wchar_t source[10+1] = SRC_STRING;\r\n size_t i, sourceLen;\r\n sourceLen = wcslen(source);\r\n /* Copy length + 1 to include NUL terminator from source */\r\n /* POTENTIAL FLAW: data may not have enough space to hold source */\r\n for (i = 0; i < sourceLen + 1; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printWLine(data);\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_05.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_05.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":4558,"source_sha256":"1ee2bea21dc4a59067f4d81cf38192cf45c982b11a248b7aa793d673b9918618","line_count":142,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_05.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE193.label.xml\r\nTemplate File: sources-sink-05.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Point data to a buffer that does not have space for a NULL terminator\r\n * GoodSource: Point data to a buffer that includes space for a NULL terminator\r\n * Sink: memcpy\r\n * BadSink : Copy string to data using memcpy()\r\n * Flow Variant: 05 Control flow: if(staticTrue) and if(staticFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n/* MAINTENANCE NOTE: The length of this string should equal the 10 */\r\n#define SRC_STRING L\"AAAAAAAAAA\"\r\n\r\n/* The two variables below are not defined as \"const\", but are never\r\n * assigned any other value, so a tool should be able to identify that\r\n * reads of these will always return their initialized values.\r\n */\r\nstatic int staticTrue = 1; /* true */\r\nstatic int staticFalse = 0; /* false */\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_05_bad()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBadBuffer[10];\r\n wchar_t dataGoodBuffer[10+1];\r\n if(staticTrue)\r\n {\r\n /* FLAW: Set a pointer to a buffer that does not leave room for a NULL terminator when performing\r\n * string copies in the sinks */\r\n data = dataBadBuffer;\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n wchar_t source[10+1] = SRC_STRING;\r\n /* Copy length + 1 to include NUL terminator from source */\r\n /* POTENTIAL FLAW: data may not have enough space to hold source */\r\n memcpy(data, source, (wcslen(source) + 1) * sizeof(wchar_t));\r\n printWLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the staticTrue to staticFalse */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBadBuffer[10];\r\n wchar_t dataGoodBuffer[10+1];\r\n if(staticFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Set a pointer to a buffer that leaves room for a NULL terminator when performing\r\n * string copies in the sinks */\r\n data = dataGoodBuffer;\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n wchar_t source[10+1] = SRC_STRING;\r\n /* Copy length + 1 to include NUL terminator from source */\r\n /* POTENTIAL FLAW: data may not have enough space to hold source */\r\n memcpy(data, source, (wcslen(source) + 1) * sizeof(wchar_t));\r\n printWLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBadBuffer[10];\r\n wchar_t dataGoodBuffer[10+1];\r\n if(staticTrue)\r\n {\r\n /* FIX: Set a pointer to a buffer that leaves room for a NULL terminator when performing\r\n * string copies in the sinks */\r\n data = dataGoodBuffer;\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n wchar_t source[10+1] = SRC_STRING;\r\n /* Copy length + 1 to include NUL terminator from source */\r\n /* POTENTIAL FLAW: data may not have enough space to hold source */\r\n memcpy(data, source, (wcslen(source) + 1) * sizeof(wchar_t));\r\n printWLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_05_good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_05_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_05_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_54d.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_54d.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":1813,"source_sha256":"63f379658e6cff10944852af63146f82752ab28044089b6fa8a2b61e32c8b752","line_count":51,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_54d.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE193.label.xml\r\nTemplate File: sources-sink-54d.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Point data to a buffer that does not have space for a NULL terminator\r\n * GoodSource: Point data to a buffer that includes space for a NULL terminator\r\n * Sink: memcpy\r\n * BadSink : Copy string to data using memcpy()\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n/* MAINTENANCE NOTE: The length of this string should equal the 10 */\r\n#define SRC_STRING L\"AAAAAAAAAA\"\r\n\r\n/* all the sinks are the same, we just want to know where the hit originated if a tool flags one */\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_54e_badSink(wchar_t * data);\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_54d_badSink(wchar_t * data)\r\n{\r\n CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_54e_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declaration */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_54e_goodG2BSink(wchar_t * data);\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_54d_goodG2BSink(wchar_t * data)\r\n{\r\n CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_54e_goodG2BSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_73a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_73a.cpp","cwe_id":"CWE121","file_extension":".cpp","language_hint":"C++","source_size_bytes":3168,"source_sha256":"9bbade3379a1b87a468dc3637f84ae7e3cfe381cec01b77da05a0c0a7c15b049","line_count":116,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_73a.cpp\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE193.label.xml\r\nTemplate File: sources-sink-73a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Point data to a buffer that does not have space for a NULL terminator\r\n * GoodSource: Point data to a buffer that includes space for a NULL terminator\r\n * Sinks: memcpy\r\n * BadSink : Copy string to data using memcpy()\r\n * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n/* MAINTENANCE NOTE: The length of this string should equal the 10 */\r\n#define SRC_STRING L\"AAAAAAAAAA\"\r\n\r\nusing namespace std;\r\n\r\nnamespace CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_73\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(list dataList);\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n list dataList;\r\n wchar_t dataBadBuffer[10];\r\n wchar_t dataGoodBuffer[10+1];\r\n /* FLAW: Set a pointer to a buffer that does not leave room for a NULL terminator when performing\r\n * string copies in the sinks */\r\n data = dataBadBuffer;\r\n data[0] = L'\\0'; /* null terminate */\r\n /* Put data in a list */\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n badSink(dataList);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(list dataList);\r\n\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n list dataList;\r\n wchar_t dataBadBuffer[10];\r\n wchar_t dataGoodBuffer[10+1];\r\n /* FIX: Set a pointer to a buffer that leaves room for a NULL terminator when performing\r\n * string copies in the sinks */\r\n data = dataGoodBuffer;\r\n data[0] = L'\\0'; /* null terminate */\r\n /* Put data in a list */\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n goodG2BSink(dataList);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_73; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_81a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_81a.cpp","cwe_id":"CWE121","file_extension":".cpp","language_hint":"C++","source_size_bytes":2947,"source_sha256":"afe5e4f59154d8af86cb35e125c336b6027d702287ec5ac0a1d59c20c15c9859","line_count":92,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_81a.cpp\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE193.label.xml\r\nTemplate File: sources-sink-81a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Point data to a buffer that does not have space for a NULL terminator\r\n * GoodSource: Point data to a buffer that includes space for a NULL terminator\r\n * Sinks: memcpy\r\n * BadSink : Copy string to data using memcpy()\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_81.h\"\r\n\r\nnamespace CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_81\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBadBuffer[10];\r\n wchar_t dataGoodBuffer[10+1];\r\n /* FLAW: Set a pointer to a buffer that does not leave room for a NULL terminator when performing\r\n * string copies in the sinks */\r\n data = dataBadBuffer;\r\n data[0] = L'\\0'; /* null terminate */\r\n const CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_81_base& baseObject = CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_81_bad();\r\n baseObject.action(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBadBuffer[10];\r\n wchar_t dataGoodBuffer[10+1];\r\n /* FIX: Set a pointer to a buffer that leaves room for a NULL terminator when performing\r\n * string copies in the sinks */\r\n data = dataGoodBuffer;\r\n data[0] = L'\\0'; /* null terminate */\r\n const CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_81_base& baseObject = CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_81_goodG2B();\r\n baseObject.action(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_81; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_82_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_82_goodG2B.cpp","cwe_id":"CWE121","file_extension":".cpp","language_hint":"C++","source_size_bytes":1330,"source_sha256":"9f7e97129701a4f7ddfd3108fb84ddc85449c58a82e7e9c830ea1360a713fe3a","line_count":36,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_82_goodG2B.cpp\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE193.label.xml\r\nTemplate File: sources-sink-82_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Point data to a buffer that does not have space for a NULL terminator\r\n * GoodSource: Point data to a buffer that includes space for a NULL terminator\r\n * Sinks: memcpy\r\n * BadSink : Copy string to data using memcpy()\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_82.h\"\r\n\r\nnamespace CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_82\r\n{\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memcpy_82_goodG2B::action(wchar_t * data)\r\n{\r\n {\r\n wchar_t source[10+1] = SRC_STRING;\r\n /* Copy length + 1 to include NUL terminator from source */\r\n /* POTENTIAL FLAW: data may not have enough space to hold source */\r\n memcpy(data, source, (wcslen(source) + 1) * sizeof(wchar_t));\r\n printWLine(data);\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memmove_53d.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memmove_53d.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":1964,"source_sha256":"ae20ba98115ba9c0b76f4adc91d2ef804ac4ace71186dab08c901e2ec00bd52b","line_count":57,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memmove_53d.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE193.label.xml\r\nTemplate File: sources-sink-53d.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Point data to a buffer that does not have space for a NULL terminator\r\n * GoodSource: Point data to a buffer that includes space for a NULL terminator\r\n * Sink: memmove\r\n * BadSink : Copy string to data using memmove()\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n/* MAINTENANCE NOTE: The length of this string should equal the 10 */\r\n#define SRC_STRING L\"AAAAAAAAAA\"\r\n\r\n/* all the sinks are the same, we just want to know where the hit originated if a tool flags one */\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memmove_53d_badSink(wchar_t * data)\r\n{\r\n {\r\n wchar_t source[10+1] = SRC_STRING;\r\n /* Copy length + 1 to include NUL terminator from source */\r\n /* POTENTIAL FLAW: data may not have enough space to hold source */\r\n memmove(data, source, (wcslen(source) + 1) * sizeof(wchar_t));\r\n printWLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memmove_53d_goodG2BSink(wchar_t * data)\r\n{\r\n {\r\n wchar_t source[10+1] = SRC_STRING;\r\n /* Copy length + 1 to include NUL terminator from source */\r\n /* POTENTIAL FLAW: data may not have enough space to hold source */\r\n memmove(data, source, (wcslen(source) + 1) * sizeof(wchar_t));\r\n printWLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memmove_73b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memmove_73b.cpp","cwe_id":"CWE121","file_extension":".cpp","language_hint":"C++","source_size_bytes":1964,"source_sha256":"6dc8f97550bd03c774f2f499f0e9b1fa916b8b245abfd24aae0f4f5f7e8a0415","line_count":66,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memmove_73b.cpp\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE193.label.xml\r\nTemplate File: sources-sink-73b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Point data to a buffer that does not have space for a NULL terminator\r\n * GoodSource: Point data to a buffer that includes space for a NULL terminator\r\n * Sinks: memmove\r\n * BadSink : Copy string to data using memmove()\r\n * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n/* MAINTENANCE NOTE: The length of this string should equal the 10 */\r\n#define SRC_STRING L\"AAAAAAAAAA\"\r\n\r\nusing namespace std;\r\n\r\nnamespace CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_memmove_73\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(list dataList)\r\n{\r\n /* copy data out of dataList */\r\n wchar_t * data = dataList.back();\r\n {\r\n wchar_t source[10+1] = SRC_STRING;\r\n /* Copy length + 1 to include NUL terminator from source */\r\n /* POTENTIAL FLAW: data may not have enough space to hold source */\r\n memmove(data, source, (wcslen(source) + 1) * sizeof(wchar_t));\r\n printWLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(list dataList)\r\n{\r\n wchar_t * data = dataList.back();\r\n {\r\n wchar_t source[10+1] = SRC_STRING;\r\n /* Copy length + 1 to include NUL terminator from source */\r\n /* POTENTIAL FLAW: data may not have enough space to hold source */\r\n memmove(data, source, (wcslen(source) + 1) * sizeof(wchar_t));\r\n printWLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_18.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_18.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":3223,"source_sha256":"a6ce4464a20b2b56ee8340e50335aa0253507603e2162a4d564bf8b429d3e9e5","line_count":105,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_18.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE193.label.xml\r\nTemplate File: sources-sink-18.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Point data to a buffer that does not have space for a NULL terminator\r\n * GoodSource: Point data to a buffer that includes space for a NULL terminator\r\n * Sink: ncpy\r\n * BadSink : Copy string to data using wcsncpy()\r\n * Flow Variant: 18 Control flow: goto statements\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n/* MAINTENANCE NOTE: The length of this string should equal the 10 */\r\n#define SRC_STRING L\"AAAAAAAAAA\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_18_bad()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBadBuffer[10];\r\n wchar_t dataGoodBuffer[10+1];\r\n goto source;\r\nsource:\r\n /* FLAW: Set a pointer to a buffer that does not leave room for a NULL terminator when performing\r\n * string copies in the sinks */\r\n data = dataBadBuffer;\r\n data[0] = L'\\0'; /* null terminate */\r\n {\r\n wchar_t source[10+1] = SRC_STRING;\r\n /* Copy length + 1 to include NUL terminator from source */\r\n /* POTENTIAL FLAW: data may not have enough space to hold source */\r\n wcsncpy(data, source, wcslen(source) + 1);\r\n printWLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() - use goodsource and badsink by reversing the blocks on the goto statement */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBadBuffer[10];\r\n wchar_t dataGoodBuffer[10+1];\r\n goto source;\r\nsource:\r\n /* FIX: Set a pointer to a buffer that leaves room for a NULL terminator when performing\r\n * string copies in the sinks */\r\n data = dataGoodBuffer;\r\n data[0] = L'\\0'; /* null terminate */\r\n {\r\n wchar_t source[10+1] = SRC_STRING;\r\n /* Copy length + 1 to include NUL terminator from source */\r\n /* POTENTIAL FLAW: data may not have enough space to hold source */\r\n wcsncpy(data, source, wcslen(source) + 1);\r\n printWLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_18_good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_18_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_18_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_51b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_51b.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":1868,"source_sha256":"d489ab98237ed5f233cf0ae081eca90fb692d78bd8919c1ab13ab66da2eb4cac","line_count":57,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_51b.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE193.label.xml\r\nTemplate File: sources-sink-51b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Point data to a buffer that does not have space for a NULL terminator\r\n * GoodSource: Point data to a buffer that includes space for a NULL terminator\r\n * Sink: ncpy\r\n * BadSink : Copy string to data using wcsncpy()\r\n * Flow Variant: 51 Data flow: data passed as an argument from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n/* MAINTENANCE NOTE: The length of this string should equal the 10 */\r\n#define SRC_STRING L\"AAAAAAAAAA\"\r\n\r\n/* all the sinks are the same, we just want to know where the hit originated if a tool flags one */\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_51b_badSink(wchar_t * data)\r\n{\r\n {\r\n wchar_t source[10+1] = SRC_STRING;\r\n /* Copy length + 1 to include NUL terminator from source */\r\n /* POTENTIAL FLAW: data may not have enough space to hold source */\r\n wcsncpy(data, source, wcslen(source) + 1);\r\n printWLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_51b_goodG2BSink(wchar_t * data)\r\n{\r\n {\r\n wchar_t source[10+1] = SRC_STRING;\r\n /* Copy length + 1 to include NUL terminator from source */\r\n /* POTENTIAL FLAW: data may not have enough space to hold source */\r\n wcsncpy(data, source, wcslen(source) + 1);\r\n printWLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_68a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_68a.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":3410,"source_sha256":"79a174f154476ac03ef526d947e93f2025772bd6c068cc7c55c81a6d4606d4d0","line_count":100,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_68a.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE193.label.xml\r\nTemplate File: sources-sink-68a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Point data to a buffer that does not have space for a NULL terminator\r\n * GoodSource: Point data to a buffer that includes space for a NULL terminator\r\n * Sink: ncpy\r\n * BadSink : Copy string to data using wcsncpy()\r\n * Flow Variant: 68 Data flow: data passed as a global variable from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n/* MAINTENANCE NOTE: The length of this string should equal the 10 */\r\n#define SRC_STRING L\"AAAAAAAAAA\"\r\n\r\nwchar_t * CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_68_badData;\r\nwchar_t * CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_68_goodG2BData;\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_68b_badSink();\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_68_bad()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBadBuffer[10];\r\n wchar_t dataGoodBuffer[10+1];\r\n /* FLAW: Set a pointer to a buffer that does not leave room for a NULL terminator when performing\r\n * string copies in the sinks */\r\n data = dataBadBuffer;\r\n data[0] = L'\\0'; /* null terminate */\r\n CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_68_badData = data;\r\n CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_68b_badSink();\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_68b_goodG2BSink();\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBadBuffer[10];\r\n wchar_t dataGoodBuffer[10+1];\r\n /* FIX: Set a pointer to a buffer that leaves room for a NULL terminator when performing\r\n * string copies in the sinks */\r\n data = dataGoodBuffer;\r\n data[0] = L'\\0'; /* null terminate */\r\n CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_68_goodG2BData = data;\r\n CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_68b_goodG2BSink();\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_68_good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_68_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE193_wchar_t_declare_ncpy_68_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_03.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_03.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":4659,"source_sha256":"80b082ae1eff1f9a3c4c00bd99775b82b28b9391f59abdec9790456bad13eb98","line_count":146,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_03.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-03.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: loop\r\n * BadSink : Copy string to data using a loop\r\n * Flow Variant: 03 Control flow: if(5==5) and if(5!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_03_bad()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n if(5==5)\r\n {\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n size_t i;\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the 5==5 to 5!=5 */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n size_t i;\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n if(5==5)\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n size_t i;\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_03_good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_03_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_03_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_17.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_17.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":3603,"source_sha256":"e2fea3d3428f266bcb893daf622e5f8367379772156b5d4eb5983827cc9c6de4","line_count":115,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_17.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-17.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: loop\r\n * BadSink : Copy string to data using a loop\r\n * Flow Variant: 17 Control flow: for loops\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_17_bad()\r\n{\r\n int i;\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n for(i = 0; i < 1; i++)\r\n {\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n size_t i;\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the conditions on the for statements */\r\nstatic void goodG2B()\r\n{\r\n int h;\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n for(h = 0; h < 1; h++)\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n size_t i;\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_17_good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_17_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_17_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_18.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_18.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":3524,"source_sha256":"3d1dbddb13188baaf33bd4037f661f0ed814e9754dbe6f782545166f848758d5","line_count":111,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_18.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-18.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: loop\r\n * BadSink : Copy string to data using a loop\r\n * Flow Variant: 18 Control flow: goto statements\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_18_bad()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n goto source;\r\nsource:\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n {\r\n size_t i;\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() - use goodsource and badsink by reversing the blocks on the goto statement */\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n goto source;\r\nsource:\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n {\r\n size_t i;\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_18_good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_18_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_18_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_52b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_52b.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":1539,"source_sha256":"c230830cc247ddfdbf26e76547dc5ced2a7c5491f01526047a393e0d1ccf3cd4","line_count":46,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_52b.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-52b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: loop\r\n * BadSink : Copy string to data using a loop\r\n * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* all the sinks are the same, we just want to know where the hit originated if a tool flags one */\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_52c_badSink(char * data);\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_52b_badSink(char * data)\r\n{\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_52c_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declaration */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_52c_goodG2BSink(char * data);\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_52b_goodG2BSink(char * data)\r\n{\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_52c_goodG2BSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_63b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_63b.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":2017,"source_sha256":"9f5fb90f484db4c675d124470fc775e2876cc2f2b1fc2d4662ce01da49c74b8c","line_count":64,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_63b.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-63b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sinks: loop\r\n * BadSink : Copy string to data using a loop\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_63b_badSink(char * * dataPtr)\r\n{\r\n char * data = *dataPtr;\r\n {\r\n size_t i;\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_63b_goodG2BSink(char * * dataPtr)\r\n{\r\n char * data = *dataPtr;\r\n {\r\n size_t i;\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_68b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_68b.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":2405,"source_sha256":"b93a75b9dec1d93a1ab70f947add7c4e65beb5d6ca1c2334513d8147a2fc1a1f","line_count":69,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_68b.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-68b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: loop\r\n * BadSink : Copy string to data using a loop\r\n * Flow Variant: 68 Data flow: data passed as a global variable from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nextern char * CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_68_badData;\r\nextern char * CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_68_goodG2BData;\r\n\r\n/* all the sinks are the same, we just want to know where the hit originated if a tool flags one */\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_68b_badSink()\r\n{\r\n char * data = CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_68_badData;\r\n {\r\n size_t i;\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_68b_goodG2BSink()\r\n{\r\n char * data = CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_68_goodG2BData;\r\n {\r\n size_t i;\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_73a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_73a.cpp","cwe_id":"CWE121","file_extension":".cpp","language_hint":"C++","source_size_bytes":3062,"source_sha256":"6c6be95f425d22f313e8fba3f438f87a87ec101ae914305bd998a86492582e6b","line_count":110,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_73a.cpp\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-73a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sinks: loop\r\n * BadSink : Copy string to data using a loop\r\n * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_73\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(list dataList);\r\n\r\nvoid bad()\r\n{\r\n char * data;\r\n list dataList;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n /* Put data in a list */\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n badSink(dataList);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(list dataList);\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n list dataList;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n /* Put data in a list */\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n goodG2BSink(dataList);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_73; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_82_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_82_goodG2B.cpp","cwe_id":"CWE121","file_extension":".cpp","language_hint":"C++","source_size_bytes":1421,"source_sha256":"56ae5014b69068fa2f8b15c7268f00abd04432a1f0fd40c858d60691b4a75be2","line_count":42,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_82_goodG2B.cpp\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-82_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sinks: loop\r\n * BadSink : Copy string to data using a loop\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_82.h\"\r\n\r\nnamespace CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_82\r\n{\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_loop_82_goodG2B::action(char * data)\r\n{\r\n {\r\n size_t i;\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memcpy_33.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memcpy_33.cpp","cwe_id":"CWE121","file_extension":".cpp","language_hint":"C++","source_size_bytes":3562,"source_sha256":"abe44fd10e1b0375ffe2c71ee5b5245d818b3468d2983ab04d20148925bae974","line_count":113,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memcpy_33.cpp\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-33.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sinks: memcpy\r\n * BadSink : Copy string to data using memcpy\r\n * Flow Variant: 33 Data flow: use of a C++ reference to data within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memcpy_33\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n char * data;\r\n char * &dataRef = data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n {\r\n char * data = dataRef;\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char * &dataRef = data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n {\r\n char * data = dataRef;\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memcpy_33; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memcpy_73a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memcpy_73a.cpp","cwe_id":"CWE121","file_extension":".cpp","language_hint":"C++","source_size_bytes":3070,"source_sha256":"6f16c25c8b32ceb455a4b5cfe9079e114e7e10fd86be9881d5dae0a3bbb06618","line_count":110,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memcpy_73a.cpp\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-73a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sinks: memcpy\r\n * BadSink : Copy string to data using memcpy\r\n * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memcpy_73\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(list dataList);\r\n\r\nvoid bad()\r\n{\r\n char * data;\r\n list dataList;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n /* Put data in a list */\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n badSink(dataList);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(list dataList);\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n list dataList;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n /* Put data in a list */\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n goodG2BSink(dataList);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memcpy_73; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memcpy_82_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memcpy_82_bad.cpp","cwe_id":"CWE121","file_extension":".cpp","language_hint":"C++","source_size_bytes":1356,"source_sha256":"7b3046bfb8fa06d194a90541c0241a915a4ecb3ba7a221b243c4925a5b08c04a","line_count":38,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memcpy_82_bad.cpp\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-82_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sinks: memcpy\r\n * BadSink : Copy string to data using memcpy\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memcpy_82.h\"\r\n\r\nnamespace CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memcpy_82\r\n{\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memcpy_82_bad::action(char * data)\r\n{\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memcpy_82_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memcpy_82_goodG2B.cpp","cwe_id":"CWE121","file_extension":".cpp","language_hint":"C++","source_size_bytes":1370,"source_sha256":"e50b800e1375a517b1197f4976fdad19c45f9856a02b0db3f10f369b2f30d298","line_count":38,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memcpy_82_goodG2B.cpp\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-82_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sinks: memcpy\r\n * BadSink : Copy string to data using memcpy\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memcpy_82.h\"\r\n\r\nnamespace CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memcpy_82\r\n{\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memcpy_82_goodG2B::action(char * data)\r\n{\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memmove_15.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memmove_15.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":4883,"source_sha256":"d3477b6a60c2326cef99a3bd813f6390b749327a62e233746202b43fce5a9b68","line_count":147,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memmove_15.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-15.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: memmove\r\n * BadSink : Copy string to data using memmove\r\n * Flow Variant: 15 Control flow: switch(6)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memmove_15_bad()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n switch(6)\r\n {\r\n case 6:\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memmove(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the switch to switch(5) */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n switch(5)\r\n {\r\n case 6:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n break;\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memmove(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the switch */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n switch(6)\r\n {\r\n case 6:\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memmove(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memmove_15_good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memmove_15_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memmove_15_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memmove_72a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memmove_72a.cpp","cwe_id":"CWE121","file_extension":".cpp","language_hint":"C++","source_size_bytes":3223,"source_sha256":"01c498728e8174753327e6601347a8852deb802a820459fd04928d826d654f24","line_count":110,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memmove_72a.cpp\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-72a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sinks: memmove\r\n * BadSink : Copy string to data using memmove\r\n * Flow Variant: 72 Data flow: data passed in a vector from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memmove_72\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(vector dataVector);\r\n\r\nvoid bad()\r\n{\r\n char * data;\r\n vector dataVector;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n /* Put data in a vector */\r\n dataVector.insert(dataVector.end(), 1, data);\r\n dataVector.insert(dataVector.end(), 1, data);\r\n dataVector.insert(dataVector.end(), 1, data);\r\n badSink(dataVector);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(vector dataVector);\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n vector dataVector;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n /* Put data in a vector */\r\n dataVector.insert(dataVector.end(), 1, data);\r\n dataVector.insert(dataVector.end(), 1, data);\r\n dataVector.insert(dataVector.end(), 1, data);\r\n goodG2BSink(dataVector);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memmove_72; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memmove_82_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memmove_82_bad.cpp","cwe_id":"CWE121","file_extension":".cpp","language_hint":"C++","source_size_bytes":1363,"source_sha256":"ba652fd8d369ec227d035d2b6da2e153c3104a1a9ad64384ac4afe109c3f1f51","line_count":38,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memmove_82_bad.cpp\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-82_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sinks: memmove\r\n * BadSink : Copy string to data using memmove\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memmove_82.h\"\r\n\r\nnamespace CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memmove_82\r\n{\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_memmove_82_bad::action(char * data)\r\n{\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memmove(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_03.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_03.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":4228,"source_sha256":"d8449bbb791c8656002d219f2ecb16164a58d78021c4c4a3521d02d07f408c35","line_count":131,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_03.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-03.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: ncat\r\n * BadSink : Copy string to data using strncat\r\n * Flow Variant: 03 Control flow: if(5==5) and if(5!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_03_bad()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n if(5==5)\r\n {\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the sizeof(data)-strlen(data) is less than the length of source */\r\n strncat(data, source, 100);\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the 5==5 to 5!=5 */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the sizeof(data)-strlen(data) is less than the length of source */\r\n strncat(data, source, 100);\r\n printLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n if(5==5)\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the sizeof(data)-strlen(data) is less than the length of source */\r\n strncat(data, source, 100);\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_03_good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_03_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_03_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_10.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_10.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":4273,"source_sha256":"3f507d1d99139c287f1e047bad83970c31a27ed7416aa3b91989836636734c52","line_count":131,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_10.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-10.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: ncat\r\n * BadSink : Copy string to data using strncat\r\n * Flow Variant: 10 Control flow: if(globalTrue) and if(globalFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_10_bad()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n if(globalTrue)\r\n {\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the sizeof(data)-strlen(data) is less than the length of source */\r\n strncat(data, source, 100);\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the globalTrue to globalFalse */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the sizeof(data)-strlen(data) is less than the length of source */\r\n strncat(data, source, 100);\r\n printLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n if(globalTrue)\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the sizeof(data)-strlen(data) is less than the length of source */\r\n strncat(data, source, 100);\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_10_good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_10_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_10_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_68a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_68a.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":3278,"source_sha256":"64795025df55e89f5eac85c9b265053d58eca485f8ed917bf6f97346de6096cc","line_count":94,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_68a.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-68a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: ncat\r\n * BadSink : Copy string to data using strncat\r\n * Flow Variant: 68 Data flow: data passed as a global variable from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nchar * CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_68_badData;\r\nchar * CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_68_goodG2BData;\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_68b_badSink();\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_68_bad()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_68_badData = data;\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_68b_badSink();\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_68b_goodG2BSink();\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_68_goodG2BData = data;\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_68b_goodG2BSink();\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_68_good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_68_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_68_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_73a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_73a.cpp","cwe_id":"CWE121","file_extension":".cpp","language_hint":"C++","source_size_bytes":3063,"source_sha256":"86dea95cdbf327aa64de67041eb0dbc84882de44f9df41c75d4ec60f5933b8a5","line_count":110,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_73a.cpp\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-73a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sinks: ncat\r\n * BadSink : Copy string to data using strncat\r\n * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_73\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(list dataList);\r\n\r\nvoid bad()\r\n{\r\n char * data;\r\n list dataList;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n /* Put data in a list */\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n badSink(dataList);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(list dataList);\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n list dataList;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n /* Put data in a list */\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n goodG2BSink(dataList);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_73; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_73b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_73b.cpp","cwe_id":"CWE121","file_extension":".cpp","language_hint":"C++","source_size_bytes":1813,"source_sha256":"d4f0e240a73148cbbb8c187f138ac21c91838b703d10872ed481d6a47d4a4e7e","line_count":63,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_73b.cpp\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-73b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sinks: ncat\r\n * BadSink : Copy string to data using strncat\r\n * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_73\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(list dataList)\r\n{\r\n /* copy data out of dataList */\r\n char * data = dataList.back();\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the sizeof(data)-strlen(data) is less than the length of source */\r\n strncat(data, source, 100);\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(list dataList)\r\n{\r\n char * data = dataList.back();\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the sizeof(data)-strlen(data) is less than the length of source */\r\n strncat(data, source, 100);\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_74a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_74a.cpp","cwe_id":"CWE121","file_extension":".cpp","language_hint":"C++","source_size_bytes":3027,"source_sha256":"74cf2a88f01a89a6eca6e7f09f1f97a4d91cb5668b07d5f0bb6d075b04b508dc","line_count":110,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_74a.cpp\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-74a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sinks: ncat\r\n * BadSink : Copy string to data using strncat\r\n * Flow Variant: 74 Data flow: data passed in a map from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_74\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(map dataMap);\r\n\r\nvoid bad()\r\n{\r\n char * data;\r\n map dataMap;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n /* Put data in a map */\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n badSink(dataMap);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(map dataMap);\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n map dataMap;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n /* Put data in a map */\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n goodG2BSink(dataMap);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_74; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_82a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_82a.cpp","cwe_id":"CWE121","file_extension":".cpp","language_hint":"C++","source_size_bytes":2995,"source_sha256":"03ebce0b29029f86634175e9be11c141c69d24a041418892fec40fdd32e86945","line_count":93,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_82a.cpp\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-82a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sinks: ncat\r\n * BadSink : Copy string to data using strncat\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_82.h\"\r\n\r\nnamespace CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_82\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_82_base* baseObject = new CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_82_bad;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_82_base* baseObject = new CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_82_goodG2B;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncat_82; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_10.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_10.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":4492,"source_sha256":"7bb87d664d6ce57acd6817a430de64c9d2921308696bf897644330eeb6956081","line_count":134,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_10.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-10.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: ncpy\r\n * BadSink : Copy string to data using strncpy\r\n * Flow Variant: 10 Control flow: if(globalTrue) and if(globalFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_10_bad()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n if(globalTrue)\r\n {\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n strncpy(data, source, 100-1);\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the globalTrue to globalFalse */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n strncpy(data, source, 100-1);\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n if(globalTrue)\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n strncpy(data, source, 100-1);\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_10_good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_10_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_10_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_12.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_12.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":3896,"source_sha256":"8121e42fc840e9be848df967a756145fee3c0bfad1322c534be782228bb59d9d","line_count":118,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_12.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-12.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: ncpy\r\n * BadSink : Copy string to data using strncpy\r\n * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_12_bad()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n else\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n strncpy(data, source, 100-1);\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the \"if\" so that\r\n * both branches use the GoodSource */\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n else\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n strncpy(data, source, 100-1);\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_12_good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_12_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_12_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_16.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_16.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":3443,"source_sha256":"89f261a851c306f8ace63ef8a11a4eca5176d9fb008c6fcc5a95fbdb469de12f","line_count":107,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_16.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-16.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: ncpy\r\n * BadSink : Copy string to data using strncpy\r\n * Flow Variant: 16 Control flow: while(1)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_16_bad()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n while(1)\r\n {\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n break;\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n strncpy(data, source, 100-1);\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the conditions on the while statements */\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n while(1)\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n break;\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n strncpy(data, source, 100-1);\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_16_good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_16_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_16_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_54b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_54b.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":1568,"source_sha256":"2d666b83a7a47c3669ade5c426c7291601bb837f47fc5c6bb7c755a81ad06ac3","line_count":46,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_54b.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-54b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: ncpy\r\n * BadSink : Copy string to data using strncpy\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* all the sinks are the same, we just want to know where the hit originated if a tool flags one */\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_54c_badSink(char * data);\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_54b_badSink(char * data)\r\n{\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_54c_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declaration */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_54c_goodG2BSink(char * data);\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_54b_goodG2BSink(char * data)\r\n{\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_54c_goodG2BSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_65a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_65a.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":3128,"source_sha256":"5decf78cb648d0da681076e97317bbbbbfd2ec4d5fc8e7546f295ecb0b2078b3","line_count":92,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_65a.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-65a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sinks: ncpy\r\n * BadSink : Copy string to data using strncpy\r\n * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_65b_badSink(char * data);\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_65_bad()\r\n{\r\n char * data;\r\n /* define a function pointer */\r\n void (*funcPtr) (char *) = CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_65b_badSink;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n /* use the function pointer */\r\n funcPtr(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_65b_goodG2BSink(char * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n void (*funcPtr) (char *) = CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_65b_goodG2BSink;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n funcPtr(data);\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_65_good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_65_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_65_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_67b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_67b.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":2232,"source_sha256":"08e1e6035c146dd0c07e3c44f253350187775e9a16ba38b19ed4e433da3e58e5","line_count":61,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_67b.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-67b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sinks: ncpy\r\n * BadSink : Copy string to data using strncpy\r\n * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\ntypedef struct _CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_67_structType\r\n{\r\n char * structFirst;\r\n} CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_67_structType;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_67b_badSink(CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_67_structType myStruct)\r\n{\r\n char * data = myStruct.structFirst;\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n strncpy(data, source, 100-1);\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_67b_goodG2BSink(CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_67_structType myStruct)\r\n{\r\n char * data = myStruct.structFirst;\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n strncpy(data, source, 100-1);\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_72b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_72b.cpp","cwe_id":"CWE121","file_extension":".cpp","language_hint":"C++","source_size_bytes":1969,"source_sha256":"7a59364663a94573b2cddd6e2fbb497a87872e7afad19e7502abc6193037a6db","line_count":65,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_72b.cpp\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-72b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sinks: ncpy\r\n * BadSink : Copy string to data using strncpy\r\n * Flow Variant: 72 Data flow: data passed in a vector from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_ncpy_72\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(vector dataVector)\r\n{\r\n /* copy data out of dataVector */\r\n char * data = dataVector[2];\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n strncpy(data, source, 100-1);\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(vector dataVector)\r\n{\r\n char * data = dataVector[2];\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n strncpy(data, source, 100-1);\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_02.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_02.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":4301,"source_sha256":"14102238c575f7ae5dfb0e6de2530927f1f542fa8a677c14032993281b921f18","line_count":137,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_02.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-02.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: snprintf\r\n * BadSink : Copy string to data using snprintf\r\n * Flow Variant: 02 Control flow: if(1) and if(0)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifdef _WIN32\r\n#define SNPRINTF _snprintf\r\n#else\r\n#define SNPRINTF snprintf\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_02_bad()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n if(1)\r\n {\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n SNPRINTF(data, 100, \"%s\", source);\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the 1 to 0 */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n SNPRINTF(data, 100, \"%s\", source);\r\n printLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n if(1)\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n SNPRINTF(data, 100, \"%s\", source);\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_02_good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_02_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_02_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_14.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_14.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":4385,"source_sha256":"4cd0dd5313134b87608114319dd104c9d049d0a6533182b47d61041dfb4e3e00","line_count":137,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_14.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-14.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: snprintf\r\n * BadSink : Copy string to data using snprintf\r\n * Flow Variant: 14 Control flow: if(globalFive==5) and if(globalFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifdef _WIN32\r\n#define SNPRINTF _snprintf\r\n#else\r\n#define SNPRINTF snprintf\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_14_bad()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n if(globalFive==5)\r\n {\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n SNPRINTF(data, 100, \"%s\", source);\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the globalFive==5 to globalFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n SNPRINTF(data, 100, \"%s\", source);\r\n printLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n if(globalFive==5)\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n SNPRINTF(data, 100, \"%s\", source);\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_14_good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_14_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_14_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_41.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_41.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":3690,"source_sha256":"558dea709f8ac97cf256199a8170b8ddf36ea54b96ede93fc309be68ab50bd98","line_count":113,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_41.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-41.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: snprintf\r\n * BadSink : Copy string to data using snprintf\r\n * Flow Variant: 41 Data flow: data passed as an argument from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifdef _WIN32\r\n#define SNPRINTF _snprintf\r\n#else\r\n#define SNPRINTF snprintf\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_41_badSink(char * data)\r\n{\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n SNPRINTF(data, 100, \"%s\", source);\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_41_bad()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_41_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_41_goodG2BSink(char * data)\r\n{\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n SNPRINTF(data, 100, \"%s\", source);\r\n printLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char * dataBadBuffer = (char *)ALLOCA(50*sizeof(char));\r\n char * dataGoodBuffer = (char *)ALLOCA(100*sizeof(char));\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_41_goodG2BSink(data);\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_41_good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_41_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_41_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_53b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_53b.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":1687,"source_sha256":"5183e6e424261bba798210a9a4aedb41eb59670cf953351f52dbf2c13a90827b","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_53b.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-53b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: snprintf\r\n * BadSink : Copy string to data using snprintf\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifdef _WIN32\r\n#define SNPRINTF _snprintf\r\n#else\r\n#define SNPRINTF snprintf\r\n#endif\r\n\r\n/* all the sinks are the same, we just want to know where the hit originated if a tool flags one */\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_53c_badSink(char * data);\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_53b_badSink(char * data)\r\n{\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_53c_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declaration */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_53c_goodG2BSink(char * data);\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_53b_goodG2BSink(char * data)\r\n{\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_53c_goodG2BSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_54c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_54c.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":1688,"source_sha256":"ca8d8795a603be3c4127c3f840c0db432e95b11382b2073390b38b02e128a3cf","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_54c.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-54c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: snprintf\r\n * BadSink : Copy string to data using snprintf\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifdef _WIN32\r\n#define SNPRINTF _snprintf\r\n#else\r\n#define SNPRINTF snprintf\r\n#endif\r\n\r\n/* all the sinks are the same, we just want to know where the hit originated if a tool flags one */\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_54d_badSink(char * data);\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_54c_badSink(char * data)\r\n{\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_54d_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declaration */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_54d_goodG2BSink(char * data);\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_54c_goodG2BSink(char * data)\r\n{\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_alloca_snprintf_54d_goodG2BSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_loop_11.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_loop_11.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":4580,"source_sha256":"cff2d3650ae50467ed92f7232f48b5eb4f6151f6ebb921ad8f550345068d2a8d","line_count":146,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_loop_11.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-11.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: loop\r\n * BadSink : Copy string to data using a loop\r\n * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_loop_11_bad()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(globalReturnsTrue())\r\n {\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n size_t i;\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n size_t i;\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n size_t i;\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_loop_11_good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_loop_11_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_loop_11_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_loop_53b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_loop_53b.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":1573,"source_sha256":"a30112ee81300ce04eeae6de2a119e49cc932bf8443e5ecd72c8361fbe460389","line_count":46,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_loop_53b.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-53b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: loop\r\n * BadSink : Copy string to data using a loop\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* all the sinks are the same, we just want to know where the hit originated if a tool flags one */\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_loop_53c_badSink(char * data);\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_loop_53b_badSink(char * data)\r\n{\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_loop_53c_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declaration */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_loop_53c_goodG2BSink(char * data);\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_loop_53b_goodG2BSink(char * data)\r\n{\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_loop_53c_goodG2BSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_loop_64b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_loop_64b.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":2341,"source_sha256":"01b564b917eeb93abcce342c0af0e72942acd6a5d192db7d74ee52c2a17ee160","line_count":70,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_loop_64b.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-64b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sinks: loop\r\n * BadSink : Copy string to data using a loop\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_loop_64b_badSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n char * * dataPtr = (char * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n char * data = (*dataPtr);\r\n {\r\n size_t i;\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_loop_64b_goodG2BSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n char * * dataPtr = (char * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n char * data = (*dataPtr);\r\n {\r\n size_t i;\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_06.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_06.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":4603,"source_sha256":"e1e76dbe43112dfca484cb1e4f602bfd2745fae44fabdf891d1d57586355e11f","line_count":138,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_06.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-06.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: memcpy\r\n * BadSink : Copy string to data using memcpy\r\n * Flow Variant: 06 Control flow: if(STATIC_CONST_FIVE==5) and if(STATIC_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The variable below is declared \"const\", so a tool should be able\r\n * to identify that reads of this will always give its initialized value. */\r\nstatic const int STATIC_CONST_FIVE = 5;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_06_bad()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_06_good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_06_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_06_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_08.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_08.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":4689,"source_sha256":"ac745a36c3db5c8e7dd52a7da44149f314e1dd7ccba34f71b0658e2197cf624d","line_count":148,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_08.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-08.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: memcpy\r\n * BadSink : Copy string to data using memcpy\r\n * Flow Variant: 08 Control flow: if(staticReturnsTrue()) and if(staticReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The two function below always return the same value, so a tool\r\n * should be able to identify that calls to the functions will always\r\n * return a fixed value.\r\n */\r\nstatic int staticReturnsTrue()\r\n{\r\n return 1;\r\n}\r\n\r\nstatic int staticReturnsFalse()\r\n{\r\n return 0;\r\n}\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_08_bad()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(staticReturnsTrue())\r\n {\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_08_good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_08_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_08_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_11.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_11.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":4409,"source_sha256":"1e2c8a50dbf9132fdfa3882d4e5029583cca575743a607d43afa73a8f349c551","line_count":134,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_11.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-11.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: memcpy\r\n * BadSink : Copy string to data using memcpy\r\n * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_11_bad()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(globalReturnsTrue())\r\n {\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_11_good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_11_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_11_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_12.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_12.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":3804,"source_sha256":"a6dd663dd6bd88412bbfb1ec1a17a244029ca8be650b2324ed7bceb0f5b0915b","line_count":118,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_12.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-12.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: memcpy\r\n * BadSink : Copy string to data using memcpy\r\n * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_12_bad()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n else\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the \"if\" so that\r\n * both branches use the GoodSource */\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n else\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_12_good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_12_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_12_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_13.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_13.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":4413,"source_sha256":"0b8f1ec8f28cfcaba8a4d91728f6ef857aef560af3f6e3a0ba81ee505fbce4b9","line_count":134,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_13.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-13.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: memcpy\r\n * BadSink : Copy string to data using memcpy\r\n * Flow Variant: 13 Control flow: if(GLOBAL_CONST_FIVE==5) and if(GLOBAL_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_13_bad()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the GLOBAL_CONST_FIVE==5 to GLOBAL_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(GLOBAL_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_13_good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_13_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_13_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_14.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_14.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":4364,"source_sha256":"3fc06c6663b5ac069ad4bc036f75c4160ac74c1522f848b440ef4ef4880dba19","line_count":134,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_14.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-14.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: memcpy\r\n * BadSink : Copy string to data using memcpy\r\n * Flow Variant: 14 Control flow: if(globalFive==5) and if(globalFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_14_bad()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(globalFive==5)\r\n {\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the globalFive==5 to globalFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(globalFive==5)\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_14_good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_14_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_14_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_32.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_32.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":3709,"source_sha256":"3d3789d649afda8492f0d69281db957fff0f01817386520341be39b67857bc26","line_count":116,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_32.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-32.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: memcpy\r\n * BadSink : Copy string to data using memcpy\r\n * Flow Variant: 32 Data flow using two pointers to the same value within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_32_bad()\r\n{\r\n char * data;\r\n char * *dataPtr1 = &data;\r\n char * *dataPtr2 = &data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n {\r\n char * data = *dataPtr1;\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n *dataPtr1 = data;\r\n }\r\n {\r\n char * data = *dataPtr2;\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char * *dataPtr1 = &data;\r\n char * *dataPtr2 = &data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n {\r\n char * data = *dataPtr1;\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n *dataPtr1 = data;\r\n }\r\n {\r\n char * data = *dataPtr2;\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_32_good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_32_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_32_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_44.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_44.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":3587,"source_sha256":"d771020d0173fe05eb9bf8af127df8d30cc97dc5bf9821501a8603442478236d","line_count":112,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_44.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-44.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sinks: memcpy\r\n * BadSink : Copy string to data using memcpy\r\n * 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\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink(char * data)\r\n{\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_44_bad()\r\n{\r\n char * data;\r\n /* define a function pointer */\r\n void (*funcPtr) (char *) = badSink;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n /* use the function pointer */\r\n funcPtr(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink(char * data)\r\n{\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n void (*funcPtr) (char *) = goodG2BSink;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n funcPtr(data);\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_44_good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_44_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_44_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_51b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_51b.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":1944,"source_sha256":"d760a31f9429937c35f6475a5aabe50f06a99cd34e6b0c7fa28838a17fc4a7f7","line_count":56,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_51b.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-51b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: memcpy\r\n * BadSink : Copy string to data using memcpy\r\n * Flow Variant: 51 Data flow: data passed as an argument from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* all the sinks are the same, we just want to know where the hit originated if a tool flags one */\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_51b_badSink(char * data)\r\n{\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_51b_goodG2BSink(char * data)\r\n{\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_54a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_54a.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":2908,"source_sha256":"f28429d3ebf3556ffd4e4e667e313db8146cfc90df56b3f048406f32928e12b6","line_count":89,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_54a.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-54a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: memcpy\r\n * BadSink : Copy string to data using memcpy\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_54b_badSink(char * data);\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_54_bad()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_54b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declaration */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_54b_goodG2BSink(char * data);\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_54b_goodG2BSink(data);\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_54_good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_54_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_54_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_54e.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_54e.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":1989,"source_sha256":"ad206c1af40a9fb174846709719376a22c9df4ccda25915081a91c6ffa17cf16","line_count":56,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_54e.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-54e.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: memcpy\r\n * BadSink : Copy string to data using memcpy\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* all the sinks are the same, we just want to know where the hit originated if a tool flags one */\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_54e_badSink(char * data)\r\n{\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memcpy_54e_goodG2BSink(char * data)\r\n{\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memcpy(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_04.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_04.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":4678,"source_sha256":"cd17c762a44fc34cfa338185a4c8fd9de55163201b5ed94b2b4098c7bd306b7b","line_count":141,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_04.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-04.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: memmove\r\n * BadSink : Copy string to data using memmove\r\n * Flow Variant: 04 Control flow: if(STATIC_CONST_TRUE) and if(STATIC_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The two variables below are declared \"const\", so a tool should\r\n * be able to identify that reads of these will always return their\r\n * initialized values.\r\n */\r\nstatic const int STATIC_CONST_TRUE = 1; /* true */\r\nstatic const int STATIC_CONST_FALSE = 0; /* false */\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_04_bad()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memmove(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the STATIC_CONST_TRUE to STATIC_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(STATIC_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memmove(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memmove(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_04_good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_04_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_04_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_08.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_08.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":4699,"source_sha256":"950b30711efffb7e777776e8fceaece188bfee1b1b78fec82dfcf09769dba592","line_count":148,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_08.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-08.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: memmove\r\n * BadSink : Copy string to data using memmove\r\n * Flow Variant: 08 Control flow: if(staticReturnsTrue()) and if(staticReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The two function below always return the same value, so a tool\r\n * should be able to identify that calls to the functions will always\r\n * return a fixed value.\r\n */\r\nstatic int staticReturnsTrue()\r\n{\r\n return 1;\r\n}\r\n\r\nstatic int staticReturnsFalse()\r\n{\r\n return 0;\r\n}\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_08_bad()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(staticReturnsTrue())\r\n {\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memmove(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memmove(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memmove(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_08_good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_08_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_08_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_15.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_15.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":4696,"source_sha256":"114c0f9959f0e7c34f06ab89bb831384453481ed6b6dccf54c166597adb9cc4b","line_count":147,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_15.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-15.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: memmove\r\n * BadSink : Copy string to data using memmove\r\n * Flow Variant: 15 Control flow: switch(6)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_15_bad()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n switch(6)\r\n {\r\n case 6:\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memmove(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the switch to switch(5) */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n switch(5)\r\n {\r\n case 6:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n break;\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memmove(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the switch */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n switch(6)\r\n {\r\n case 6:\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memmove(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_15_good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_15_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_15_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_65b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_65b.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":1891,"source_sha256":"ed731d9e316fa5c0908245b13fc4cea508228cf8802259719629dc87cd744e3d","line_count":54,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_65b.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-65b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sinks: memmove\r\n * BadSink : Copy string to data using memmove\r\n * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_65b_badSink(char * data)\r\n{\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memmove(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_65b_goodG2BSink(char * data)\r\n{\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memmove(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_67b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_67b.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":2285,"source_sha256":"2cc5af61d15ca7b0892672a71819fecd9a3050685b17dde2f09ab9d9b33b8693","line_count":61,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_67b.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-67b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sinks: memmove\r\n * BadSink : Copy string to data using memmove\r\n * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\ntypedef struct _CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_67_structType\r\n{\r\n char * structFirst;\r\n} CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_67_structType;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_67b_badSink(CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_67_structType myStruct)\r\n{\r\n char * data = myStruct.structFirst;\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memmove(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_67b_goodG2BSink(CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_67_structType myStruct)\r\n{\r\n char * data = myStruct.structFirst;\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n memmove(data, source, 100*sizeof(char));\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_68a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_68a.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":3205,"source_sha256":"bf9e5433fd6d826d30436ac8ff9b4ba7598cdf44bc7d139da7b9f4e00ffe28ae","line_count":94,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_68a.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-68a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: memmove\r\n * BadSink : Copy string to data using memmove\r\n * Flow Variant: 68 Data flow: data passed as a global variable from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nchar * CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_68_badData;\r\nchar * CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_68_goodG2BData;\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_68b_badSink();\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_68_bad()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_68_badData = data;\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_68b_badSink();\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_68b_goodG2BSink();\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_68_goodG2BData = data;\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_68b_goodG2BSink();\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_68_good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_68_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_68_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_74a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_74a.cpp","cwe_id":"CWE121","file_extension":".cpp","language_hint":"C++","source_size_bytes":2914,"source_sha256":"976f9030d61624e0de85bd26f630b9f4d2ffdc3cff09cc38e829aad52cea65bd","line_count":110,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_74a.cpp\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-74a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sinks: memmove\r\n * BadSink : Copy string to data using memmove\r\n * Flow Variant: 74 Data flow: data passed in a map from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_74\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(map dataMap);\r\n\r\nvoid bad()\r\n{\r\n char * data;\r\n map dataMap;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n /* Put data in a map */\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n badSink(dataMap);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(map dataMap);\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n map dataMap;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n /* Put data in a map */\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n goodG2BSink(dataMap);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_memmove_74; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_15.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_15.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":4426,"source_sha256":"66508a341e4ddc99e0d7e1ecd19d821c0fe1bffa3e1071e2e6e16abaffe5ac76","line_count":144,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_15.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-15.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: ncat\r\n * BadSink : Copy string to data using strncat\r\n * Flow Variant: 15 Control flow: switch(6)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_15_bad()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n switch(6)\r\n {\r\n case 6:\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the sizeof(data)-strlen(data) is less than the length of source */\r\n strncat(data, source, 100);\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the switch to switch(5) */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n switch(5)\r\n {\r\n case 6:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n break;\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the sizeof(data)-strlen(data) is less than the length of source */\r\n strncat(data, source, 100);\r\n printLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the switch */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n switch(6)\r\n {\r\n case 6:\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the sizeof(data)-strlen(data) is less than the length of source */\r\n strncat(data, source, 100);\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_15_good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_15_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_15_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_41.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_41.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":3455,"source_sha256":"2346a15dbfb8dbf6fbc28b68c43648093645122560f0327e5879f7518c9e4b5d","line_count":107,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_41.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-41.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: ncat\r\n * BadSink : Copy string to data using strncat\r\n * Flow Variant: 41 Data flow: data passed as an argument from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_41_badSink(char * data)\r\n{\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the sizeof(data)-strlen(data) is less than the length of source */\r\n strncat(data, source, 100);\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_41_bad()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_41_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_41_goodG2BSink(char * data)\r\n{\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the sizeof(data)-strlen(data) is less than the length of source */\r\n strncat(data, source, 100);\r\n printLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_41_goodG2BSink(data);\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_41_good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_41_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_41_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_44.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_44.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":3410,"source_sha256":"da35729a1d4e218b275869701e5a74c2fd969aa53c55cef85c843603695e475e","line_count":110,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_44.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-44.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sinks: ncat\r\n * BadSink : Copy string to data using strncat\r\n * 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\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink(char * data)\r\n{\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the sizeof(data)-strlen(data) is less than the length of source */\r\n strncat(data, source, 100);\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_44_bad()\r\n{\r\n char * data;\r\n /* define a function pointer */\r\n void (*funcPtr) (char *) = badSink;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n /* use the function pointer */\r\n funcPtr(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink(char * data)\r\n{\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the sizeof(data)-strlen(data) is less than the length of source */\r\n strncat(data, source, 100);\r\n printLine(data);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n void (*funcPtr) (char *) = goodG2BSink;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n funcPtr(data);\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_44_good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_44_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_44_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_53b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_53b.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":1574,"source_sha256":"c68c92178e35ccd741c0148f0fd7253f5f6482888365ef696f723c28caeecdca","line_count":46,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_53b.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-53b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: ncat\r\n * BadSink : Copy string to data using strncat\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* all the sinks are the same, we just want to know where the hit originated if a tool flags one */\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_53c_badSink(char * data);\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_53b_badSink(char * data)\r\n{\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_53c_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declaration */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_53c_goodG2BSink(char * data);\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_53b_goodG2BSink(char * data)\r\n{\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_53c_goodG2BSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_81_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_81_goodG2B.cpp","cwe_id":"CWE121","file_extension":".cpp","language_hint":"C++","source_size_bytes":1290,"source_sha256":"8c4caae4fff32ffb5c8a4e073dc7fcf28a250bc4b40172feee598f1f285b10e0","line_count":37,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_81_goodG2B.cpp\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-81_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sinks: ncat\r\n * BadSink : Copy string to data using strncat\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_81.h\"\r\n\r\nnamespace CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_81\r\n{\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncat_81_goodG2B::action(char * data) const\r\n{\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the sizeof(data)-strlen(data) is less than the length of source */\r\n strncat(data, source, 100);\r\n printLine(data);\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_10.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_10.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":4305,"source_sha256":"9b7b39578199c7834e0f8229f9f84761ac816ebb5b5a8c7144e3fc858b6d02e4","line_count":134,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_10.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-10.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: ncpy\r\n * BadSink : Copy string to data using strncpy\r\n * Flow Variant: 10 Control flow: if(globalTrue) and if(globalFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_10_bad()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(globalTrue)\r\n {\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n strncpy(data, source, 100-1);\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the globalTrue to globalFalse */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n strncpy(data, source, 100-1);\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n if(globalTrue)\r\n {\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n }\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n strncpy(data, source, 100-1);\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_10_good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_10_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_10_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_41.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_41.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":3601,"source_sha256":"9193acaa470e0e088dd2484d6d4b882c5182b4c7e6e99a8d68e2c9b8b547247b","line_count":109,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_41.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-41.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: ncpy\r\n * BadSink : Copy string to data using strncpy\r\n * Flow Variant: 41 Data flow: data passed as an argument from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_41_badSink(char * data)\r\n{\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n strncpy(data, source, 100-1);\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_41_bad()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n /* FLAW: Set a pointer to a \"small\" buffer. This buffer will be used in the sinks as a destination\r\n * buffer in various memory copying functions using a \"large\" source buffer. */\r\n data = dataBadBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_41_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_41_goodG2BSink(char * data)\r\n{\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n strncpy(data, source, 100-1);\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBadBuffer[50];\r\n char dataGoodBuffer[100];\r\n /* FIX: Set a pointer to a \"large\" buffer, thus avoiding buffer overflows in the sinks. */\r\n data = dataGoodBuffer;\r\n data[0] = '\\0'; /* null terminate */\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_41_goodG2BSink(data);\r\n}\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_41_good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n * its own for testing or for building a binary to use in testing binary\r\n * analysis tools. It is not used when compiling all the testcases as one\r\n * application, which is how source code analysis tools are tested.\r\n */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_41_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_41_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_51b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_51b.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":1917,"source_sha256":"cad4a0f36db3a83eb869cb8bc4ab2fd82396ee4be4b8ab1aa7d1f0a03a6f1608","line_count":56,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_51b.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-51b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: ncpy\r\n * BadSink : Copy string to data using strncpy\r\n * Flow Variant: 51 Data flow: data passed as an argument from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* all the sinks are the same, we just want to know where the hit originated if a tool flags one */\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_51b_badSink(char * data)\r\n{\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n strncpy(data, source, 100-1);\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_51b_goodG2BSink(char * data)\r\n{\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n strncpy(data, source, 100-1);\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_53d.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_53d.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":1961,"source_sha256":"a57cba8d3883706725bcd5dc19a41e68454ef7657d2b864684e3ab093917a38f","line_count":56,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_53d.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-53d.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: ncpy\r\n * BadSink : Copy string to data using strncpy\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* all the sinks are the same, we just want to know where the hit originated if a tool flags one */\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_53d_badSink(char * data)\r\n{\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n strncpy(data, source, 100-1);\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_53d_goodG2BSink(char * data)\r\n{\r\n {\r\n char source[100];\r\n memset(source, 'C', 100-1); /* fill with 'C's */\r\n source[100-1] = '\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if the size of data is less than the length of source */\r\n strncpy(data, source, 100-1);\r\n data[100-1] = '\\0'; /* Ensure the destination buffer is null terminated */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_54c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE121_Stack_Based_Buffer_Overflow/s03/CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_54c.c","cwe_id":"CWE121","file_extension":".c","language_hint":"C","source_size_bytes":1575,"source_sha256":"b05d740fa92c16fd41346bbe4b27e5e676a41187079774c2c3e741b8265d0633","line_count":46,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_54c.c\r\nLabel Definition File: CWE121_Stack_Based_Buffer_Overflow__CWE805.string.label.xml\r\nTemplate File: sources-sink-54c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 121 Stack Based Buffer Overflow\r\n * BadSource: Set data pointer to the bad buffer\r\n * GoodSource: Set data pointer to the good buffer\r\n * Sink: ncpy\r\n * BadSink : Copy string to data using strncpy\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* all the sinks are the same, we just want to know where the hit originated if a tool flags one */\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_54d_badSink(char * data);\r\n\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_54c_badSink(char * data)\r\n{\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_54d_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declaration */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_54d_goodG2BSink(char * data);\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_54c_goodG2BSink(char * data)\r\n{\r\n CWE121_Stack_Based_Buffer_Overflow__CWE805_char_declare_ncpy_54d_goodG2BSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_02.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_02.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":4207,"source_sha256":"fae42c89e946dd3de4e21895efc045470bc6e30dc2fef3c1c3ce29c06706f766","line_count":152,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_02.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-02.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memcpy\r\n * BadSink : Copy TwoIntsClass array to data using memcpy\r\n * Flow Variant: 02 Control flow: if(1) and if(0)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_02\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n if(1)\r\n {\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new TwoIntsClass[50];\r\n }\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the 1 to 0 */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new TwoIntsClass[100];\r\n }\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the statements in the if */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n if(1)\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new TwoIntsClass[100];\r\n }\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_02; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_06.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_06.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":4540,"source_sha256":"079a38734161864bb9fe15a2a3d35a49b24ee84215cc2db5ec1813401ee6dffe","line_count":157,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_06.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-06.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memcpy\r\n * BadSink : Copy TwoIntsClass array to data using memcpy\r\n * Flow Variant: 06 Control flow: if(STATIC_CONST_FIVE==5) and if(STATIC_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The variable below is declared \"const\", so a tool should be able\r\n to identify that reads of this will always give its initialized\r\n value. */\r\nstatic const int STATIC_CONST_FIVE = 5;\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_06\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new TwoIntsClass[50];\r\n }\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new TwoIntsClass[100];\r\n }\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new TwoIntsClass[100];\r\n }\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_06; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_08.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_08.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":4620,"source_sha256":"21bc6a09cfd07b0b945029c317a28baa6aa41d14806bc5dbb7b4c8451f4fbbef","line_count":165,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_08.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-08.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memcpy\r\n * BadSink : Copy TwoIntsClass array to data using memcpy\r\n * Flow Variant: 08 Control flow: if(staticReturnsTrue()) and if(staticReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The two function below always return the same value, so a tool\r\n should be able to identify that calls to the functions will always\r\n return a fixed value. */\r\nstatic int staticReturnsTrue()\r\n{\r\n return 1;\r\n}\r\n\r\nstatic int staticReturnsFalse()\r\n{\r\n return 0;\r\n}\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_08\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new TwoIntsClass[50];\r\n }\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new TwoIntsClass[100];\r\n }\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new TwoIntsClass[100];\r\n }\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_08; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_11.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_11.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":4342,"source_sha256":"6f15e686ff9b15b2e27cdbd1242f1685b9610dd403570042ca2fa8d22f9995a5","line_count":152,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_11.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-11.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memcpy\r\n * BadSink : Copy TwoIntsClass array to data using memcpy\r\n * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_11\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new TwoIntsClass[50];\r\n }\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new TwoIntsClass[100];\r\n }\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new TwoIntsClass[100];\r\n }\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_11; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_16.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_16.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":3272,"source_sha256":"7f72528f3518ead471bc2f58d8c90af5dd2719ff8b1c79bd5d1470908c639bda","line_count":120,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_16.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-16.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memcpy\r\n * BadSink : Copy TwoIntsClass array to data using memcpy\r\n * Flow Variant: 16 Control flow: while(1)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_16\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n while(1)\r\n {\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new TwoIntsClass[50];\r\n break;\r\n }\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the conditions on the while statements */\r\nstatic void goodG2B()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n while(1)\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new TwoIntsClass[100];\r\n break;\r\n }\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_16; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_44.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_44.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":3554,"source_sha256":"f86baee3ed9d8e2ec74d7ff1fe2109fd6375ee8290441aad4d286d44993b19e4","line_count":126,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_44.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-44.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memcpy\r\n * BadSink : Copy TwoIntsClass array to data using memcpy\r\n * 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\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_44\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink(TwoIntsClass * data)\r\n{\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n /* define a function pointer */\r\n void (*funcPtr) (TwoIntsClass *) = badSink;\r\n data = NULL;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new TwoIntsClass[50];\r\n /* use the function pointer */\r\n funcPtr(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink(TwoIntsClass * data)\r\n{\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n TwoIntsClass * data;\r\n void (*funcPtr) (TwoIntsClass *) = goodG2BSink;\r\n data = NULL;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new TwoIntsClass[100];\r\n funcPtr(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_44; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_54d.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_54d.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":1338,"source_sha256":"88624ef5a77118b163edc1c445578b0c6858b07db8c32cdb96da566e88541207","line_count":48,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_54d.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-54d.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memcpy\r\n * BadSink : Copy TwoIntsClass array to data using memcpy\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_54\r\n{\r\n\r\n/* all the sinks are the same, we just want to know where the hit originated if a tool flags one */\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink_e(TwoIntsClass * data);\r\n\r\nvoid badSink_d(TwoIntsClass * data)\r\n{\r\n badSink_e(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink_e(TwoIntsClass * data);\r\n\r\nvoid goodG2BSink_d(TwoIntsClass * data)\r\n{\r\n goodG2BSink_e(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_66b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_66b.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":2090,"source_sha256":"43992aca971862948d167f3b98f1e3277fdc18b34c007be582f7bea78cc1bf12","line_count":74,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_66b.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-66b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memcpy\r\n * BadSink : Copy TwoIntsClass array to data using memcpy\r\n * Flow Variant: 66 Data flow: data passed in an array from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_66\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(TwoIntsClass * dataArray[])\r\n{\r\n /* copy data out of dataArray */\r\n TwoIntsClass * data = dataArray[2];\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(TwoIntsClass * dataArray[])\r\n{\r\n TwoIntsClass * data = dataArray[2];\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_81_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_81_bad.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":1463,"source_sha256":"a84929a85b4fb1f813ee5901c83019ae86553e410e5a56bcc76aea2119c0e8c0","line_count":45,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_81_bad.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-81_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memcpy\r\n * BadSink : Copy TwoIntsClass array to data using memcpy\r\n * Flow Variant: 81 Data flow: data passed in a parameter to a virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_81.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_81\r\n{\r\n\r\nvoid CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_81_bad::action(TwoIntsClass * data) const\r\n{\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_82.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_82.h","cwe_id":"CWE122","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1431,"source_sha256":"46f34cc87fb455d64a5776369d25c198c5896b9095800f31b86c1827bcbc72fc","line_count":49,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_82.h\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-82.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memcpy\r\n * BadSink : Copy TwoIntsClass array to data using memcpy\r\n * Flow Variant: 82 Data flow: data passed in a parameter to a virtual method called via a pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_82\r\n{\r\n\r\nclass CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_82_base\r\n{\r\npublic:\r\n /* pure virtual function */\r\n virtual void action(TwoIntsClass * data) = 0;\r\n};\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_82_bad : public CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_82_base\r\n{\r\npublic:\r\n void action(TwoIntsClass * data);\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_82_goodG2B : public CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memcpy_82_base\r\n{\r\npublic:\r\n void action(TwoIntsClass * data);\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_01.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_01.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":3116,"source_sha256":"f751885791d8c5db01c8d27bfcb192d74f2d66f6ed0a5a4427012a5ba69c4d13","line_count":112,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_01.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-01.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memmove\r\n * BadSink : Copy TwoIntsClass array to data using memmove\r\n * Flow Variant: 01 Baseline\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_01\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new TwoIntsClass[50];\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new TwoIntsClass[100];\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_01; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_07.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_07.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":4528,"source_sha256":"c17e3576efdad1f70c3f8b536a1539bc36fcd9e6a95bf4563d2069f7511965fa","line_count":157,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_07.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-07.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memmove\r\n * BadSink : Copy TwoIntsClass array to data using memmove\r\n * Flow Variant: 07 Control flow: if(staticFive==5) and if(staticFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The variable below is not declared \"const\", but is never assigned\r\n any other value so a tool should be able to identify that reads of\r\n this will always give its initialized value. */\r\nstatic int staticFive = 5;\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_07\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new TwoIntsClass[50];\r\n }\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the staticFive==5 to staticFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new TwoIntsClass[100];\r\n }\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new TwoIntsClass[100];\r\n }\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_07; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_11.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_11.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":4350,"source_sha256":"a32be2ed81ee74000d44311d42ab02c0472c14591948dd2b38582416a29f9b6f","line_count":152,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_11.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-11.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memmove\r\n * BadSink : Copy TwoIntsClass array to data using memmove\r\n * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_11\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new TwoIntsClass[50];\r\n }\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new TwoIntsClass[100];\r\n }\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new TwoIntsClass[100];\r\n }\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_11; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_18.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_18.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":3231,"source_sha256":"f186066c07a40930e6fd14e33ca23fcbeeea0caf831d3f4468c8bd8b95e3ed32","line_count":116,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_18.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-18.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memmove\r\n * BadSink : Copy TwoIntsClass array to data using memmove\r\n * Flow Variant: 18 Control flow: goto statements\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_18\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n goto source;\r\nsource:\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new TwoIntsClass[50];\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() - use goodsource and badsink by reversing the blocks on the goto statement */\r\nstatic void goodG2B()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n goto source;\r\nsource:\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new TwoIntsClass[100];\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_18; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_33.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_33.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":3485,"source_sha256":"7112add34c52016edf706b5d3b58aef115bbe198d9119515dd36c9a16161bd03","line_count":119,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_33.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-33.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memmove\r\n * BadSink : Copy TwoIntsClass array to data using memmove\r\n * Flow Variant: 33 Data flow: use of a C++ reference to data within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_33\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass * &dataRef = data;\r\n data = NULL;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new TwoIntsClass[50];\r\n {\r\n TwoIntsClass * data = dataRef;\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass * &dataRef = data;\r\n data = NULL;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new TwoIntsClass[100];\r\n {\r\n TwoIntsClass * data = dataRef;\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_33; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_53a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_53a.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":2506,"source_sha256":"c2ca36aecc64f93b2cb1d7c62537eba3dc71b308c00c18b164349ae3fdcbca8c","line_count":88,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_53a.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-53a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memmove\r\n * BadSink : Copy TwoIntsClass array to data using memmove\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_53\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink_b(TwoIntsClass * data);\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new TwoIntsClass[50];\r\n badSink_b(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\nvoid goodG2BSink_b(TwoIntsClass * data);\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new TwoIntsClass[100];\r\n goodG2BSink_b(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_53; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_54a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_54a.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":2507,"source_sha256":"de64ecde466cafc601eb8ad0dc88984b3ea828c94b97091cf7e7ce00f86ecec5","line_count":88,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_54a.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-54a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memmove\r\n * BadSink : Copy TwoIntsClass array to data using memmove\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_54\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink_b(TwoIntsClass * data);\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new TwoIntsClass[50];\r\n badSink_b(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\nvoid goodG2BSink_b(TwoIntsClass * data);\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new TwoIntsClass[100];\r\n goodG2BSink_b(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_54; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_63a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_63a.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":2426,"source_sha256":"e270deedcde2db6b5cff5f7b8d845931a7f21143dbfc2202b51ece2cfd4ab44b","line_count":87,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_63a.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-63a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memmove\r\n * BadSink : Copy TwoIntsClass array to data using memmove\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_63\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(TwoIntsClass * * dataPtr);\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new TwoIntsClass[50];\r\n badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(TwoIntsClass * * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new TwoIntsClass[100];\r\n goodG2BSink(&data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_63; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_66a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_66a.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":2627,"source_sha256":"2caa8c1de2d15da6124d2cbf9d159ba95f76fe4982192f108145f80303c0ed01","line_count":94,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_66a.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-66a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memmove\r\n * BadSink : Copy TwoIntsClass array to data using memmove\r\n * Flow Variant: 66 Data flow: data passed in an array from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_66\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(TwoIntsClass * dataArray[]);\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass * dataArray[5];\r\n data = NULL;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new TwoIntsClass[50];\r\n /* put data in array */\r\n dataArray[2] = data;\r\n badSink(dataArray);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(TwoIntsClass * dataArray[]);\r\n\r\nstatic void goodG2B()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass * dataArray[5];\r\n data = NULL;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new TwoIntsClass[100];\r\n dataArray[2] = data;\r\n goodG2BSink(dataArray);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_66; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_81_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_81_bad.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":1470,"source_sha256":"870c0a3664b1debb73afe024cf14500c9430aa92272c734bd78a873624b5c05c","line_count":45,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_81_bad.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-81_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memmove\r\n * BadSink : Copy TwoIntsClass array to data using memmove\r\n * Flow Variant: 81 Data flow: data passed in a parameter to a virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_81.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_81\r\n{\r\n\r\nvoid CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_81_bad::action(TwoIntsClass * data) const\r\n{\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_83_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_83_bad.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":1870,"source_sha256":"65ceccf36cf5c48d6c439b93121f1be8a627546e1896498340e01994cfd3c09d","line_count":50,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_83_bad.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-83_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memmove\r\n * BadSink : Copy TwoIntsClass array to data using memmove\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_83.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_83\r\n{\r\nCWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_83_bad::CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_83_bad(TwoIntsClass * dataCopy)\r\n{\r\n data = dataCopy;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new TwoIntsClass[50];\r\n}\r\n\r\nCWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_83_bad::~CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_83_bad()\r\n{\r\n {\r\n TwoIntsClass source[100];\r\n {\r\n size_t i;\r\n /* Initialize array */\r\n for (i = 0; i < 100; i++)\r\n {\r\n source[i].intOne = 0;\r\n source[i].intTwo = 0;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(TwoIntsClass));\r\n printIntLine(data[0].intOne);\r\n delete [] data;\r\n }\r\n}\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_84a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_84a.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":2437,"source_sha256":"91568e85d3e884feaf8709150143141598be658d188897541cfd839ee2df073e","line_count":81,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_84a.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-84a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memmove\r\n * BadSink : Copy TwoIntsClass array to data using memmove\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_84.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_84\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_84_bad * badObject = new CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_84_bad(data);\r\n delete badObject;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n TwoIntsClass * data;\r\n data = NULL;\r\n CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_84_goodG2B * goodG2BObject = new CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_84_goodG2B(data);\r\n delete goodG2BObject;\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_class_memmove_84; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_07.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_07.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":4177,"source_sha256":"3f281b7c25084f4cddcf4ce7a72b2b58cfa6d8f672c6bbd92033edc4777af9c2","line_count":148,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_07.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-07.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: loop\r\n * BadSink : Copy int64_t array to data using a loop\r\n * Flow Variant: 07 Control flow: if(staticFive==5) and if(staticFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The variable below is not declared \"const\", but is never assigned\r\n any other value so a tool should be able to identify that reads of\r\n this will always give its initialized value. */\r\nstatic int staticFive = 5;\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_07\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new int64_t[50];\r\n }\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n {\r\n size_t i;\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the staticFive==5 to staticFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n int64_t * data;\r\n data = NULL;\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int64_t[100];\r\n }\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n {\r\n size_t i;\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n int64_t * data;\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int64_t[100];\r\n }\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n {\r\n size_t i;\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_07; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_15.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_15.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":4276,"source_sha256":"31a77f0296e69980f0366dee51ec237bf37340099e32fc3adce70c6c0116322d","line_count":156,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_15.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-15.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: loop\r\n * BadSink : Copy int64_t array to data using a loop\r\n * Flow Variant: 15 Control flow: switch(6)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_15\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new int64_t[50];\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n {\r\n size_t i;\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the switch to switch(5) */\r\nstatic void goodG2B1()\r\n{\r\n int64_t * data;\r\n data = NULL;\r\n switch(5)\r\n {\r\n case 6:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int64_t[100];\r\n break;\r\n }\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n {\r\n size_t i;\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the switch */\r\nstatic void goodG2B2()\r\n{\r\n int64_t * data;\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int64_t[100];\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n {\r\n size_t i;\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_15; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_54b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_54b.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":1311,"source_sha256":"c8f8a53f6b160aead70825f21d549b831acb5f5e8fc33b82d690c811bb71be59","line_count":48,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_54b.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-54b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: loop\r\n * BadSink : Copy int64_t array to data using a loop\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_54\r\n{\r\n\r\n/* all the sinks are the same, we just want to know where the hit originated if a tool flags one */\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink_c(int64_t * data);\r\n\r\nvoid badSink_b(int64_t * data)\r\n{\r\n badSink_c(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink_c(int64_t * data);\r\n\r\nvoid goodG2BSink_b(int64_t * data)\r\n{\r\n goodG2BSink_c(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_61b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_61b.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":1364,"source_sha256":"693e7eaa9410e8256568581cf44eb24cfc6fbdd711b7db6ca7b02ef7640cb504","line_count":45,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_61b.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-61b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: loop\r\n * BadSink : Copy int64_t array to data using a loop\r\n * Flow Variant: 61 Data flow: data returned from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_61\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nint64_t * badSource(int64_t * data)\r\n{\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new int64_t[50];\r\n return data;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nint64_t * goodG2BSource(int64_t * data)\r\n{\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int64_t[100];\r\n return data;\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_63b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_63b.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":1808,"source_sha256":"1b22cf5df42a8dacdc53ea00566d0eff0bd386e14dee077e338ce4a592e53dde","line_count":67,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_63b.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-63b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: loop\r\n * BadSink : Copy int64_t array to data using a loop\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_63\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(int64_t * * dataPtr)\r\n{\r\n int64_t * data = *dataPtr;\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n {\r\n size_t i;\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(int64_t * * dataPtr)\r\n{\r\n int64_t * data = *dataPtr;\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n {\r\n size_t i;\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_65a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_65a.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":2582,"source_sha256":"91e478ade808ffcfc60d35f4b36b210dfc36e18e4ed11f80a059d21e84a27677","line_count":91,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_65a.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-65a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: loop\r\n * BadSink : Copy int64_t array to data using a loop\r\n * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_65\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(int64_t * data);\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n /* define a function pointer */\r\n void (*funcPtr) (int64_t *) = badSink;\r\n data = NULL;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new int64_t[50];\r\n /* use the function pointer */\r\n funcPtr(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(int64_t * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int64_t * data;\r\n void (*funcPtr) (int64_t *) = goodG2BSink;\r\n data = NULL;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int64_t[100];\r\n funcPtr(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_65; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_83_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_83_bad.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":1741,"source_sha256":"23b3c136f5c15b733c9ec8752323ae04cda3d55353a74b25f2a414fd686640d1","line_count":47,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_83_bad.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-83_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: loop\r\n * BadSink : Copy int64_t array to data using a loop\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_83.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_83\r\n{\r\nCWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_83_bad::CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_83_bad(int64_t * dataCopy)\r\n{\r\n data = dataCopy;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new int64_t[50];\r\n}\r\n\r\nCWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_83_bad::~CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_83_bad()\r\n{\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n {\r\n size_t i;\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_84_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_84_bad.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":1766,"source_sha256":"5d6eb99c61d1527f111beb5a2e617e1d1956f71910216f359d8869dd5c1fee9b","line_count":47,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_84_bad.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-84_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: loop\r\n * BadSink : Copy int64_t array to data using a loop\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_84.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_84\r\n{\r\nCWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_84_bad::CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_84_bad(int64_t * dataCopy)\r\n{\r\n data = dataCopy;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new int64_t[50];\r\n}\r\n\r\nCWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_84_bad::~CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_84_bad()\r\n{\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n {\r\n size_t i;\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_84_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_84_goodG2B.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":1799,"source_sha256":"3b2e26997e82123787fd51cd4840c4a3da53109f7b205f5c1bfa636cb462c0a9","line_count":47,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_84_goodG2B.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-84_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: loop\r\n * BadSink : Copy int64_t array to data using a loop\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_84.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_84\r\n{\r\nCWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_84_goodG2B::CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_84_goodG2B(int64_t * dataCopy)\r\n{\r\n data = dataCopy;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int64_t[100];\r\n}\r\n\r\nCWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_84_goodG2B::~CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_loop_84_goodG2B()\r\n{\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n {\r\n size_t i;\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_03.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_03.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":3563,"source_sha256":"622c0002dbb2f5961e151c11f7773ffdb3ce38f514d3459be07a45f993498aa1","line_count":125,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_03.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-03.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memcpy\r\n * BadSink : Copy int64_t array to data using memcpy\r\n * Flow Variant: 03 Control flow: if(5==5) and if(5!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_03\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n data = NULL;\r\n if(5==5)\r\n {\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new int64_t[50];\r\n }\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(int64_t));\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the 5==5 to 5!=5 */\r\nstatic void goodG2B1()\r\n{\r\n int64_t * data;\r\n data = NULL;\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int64_t[100];\r\n }\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(int64_t));\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n int64_t * data;\r\n data = NULL;\r\n if(5==5)\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int64_t[100];\r\n }\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(int64_t));\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_03; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_18.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_18.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":2777,"source_sha256":"f7f4721a99c6b92fc7320b1f125aa11227951adafc845d62d38aa8443d50b272","line_count":98,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_18.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-18.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memcpy\r\n * BadSink : Copy int64_t array to data using memcpy\r\n * Flow Variant: 18 Control flow: goto statements\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_18\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n data = NULL;\r\n goto source;\r\nsource:\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new int64_t[50];\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(int64_t));\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() - use goodsource and badsink by reversing the blocks on the goto statement */\r\nstatic void goodG2B()\r\n{\r\n int64_t * data;\r\n data = NULL;\r\n goto source;\r\nsource:\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int64_t[100];\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(int64_t));\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_18; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_22b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_22b.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":2472,"source_sha256":"f50f0630e2f4c705e4b2a689fda466cff4fa5ebe759de25da3316272804c57be","line_count":76,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_22b.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-22b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memcpy\r\n * BadSink : Copy int64_t array to data using memcpy\r\n * Flow Variant: 22 Control flow: Flow controlled by value of a global variable. Sink functions are in a separate file from sources.\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_22\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* The global variable below is used to drive control flow in the source function. Since it is in\r\na C++ namespace, it doesn't need a globally unique name. */\r\nextern int badGlobal;\r\n\r\nint64_t * badSource(int64_t * data)\r\n{\r\n if(badGlobal)\r\n {\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new int64_t[50];\r\n }\r\n return data;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* The global variables below are used to drive control flow in the source functions. Since they are in\r\na C++ namespace, they don't need globally unique names. */\r\nextern int goodG2B1Global;\r\nextern int goodG2B2Global;\r\n\r\n/* goodG2B1() - use goodsource and badsink by setting the global variable to false instead of true */\r\nint64_t * goodG2B1Source(int64_t * data)\r\n{\r\n if(goodG2B1Global)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int64_t[100];\r\n }\r\n return data;\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if in the source function */\r\nint64_t * goodG2B2Source(int64_t * data)\r\n{\r\n if(goodG2B2Global)\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int64_t[100];\r\n }\r\n return data;\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_74a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_74a.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":2759,"source_sha256":"0a875556ab0040f89f37e0e5e404d49df59888028d7cbed32e271ff7d895c263","line_count":102,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_74a.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-74a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memcpy\r\n * BadSink : Copy int64_t array to data using memcpy\r\n * Flow Variant: 74 Data flow: data passed in a map from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_74\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(map dataMap);\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n map dataMap;\r\n data = NULL;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new int64_t[50];\r\n /* Put data in a map */\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n badSink(dataMap);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(map dataMap);\r\n\r\nstatic void goodG2B()\r\n{\r\n int64_t * data;\r\n map dataMap;\r\n data = NULL;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int64_t[100];\r\n /* Put data in a map */\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n goodG2BSink(dataMap);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_74; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_82a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_82a.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":2742,"source_sha256":"d62ef5d2c2fe3b87cbae4b0848dcc9c915daf16a3c6c9f71b0c1465961971677","line_count":87,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_82a.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-82a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memcpy\r\n * BadSink : Copy int64_t array to data using memcpy\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_82.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_82\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n data = NULL;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new int64_t[50];\r\n CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_82_base* baseObject = new CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_82_bad;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int64_t * data;\r\n data = NULL;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int64_t[100];\r\n CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_82_base* baseObject = new CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_82_goodG2B;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_82; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_83_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_83_goodG2B.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":1678,"source_sha256":"72b8417e0f9d48565084e9db400a4cb362ddd1f040921539623a940fc7f2b47b","line_count":41,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_83_goodG2B.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-83_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memcpy\r\n * BadSink : Copy int64_t array to data using memcpy\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_83.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_83\r\n{\r\nCWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_83_goodG2B::CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_83_goodG2B(int64_t * dataCopy)\r\n{\r\n data = dataCopy;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int64_t[100];\r\n}\r\n\r\nCWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_83_goodG2B::~CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memcpy_83_goodG2B()\r\n{\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(int64_t));\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_09.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_09.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":3665,"source_sha256":"ccb10503220c9331c9fa6573cd81f9ecc4ed34ad320d6236762630cccac14dd0","line_count":125,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_09.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-09.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memmove\r\n * BadSink : Copy int64_t array to data using memmove\r\n * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_09\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new int64_t[50];\r\n }\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(int64_t));\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n int64_t * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int64_t[100];\r\n }\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(int64_t));\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n int64_t * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int64_t[100];\r\n }\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(int64_t));\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_09; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_10.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_10.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":3616,"source_sha256":"5a397f5258f8f7ca78d0ec61fe5534b293e42fb7d984018317638060b45d6447","line_count":125,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_10.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-10.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memmove\r\n * BadSink : Copy int64_t array to data using memmove\r\n * Flow Variant: 10 Control flow: if(globalTrue) and if(globalFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_10\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new int64_t[50];\r\n }\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(int64_t));\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the globalTrue to globalFalse */\r\nstatic void goodG2B1()\r\n{\r\n int64_t * data;\r\n data = NULL;\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int64_t[100];\r\n }\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(int64_t));\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n int64_t * data;\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int64_t[100];\r\n }\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(int64_t));\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_10; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_31.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_31.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":2945,"source_sha256":"68cd10217bf58436832484b6df98895fa2a6c497e1a4ff76ed13110cb3acaa21","line_count":101,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_31.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-31.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memmove\r\n * BadSink : Copy int64_t array to data using memmove\r\n * Flow Variant: 31 Data flow using a copy of data within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_31\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n data = NULL;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new int64_t[50];\r\n {\r\n int64_t * dataCopy = data;\r\n int64_t * data = dataCopy;\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(int64_t));\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int64_t * data;\r\n data = NULL;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int64_t[100];\r\n {\r\n int64_t * dataCopy = data;\r\n int64_t * data = dataCopy;\r\n {\r\n int64_t source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(int64_t));\r\n printLongLongLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_31; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_54c.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_54c.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":1321,"source_sha256":"20212f4d80039bbb22b86928eee2f0719259e10e3e79d042f6c56b8842a7e86f","line_count":48,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_54c.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-54c.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memmove\r\n * BadSink : Copy int64_t array to data using memmove\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_54\r\n{\r\n\r\n/* all the sinks are the same, we just want to know where the hit originated if a tool flags one */\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink_d(int64_t * data);\r\n\r\nvoid badSink_c(int64_t * data)\r\n{\r\n badSink_d(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink_d(int64_t * data);\r\n\r\nvoid goodG2BSink_c(int64_t * data)\r\n{\r\n goodG2BSink_d(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_62b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_62b.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":1349,"source_sha256":"7757063c3afc32b1dcdf4f84f3247ede5af31b61e75437313d8b14a275f6ba93","line_count":43,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_62b.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-62b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memmove\r\n * BadSink : Copy int64_t array to data using memmove\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSource(int64_t * &data)\r\n{\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new int64_t[50];\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(int64_t * &data)\r\n{\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int64_t[100];\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_74a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_74a.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":2764,"source_sha256":"c5768e60ee649321e5b19b41000427edbec0bfb1b3b1dce9f5d9c4d44d46f51c","line_count":102,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_74a.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-74a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memmove\r\n * BadSink : Copy int64_t array to data using memmove\r\n * Flow Variant: 74 Data flow: data passed in a map from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_74\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(map dataMap);\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n map dataMap;\r\n data = NULL;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new int64_t[50];\r\n /* Put data in a map */\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n badSink(dataMap);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(map dataMap);\r\n\r\nstatic void goodG2B()\r\n{\r\n int64_t * data;\r\n map dataMap;\r\n data = NULL;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int64_t[100];\r\n /* Put data in a map */\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n goodG2BSink(dataMap);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_74; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_81a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_81a.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":2711,"source_sha256":"a067cfcd12e4aa2a9332777a9e3bee9a3eb04064734cc1617391672e30138114","line_count":85,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_81a.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-81a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memmove\r\n * BadSink : Copy int64_t array to data using memmove\r\n * Flow Variant: 81 Data flow: data passed in a parameter to a virtual method called via a reference\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_81.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_81\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n data = NULL;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new int64_t[50];\r\n const CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_81_base& baseObject = CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_81_bad();\r\n baseObject.action(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int64_t * data;\r\n data = NULL;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int64_t[100];\r\n const CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_81_base& baseObject = CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_81_goodG2B();\r\n baseObject.action(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int64_t_memmove_81; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_loop_31.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_loop_31.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":3136,"source_sha256":"af28d552f8c82b1a55fe5954f0252cdfaf194edacc9896ea4944247651ace628","line_count":113,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_loop_31.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-31.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: loop\r\n * BadSink : Copy int array to data using a loop\r\n * Flow Variant: 31 Data flow using a copy of data within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_loop_31\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int * data;\r\n data = NULL;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new int[50];\r\n {\r\n int * dataCopy = data;\r\n int * data = dataCopy;\r\n {\r\n int source[100] = {0}; /* fill with 0's */\r\n {\r\n size_t i;\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printIntLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int * data;\r\n data = NULL;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int[100];\r\n {\r\n int * dataCopy = data;\r\n int * data = dataCopy;\r\n {\r\n int source[100] = {0}; /* fill with 0's */\r\n {\r\n size_t i;\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printIntLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_loop_31; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_loop_52c.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_loop_52c.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":1823,"source_sha256":"2915a45c53e17db662aa42290e85b17e8bd57a81f6e93f14bbcf92094e0b11ba","line_count":67,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_loop_52c.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-52c.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: loop\r\n * BadSink : Copy int array to data using a loop\r\n * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_loop_52\r\n{\r\n\r\n/* all the sinks are the same, we just want to know where the hit originated if a tool flags one */\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink_c(int * data)\r\n{\r\n {\r\n int source[100] = {0}; /* fill with 0's */\r\n {\r\n size_t i;\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printIntLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink_c(int * data)\r\n{\r\n {\r\n int source[100] = {0}; /* fill with 0's */\r\n {\r\n size_t i;\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printIntLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_loop_62a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_loop_62a.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":2758,"source_sha256":"66f3c89904cdb4dfb5f89461a4e6b28f4b8279075bab3337cb79680c75721882","line_count":109,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_loop_62a.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-62a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: loop\r\n * BadSink : Copy int array to data using a loop\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_loop_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSource(int * &data);\r\n\r\nvoid bad()\r\n{\r\n int * data;\r\n data = NULL;\r\n badSource(data);\r\n {\r\n int source[100] = {0}; /* fill with 0's */\r\n {\r\n size_t i;\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printIntLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(int * &data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int * data;\r\n data = NULL;\r\n goodG2BSource(data);\r\n {\r\n int source[100] = {0}; /* fill with 0's */\r\n {\r\n size_t i;\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printIntLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_loop_62; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_loop_63b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_loop_63b.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":1762,"source_sha256":"69b9efe2b966fe0bc4fecdac27865aebf3979f7333bb85de490dc41b85cdc7cd","line_count":67,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_loop_63b.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-63b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: loop\r\n * BadSink : Copy int array to data using a loop\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_loop_63\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(int * * dataPtr)\r\n{\r\n int * data = *dataPtr;\r\n {\r\n int source[100] = {0}; /* fill with 0's */\r\n {\r\n size_t i;\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printIntLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(int * * dataPtr)\r\n{\r\n int * data = *dataPtr;\r\n {\r\n int source[100] = {0}; /* fill with 0's */\r\n {\r\n size_t i;\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printIntLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_loop_72b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_loop_72b.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":1873,"source_sha256":"abb8a13e864a8e22ae37894c540e4dad1b47a4ec605b57dcfbb6593d3b683efa","line_count":71,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_loop_72b.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-72b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: loop\r\n * BadSink : Copy int array to data using a loop\r\n * Flow Variant: 72 Data flow: data passed in a vector from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_loop_72\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(vector dataVector)\r\n{\r\n /* copy data out of dataVector */\r\n int * data = dataVector[2];\r\n {\r\n int source[100] = {0}; /* fill with 0's */\r\n {\r\n size_t i;\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printIntLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(vector dataVector)\r\n{\r\n int * data = dataVector[2];\r\n {\r\n int source[100] = {0}; /* fill with 0's */\r\n {\r\n size_t i;\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n printIntLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memcpy_45.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memcpy_45.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":2960,"source_sha256":"9f501ece789d465f6c40eb8b0cf8c24fa2884af390537eb94f866a774afcf338","line_count":111,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memcpy_45.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-45.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memcpy\r\n * BadSink : Copy int array to data using memcpy\r\n * Flow Variant: 45 Data flow: data passed as a static global variable from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memcpy_45\r\n{\r\n\r\nstatic int * badData;\r\nstatic int * goodG2BData;\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink()\r\n{\r\n int * data = badData;\r\n {\r\n int source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(int));\r\n printIntLine(data[0]);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid bad()\r\n{\r\n int * data;\r\n data = NULL;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new int[50];\r\n badData = data;\r\n badSink();\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink()\r\n{\r\n int * data = goodG2BData;\r\n {\r\n int source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(int));\r\n printIntLine(data[0]);\r\n delete [] data;\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n int * data;\r\n data = NULL;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int[100];\r\n goodG2BData = data;\r\n goodG2BSink();\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memcpy_45; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memcpy_65b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memcpy_65b.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":1516,"source_sha256":"56e6906e8501c89b080e489af7c01ab5828f709e592399859bc0b51b020880ff","line_count":53,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memcpy_65b.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-65b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memcpy\r\n * BadSink : Copy int array to data using memcpy\r\n * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memcpy_65\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(int * data)\r\n{\r\n {\r\n int source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(int));\r\n printIntLine(data[0]);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(int * data)\r\n{\r\n {\r\n int source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memcpy(data, source, 100*sizeof(int));\r\n printIntLine(data[0]);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memcpy_81.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memcpy_81.h","cwe_id":"CWE122","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1401,"source_sha256":"d8136c409f0e6ad4a0f86644bb240e70c0c87a38cc4ff7f8eafa4b911e771952","line_count":49,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memcpy_81.h\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-81.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memcpy\r\n * BadSink : Copy int array to data using memcpy\r\n * Flow Variant: 81 Data flow: data passed in a parameter to a virtual method called via a reference\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memcpy_81\r\n{\r\n\r\nclass CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memcpy_81_base\r\n{\r\npublic:\r\n /* pure virtual function */\r\n virtual void action(int * data) const = 0;\r\n};\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memcpy_81_bad : public CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memcpy_81_base\r\n{\r\npublic:\r\n void action(int * data) const;\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memcpy_81_goodG2B : public CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memcpy_81_base\r\n{\r\npublic:\r\n void action(int * data) const;\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memmove_07.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memmove_07.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":3778,"source_sha256":"608ba7145acff3344e2d898a577a1747619cd3c6324653fb5b283ed15c35f619","line_count":130,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memmove_07.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-07.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memmove\r\n * BadSink : Copy int array to data using memmove\r\n * Flow Variant: 07 Control flow: if(staticFive==5) and if(staticFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The variable below is not declared \"const\", but is never assigned\r\n any other value so a tool should be able to identify that reads of\r\n this will always give its initialized value. */\r\nstatic int staticFive = 5;\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memmove_07\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int * data;\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new int[50];\r\n }\r\n {\r\n int source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(int));\r\n printIntLine(data[0]);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the staticFive==5 to staticFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n int * data;\r\n data = NULL;\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int[100];\r\n }\r\n {\r\n int source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(int));\r\n printIntLine(data[0]);\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n int * data;\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int[100];\r\n }\r\n {\r\n int source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(int));\r\n printIntLine(data[0]);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memmove_07; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memmove_13.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memmove_13.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":3604,"source_sha256":"bdfdef1396dca9f8985e620b76faaa2a7b4c19d7b1b9366932b91fbc30a53c0c","line_count":125,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memmove_13.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-13.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memmove\r\n * BadSink : Copy int array to data using memmove\r\n * Flow Variant: 13 Control flow: if(GLOBAL_CONST_FIVE==5) and if(GLOBAL_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memmove_13\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new int[50];\r\n }\r\n {\r\n int source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(int));\r\n printIntLine(data[0]);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the GLOBAL_CONST_FIVE==5 to GLOBAL_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n int * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int[100];\r\n }\r\n {\r\n int source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(int));\r\n printIntLine(data[0]);\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n int * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int[100];\r\n }\r\n {\r\n int source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(int));\r\n printIntLine(data[0]);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memmove_13; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memmove_31.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memmove_31.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":2871,"source_sha256":"4b05db75a306122e7656d03a64af00988e1e9d645db0be7a7d0b6d993e137495","line_count":101,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memmove_31.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-31.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memmove\r\n * BadSink : Copy int array to data using memmove\r\n * Flow Variant: 31 Data flow using a copy of data within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memmove_31\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int * data;\r\n data = NULL;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new int[50];\r\n {\r\n int * dataCopy = data;\r\n int * data = dataCopy;\r\n {\r\n int source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(int));\r\n printIntLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int * data;\r\n data = NULL;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int[100];\r\n {\r\n int * dataCopy = data;\r\n int * data = dataCopy;\r\n {\r\n int source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(int));\r\n printIntLine(data[0]);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memmove_31; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memmove_43.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memmove_43.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":2835,"source_sha256":"dd18817fef8c30f1a76a2f3e452fefeff2baf95c666e9fd5bcba447778d220a8","line_count":104,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memmove_43.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.label.xml\r\nTemplate File: sources-sink-43.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memmove\r\n * BadSink : Copy int array to data using memmove\r\n * Flow Variant: 43 Data flow: data flows using a C++ reference from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memmove_43\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSource(int * &data)\r\n{\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new int[50];\r\n}\r\n\r\nvoid bad()\r\n{\r\n int * data;\r\n data = NULL;\r\n badSource(data);\r\n {\r\n int source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(int));\r\n printIntLine(data[0]);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSource(int * &data)\r\n{\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new int[100];\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n int * data;\r\n data = NULL;\r\n goodG2BSource(data);\r\n {\r\n int source[100] = {0}; /* fill with 0's */\r\n /* POTENTIAL FLAW: Possible buffer overflow if data < 100 */\r\n memmove(data, source, 100*sizeof(int));\r\n printIntLine(data[0]);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_int_memmove_43; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_loop_09.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_loop_09.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":4523,"source_sha256":"f6d2719ca44e624b583719ffb58e6d590fdd8b3b94e323cbc0b3c6246f99e490","line_count":151,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_loop_09.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml\r\nTemplate File: sources-sink-09.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: loop\r\n * BadSink : Copy string to data using a loop\r\n * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_loop_09\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new wchar_t[50];\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n size_t i;\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new wchar_t[100];\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n size_t i;\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new wchar_t[100];\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n size_t i;\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_loop_09; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_loop_22a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_loop_22a.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":4424,"source_sha256":"a943e68f69e4980ad2c69da01b09bc42dfc1f6ca49035e0ebdb288fd0809cb9a","line_count":152,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_loop_22a.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml\r\nTemplate File: sources-sink-22a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: loop\r\n * BadSink : Copy string to data using a loop\r\n * Flow Variant: 22 Control flow: Flow controlled by value of a global variable. Sink functions are in a separate file from sources.\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_loop_22\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* The global variable below is used to drive control flow in the source function. Since it is in\r\na C++ namespace, it doesn't need a globally unique name. */\r\nint badGlobal = 0;\r\n\r\nwchar_t * badSource(wchar_t * data);\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n badGlobal = 1; /* true */\r\n data = badSource(data);\r\n {\r\n size_t i;\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n ;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* The global variables below are used to drive control flow in the source functions. Since they are in\r\na C++ namespace, they don't need globally unique names. */\r\nint goodG2B1Global = 0;\r\nint goodG2B2Global = 0;\r\n\r\n/* goodG2B1() - use goodsource and badsink by setting the global variable to false instead of true */\r\nwchar_t * goodG2B1Source(wchar_t * data);\r\n\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n goodG2B1Global = 0; /* false */\r\n data = goodG2B1Source(data);\r\n {\r\n size_t i;\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n ;\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if in the source function */\r\nwchar_t * goodG2B2Source(wchar_t * data);\r\n\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n goodG2B2Global = 1; /* true */\r\n data = goodG2B2Source(data);\r\n {\r\n size_t i;\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n ;\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_loop_22; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_loop_33.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_loop_33.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":3569,"source_sha256":"acce844f55a0980eed1c2aa89ea116b85227dc3e2b063e1926c9d32755f2c75c","line_count":119,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_loop_33.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml\r\nTemplate File: sources-sink-33.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: loop\r\n * BadSink : Copy string to data using a loop\r\n * Flow Variant: 33 Data flow: use of a C++ reference to data within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_loop_33\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n wchar_t * &dataRef = data;\r\n data = NULL;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new wchar_t[50];\r\n data[0] = L'\\0'; /* null terminate */\r\n {\r\n wchar_t * data = dataRef;\r\n {\r\n size_t i;\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n wchar_t * &dataRef = data;\r\n data = NULL;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new wchar_t[100];\r\n data[0] = L'\\0'; /* null terminate */\r\n {\r\n wchar_t * data = dataRef;\r\n {\r\n size_t i;\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_loop_33; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_loop_54e.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_loop_54e.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":2161,"source_sha256":"346b8a72d69d5417be51421c827fd5aaea6b0622954ada199da2835aa5baf7a8","line_count":71,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_loop_54e.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml\r\nTemplate File: sources-sink-54e.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: loop\r\n * BadSink : Copy string to data using a loop\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_loop_54\r\n{\r\n\r\n/* all the sinks are the same, we just want to know where the hit originated if a tool flags one */\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink_e(wchar_t * data)\r\n{\r\n {\r\n size_t i;\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink_e(wchar_t * data)\r\n{\r\n {\r\n size_t i;\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n for (i = 0; i < 100; i++)\r\n {\r\n data[i] = source[i];\r\n }\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_06.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_06.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":4569,"source_sha256":"3afe2d04464be54ff1e736dc9e4c1846365847cd9882f00f8d1aced6070a4910","line_count":144,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_06.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml\r\nTemplate File: sources-sink-06.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memcpy\r\n * BadSink : Copy string to data using memcpy\r\n * Flow Variant: 06 Control flow: if(STATIC_CONST_FIVE==5) and if(STATIC_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The variable below is declared \"const\", so a tool should be able\r\n to identify that reads of this will always give its initialized\r\n value. */\r\nstatic const int STATIC_CONST_FIVE = 5;\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_06\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new wchar_t[50];\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n memcpy(data, source, 100*sizeof(wchar_t));\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new wchar_t[100];\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n memcpy(data, source, 100*sizeof(wchar_t));\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new wchar_t[100];\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n memcpy(data, source, 100*sizeof(wchar_t));\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_06; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_09.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_09.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":4357,"source_sha256":"9f493a02cb9e58d31336cbd088ca1530c161a0ee16dfb9550366a3ac80d5d5eb","line_count":139,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_09.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml\r\nTemplate File: sources-sink-09.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memcpy\r\n * BadSink : Copy string to data using memcpy\r\n * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_09\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new wchar_t[50];\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n memcpy(data, source, 100*sizeof(wchar_t));\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new wchar_t[100];\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n memcpy(data, source, 100*sizeof(wchar_t));\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new wchar_t[100];\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n memcpy(data, source, 100*sizeof(wchar_t));\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_09; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_32.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_32.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":3689,"source_sha256":"56a16035efe79a98dd72a2ed0a18ede277411db8311869358c32b5386ddd5128","line_count":121,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_32.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml\r\nTemplate File: sources-sink-32.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memcpy\r\n * BadSink : Copy string to data using memcpy\r\n * Flow Variant: 32 Data flow using two pointers to the same value within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_32\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n wchar_t * *dataPtr1 = &data;\r\n wchar_t * *dataPtr2 = &data;\r\n data = NULL;\r\n {\r\n wchar_t * data = *dataPtr1;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new wchar_t[50];\r\n data[0] = L'\\0'; /* null terminate */\r\n *dataPtr1 = data;\r\n }\r\n {\r\n wchar_t * data = *dataPtr2;\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n memcpy(data, source, 100*sizeof(wchar_t));\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n wchar_t * *dataPtr1 = &data;\r\n wchar_t * *dataPtr2 = &data;\r\n data = NULL;\r\n {\r\n wchar_t * data = *dataPtr1;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new wchar_t[100];\r\n data[0] = L'\\0'; /* null terminate */\r\n *dataPtr1 = data;\r\n }\r\n {\r\n wchar_t * data = *dataPtr2;\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n memcpy(data, source, 100*sizeof(wchar_t));\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_32; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_41.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_41.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":3334,"source_sha256":"65a118391e1736c603f52247909970b0174a68c406f2208aeb959d2f86ef9f31","line_count":114,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_41.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml\r\nTemplate File: sources-sink-41.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memcpy\r\n * BadSink : Copy string to data using memcpy\r\n * Flow Variant: 41 Data flow: data passed as an argument from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_41\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(wchar_t * data)\r\n{\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n memcpy(data, source, 100*sizeof(wchar_t));\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new wchar_t[50];\r\n data[0] = L'\\0'; /* null terminate */\r\n badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nvoid goodG2BSink(wchar_t * data)\r\n{\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n memcpy(data, source, 100*sizeof(wchar_t));\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new wchar_t[100];\r\n data[0] = L'\\0'; /* null terminate */\r\n goodG2BSink(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_41; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_63b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_63b.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":1970,"source_sha256":"3dc867bae3c382345670e33f1d869cd513906f14f4ed255c1413e8d6a716891d","line_count":63,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_63b.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml\r\nTemplate File: sources-sink-63b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memcpy\r\n * BadSink : Copy string to data using memcpy\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_63\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(wchar_t * * dataPtr)\r\n{\r\n wchar_t * data = *dataPtr;\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n memcpy(data, source, 100*sizeof(wchar_t));\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(wchar_t * * dataPtr)\r\n{\r\n wchar_t * data = *dataPtr;\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n memcpy(data, source, 100*sizeof(wchar_t));\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_65a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_65a.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":2698,"source_sha256":"a135fe6439f756768184e6d0fd88ccd7bd13d3bc674926b3ed7d7407aaa5d114","line_count":95,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_65a.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml\r\nTemplate File: sources-sink-65a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memcpy\r\n * BadSink : Copy string to data using memcpy\r\n * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_65\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(wchar_t * data);\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n /* define a function pointer */\r\n void (*funcPtr) (wchar_t *) = badSink;\r\n data = NULL;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new wchar_t[50];\r\n data[0] = L'\\0'; /* null terminate */\r\n /* use the function pointer */\r\n funcPtr(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(wchar_t * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n void (*funcPtr) (wchar_t *) = goodG2BSink;\r\n data = NULL;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new wchar_t[100];\r\n data[0] = L'\\0'; /* null terminate */\r\n funcPtr(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_65; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_73a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_73a.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":2903,"source_sha256":"3866520059343199567d26a4d120b0db30a55cc796c383fea7a50769c702763c","line_count":106,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_73a.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml\r\nTemplate File: sources-sink-73a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memcpy\r\n * BadSink : Copy string to data using memcpy\r\n * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_73\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(list dataList);\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n list dataList;\r\n data = NULL;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new wchar_t[50];\r\n data[0] = L'\\0'; /* null terminate */\r\n /* Put data in a list */\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n badSink(dataList);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(list dataList);\r\n\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n list dataList;\r\n data = NULL;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new wchar_t[100];\r\n data[0] = L'\\0'; /* null terminate */\r\n /* Put data in a list */\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n goodG2BSink(dataList);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_73; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_81_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_81_goodG2B.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":1441,"source_sha256":"7d2c315f2ccbc46135ed4e8a44d0d837fb2105a8b8f926cfb88e2e0f0f503536","line_count":39,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_81_goodG2B.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml\r\nTemplate File: sources-sink-81_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memcpy\r\n * BadSink : Copy string to data using memcpy\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_81.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_81\r\n{\r\n\r\nvoid CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_81_goodG2B::action(wchar_t * data) const\r\n{\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n memcpy(data, source, 100*sizeof(wchar_t));\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_81a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_81a.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":2787,"source_sha256":"e31d07427c336d1171fba452cbc5bcde0264c503d8267fde7fc05804b942a9e6","line_count":87,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_81a.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml\r\nTemplate File: sources-sink-81a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: memcpy\r\n * BadSink : Copy string to data using memcpy\r\n * Flow Variant: 81 Data flow: data passed in a parameter to a virtual method called via a reference\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_81.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_81\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new wchar_t[50];\r\n data[0] = L'\\0'; /* null terminate */\r\n const CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_81_base& baseObject = CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_81_bad();\r\n baseObject.action(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new wchar_t[100];\r\n data[0] = L'\\0'; /* null terminate */\r\n const CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_81_base& baseObject = CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_81_goodG2B();\r\n baseObject.action(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memcpy_81; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memmove_11.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memmove_11.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":4379,"source_sha256":"cebf25f985d253662e7f6cb6ee992a21df7cc6c079bb29c083aaaaca58828d78","line_count":139,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memmove_11.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml\r\nTemplate File: sources-sink-11.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memmove\r\n * BadSink : Copy string to data using memmove\r\n * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memmove_11\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new wchar_t[50];\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n memmove(data, source, 100*sizeof(wchar_t));\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new wchar_t[100];\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n memmove(data, source, 100*sizeof(wchar_t));\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new wchar_t[100];\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n memmove(data, source, 100*sizeof(wchar_t));\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memmove_11; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memmove_17.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memmove_17.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":3285,"source_sha256":"514967b27e80069da3f1ab243544b04779a7d50592ef606427b5df5bec699465","line_count":112,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memmove_17.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml\r\nTemplate File: sources-sink-17.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memmove\r\n * BadSink : Copy string to data using memmove\r\n * Flow Variant: 17 Control flow: for loops\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memmove_17\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int i;\r\n wchar_t * data;\r\n data = NULL;\r\n for(i = 0; i < 1; i++)\r\n {\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new wchar_t[50];\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n memmove(data, source, 100*sizeof(wchar_t));\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() - use goodsource in the for statement */\r\nstatic void goodG2B()\r\n{\r\n int h;\r\n wchar_t * data;\r\n data = NULL;\r\n for(h = 0; h < 1; h++)\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new wchar_t[100];\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than data */\r\n memmove(data, source, 100*sizeof(wchar_t));\r\n data[100-1] = L'\\0'; /* Ensure the destination buffer is null terminated */\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memmove_17; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memmove_52b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memmove_52b.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":1315,"source_sha256":"d1cb5af2d142964a35bfca44ac690a76927276bd07ebe19ca8c79b97482f6998","line_count":50,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memmove_52b.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml\r\nTemplate File: sources-sink-52b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: memmove\r\n * BadSink : Copy string to data using memmove\r\n * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_memmove_52\r\n{\r\n\r\n/* all the sinks are the same, we just want to know where the hit originated if a tool flags one */\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink_c(wchar_t * data);\r\n\r\nvoid badSink_b(wchar_t * data)\r\n{\r\n badSink_c(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink_c(wchar_t * data);\r\n\r\nvoid goodG2BSink_b(wchar_t * data)\r\n{\r\n goodG2BSink_c(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_04.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_04.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":4384,"source_sha256":"5514a57c0ddeaa9da1be0e970203e1f0caab289c34dbecfa3e043c8a837e8e92","line_count":142,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_04.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml\r\nTemplate File: sources-sink-04.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: ncat\r\n * BadSink : Copy string to data using wcsncat\r\n * Flow Variant: 04 Control flow: if(STATIC_CONST_TRUE) and if(STATIC_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The two variables below are declared \"const\", so a tool should\r\n be able to identify that reads of these will always return their\r\n initialized values. */\r\nstatic const int STATIC_CONST_TRUE = 1; /* true */\r\nstatic const int STATIC_CONST_FALSE = 0; /* false */\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_04\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new wchar_t[50];\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than sizeof(data)-strlen(data) */\r\n wcsncat(data, source, 100);\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the STATIC_CONST_TRUE to STATIC_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(STATIC_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new wchar_t[100];\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than sizeof(data)-strlen(data) */\r\n wcsncat(data, source, 100);\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new wchar_t[100];\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than sizeof(data)-strlen(data) */\r\n wcsncat(data, source, 100);\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_04; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_09.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_09.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":4113,"source_sha256":"34cc13676d1e963d17b8f6c96a1bbe47c2804f9b6ee726a2eff1063168f54089","line_count":136,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_09.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml\r\nTemplate File: sources-sink-09.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: ncat\r\n * BadSink : Copy string to data using wcsncat\r\n * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_09\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new wchar_t[50];\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than sizeof(data)-strlen(data) */\r\n wcsncat(data, source, 100);\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new wchar_t[100];\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than sizeof(data)-strlen(data) */\r\n wcsncat(data, source, 100);\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the if statement */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new wchar_t[100];\r\n data[0] = L'\\0'; /* null terminate */\r\n }\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than sizeof(data)-strlen(data) */\r\n wcsncat(data, source, 100);\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_09; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_32.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_32.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":3516,"source_sha256":"8e1b390d14d78c28521e994a5d8d194c772b363fe6cc62eb84cb6a8d213afbd6","line_count":119,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_32.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml\r\nTemplate File: sources-sink-32.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sink: ncat\r\n * BadSink : Copy string to data using wcsncat\r\n * Flow Variant: 32 Data flow using two pointers to the same value within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_32\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n wchar_t * *dataPtr1 = &data;\r\n wchar_t * *dataPtr2 = &data;\r\n data = NULL;\r\n {\r\n wchar_t * data = *dataPtr1;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new wchar_t[50];\r\n data[0] = L'\\0'; /* null terminate */\r\n *dataPtr1 = data;\r\n }\r\n {\r\n wchar_t * data = *dataPtr2;\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than sizeof(data)-strlen(data) */\r\n wcsncat(data, source, 100);\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n wchar_t * *dataPtr1 = &data;\r\n wchar_t * *dataPtr2 = &data;\r\n data = NULL;\r\n {\r\n wchar_t * data = *dataPtr1;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new wchar_t[100];\r\n data[0] = L'\\0'; /* null terminate */\r\n *dataPtr1 = data;\r\n }\r\n {\r\n wchar_t * data = *dataPtr2;\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than sizeof(data)-strlen(data) */\r\n wcsncat(data, source, 100);\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_32; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_34.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_34.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":3453,"source_sha256":"59ba1928612d7dd3f9d6877cfcf04ddd85e9f51798183fbd5508ff29d3fb0036","line_count":117,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_34.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml\r\nTemplate File: sources-sink-34.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: ncat\r\n * BadSink : Copy string to data using wcsncat\r\n * Flow Variant: 34 Data flow: use of a union containing two methods of accessing the same data (within the same function)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_34\r\n{\r\n\r\ntypedef union\r\n{\r\n wchar_t * unionFirst;\r\n wchar_t * unionSecond;\r\n} unionType;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n unionType myUnion;\r\n data = NULL;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new wchar_t[50];\r\n data[0] = L'\\0'; /* null terminate */\r\n myUnion.unionFirst = data;\r\n {\r\n wchar_t * data = myUnion.unionSecond;\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than sizeof(data)-strlen(data) */\r\n wcsncat(data, source, 100);\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n unionType myUnion;\r\n data = NULL;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new wchar_t[100];\r\n data[0] = L'\\0'; /* null terminate */\r\n myUnion.unionFirst = data;\r\n {\r\n wchar_t * data = myUnion.unionSecond;\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than sizeof(data)-strlen(data) */\r\n wcsncat(data, source, 100);\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_34; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_65a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_65a.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":2691,"source_sha256":"a9464c9f6e4d86a0cc8c6a9d396b46fcc02dd428d217f4395fb82f7708eaba16","line_count":95,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_65a.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml\r\nTemplate File: sources-sink-65a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: ncat\r\n * BadSink : Copy string to data using wcsncat\r\n * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_65\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(wchar_t * data);\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n /* define a function pointer */\r\n void (*funcPtr) (wchar_t *) = badSink;\r\n data = NULL;\r\n /* FLAW: Allocate using new[] and point data to a small buffer that is smaller than the large buffer used in the sinks */\r\n data = new wchar_t[50];\r\n data[0] = L'\\0'; /* null terminate */\r\n /* use the function pointer */\r\n funcPtr(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(wchar_t * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n void (*funcPtr) (wchar_t *) = goodG2BSink;\r\n data = NULL;\r\n /* FIX: Allocate using new[] and point data to a large buffer that is at least as large as the large buffer used in the sink */\r\n data = new wchar_t[100];\r\n data[0] = L'\\0'; /* null terminate */\r\n funcPtr(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_65; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_73b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_73b.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":1908,"source_sha256":"fb6e113467bcf1f9d68617ebf9a414d89e6beddddb52f95c235dbf091e72842a","line_count":65,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_73b.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml\r\nTemplate File: sources-sink-73b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: ncat\r\n * BadSink : Copy string to data using wcsncat\r\n * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_73\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(list dataList)\r\n{\r\n /* copy data out of dataList */\r\n wchar_t * data = dataList.back();\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than sizeof(data)-strlen(data) */\r\n wcsncat(data, source, 100);\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(list dataList)\r\n{\r\n wchar_t * data = dataList.back();\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than sizeof(data)-strlen(data) */\r\n wcsncat(data, source, 100);\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_81.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_81.h","cwe_id":"CWE122","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1452,"source_sha256":"b4b60259f12b796d19748c2af070f1c0d8d35e3ec6f5adc5602c455d5193cf84","line_count":51,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_81.h\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml\r\nTemplate File: sources-sink-81.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: ncat\r\n * BadSink : Copy string to data using wcsncat\r\n * Flow Variant: 81 Data flow: data passed in a parameter to a virtual method called via a reference\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_81\r\n{\r\n\r\nclass CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_81_base\r\n{\r\npublic:\r\n /* pure virtual function */\r\n virtual void action(wchar_t * data) const = 0;\r\n};\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_81_bad : public CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_81_base\r\n{\r\npublic:\r\n void action(wchar_t * data) const;\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_81_goodG2B : public CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_81_base\r\n{\r\npublic:\r\n void action(wchar_t * data) const;\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_81_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE122_Heap_Based_Buffer_Overflow/s03/CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_81_bad.cpp","cwe_id":"CWE122","file_extension":".cpp","language_hint":"C++","source_size_bytes":1338,"source_sha256":"c82a67687a717e6571d92880f8fd2d511f873e5d8f783dfb566d648d594625bb","line_count":38,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_81_bad.cpp\r\nLabel Definition File: CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805.string.label.xml\r\nTemplate File: sources-sink-81_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 122 Heap Based Buffer Overflow\r\n * BadSource: Allocate using new[] and set data pointer to a small buffer\r\n * GoodSource: Allocate using new[] and set data pointer to a large buffer\r\n * Sinks: ncat\r\n * BadSink : Copy string to data using wcsncat\r\n * Flow Variant: 81 Data flow: data passed in a parameter to a virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_81.h\"\r\n\r\nnamespace CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_81\r\n{\r\n\r\nvoid CWE122_Heap_Based_Buffer_Overflow__cpp_CWE805_wchar_t_ncat_81_bad::action(wchar_t * data) const\r\n{\r\n {\r\n wchar_t source[100];\r\n wmemset(source, L'C', 100-1); /* fill with L'C's */\r\n source[100-1] = L'\\0'; /* null terminate */\r\n /* POTENTIAL FLAW: Possible buffer overflow if source is larger than sizeof(data)-strlen(data) */\r\n wcsncat(data, source, 100);\r\n printWLine(data);\r\n delete [] data;\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_file_vprintf_62a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_file_vprintf_62a.cpp","cwe_id":"CWE134","file_extension":".cpp","language_hint":"C++","source_size_bytes":3303,"source_sha256":"15ec7b9c36a985e5de1e90f3bb12e1207a36fd36945bae56f41f5f16d993be65","line_count":139,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_file_vprintf_62a.cpp\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-62a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: file Read input from a file\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: vprintf\r\n * GoodSink: vprintf with a format string\r\n * BadSink : vprintf without a format string\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__char_file_vprintf_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSource(char * &data);\r\n\r\nstatic void badVaSink(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vprintf(data, args);\r\n va_end(args);\r\n }\r\n}\r\n\r\nvoid bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n badSource(data);\r\n badVaSink(data, data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(char * &data);\r\n\r\nstatic void goodG2BVaSink(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vprintf(data, args);\r\n va_end(args);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n goodG2BSource(data);\r\n goodG2BVaSink(data, data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(char * &data);\r\n\r\nstatic void goodB2GVaSink(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n vprintf(\"%s\", args);\r\n va_end(args);\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n goodB2GSource(data);\r\n goodB2GVaSink(data, data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE134_Uncontrolled_Format_String__char_file_vprintf_62; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_file_vprintf_68a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_file_vprintf_68a.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":4816,"source_sha256":"878a273c0eedd854b222a3d48e180cc24aedb81e5df84e58dc6ba77913bd217f","line_count":152,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_file_vprintf_68a.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-68a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: file Read input from a file\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: vprintf\r\n * GoodSink: vprintf with a format string\r\n * BadSink : vprintf without a format string\r\n * Flow Variant: 68 Data flow: data passed as a global variable from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#define FILENAME \"C:\\\\temp\\\\file.txt\"\r\n#else\r\n#define FILENAME \"/tmp/file.txt\"\r\n#endif\r\n\r\nchar * CWE134_Uncontrolled_Format_String__char_file_vprintf_68_badData;\r\nchar * CWE134_Uncontrolled_Format_String__char_file_vprintf_68_goodG2BData;\r\nchar * CWE134_Uncontrolled_Format_String__char_file_vprintf_68_goodB2GData;\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE134_Uncontrolled_Format_String__char_file_vprintf_68b_badSink();\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_file_vprintf_68_bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n /* Read input from a file */\r\n size_t dataLen = strlen(data);\r\n FILE * pFile;\r\n /* if there is room in data, attempt to read the input from a file */\r\n if (100-dataLen > 1)\r\n {\r\n pFile = fopen(FILENAME, \"r\");\r\n if (pFile != NULL)\r\n {\r\n /* POTENTIAL FLAW: Read data from a file */\r\n if (fgets(data+dataLen, (int)(100-dataLen), pFile) == NULL)\r\n {\r\n printLine(\"fgets() failed\");\r\n /* Restore NUL terminator if fgets fails */\r\n data[dataLen] = '\\0';\r\n }\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n CWE134_Uncontrolled_Format_String__char_file_vprintf_68_badData = data;\r\n CWE134_Uncontrolled_Format_String__char_file_vprintf_68b_badSink();\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\nvoid CWE134_Uncontrolled_Format_String__char_file_vprintf_68b_goodG2BSink();\r\nvoid CWE134_Uncontrolled_Format_String__char_file_vprintf_68b_goodB2GSink();\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n CWE134_Uncontrolled_Format_String__char_file_vprintf_68_goodG2BData = data;\r\n CWE134_Uncontrolled_Format_String__char_file_vprintf_68b_goodG2BSink();\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n /* Read input from a file */\r\n size_t dataLen = strlen(data);\r\n FILE * pFile;\r\n /* if there is room in data, attempt to read the input from a file */\r\n if (100-dataLen > 1)\r\n {\r\n pFile = fopen(FILENAME, \"r\");\r\n if (pFile != NULL)\r\n {\r\n /* POTENTIAL FLAW: Read data from a file */\r\n if (fgets(data+dataLen, (int)(100-dataLen), pFile) == NULL)\r\n {\r\n printLine(\"fgets() failed\");\r\n /* Restore NUL terminator if fgets fails */\r\n data[dataLen] = '\\0';\r\n }\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n CWE134_Uncontrolled_Format_String__char_file_vprintf_68_goodB2GData = data;\r\n CWE134_Uncontrolled_Format_String__char_file_vprintf_68b_goodB2GSink();\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_file_vprintf_68_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_file_vprintf_68_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_file_vprintf_68_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_file_vprintf_81_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_file_vprintf_81_goodG2B.cpp","cwe_id":"CWE134","file_extension":".cpp","language_hint":"C++","source_size_bytes":1280,"source_sha256":"f8fc546d8521ee7db77b9a75431be88f5585365a9111b775a9e8f03a58297d6d","line_count":43,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_file_vprintf_81_goodG2B.cpp\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-81_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: file Read input from a file\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: vprintf\r\n * GoodSink: vprintf with a format string\r\n * BadSink : vprintf without a format string\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n#include \"CWE134_Uncontrolled_Format_String__char_file_vprintf_81.h\"\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__char_file_vprintf_81\r\n{\r\n\r\nstatic void goodG2BVaSink(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vprintf(data, args);\r\n va_end(args);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_file_vprintf_81_goodG2B::action(char * data) const\r\n{\r\n goodG2BVaSink(data, data);\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_file_vprintf_83a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_file_vprintf_83a.cpp","cwe_id":"CWE134","file_extension":".cpp","language_hint":"C++","source_size_bytes":2457,"source_sha256":"b704f73f3540162fb497f2c76e23c8ea36fbdae38c72aab495a5f81c6c903f8e","line_count":92,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_file_vprintf_83a.cpp\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-83a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: file Read input from a file\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: vprintf\r\n * GoodSink: vprintf with a format string\r\n * BadSink : vprintf without a format string\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE134_Uncontrolled_Format_String__char_file_vprintf_83.h\"\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__char_file_vprintf_83\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n CWE134_Uncontrolled_Format_String__char_file_vprintf_83_bad badObject(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n CWE134_Uncontrolled_Format_String__char_file_vprintf_83_goodG2B goodG2BObject(data);\r\n}\r\n\r\n/* goodG2B uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n CWE134_Uncontrolled_Format_String__char_file_vprintf_83_goodB2G goodB2GObject(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE134_Uncontrolled_Format_String__char_file_vprintf_83; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_01.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_01.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":4904,"source_sha256":"df06d8e1d635412193d0d633064f162c1fdfa3aa566ba8cac8fd6c5d4c740188","line_count":178,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_01.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-01.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: file Read input from a file\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: w32_vsnprintf\r\n * GoodSink: vsnprintf with a format string\r\n * BadSink : vsnprintf without a format string\r\n * Flow Variant: 01 Baseline\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#define FILENAME \"C:\\\\temp\\\\file.txt\"\r\n#else\r\n#define FILENAME \"/tmp/file.txt\"\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badVaSink(char * data, ...)\r\n{\r\n {\r\n char dest[100] = \"\";\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vsnprintf(dest, 100-1, data, args);\r\n va_end(args);\r\n printLine(dest);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_01_bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n /* Read input from a file */\r\n size_t dataLen = strlen(data);\r\n FILE * pFile;\r\n /* if there is room in data, attempt to read the input from a file */\r\n if (100-dataLen > 1)\r\n {\r\n pFile = fopen(FILENAME, \"r\");\r\n if (pFile != NULL)\r\n {\r\n /* POTENTIAL FLAW: Read data from a file */\r\n if (fgets(data+dataLen, (int)(100-dataLen), pFile) == NULL)\r\n {\r\n printLine(\"fgets() failed\");\r\n /* Restore NUL terminator if fgets fails */\r\n data[dataLen] = '\\0';\r\n }\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n badVaSink(data, data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2BVaSink(char * data, ...)\r\n{\r\n {\r\n char dest[100] = \"\";\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vsnprintf(dest, 100-1, data, args);\r\n va_end(args);\r\n printLine(dest);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n goodG2BVaSink(data, data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2GVaSink(char * data, ...)\r\n{\r\n {\r\n char dest[100] = \"\";\r\n va_list args;\r\n va_start(args, data);\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n vsnprintf(dest, 100-1, \"%s\", args);\r\n va_end(args);\r\n printLine(dest);\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n /* Read input from a file */\r\n size_t dataLen = strlen(data);\r\n FILE * pFile;\r\n /* if there is room in data, attempt to read the input from a file */\r\n if (100-dataLen > 1)\r\n {\r\n pFile = fopen(FILENAME, \"r\");\r\n if (pFile != NULL)\r\n {\r\n /* POTENTIAL FLAW: Read data from a file */\r\n if (fgets(data+dataLen, (int)(100-dataLen), pFile) == NULL)\r\n {\r\n printLine(\"fgets() failed\");\r\n /* Restore NUL terminator if fgets fails */\r\n data[dataLen] = '\\0';\r\n }\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n goodB2GVaSink(data, data);\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_01_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_01_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_01_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_31.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_31.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":5201,"source_sha256":"b4fa42101b9411ec66cd55ee72b4155a983fd1cf1ca83efc6f33f47f11588028","line_count":189,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_31.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-31.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: file Read input from a file\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: w32_vsnprintf\r\n * GoodSink: vsnprintf with a format string\r\n * BadSink : vsnprintf without a format string\r\n * Flow Variant: 31 Data flow using a copy of data within the same function\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#define FILENAME \"C:\\\\temp\\\\file.txt\"\r\n#else\r\n#define FILENAME \"/tmp/file.txt\"\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badVaSink(char * data, ...)\r\n{\r\n {\r\n char dest[100] = \"\";\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vsnprintf(dest, 100-1, data, args);\r\n va_end(args);\r\n printLine(dest);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_31_bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n /* Read input from a file */\r\n size_t dataLen = strlen(data);\r\n FILE * pFile;\r\n /* if there is room in data, attempt to read the input from a file */\r\n if (100-dataLen > 1)\r\n {\r\n pFile = fopen(FILENAME, \"r\");\r\n if (pFile != NULL)\r\n {\r\n /* POTENTIAL FLAW: Read data from a file */\r\n if (fgets(data+dataLen, (int)(100-dataLen), pFile) == NULL)\r\n {\r\n printLine(\"fgets() failed\");\r\n /* Restore NUL terminator if fgets fails */\r\n data[dataLen] = '\\0';\r\n }\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n {\r\n char * dataCopy = data;\r\n char * data = dataCopy;\r\n badVaSink(data, data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2BVaSink(char * data, ...)\r\n{\r\n {\r\n char dest[100] = \"\";\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vsnprintf(dest, 100-1, data, args);\r\n va_end(args);\r\n printLine(dest);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n {\r\n char * dataCopy = data;\r\n char * data = dataCopy;\r\n goodG2BVaSink(data, data);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2GVaSink(char * data, ...)\r\n{\r\n {\r\n char dest[100] = \"\";\r\n va_list args;\r\n va_start(args, data);\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n vsnprintf(dest, 100-1, \"%s\", args);\r\n va_end(args);\r\n printLine(dest);\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n /* Read input from a file */\r\n size_t dataLen = strlen(data);\r\n FILE * pFile;\r\n /* if there is room in data, attempt to read the input from a file */\r\n if (100-dataLen > 1)\r\n {\r\n pFile = fopen(FILENAME, \"r\");\r\n if (pFile != NULL)\r\n {\r\n /* POTENTIAL FLAW: Read data from a file */\r\n if (fgets(data+dataLen, (int)(100-dataLen), pFile) == NULL)\r\n {\r\n printLine(\"fgets() failed\");\r\n /* Restore NUL terminator if fgets fails */\r\n data[dataLen] = '\\0';\r\n }\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n {\r\n char * dataCopy = data;\r\n char * data = dataCopy;\r\n goodB2GVaSink(data, data);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_31_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_31_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_31_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53a.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":4496,"source_sha256":"bdc2a0236d7a0af73bb0b8b6f97f6ef470b238946981c98a4242be2427e8b7d4","line_count":146,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53a.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-53a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: file Read input from a file\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: w32_vsnprintf\r\n * GoodSink: vsnprintf with a format string\r\n * BadSink : vsnprintf without a format string\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#define FILENAME \"C:\\\\temp\\\\file.txt\"\r\n#else\r\n#define FILENAME \"/tmp/file.txt\"\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53b_badSink(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53_bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n /* Read input from a file */\r\n size_t dataLen = strlen(data);\r\n FILE * pFile;\r\n /* if there is room in data, attempt to read the input from a file */\r\n if (100-dataLen > 1)\r\n {\r\n pFile = fopen(FILENAME, \"r\");\r\n if (pFile != NULL)\r\n {\r\n /* POTENTIAL FLAW: Read data from a file */\r\n if (fgets(data+dataLen, (int)(100-dataLen), pFile) == NULL)\r\n {\r\n printLine(\"fgets() failed\");\r\n /* Restore NUL terminator if fgets fails */\r\n data[dataLen] = '\\0';\r\n }\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53b_goodG2BSink(char * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53b_goodB2GSink(char * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n /* Read input from a file */\r\n size_t dataLen = strlen(data);\r\n FILE * pFile;\r\n /* if there is room in data, attempt to read the input from a file */\r\n if (100-dataLen > 1)\r\n {\r\n pFile = fopen(FILENAME, \"r\");\r\n if (pFile != NULL)\r\n {\r\n /* POTENTIAL FLAW: Read data from a file */\r\n if (fgets(data+dataLen, (int)(100-dataLen), pFile) == NULL)\r\n {\r\n printLine(\"fgets() failed\");\r\n /* Restore NUL terminator if fgets fails */\r\n data[dataLen] = '\\0';\r\n }\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53c.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":1950,"source_sha256":"86513953f827fd176b7cb73fbb56aa430f634760d2b96f89f59dac456cd623b6","line_count":61,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53c.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-53c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: file Read input from a file\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: w32_vsnprintf\r\n * GoodSink: vsnprintf with a format string\r\n * BadSink : vsnprintf without a format string\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#define FILENAME \"C:\\\\temp\\\\file.txt\"\r\n#else\r\n#define FILENAME \"/tmp/file.txt\"\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53d_badSink(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53c_badSink(char * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53d_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53d_goodG2BSink(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53c_goodG2BSink(char * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53d_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53d_goodB2GSink(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53c_goodB2GSink(char * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_53d_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54a.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":4497,"source_sha256":"5c723d0c9a6e6207f5f2eba0ae3e4e29af66c6345bf5bcaa0083acc1caaa479d","line_count":146,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54a.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-54a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: file Read input from a file\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: w32_vsnprintf\r\n * GoodSink: vsnprintf with a format string\r\n * BadSink : vsnprintf without a format string\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#define FILENAME \"C:\\\\temp\\\\file.txt\"\r\n#else\r\n#define FILENAME \"/tmp/file.txt\"\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54b_badSink(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54_bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n /* Read input from a file */\r\n size_t dataLen = strlen(data);\r\n FILE * pFile;\r\n /* if there is room in data, attempt to read the input from a file */\r\n if (100-dataLen > 1)\r\n {\r\n pFile = fopen(FILENAME, \"r\");\r\n if (pFile != NULL)\r\n {\r\n /* POTENTIAL FLAW: Read data from a file */\r\n if (fgets(data+dataLen, (int)(100-dataLen), pFile) == NULL)\r\n {\r\n printLine(\"fgets() failed\");\r\n /* Restore NUL terminator if fgets fails */\r\n data[dataLen] = '\\0';\r\n }\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54b_goodG2BSink(char * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54b_goodB2GSink(char * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n /* Read input from a file */\r\n size_t dataLen = strlen(data);\r\n FILE * pFile;\r\n /* if there is room in data, attempt to read the input from a file */\r\n if (100-dataLen > 1)\r\n {\r\n pFile = fopen(FILENAME, \"r\");\r\n if (pFile != NULL)\r\n {\r\n /* POTENTIAL FLAW: Read data from a file */\r\n if (fgets(data+dataLen, (int)(100-dataLen), pFile) == NULL)\r\n {\r\n printLine(\"fgets() failed\");\r\n /* Restore NUL terminator if fgets fails */\r\n data[dataLen] = '\\0';\r\n }\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54b.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":1951,"source_sha256":"dc5c878e9aacf786e3b7ce29743c35c4854e651f5715a5bac4096be3edd8cdc5","line_count":61,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54b.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-54b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: file Read input from a file\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: w32_vsnprintf\r\n * GoodSink: vsnprintf with a format string\r\n * BadSink : vsnprintf without a format string\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#define FILENAME \"C:\\\\temp\\\\file.txt\"\r\n#else\r\n#define FILENAME \"/tmp/file.txt\"\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54c_badSink(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54b_badSink(char * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54c_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54c_goodG2BSink(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54b_goodG2BSink(char * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54c_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54c_goodB2GSink(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54b_goodB2GSink(char * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_54c_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_62b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_62b.cpp","cwe_id":"CWE134","file_extension":".cpp","language_hint":"C++","source_size_bytes":2796,"source_sha256":"e441defbd250e16c96aab44bf2f76236c3cd816dff7f6bbe1e05005b25c639bf","line_count":99,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_62b.cpp\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-62b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: file Read input from a file\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: w32_vsnprintf\r\n * GoodSink: vsnprintf with a format string\r\n * BadSink : vsnprintf without a format string\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#define FILENAME \"C:\\\\temp\\\\file.txt\"\r\n#else\r\n#define FILENAME \"/tmp/file.txt\"\r\n#endif\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__char_file_w32_vsnprintf_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSource(char * &data)\r\n{\r\n {\r\n /* Read input from a file */\r\n size_t dataLen = strlen(data);\r\n FILE * pFile;\r\n /* if there is room in data, attempt to read the input from a file */\r\n if (100-dataLen > 1)\r\n {\r\n pFile = fopen(FILENAME, \"r\");\r\n if (pFile != NULL)\r\n {\r\n /* POTENTIAL FLAW: Read data from a file */\r\n if (fgets(data+dataLen, (int)(100-dataLen), pFile) == NULL)\r\n {\r\n printLine(\"fgets() failed\");\r\n /* Restore NUL terminator if fgets fails */\r\n data[dataLen] = '\\0';\r\n }\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(char * &data)\r\n{\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(char * &data)\r\n{\r\n {\r\n /* Read input from a file */\r\n size_t dataLen = strlen(data);\r\n FILE * pFile;\r\n /* if there is room in data, attempt to read the input from a file */\r\n if (100-dataLen > 1)\r\n {\r\n pFile = fopen(FILENAME, \"r\");\r\n if (pFile != NULL)\r\n {\r\n /* POTENTIAL FLAW: Read data from a file */\r\n if (fgets(data+dataLen, (int)(100-dataLen), pFile) == NULL)\r\n {\r\n printLine(\"fgets() failed\");\r\n /* Restore NUL terminator if fgets fails */\r\n data[dataLen] = '\\0';\r\n }\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_53a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_53a.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":8349,"source_sha256":"cb6ad0a1761bf1b8d8f6e96c0b99b9476cdd3659a921585af338bd5c7c741688","line_count":276,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_53a.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-53a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: fprintf\r\n * GoodSink: fprintf with \"%s\" as the second argument and data as the third\r\n * BadSink : fprintf with data as the second argument\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_53b_badSink(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_53_bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_53b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_53b_goodG2BSink(char * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_53b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_53b_goodB2GSink(char * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_53b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_53_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_53_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_53_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_61a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_61a.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":3736,"source_sha256":"a3b13989123a566fc512c6ae7d751b6d165fbddc28b2c58dcb6a09d0d73d8fc3","line_count":122,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_61a.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-61a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: fprintf\r\n * GoodSink: fprintf with \"%s\" as the second argument and data as the third\r\n * BadSink : fprintf with data as the second argument\r\n * Flow Variant: 61 Data flow: data returned from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nchar * CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_61b_badSource(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_61_bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n data = CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_61b_badSource(data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n fprintf(stdout, data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nchar * CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_61b_goodG2BSource(char * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n data = CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_61b_goodG2BSource(data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n fprintf(stdout, data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nchar * CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_61b_goodB2GSource(char * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n data = CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_61b_goodB2GSource(data);\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n fprintf(stdout, \"%s\\n\", data);\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_61_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_61_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_61_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68a.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":8820,"source_sha256":"c3ca4e614b6ac28c59eeff2f35e9aec6a36e7d24d5f34378c08488fb67b3fff4","line_count":283,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68a.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-68a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: fprintf\r\n * GoodSink: fprintf with \"%s\" as the second argument and data as the third\r\n * BadSink : fprintf with data as the second argument\r\n * Flow Variant: 68 Data flow: data passed as a global variable from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\nchar * CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68_badData;\r\nchar * CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68_goodG2BData;\r\nchar * CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68_goodB2GData;\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68b_badSink();\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68_bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68_badData = data;\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68b_badSink();\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68b_goodG2BSink();\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68b_goodB2GSink();\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68_goodG2BData = data;\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68b_goodG2BSink();\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68_goodB2GData = data;\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68b_goodB2GSink();\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68b.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":2652,"source_sha256":"9e8c6d575c4c633bd1a1807f100a41daab72561a037f8b18914c03eedac3e4b7","line_count":78,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68b.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-68b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: fprintf\r\n * GoodSink: fprintf with \"%s\" as the second argument and data as the third\r\n * BadSink : fprintf with data as the second argument\r\n * Flow Variant: 68 Data flow: data passed as a global variable from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\nextern char * CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68_badData;\r\nextern char * CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68_goodG2BData;\r\nextern char * CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68_goodB2GData;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68b_badSink()\r\n{\r\n char * data = CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68_badData;\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n fprintf(stdout, data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68b_goodG2BSink()\r\n{\r\n char * data = CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68_goodG2BData;\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n fprintf(stdout, data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68b_goodB2GSink()\r\n{\r\n char * data = CWE134_Uncontrolled_Format_String__char_listen_socket_fprintf_68_goodB2GData;\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n fprintf(stdout, \"%s\\n\", data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_printf_16.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_printf_16.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":8945,"source_sha256":"cb1803097166743c5e3ca63fc7dc12936b4d737d02c2bf57f93c1356afab3a61","line_count":296,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_printf_16.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-16.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: printf\r\n * GoodSink: printf with \"%s\" as the first argument and data as the second\r\n * BadSink : printf with only data as an argument\r\n * Flow Variant: 16 Control flow: while(1)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_printf_16_bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n while(1)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n printf(data);\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the sinks in the second while statement */\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n while(1)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n printf(\"%s\\n\", data);\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the sources in the first while statement */\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n while(1)\r\n {\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n printf(data);\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_printf_16_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_printf_16_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_printf_16_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_printf_18.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_printf_18.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":8222,"source_sha256":"1c6133473e2bd38cc426a436818c2309466dc4a48ba3363ecd7fc85ffdf83d2e","line_count":284,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_printf_18.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-18.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: printf\r\n * GoodSink: printf with \"%s\" as the first argument and data as the second\r\n * BadSink : printf with only data as an argument\r\n * Flow Variant: 18 Control flow: goto statements\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_printf_18_bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n goto source;\r\nsource:\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n printf(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by reversing the blocks on the second goto statement */\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n goto source;\r\nsource:\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n goto sink;\r\nsink:\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n printf(\"%s\\n\", data);\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by reversing the blocks on the first goto statement */\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n goto source;\r\nsource:\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n printf(data);\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_printf_18_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_printf_18_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_printf_18_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_printf_54e.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_printf_54e.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":2123,"source_sha256":"d1de8488cbabb6c17c10d7207040101e7b107dea27202f07e26709d25a69e2c8","line_count":71,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_printf_54e.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-54e.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: printf\r\n * GoodSink: printf with \"%s\" as the first argument and data as the second\r\n * BadSink : printf with only data as an argument\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_printf_54e_badSink(char * data)\r\n{\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n printf(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_printf_54e_goodG2BSink(char * data)\r\n{\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n printf(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_printf_54e_goodB2GSink(char * data)\r\n{\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n printf(\"%s\\n\", data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_printf_66a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_printf_66a.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":8506,"source_sha256":"59735535bc351098f2ee4f0eecf07dcd7c51fad7a45a63275fb8366eda895738","line_count":283,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_printf_66a.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-66a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: printf\r\n * GoodSink: printf with \"%s\" as the first argument and data as the second\r\n * BadSink : printf with only data as an argument\r\n * Flow Variant: 66 Data flow: data passed in an array from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_printf_66b_badSink(char * dataArray[]);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_printf_66_bad()\r\n{\r\n char * data;\r\n char * dataArray[5];\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n /* put data in array */\r\n dataArray[2] = data;\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_printf_66b_badSink(dataArray);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_printf_66b_goodG2BSink(char * dataArray[]);\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char * dataArray[5];\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n dataArray[2] = data;\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_printf_66b_goodG2BSink(dataArray);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_printf_66b_goodB2GSink(char * dataArray[]);\r\n\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n char * dataArray[5];\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n dataArray[2] = data;\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_printf_66b_goodB2GSink(dataArray);\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_printf_66_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_printf_66_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_printf_66_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_printf_72b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_printf_72b.cpp","cwe_id":"CWE134","file_extension":".cpp","language_hint":"C++","source_size_bytes":1744,"source_sha256":"89aa0e431f2e60774657dfc293fcb9719d1ba712f55b6046ea9c56b91342cdcc","line_count":62,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_printf_72b.cpp\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-72b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: printf\r\n * GoodSink: printf with \"%s\" as the first argument and data as the second\r\n * BadSink : printf with only data as an argument\r\n * Flow Variant: 72 Data flow: data passed in a vector from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\nusing namespace std;\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__char_listen_socket_printf_72\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(vector dataVector)\r\n{\r\n /* copy data out of dataVector */\r\n char * data = dataVector[2];\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n printf(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(vector dataVector)\r\n{\r\n char * data = dataVector[2];\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n printf(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(vector dataVector)\r\n{\r\n char * data = dataVector[2];\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n printf(\"%s\\n\", data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_printf_83_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_printf_83_bad.cpp","cwe_id":"CWE134","file_extension":".cpp","language_hint":"C++","source_size_bytes":4285,"source_sha256":"432276879ebc15ccd8140614af14b3d0d07ae6d18697b4e7fda71a53cc224d67","line_count":135,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_printf_83_bad.cpp\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-83_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: printf\r\n * GoodSink: printf with \"%s\" as the first argument and data as the second\r\n * BadSink : printf with only data as an argument\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE134_Uncontrolled_Format_String__char_listen_socket_printf_83.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__char_listen_socket_printf_83\r\n{\r\nCWE134_Uncontrolled_Format_String__char_listen_socket_printf_83_bad::CWE134_Uncontrolled_Format_String__char_listen_socket_printf_83_bad(char * dataCopy)\r\n{\r\n data = dataCopy;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n}\r\n\r\nCWE134_Uncontrolled_Format_String__char_listen_socket_printf_83_bad::~CWE134_Uncontrolled_Format_String__char_listen_socket_printf_83_bad()\r\n{\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n printf(data);\r\n}\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_printf_83_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_printf_83_goodB2G.cpp","cwe_id":"CWE134","file_extension":".cpp","language_hint":"C++","source_size_bytes":4295,"source_sha256":"170b8f65ef20e79f1f1c934a09b3c633d176ecfd5f4baa918a4b4ee8651939de","line_count":135,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_printf_83_goodB2G.cpp\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-83_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: printf\r\n * GoodSink: printf with \"%s\" as the first argument and data as the second\r\n * BadSink : printf with only data as an argument\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE134_Uncontrolled_Format_String__char_listen_socket_printf_83.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__char_listen_socket_printf_83\r\n{\r\nCWE134_Uncontrolled_Format_String__char_listen_socket_printf_83_goodB2G::CWE134_Uncontrolled_Format_String__char_listen_socket_printf_83_goodB2G(char * dataCopy)\r\n{\r\n data = dataCopy;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n}\r\n\r\nCWE134_Uncontrolled_Format_String__char_listen_socket_printf_83_goodB2G::~CWE134_Uncontrolled_Format_String__char_listen_socket_printf_83_goodB2G()\r\n{\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n printf(\"%s\\n\", data);\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_08.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_08.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":13904,"source_sha256":"6ecb69aab8b3ac24b5e7b47da746686f8d6a5155179aead9ed42e547c60acd74","line_count":454,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_08.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-08.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: snprintf\r\n * GoodSink: snprintf with \"%s\" as the third argument and data as the fourth\r\n * BadSink : snprintf with data as the third argument\r\n * Flow Variant: 08 Control flow: if(staticReturnsTrue()) and if(staticReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifdef _WIN32\r\n#define SNPRINTF _snprintf\r\n#else\r\n#define SNPRINTF snprintf\r\n#endif\r\n\r\n/* The two function below always return the same value, so a tool\r\n should be able to identify that calls to the functions will always\r\n return a fixed value. */\r\nstatic int staticReturnsTrue()\r\n{\r\n return 1;\r\n}\r\n\r\nstatic int staticReturnsFalse()\r\n{\r\n return 0;\r\n}\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_08_bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(staticReturnsTrue())\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n {\r\n char dest[100] = \"\";\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n SNPRINTF(dest, 100-1, data);\r\n printLine(dest);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(staticReturnsTrue())\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n {\r\n char dest[100] = \"\";\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n SNPRINTF(dest, 100-1, \"%s\", data);\r\n printLine(dest);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(staticReturnsTrue())\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n {\r\n char dest[100] = \"\";\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n SNPRINTF(dest, 100-1, \"%s\", data);\r\n printLine(dest);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n {\r\n char dest[100] = \"\";\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n SNPRINTF(dest, 100-1, data);\r\n printLine(dest);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n {\r\n char dest[100] = \"\";\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n SNPRINTF(dest, 100-1, data);\r\n printLine(dest);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_08_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_08_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_08_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_12.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_12.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":13377,"source_sha256":"5c4e7b2c58a0b7845cb910fc7b4e3f8ff193d84613833a1a14bf5ff853589bb0","line_count":431,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_12.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-12.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: snprintf\r\n * GoodSink: snprintf with \"%s\" as the third argument and data as the fourth\r\n * BadSink : snprintf with data as the third argument\r\n * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifdef _WIN32\r\n#define SNPRINTF _snprintf\r\n#else\r\n#define SNPRINTF snprintf\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_12_bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n else\r\n {\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n char dest[100] = \"\";\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n SNPRINTF(dest, 100-1, data);\r\n printLine(dest);\r\n }\r\n }\r\n else\r\n {\r\n {\r\n char dest[100] = \"\";\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n SNPRINTF(dest, 100-1, \"%s\", data);\r\n printLine(dest);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the first \"if\" so that\r\n both branches use the BadSource and the second \"if\" so that both branches\r\n use the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n else\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n char dest[100] = \"\";\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n SNPRINTF(dest, 100-1, \"%s\", data);\r\n printLine(dest);\r\n }\r\n }\r\n else\r\n {\r\n {\r\n char dest[100] = \"\";\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n SNPRINTF(dest, 100-1, \"%s\", data);\r\n printLine(dest);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the first \"if\" so that\r\n both branches use the GoodSource and the second \"if\" so that both branches\r\n use the BadSink */\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n char dest[100] = \"\";\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n SNPRINTF(dest, 100-1, data);\r\n printLine(dest);\r\n }\r\n }\r\n else\r\n {\r\n {\r\n char dest[100] = \"\";\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n SNPRINTF(dest, 100-1, data);\r\n printLine(dest);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_12_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_12_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_12_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_54c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_54c.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":2482,"source_sha256":"b445662afae86ce5217d14f6a5db55288d2f505159942bc266cd81625e0205f5","line_count":81,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_54c.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-54c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: snprintf\r\n * GoodSink: snprintf with \"%s\" as the third argument and data as the fourth\r\n * BadSink : snprintf with data as the third argument\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifdef _WIN32\r\n#define SNPRINTF _snprintf\r\n#else\r\n#define SNPRINTF snprintf\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_54d_badSink(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_54c_badSink(char * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_54d_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_54d_goodG2BSink(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_54c_goodG2BSink(char * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_54d_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_54d_goodB2GSink(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_54c_goodB2GSink(char * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_54d_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_61a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_61a.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":4086,"source_sha256":"2040c2a5e0545b9baacb8cdef68f29a19db98af54ed156e39c223ca329a47bbc","line_count":140,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_61a.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-61a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: snprintf\r\n * GoodSink: snprintf with \"%s\" as the third argument and data as the fourth\r\n * BadSink : snprintf with data as the third argument\r\n * Flow Variant: 61 Data flow: data returned from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifdef _WIN32\r\n#define SNPRINTF _snprintf\r\n#else\r\n#define SNPRINTF snprintf\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nchar * CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_61b_badSource(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_61_bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n data = CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_61b_badSource(data);\r\n {\r\n char dest[100] = \"\";\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n SNPRINTF(dest, 100-1, data);\r\n printLine(dest);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nchar * CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_61b_goodG2BSource(char * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n data = CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_61b_goodG2BSource(data);\r\n {\r\n char dest[100] = \"\";\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n SNPRINTF(dest, 100-1, data);\r\n printLine(dest);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nchar * CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_61b_goodB2GSource(char * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n data = CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_61b_goodB2GSource(data);\r\n {\r\n char dest[100] = \"\";\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n SNPRINTF(dest, 100-1, \"%s\", data);\r\n printLine(dest);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_61_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_61_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_61_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_62b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_62b.cpp","cwe_id":"CWE134","file_extension":".cpp","language_hint":"C++","source_size_bytes":6647,"source_sha256":"53c0dddbf146a97a80747a418620d47658e981dce227dc68b1de60a66e8cad2c","line_count":230,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_62b.cpp\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-62b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: snprintf\r\n * GoodSink: snprintf with \"%s\" as the third argument and data as the fourth\r\n * BadSink : snprintf with data as the third argument\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSource(char * &data)\r\n{\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(char * &data)\r\n{\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(char * &data)\r\n{\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_64b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_64b.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":3035,"source_sha256":"d3a11b3ad93fb29f3f9e80f0c4477bfef2cd3b203664d933b5b775bc9b1921a9","line_count":101,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_64b.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-64b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: snprintf\r\n * GoodSink: snprintf with \"%s\" as the third argument and data as the fourth\r\n * BadSink : snprintf with data as the third argument\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifdef _WIN32\r\n#define SNPRINTF _snprintf\r\n#else\r\n#define SNPRINTF snprintf\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_64b_badSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n char * * dataPtr = (char * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n char * data = (*dataPtr);\r\n {\r\n char dest[100] = \"\";\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n SNPRINTF(dest, 100-1, data);\r\n printLine(dest);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_64b_goodG2BSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n char * * dataPtr = (char * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n char * data = (*dataPtr);\r\n {\r\n char dest[100] = \"\";\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n SNPRINTF(dest, 100-1, data);\r\n printLine(dest);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_64b_goodB2GSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n char * * dataPtr = (char * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n char * data = (*dataPtr);\r\n {\r\n char dest[100] = \"\";\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n SNPRINTF(dest, 100-1, \"%s\", data);\r\n printLine(dest);\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_65a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_65a.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":8643,"source_sha256":"732b8dacbf5ed1c9e3afcac58aa8c0a144f989018ffaddeeac54c640a320811d","line_count":287,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_65a.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-65a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: snprintf\r\n * GoodSink: snprintf with \"%s\" as the third argument and data as the fourth\r\n * BadSink : snprintf with data as the third argument\r\n * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifdef _WIN32\r\n#define SNPRINTF _snprintf\r\n#else\r\n#define SNPRINTF snprintf\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_65b_badSink(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_65_bad()\r\n{\r\n char * data;\r\n /* define a function pointer */\r\n void (*funcPtr) (char *) = CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_65b_badSink;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n /* use the function pointer */\r\n funcPtr(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_65b_goodG2BSink(char * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n void (*funcPtr) (char *) = CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_65b_goodG2BSink;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n funcPtr(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_65b_goodB2GSink(char * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n void (*funcPtr) (char *) = CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_65b_goodB2GSink;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n funcPtr(data);\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_65_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_65_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_65_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_81.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_81.h","cwe_id":"CWE134","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1755,"source_sha256":"9c4392d0fe33a3d890abe1f4f75bed230c1e8744e933b04021c7692bdaae1ea2","line_count":60,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_81.h\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-81.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: snprintf\r\n * GoodSink: snprintf with \"%s\" as the third argument and data as the fourth\r\n * BadSink : snprintf with data as the third argument\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_81\r\n{\r\n\r\nclass CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_81_base\r\n{\r\npublic:\r\n /* pure virtual function */\r\n virtual void action(char * data) const = 0;\r\n};\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_81_bad : public CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_81_base\r\n{\r\npublic:\r\n void action(char * data) const;\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_81_goodG2B : public CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_81_base\r\n{\r\npublic:\r\n void action(char * data) const;\r\n};\r\n\r\nclass CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_81_goodB2G : public CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_81_base\r\n{\r\npublic:\r\n void action(char * data) const;\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_81_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_81_goodB2G.cpp","cwe_id":"CWE134","file_extension":".cpp","language_hint":"C++","source_size_bytes":1337,"source_sha256":"c5d065e17464005debe7490d9b8bb58f2edde740ba31054f1b5eae402845f215","line_count":42,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_81_goodB2G.cpp\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-81_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: snprintf\r\n * GoodSink: snprintf with \"%s\" as the third argument and data as the fourth\r\n * BadSink : snprintf with data as the third argument\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_81.h\"\r\n\r\n#ifdef _WIN32\r\n#define SNPRINTF _snprintf\r\n#else\r\n#define SNPRINTF snprintf\r\n#endif\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_81\r\n{\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_snprintf_81_goodB2G::action(char * data) const\r\n{\r\n {\r\n char dest[100] = \"\";\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n SNPRINTF(dest, 100-1, \"%s\", data);\r\n printLine(dest);\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_31.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_31.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":8846,"source_sha256":"c4d07fe50e6a48f3a8e0ca14336aee408db51eb292d710eced0654990cc91127","line_count":314,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_31.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-31.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: vfprintf\r\n * GoodSink: vfprintf with a format string\r\n * BadSink : vfprintf without a format string\r\n * Flow Variant: 31 Data flow using a copy of data within the same function\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badVaSink(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vfprintf(stdout, data, args);\r\n va_end(args);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_31_bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n {\r\n char * dataCopy = data;\r\n char * data = dataCopy;\r\n badVaSink(data, data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2BVaSink(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vfprintf(stdout, data, args);\r\n va_end(args);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n {\r\n char * dataCopy = data;\r\n char * data = dataCopy;\r\n goodG2BVaSink(data, data);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2GVaSink(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n vfprintf(stdout, \"%s\", args);\r\n va_end(args);\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n {\r\n char * dataCopy = data;\r\n char * data = dataCopy;\r\n goodB2GVaSink(data, data);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_31_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_31_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_31_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_32.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_32.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":9769,"source_sha256":"16494dba25a83047fe298b54d16dc85326cc1e9f9aff914801f523781dd3b257","line_count":329,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_32.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-32.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: vfprintf\r\n * GoodSink: vfprintf with a format string\r\n * BadSink : vfprintf without a format string\r\n * Flow Variant: 32 Data flow using two pointers to the same value within the same function\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badVaSink(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vfprintf(stdout, data, args);\r\n va_end(args);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_32_bad()\r\n{\r\n char * data;\r\n char * *dataPtr1 = &data;\r\n char * *dataPtr2 = &data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n char * data = *dataPtr1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n *dataPtr1 = data;\r\n }\r\n {\r\n char * data = *dataPtr2;\r\n badVaSink(data, data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2BVaSink(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vfprintf(stdout, data, args);\r\n va_end(args);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char * *dataPtr1 = &data;\r\n char * *dataPtr2 = &data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n char * data = *dataPtr1;\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n *dataPtr1 = data;\r\n }\r\n {\r\n char * data = *dataPtr2;\r\n goodG2BVaSink(data, data);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2GVaSink(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n vfprintf(stdout, \"%s\", args);\r\n va_end(args);\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n char * *dataPtr1 = &data;\r\n char * *dataPtr2 = &data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n char * data = *dataPtr1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n *dataPtr1 = data;\r\n }\r\n {\r\n char * data = *dataPtr2;\r\n goodB2GVaSink(data, data);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_32_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_32_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_32_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_51a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_51a.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":8307,"source_sha256":"49dc352a1de7c0d3b08223efae25aa0fd76e5c903938b506c3dbc0b6a0159b16","line_count":277,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_51a.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-51a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: vfprintf\r\n * GoodSink: vfprintf with a format string\r\n * BadSink : vfprintf without a format string\r\n * Flow Variant: 51 Data flow: data passed as an argument from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_51b_badSink(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_51_bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_51b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_51b_goodG2BSink(char * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_51b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_51b_goodB2GSink(char * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_51b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_51_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_51_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_51_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_53c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_53c.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":2383,"source_sha256":"65fb08047c56dc86f37b7d7eeb881ab7286f27fb6a4a82d3b80f138c329382b3","line_count":76,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_53c.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-53c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: vfprintf\r\n * GoodSink: vfprintf with a format string\r\n * BadSink : vfprintf without a format string\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_53d_badSink(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_53c_badSink(char * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_53d_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_53d_goodG2BSink(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_53c_goodG2BSink(char * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_53d_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_53d_goodB2GSink(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_53c_goodB2GSink(char * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_53d_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_53d.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_53d.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":2711,"source_sha256":"2da6050bedecdd599bda80c3ad8036baa5544508fcf7cb0b2bd4503bf6d8e780","line_count":102,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_53d.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-53d.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: vfprintf\r\n * GoodSink: vfprintf with a format string\r\n * BadSink : vfprintf without a format string\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badVaSink(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vfprintf(stdout, data, args);\r\n va_end(args);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_53d_badSink(char * data)\r\n{\r\n badVaSink(data, data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2BVaSink(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vfprintf(stdout, data, args);\r\n va_end(args);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_53d_goodG2BSink(char * data)\r\n{\r\n goodG2BVaSink(data, data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2GVaSink(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n vfprintf(stdout, \"%s\", args);\r\n va_end(args);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_53d_goodB2GSink(char * data)\r\n{\r\n goodB2GVaSink(data, data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54a.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":8352,"source_sha256":"8e60a6060557b5c2535d232a4561a4280843ffdb43a6ba0e47ab48a8d8c08a32","line_count":277,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54a.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-54a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: vfprintf\r\n * GoodSink: vfprintf with a format string\r\n * BadSink : vfprintf without a format string\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54b_badSink(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54_bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54b_goodG2BSink(char * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54b_goodB2GSink(char * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54d.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54d.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":2384,"source_sha256":"fdbec48dfb849280bf8109c349283358b9d329050fd9477771289d74ffcaf30e","line_count":76,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54d.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-54d.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: vfprintf\r\n * GoodSink: vfprintf with a format string\r\n * BadSink : vfprintf without a format string\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54e_badSink(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54d_badSink(char * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54e_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54e_goodG2BSink(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54d_goodG2BSink(char * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54e_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54e_goodB2GSink(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54d_goodB2GSink(char * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_54e_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_82.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_82.h","cwe_id":"CWE134","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1697,"source_sha256":"85e47de23b8cd54141490a9f3d37bc76ec1fbf5e1b19957440f8e3dfac644a61","line_count":60,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_82.h\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-82.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: vfprintf\r\n * GoodSink: vfprintf with a format string\r\n * BadSink : vfprintf without a format string\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_82\r\n{\r\n\r\nclass CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_82_base\r\n{\r\npublic:\r\n /* pure virtual function */\r\n virtual void action(char * data) = 0;\r\n};\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_82_bad : public CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_82_base\r\n{\r\npublic:\r\n void action(char * data);\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_82_goodG2B : public CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_82_base\r\n{\r\npublic:\r\n void action(char * data);\r\n};\r\n\r\nclass CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_82_goodB2G : public CWE134_Uncontrolled_Format_String__char_listen_socket_vfprintf_82_base\r\n{\r\npublic:\r\n void action(char * data);\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_07.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_07.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":14030,"source_sha256":"04aedbc62e6bc1e5fdecd169a70c0d4eb20a99c8b99a29159fa458d555548373","line_count":471,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_07.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-07.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: vprintf\r\n * GoodSink: vprintf with a format string\r\n * BadSink : vprintf without a format string\r\n * Flow Variant: 07 Control flow: if(staticFive==5) and if(staticFive!=5)\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n/* The variable below is not declared \"const\", but is never assigned\r\n any other value so a tool should be able to identify that reads of\r\n this will always give its initialized value. */\r\nstatic int staticFive = 5;\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badVaSinkB(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vprintf(data, args);\r\n va_end(args);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_07_bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(staticFive==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(staticFive==5)\r\n {\r\n badVaSinkB(data, data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nstatic void goodB2G1VaSinkG(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n vprintf(\"%s\", args);\r\n va_end(args);\r\n }\r\n}\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticFive==5 to staticFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(staticFive==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n goodB2G1VaSinkG(data, data);\r\n }\r\n}\r\n\r\nstatic void goodB2G2VaSinkG(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n vprintf(\"%s\", args);\r\n va_end(args);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(staticFive==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(staticFive==5)\r\n {\r\n goodB2G2VaSinkG(data, data);\r\n }\r\n}\r\n\r\nstatic void goodG2B1VaSinkB(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vprintf(data, args);\r\n va_end(args);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticFive==5 to staticFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n }\r\n if(staticFive==5)\r\n {\r\n goodG2B1VaSinkB(data, data);\r\n }\r\n}\r\n\r\nstatic void goodG2B2VaSinkB(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vprintf(data, args);\r\n va_end(args);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(staticFive==5)\r\n {\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n }\r\n if(staticFive==5)\r\n {\r\n goodG2B2VaSinkB(data, data);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_07_good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n goodB2G1();\r\n goodB2G2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_07_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_07_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_09.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_09.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":13876,"source_sha256":"27db48fe9fb88d8a0ca7c414f4b21d29a6e428b0cc42dcfcbe492553377afa11","line_count":466,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_09.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-09.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: vprintf\r\n * GoodSink: vprintf with a format string\r\n * BadSink : vprintf without a format string\r\n * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badVaSinkB(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vprintf(data, args);\r\n va_end(args);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_09_bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n badVaSinkB(data, data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nstatic void goodB2G1VaSinkG(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n vprintf(\"%s\", args);\r\n va_end(args);\r\n }\r\n}\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n goodB2G1VaSinkG(data, data);\r\n }\r\n}\r\n\r\nstatic void goodB2G2VaSinkG(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n vprintf(\"%s\", args);\r\n va_end(args);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n goodB2G2VaSinkG(data, data);\r\n }\r\n}\r\n\r\nstatic void goodG2B1VaSinkB(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vprintf(data, args);\r\n va_end(args);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n goodG2B1VaSinkB(data, data);\r\n }\r\n}\r\n\r\nstatic void goodG2B2VaSinkB(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vprintf(data, args);\r\n va_end(args);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n goodG2B2VaSinkB(data, data);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_09_good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n goodB2G1();\r\n goodB2G2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_09_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_09_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_22a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_22a.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":12158,"source_sha256":"6d25b3760813dbffa471dbdafd4811df2cb9bd86a35b7115ddd142e56d6194f0","line_count":379,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_22a.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-22a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: vprintf\r\n * GoodSink: vprintf with a format string\r\n * BadSink : vprintf without a format string\r\n * Flow Variant: 22 Control flow: Flow controlled by value of a global variable. Sink functions are in a separate file from sources.\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifndef OMITBAD\r\n\r\n/* The global variable below is used to drive control flow in the sink function */\r\nint CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_22_badGlobal = 0;\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_22_badVaSink(char * data, ...);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_22_bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_22_badGlobal = 1; /* true */\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_22_badVaSink(data, data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* The global variables below are used to drive control flow in the sink functions. */\r\nint CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_22_goodB2G1Global = 0;\r\nint CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_22_goodB2G2Global = 0;\r\nint CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_22_goodG2BGlobal = 0;\r\n\r\n/* goodB2G1() - use badsource and goodsink by setting the static variable to false instead of true */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_22_goodB2G1_vasink(char * data, ...);\r\n\r\nstatic void goodB2G1()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_22_goodB2G1Global = 0; /* false */\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_22_goodB2G1_vasink(data, data);\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_22_goodB2G2_vasink(char * data, ...);\r\n\r\nstatic void goodB2G2()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_22_goodB2G2Global = 1; /* true */\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_22_goodB2G2_vasink(data, data);\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_22_goodG2BVaSink(char * data, ...);\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_22_goodG2BGlobal = 1; /* true */\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_22_goodG2BVaSink(data, data);\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_22_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_22_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_22_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_51b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_51b.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":2633,"source_sha256":"27e4ab44c30f8c91e5b6c881ee89a1ff4eaa58b782b5074052528957c5ab9e02","line_count":102,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_51b.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-51b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: vprintf\r\n * GoodSink: vprintf with a format string\r\n * BadSink : vprintf without a format string\r\n * Flow Variant: 51 Data flow: data passed as an argument from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badVaSink(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vprintf(data, args);\r\n va_end(args);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_51b_badSink(char * data)\r\n{\r\n badVaSink(data, data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2BVaSink(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vprintf(data, args);\r\n va_end(args);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_51b_goodG2BSink(char * data)\r\n{\r\n goodG2BVaSink(data, data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2GVaSink(char * data, ...)\r\n{\r\n {\r\n va_list args;\r\n va_start(args, data);\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n vprintf(\"%s\", args);\r\n va_end(args);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_51b_goodB2GSink(char * data)\r\n{\r\n goodB2GVaSink(data, data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_54c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_54c.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":2371,"source_sha256":"6fe3aeb1a5d48d075410e22b23b353d33a2f9ece81a2cc7e2a90689ee12b12cc","line_count":76,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_54c.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-54c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: vprintf\r\n * GoodSink: vprintf with a format string\r\n * BadSink : vprintf without a format string\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_54d_badSink(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_54c_badSink(char * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_54d_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_54d_goodG2BSink(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_54c_goodG2BSink(char * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_54d_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_54d_goodB2GSink(char * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_54c_goodB2GSink(char * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_54d_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_61b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_61b.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":6755,"source_sha256":"8935e56b35770e1a3b1e54524c1dbc08742460bbbb25518c251ffee985f06f45","line_count":229,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_61b.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-61b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: vprintf\r\n * GoodSink: vprintf with a format string\r\n * BadSink : vprintf without a format string\r\n * Flow Variant: 61 Data flow: data returned from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifndef OMITBAD\r\n\r\nchar * CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_61b_badSource(char * data)\r\n{\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n return data;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nchar * CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_61b_goodG2BSource(char * data)\r\n{\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n return data;\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nchar * CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_61b_goodB2GSource(char * data)\r\n{\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n return data;\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84.h","cwe_id":"CWE134","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1847,"source_sha256":"b51ddb10e11ad5713c0db76eae3cc7675d8ed96e8e7efcf850f49cc172db6e85","line_count":65,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84.h\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-84.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: vprintf\r\n * GoodSink: vprintf with a format string\r\n * BadSink : vprintf without a format string\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84_bad\r\n{\r\npublic:\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84_bad(char * dataCopy);\r\n ~CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84_bad();\r\n\r\nprivate:\r\n char * data;\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84_goodG2B\r\n{\r\npublic:\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84_goodG2B(char * dataCopy);\r\n ~CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84_goodG2B();\r\n\r\nprivate:\r\n char * data;\r\n};\r\n\r\nclass CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84_goodB2G\r\n{\r\npublic:\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84_goodB2G(char * dataCopy);\r\n ~CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84_goodB2G();\r\n\r\nprivate:\r\n char * data;\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84a.cpp","cwe_id":"CWE134","file_extension":".cpp","language_hint":"C++","source_size_bytes":2893,"source_sha256":"0c7df8b3c38447de0014680e1b94a8100560cdbfffa157c29fab01afadfd0642","line_count":95,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84a.cpp\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-84a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: vprintf\r\n * GoodSink: vprintf with a format string\r\n * BadSink : vprintf without a format string\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84.h\"\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84_bad * badObject = new CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84_bad(data);\r\n delete badObject;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84_goodG2B * goodG2BObject = new CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84_goodG2B(data);\r\n delete goodG2BObject;\r\n}\r\n\r\n/* goodG2B uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84_goodB2G * goodB2GObject = new CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84_goodB2G(data);\r\n delete goodB2GObject;\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE134_Uncontrolled_Format_String__char_listen_socket_vprintf_84; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_07.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_07.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":14425,"source_sha256":"18be5668a7a6f833328eefc4423220e84d380e83d4ddcf8855928a0744b6b2ff","line_count":481,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_07.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-07.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: w32_vsnprintf\r\n * GoodSink: vsnprintf with a format string\r\n * BadSink : vsnprintf without a format string\r\n * Flow Variant: 07 Control flow: if(staticFive==5) and if(staticFive!=5)\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n/* The variable below is not declared \"const\", but is never assigned\r\n any other value so a tool should be able to identify that reads of\r\n this will always give its initialized value. */\r\nstatic int staticFive = 5;\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badVaSinkB(char * data, ...)\r\n{\r\n {\r\n char dest[100] = \"\";\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vsnprintf(dest, 100-1, data, args);\r\n va_end(args);\r\n printLine(dest);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_07_bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(staticFive==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(staticFive==5)\r\n {\r\n badVaSinkB(data, data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nstatic void goodB2G1VaSinkG(char * data, ...)\r\n{\r\n {\r\n char dest[100] = \"\";\r\n va_list args;\r\n va_start(args, data);\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n vsnprintf(dest, 100-1, \"%s\", args);\r\n va_end(args);\r\n printLine(dest);\r\n }\r\n}\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticFive==5 to staticFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(staticFive==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n goodB2G1VaSinkG(data, data);\r\n }\r\n}\r\n\r\nstatic void goodB2G2VaSinkG(char * data, ...)\r\n{\r\n {\r\n char dest[100] = \"\";\r\n va_list args;\r\n va_start(args, data);\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n vsnprintf(dest, 100-1, \"%s\", args);\r\n va_end(args);\r\n printLine(dest);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(staticFive==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(staticFive==5)\r\n {\r\n goodB2G2VaSinkG(data, data);\r\n }\r\n}\r\n\r\nstatic void goodG2B1VaSinkB(char * data, ...)\r\n{\r\n {\r\n char dest[100] = \"\";\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vsnprintf(dest, 100-1, data, args);\r\n va_end(args);\r\n printLine(dest);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticFive==5 to staticFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n }\r\n if(staticFive==5)\r\n {\r\n goodG2B1VaSinkB(data, data);\r\n }\r\n}\r\n\r\nstatic void goodG2B2VaSinkB(char * data, ...)\r\n{\r\n {\r\n char dest[100] = \"\";\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vsnprintf(dest, 100-1, data, args);\r\n va_end(args);\r\n printLine(dest);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(staticFive==5)\r\n {\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n }\r\n if(staticFive==5)\r\n {\r\n goodG2B2VaSinkB(data, data);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_07_good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n goodB2G1();\r\n goodB2G2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_07_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_07_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_14.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_14.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":14202,"source_sha256":"07c2e8d702a5fbfe67590711c30e422edeee54423d289bee7dd71279647c5fe9","line_count":476,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_14.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-14.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: w32_vsnprintf\r\n * GoodSink: vsnprintf with a format string\r\n * BadSink : vsnprintf without a format string\r\n * Flow Variant: 14 Control flow: if(globalFive==5) and if(globalFive!=5)\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badVaSinkB(char * data, ...)\r\n{\r\n {\r\n char dest[100] = \"\";\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vsnprintf(dest, 100-1, data, args);\r\n va_end(args);\r\n printLine(dest);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_14_bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(globalFive==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalFive==5)\r\n {\r\n badVaSinkB(data, data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nstatic void goodB2G1VaSinkG(char * data, ...)\r\n{\r\n {\r\n char dest[100] = \"\";\r\n va_list args;\r\n va_start(args, data);\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n vsnprintf(dest, 100-1, \"%s\", args);\r\n va_end(args);\r\n printLine(dest);\r\n }\r\n}\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalFive==5 to globalFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(globalFive==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n goodB2G1VaSinkG(data, data);\r\n }\r\n}\r\n\r\nstatic void goodB2G2VaSinkG(char * data, ...)\r\n{\r\n {\r\n char dest[100] = \"\";\r\n va_list args;\r\n va_start(args, data);\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n vsnprintf(dest, 100-1, \"%s\", args);\r\n va_end(args);\r\n printLine(dest);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(globalFive==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalFive==5)\r\n {\r\n goodB2G2VaSinkG(data, data);\r\n }\r\n}\r\n\r\nstatic void goodG2B1VaSinkB(char * data, ...)\r\n{\r\n {\r\n char dest[100] = \"\";\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vsnprintf(dest, 100-1, data, args);\r\n va_end(args);\r\n printLine(dest);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalFive==5 to globalFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n }\r\n if(globalFive==5)\r\n {\r\n goodG2B1VaSinkB(data, data);\r\n }\r\n}\r\n\r\nstatic void goodG2B2VaSinkB(char * data, ...)\r\n{\r\n {\r\n char dest[100] = \"\";\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vsnprintf(dest, 100-1, data, args);\r\n va_end(args);\r\n printLine(dest);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n if(globalFive==5)\r\n {\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n }\r\n if(globalFive==5)\r\n {\r\n goodG2B2VaSinkB(data, data);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_14_good()\r\n{\r\n goodG2B1();\r\n goodG2B2();\r\n goodB2G1();\r\n goodB2G2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_14_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_14_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_18.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_18.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":9068,"source_sha256":"4bc30b7359fe001685f805d0e6d64d2c58a2fcdc184f41ef242ec6befb59b28d","line_count":321,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_18.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-18.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: w32_vsnprintf\r\n * GoodSink: vsnprintf with a format string\r\n * BadSink : vsnprintf without a format string\r\n * Flow Variant: 18 Control flow: goto statements\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badVaSinkB(char * data, ...)\r\n{\r\n {\r\n char dest[100] = \"\";\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vsnprintf(dest, 100-1, data, args);\r\n va_end(args);\r\n printLine(dest);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_18_bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n goto source;\r\nsource:\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n goto sink;\r\nsink:\r\n badVaSinkB(data, data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nstatic void goodB2GVaSinkG(char * data, ...)\r\n{\r\n {\r\n char dest[100] = \"\";\r\n va_list args;\r\n va_start(args, data);\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n vsnprintf(dest, 100-1, \"%s\", args);\r\n va_end(args);\r\n printLine(dest);\r\n }\r\n}\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the conditions on the third and fourth while statements */\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n goto source;\r\nsource:\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n goto sink;\r\nsink:\r\n goodB2GVaSinkG(data, data);\r\n}\r\n\r\nstatic void goodG2BVaSinkB(char * data, ...)\r\n{\r\n {\r\n char dest[100] = \"\";\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vsnprintf(dest, 100-1, data, args);\r\n va_end(args);\r\n printLine(dest);\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the conditions on the first and second while statements */\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n goto source;\r\nsource:\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n goto sink;\r\nsink:\r\n goodG2BVaSinkB(data, data);\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_18_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_18_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_18_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_22a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_22a.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":12294,"source_sha256":"88205e3be0827f2d87a95fff328c92a25a0ef63545f0c36c42b2fb57453c77ff","line_count":379,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_22a.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-22a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: w32_vsnprintf\r\n * GoodSink: vsnprintf with a format string\r\n * BadSink : vsnprintf without a format string\r\n * Flow Variant: 22 Control flow: Flow controlled by value of a global variable. Sink functions are in a separate file from sources.\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifndef OMITBAD\r\n\r\n/* The global variable below is used to drive control flow in the sink function */\r\nint CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_22_badGlobal = 0;\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_22_badVaSink(char * data, ...);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_22_bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_22_badGlobal = 1; /* true */\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_22_badVaSink(data, data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* The global variables below are used to drive control flow in the sink functions. */\r\nint CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_22_goodB2G1Global = 0;\r\nint CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_22_goodB2G2Global = 0;\r\nint CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_22_goodG2BGlobal = 0;\r\n\r\n/* goodB2G1() - use badsource and goodsink by setting the static variable to false instead of true */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_22_goodB2G1_vasink(char * data, ...);\r\n\r\nstatic void goodB2G1()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_22_goodB2G1Global = 0; /* false */\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_22_goodB2G1_vasink(data, data);\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_22_goodB2G2_vasink(char * data, ...);\r\n\r\nstatic void goodB2G2()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_22_goodB2G2Global = 1; /* true */\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_22_goodB2G2_vasink(data, data);\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_22_goodG2BVaSink(char * data, ...);\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_22_goodG2BGlobal = 1; /* true */\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_22_goodG2BVaSink(data, data);\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_22_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_22_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_22_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_53d.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_53d.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":2924,"source_sha256":"14ee9e831584b4e904b1d959fc93e105b8714380c1473b90786f2fd3ccc83447","line_count":108,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_53d.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-53d.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: w32_vsnprintf\r\n * GoodSink: vsnprintf with a format string\r\n * BadSink : vsnprintf without a format string\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badVaSink(char * data, ...)\r\n{\r\n {\r\n char dest[100] = \"\";\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vsnprintf(dest, 100-1, data, args);\r\n va_end(args);\r\n printLine(dest);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_53d_badSink(char * data)\r\n{\r\n badVaSink(data, data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2BVaSink(char * data, ...)\r\n{\r\n {\r\n char dest[100] = \"\";\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vsnprintf(dest, 100-1, data, args);\r\n va_end(args);\r\n printLine(dest);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_53d_goodG2BSink(char * data)\r\n{\r\n goodG2BVaSink(data, data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2GVaSink(char * data, ...)\r\n{\r\n {\r\n char dest[100] = \"\";\r\n va_list args;\r\n va_start(args, data);\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n vsnprintf(dest, 100-1, \"%s\", args);\r\n va_end(args);\r\n printLine(dest);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_53d_goodB2GSink(char * data)\r\n{\r\n goodB2GVaSink(data, data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_63a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_63a.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":8377,"source_sha256":"9579b8eb4650b3258be87920b63eb769a6d6405df3544eaf1a21d2d5151fddc9","line_count":277,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_63a.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-63a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: w32_vsnprintf\r\n * GoodSink: vsnprintf with a format string\r\n * BadSink : vsnprintf without a format string\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_63b_badSink(char * * dataPtr);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_63_bad()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_63b_badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_63b_goodG2BSink(char * * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n strcpy(data, \"fixedstringtest\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_63b_goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_63b_goodB2GSink(char * * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n char dataBuffer[100] = \"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n char *replace;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n size_t dataLen = strlen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, (char *)(data + dataLen), sizeof(char) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(char)] = '\\0';\r\n /* Eliminate CRLF */\r\n replace = strchr(data, '\\r');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n replace = strchr(data, '\\n');\r\n if (replace)\r\n {\r\n *replace = '\\0';\r\n }\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_63b_goodB2GSink(&data);\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_63_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_63_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_63_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_82_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_82_bad.cpp","cwe_id":"CWE134","file_extension":".cpp","language_hint":"C++","source_size_bytes":1423,"source_sha256":"2fab94e552ab1862a4da2022f6472e7ab832de21597d60465c5427d16c6ecf11","line_count":45,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_82_bad.cpp\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.vasinks.label.xml\r\nTemplate File: sources-vasinks-82_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: w32_vsnprintf\r\n * GoodSink: vsnprintf with a format string\r\n * BadSink : vsnprintf without a format string\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \r\n#include \"std_testcase.h\"\r\n#include \"CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_82.h\"\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_82\r\n{\r\n\r\nstatic void badVaSink(char * data, ...)\r\n{\r\n {\r\n char dest[100] = \"\";\r\n va_list args;\r\n va_start(args, data);\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n vsnprintf(dest, 100-1, data, args);\r\n va_end(args);\r\n printLine(dest);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__char_listen_socket_w32_vsnprintf_82_bad::action(char * data)\r\n{\r\n badVaSink(data, data);\r\n}\r\n\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_01.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_01.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":7591,"source_sha256":"b71e2ec0c22ebc6414617a87320a63be7da4be9a1bd13a2d9b3ed2a63286288f","line_count":248,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_01.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-01.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: fprintf\r\n * GoodSink: fwprintf with \"%s\" as the second argument and data as the third\r\n * BadSink : fwprintf with data as the second argument\r\n * Flow Variant: 01 Baseline\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_01_bad()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n fwprintf(stdout, data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n wcscpy(data, L\"fixedstringtest\");\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n fwprintf(stdout, data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n fwprintf(stdout, L\"%s\\n\", data);\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_01_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_01_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_01_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_13.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_13.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":12277,"source_sha256":"14dd3e4136ac66ce648cb4c79217b435fe93278034a1aed8cbb3276816efc466","line_count":379,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_13.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-13.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: fprintf\r\n * GoodSink: fwprintf with \"%s\" as the second argument and data as the third\r\n * BadSink : fwprintf with data as the second argument\r\n * Flow Variant: 13 Control flow: if(GLOBAL_CONST_FIVE==5) and if(GLOBAL_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_13_bad()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n fwprintf(stdout, data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_FIVE==5 to GLOBAL_CONST_FIVE!=5 */\r\nstatic void goodB2G1()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(GLOBAL_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n fwprintf(stdout, L\"%s\\n\", data);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n fwprintf(stdout, L\"%s\\n\", data);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_FIVE==5 to GLOBAL_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n if(GLOBAL_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n wcscpy(data, L\"fixedstringtest\");\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n fwprintf(stdout, data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n wcscpy(data, L\"fixedstringtest\");\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n fwprintf(stdout, data);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_13_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_13_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_13_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_14.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_14.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":12165,"source_sha256":"e689c72db0a44b9d69ee879d9329a478ae22e50d8cbbb4d11e2f4b5a96ac6dbb","line_count":379,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_14.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-14.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: fprintf\r\n * GoodSink: fwprintf with \"%s\" as the second argument and data as the third\r\n * BadSink : fwprintf with data as the second argument\r\n * Flow Variant: 14 Control flow: if(globalFive==5) and if(globalFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_14_bad()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n if(globalFive==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n fwprintf(stdout, data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalFive==5 to globalFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n if(globalFive==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n fwprintf(stdout, L\"%s\\n\", data);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n if(globalFive==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalFive==5)\r\n {\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n fwprintf(stdout, L\"%s\\n\", data);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalFive==5 to globalFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n wcscpy(data, L\"fixedstringtest\");\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n fwprintf(stdout, data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n if(globalFive==5)\r\n {\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n wcscpy(data, L\"fixedstringtest\");\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n fwprintf(stdout, data);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_14_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_14_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_14_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_63b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_63b.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":2278,"source_sha256":"235d571eaf3fafc54a45cfcec7472fc1dae2f87eab6743086dccd6adcb0bb94c","line_count":74,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_63b.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-63b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: fprintf\r\n * GoodSink: fwprintf with \"%s\" as the second argument and data as the third\r\n * BadSink : fwprintf with data as the second argument\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_63b_badSink(wchar_t * * dataPtr)\r\n{\r\n wchar_t * data = *dataPtr;\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n fwprintf(stdout, data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_63b_goodG2BSink(wchar_t * * dataPtr)\r\n{\r\n wchar_t * data = *dataPtr;\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n fwprintf(stdout, data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_63b_goodB2GSink(wchar_t * * dataPtr)\r\n{\r\n wchar_t * data = *dataPtr;\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n fwprintf(stdout, L\"%s\\n\", data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_72b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_72b.cpp","cwe_id":"CWE134","file_extension":".cpp","language_hint":"C++","source_size_bytes":1813,"source_sha256":"33a445a7736c64ad8b3696f65a609b0189de0a8fdd77c93ae421e9b87642dab2","line_count":62,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_72b.cpp\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-72b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: fprintf\r\n * GoodSink: fwprintf with \"%s\" as the second argument and data as the third\r\n * BadSink : fwprintf with data as the second argument\r\n * Flow Variant: 72 Data flow: data passed in a vector from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\nusing namespace std;\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_72\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(vector dataVector)\r\n{\r\n /* copy data out of dataVector */\r\n wchar_t * data = dataVector[2];\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n fwprintf(stdout, data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(vector dataVector)\r\n{\r\n wchar_t * data = dataVector[2];\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n fwprintf(stdout, data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(vector dataVector)\r\n{\r\n wchar_t * data = dataVector[2];\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n fwprintf(stdout, L\"%s\\n\", data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_81_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_81_goodG2B.cpp","cwe_id":"CWE134","file_extension":".cpp","language_hint":"C++","source_size_bytes":1202,"source_sha256":"00d28b3fb592ebe3fb973c5a27f7e503ed33b255446760eacc8b4b2f1c5901e3","line_count":32,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_81_goodG2B.cpp\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-81_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: fprintf\r\n * GoodSink: fwprintf with \"%s\" as the second argument and data as the third\r\n * BadSink : fwprintf with data as the second argument\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_81.h\"\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_81\r\n{\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_81_goodG2B::action(wchar_t * data) const\r\n{\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n fwprintf(stdout, data);\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_82a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_82a.cpp","cwe_id":"CWE134","file_extension":".cpp","language_hint":"C++","source_size_bytes":7986,"source_sha256":"a7a06ef29ee90b13fa9be9981ca9c10c45abde89cac4e3d939be1f3dbe31bb14","line_count":255,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_82a.cpp\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-82a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: fprintf\r\n * GoodSink: fwprintf with \"%s\" as the second argument and data as the third\r\n * BadSink : fwprintf with data as the second argument\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_82.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_82\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_82_base* baseObject = new CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_82_bad;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n wcscpy(data, L\"fixedstringtest\");\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_82_base* baseObject = new CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_82_goodG2B;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_82_base* baseObject = new CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_82_goodB2G;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_82; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_83.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_83.h","cwe_id":"CWE134","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1921,"source_sha256":"13a6eacda4faf00cb00a3bea6f5c73b10d10c37775a4d4a77e106979e7e3722d","line_count":65,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_83.h\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-83.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: fprintf\r\n * GoodSink: fwprintf with \"%s\" as the second argument and data as the third\r\n * BadSink : fwprintf with data as the second argument\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_83\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_83_bad\r\n{\r\npublic:\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_83_bad(wchar_t * dataCopy);\r\n ~CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_83_bad();\r\n\r\nprivate:\r\n wchar_t * data;\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_83_goodG2B\r\n{\r\npublic:\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_83_goodG2B(wchar_t * dataCopy);\r\n ~CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_83_goodG2B();\r\n\r\nprivate:\r\n wchar_t * data;\r\n};\r\n\r\nclass CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_83_goodB2G\r\n{\r\npublic:\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_83_goodB2G(wchar_t * dataCopy);\r\n ~CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_83_goodB2G();\r\n\r\nprivate:\r\n wchar_t * data;\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_83_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_83_bad.cpp","cwe_id":"CWE134","file_extension":".cpp","language_hint":"C++","source_size_bytes":4127,"source_sha256":"7ec5a60eb19fc6248c565f2b5374c8dfdaca3c8393053869b8a89cfcbd54e063","line_count":123,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_83_bad.cpp\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-83_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: fprintf\r\n * GoodSink: fwprintf with \"%s\" as the second argument and data as the third\r\n * BadSink : fwprintf with data as the second argument\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_83.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_83\r\n{\r\nCWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_83_bad::CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_83_bad(wchar_t * dataCopy)\r\n{\r\n data = dataCopy;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n}\r\n\r\nCWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_83_bad::~CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_fprintf_83_bad()\r\n{\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n fwprintf(stdout, data);\r\n}\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_14.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_14.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":12109,"source_sha256":"747b9291816d66ae375b72c306d6d5e0c7244a8a02b6aeb6acb2c8378cd6649b","line_count":379,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_14.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-14.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: printf\r\n * GoodSink: wprintf with \"%s\" as the first argument and data as the second\r\n * BadSink : wprintf with only data as an argument\r\n * Flow Variant: 14 Control flow: if(globalFive==5) and if(globalFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_14_bad()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n if(globalFive==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n wprintf(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalFive==5 to globalFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n if(globalFive==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n wprintf(L\"%s\\n\", data);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n if(globalFive==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalFive==5)\r\n {\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n wprintf(L\"%s\\n\", data);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalFive==5 to globalFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n wcscpy(data, L\"fixedstringtest\");\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n wprintf(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n if(globalFive==5)\r\n {\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n wcscpy(data, L\"fixedstringtest\");\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n wprintf(data);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_14_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_14_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_14_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_34.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_34.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":8385,"source_sha256":"059b19126317bf708d9ebfe1078e7611cb94ce8ba0de6e5ddc7238f43fc96c14","line_count":268,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_34.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-34.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: printf\r\n * GoodSink: wprintf with \"%s\" as the first argument and data as the second\r\n * BadSink : wprintf with only data as an argument\r\n * Flow Variant: 34 Data flow: use of a union containing two methods of accessing the same data (within the same function)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\ntypedef union\r\n{\r\n wchar_t * unionFirst;\r\n wchar_t * unionSecond;\r\n} CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_34_unionType;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_34_bad()\r\n{\r\n wchar_t * data;\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_34_unionType myUnion;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n myUnion.unionFirst = data;\r\n {\r\n wchar_t * data = myUnion.unionSecond;\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n wprintf(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_34_unionType myUnion;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n wcscpy(data, L\"fixedstringtest\");\r\n myUnion.unionFirst = data;\r\n {\r\n wchar_t * data = myUnion.unionSecond;\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n wprintf(data);\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_34_unionType myUnion;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n myUnion.unionFirst = data;\r\n {\r\n wchar_t * data = myUnion.unionSecond;\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n wprintf(L\"%s\\n\", data);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_34_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_34_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_34_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_51b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_51b.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":2134,"source_sha256":"fbc12997243bdbd1889b31643d876569c98b8f3c89a1200342f5523da8b742cc","line_count":71,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_51b.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-51b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: printf\r\n * GoodSink: wprintf with \"%s\" as the first argument and data as the second\r\n * BadSink : wprintf with only data as an argument\r\n * Flow Variant: 51 Data flow: data passed as an argument from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_51b_badSink(wchar_t * data)\r\n{\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n wprintf(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_51b_goodG2BSink(wchar_t * data)\r\n{\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n wprintf(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_51b_goodB2GSink(wchar_t * data)\r\n{\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n wprintf(L\"%s\\n\", data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_52b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_52b.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":2424,"source_sha256":"d5e02281b01d7f9b2430dc7fda1864720a956c8444d1318d4cac507d71f31856","line_count":75,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_52b.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-52b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: printf\r\n * GoodSink: wprintf with \"%s\" as the first argument and data as the second\r\n * BadSink : wprintf with only data as an argument\r\n * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_52c_badSink(wchar_t * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_52b_badSink(wchar_t * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_52c_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_52c_goodG2BSink(wchar_t * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_52b_goodG2BSink(wchar_t * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_52c_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_52c_goodB2GSink(wchar_t * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_52b_goodB2GSink(wchar_t * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_52c_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_53a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_53a.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":7956,"source_sha256":"8f6ce309eca383bdd8beacddcf8a1f42605186edcd02caacb4abac2cc54b97ad","line_count":252,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_53a.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-53a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: printf\r\n * GoodSink: wprintf with \"%s\" as the first argument and data as the second\r\n * BadSink : wprintf with only data as an argument\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_53b_badSink(wchar_t * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_53_bad()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_53b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_53b_goodG2BSink(wchar_t * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n wcscpy(data, L\"fixedstringtest\");\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_53b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_53b_goodB2GSink(wchar_t * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_53b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_53_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_53_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_53_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_66b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_66b.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":2298,"source_sha256":"53caae5e5cbb3a57afe6dcacfc9f74f26cab12b85340563a66a71e0d8ac16336","line_count":75,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_66b.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-66b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: printf\r\n * GoodSink: wprintf with \"%s\" as the first argument and data as the second\r\n * BadSink : wprintf with only data as an argument\r\n * Flow Variant: 66 Data flow: data passed in an array from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_66b_badSink(wchar_t * dataArray[])\r\n{\r\n /* copy data out of dataArray */\r\n wchar_t * data = dataArray[2];\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n wprintf(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_66b_goodG2BSink(wchar_t * dataArray[])\r\n{\r\n wchar_t * data = dataArray[2];\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n wprintf(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_66b_goodB2GSink(wchar_t * dataArray[])\r\n{\r\n wchar_t * data = dataArray[2];\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n wprintf(L\"%s\\n\", data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_81_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_81_goodG2B.cpp","cwe_id":"CWE134","file_extension":".cpp","language_hint":"C++","source_size_bytes":1183,"source_sha256":"149e237c406b263739629106d671c879e19dd02f59d061c8bbf42890f5316bb6","line_count":32,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_81_goodG2B.cpp\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-81_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: printf\r\n * GoodSink: wprintf with \"%s\" as the first argument and data as the second\r\n * BadSink : wprintf with only data as an argument\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_81.h\"\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_81\r\n{\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_81_goodG2B::action(wchar_t * data) const\r\n{\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n wprintf(data);\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_82a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_82a.cpp","cwe_id":"CWE134","file_extension":".cpp","language_hint":"C++","source_size_bytes":7970,"source_sha256":"f750c5f3e39e2dbd59fcaac84132b92d7834092d1c7c870543732dc528648065","line_count":255,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_82a.cpp\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-82a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: printf\r\n * GoodSink: wprintf with \"%s\" as the first argument and data as the second\r\n * BadSink : wprintf with only data as an argument\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_82.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_82\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_82_base* baseObject = new CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_82_bad;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n wcscpy(data, L\"fixedstringtest\");\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_82_base* baseObject = new CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_82_goodG2B;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_82_base* baseObject = new CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_82_goodB2G;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_82; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_83_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_83_goodG2B.cpp","cwe_id":"CWE134","file_extension":".cpp","language_hint":"C++","source_size_bytes":2059,"source_sha256":"5557672ac24fb1b9998305ee6d2aacaf16852804ebc576e4d6900ebe1f038e8b","line_count":58,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_83_goodG2B.cpp\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-83_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: printf\r\n * GoodSink: wprintf with \"%s\" as the first argument and data as the second\r\n * BadSink : wprintf with only data as an argument\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_83.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_83\r\n{\r\nCWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_83_goodG2B::CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_83_goodG2B(wchar_t * dataCopy)\r\n{\r\n data = dataCopy;\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n wcscpy(data, L\"fixedstringtest\");\r\n}\r\n\r\nCWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_83_goodG2B::~CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_83_goodG2B()\r\n{\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n wprintf(data);\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_84_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_84_goodB2G.cpp","cwe_id":"CWE134","file_extension":".cpp","language_hint":"C++","source_size_bytes":4141,"source_sha256":"03348f026ee28fba73a6a5761ee17d72aa4712a9b8b85e5d086649981c5f28ec","line_count":123,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_84_goodB2G.cpp\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-84_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: printf\r\n * GoodSink: wprintf with \"%s\" as the first argument and data as the second\r\n * BadSink : wprintf with only data as an argument\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_84.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_84\r\n{\r\nCWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_84_goodB2G::CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_84_goodB2G(wchar_t * dataCopy)\r\n{\r\n data = dataCopy;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n}\r\n\r\nCWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_84_goodB2G::~CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_printf_84_goodB2G()\r\n{\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n wprintf(L\"%s\\n\", data);\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_44.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_44.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":8489,"source_sha256":"6a4491848fd81cb2f2a51c9d8b09e62544a646fdd5c0f40e933bd949e2c09297","line_count":285,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_44.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-44.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: swprintf\r\n * GoodSink: snwprintf with \"%s\" as the third argument and data as the fourth\r\n * BadSink : snwprintf with data as the third argument\r\n * 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\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\n#ifdef _WIN32\r\n#define SNPRINTF _snwprintf\r\n#else\r\n#define SNPRINTF swprintf\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink(wchar_t * data)\r\n{\r\n {\r\n wchar_t dest[100] = L\"\";\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n SNPRINTF(dest, 100-1, data);\r\n printWLine(dest);\r\n }\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_44_bad()\r\n{\r\n wchar_t * data;\r\n /* define a function pointer */\r\n void (*funcPtr) (wchar_t *) = badSink;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n /* use the function pointer */\r\n funcPtr(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink(wchar_t * data)\r\n{\r\n {\r\n wchar_t dest[100] = L\"\";\r\n /* POTENTIAL FLAW: Do not specify the format allowing a possible format string vulnerability */\r\n SNPRINTF(dest, 100-1, data);\r\n printWLine(dest);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n void (*funcPtr) (wchar_t *) = goodG2BSink;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n wcscpy(data, L\"fixedstringtest\");\r\n funcPtr(data);\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink(wchar_t * data)\r\n{\r\n {\r\n wchar_t dest[100] = L\"\";\r\n /* FIX: Specify the format disallowing a format string vulnerability */\r\n SNPRINTF(dest, 100-1, L\"%s\", data);\r\n printWLine(dest);\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n void (*funcPtr) (wchar_t *) = goodB2GSink;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n funcPtr(data);\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_44_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_44_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_44_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54a.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":8075,"source_sha256":"1d4fb6ab6f1a91bbb8e2c268931b35a05ff89de94e6c0dc71f63d9439d564187","line_count":258,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54a.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-54a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: snprintf\r\n * GoodSink: snwprintf with \"%s\" as the third argument and data as the fourth\r\n * BadSink : snwprintf with data as the third argument\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\n#ifdef _WIN32\r\n#define SNPRINTF _snwprintf\r\n#else\r\n#define SNPRINTF snprintf\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54b_badSink(wchar_t * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54_bad()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54b_goodG2BSink(wchar_t * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n wcscpy(data, L\"fixedstringtest\");\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54b_goodB2GSink(wchar_t * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54b.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":2568,"source_sha256":"408fb66aa0df119d565272feef0cc4f2449f7f6cbdc7f49c1f28a1b3f2e23da4","line_count":81,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54b.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-54b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: snprintf\r\n * GoodSink: snwprintf with \"%s\" as the third argument and data as the fourth\r\n * BadSink : snwprintf with data as the third argument\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\n#ifdef _WIN32\r\n#define SNPRINTF _snwprintf\r\n#else\r\n#define SNPRINTF snprintf\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54c_badSink(wchar_t * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54b_badSink(wchar_t * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54c_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54c_goodG2BSink(wchar_t * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54b_goodG2BSink(wchar_t * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54c_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54c_goodB2GSink(wchar_t * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54b_goodB2GSink(wchar_t * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54c_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54d.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54d.c","cwe_id":"CWE134","file_extension":".c","language_hint":"C","source_size_bytes":2568,"source_sha256":"0d3df143f808bdfec188fa0c50839ece5222a06de0e9f33e1b2dc56f2553cd23","line_count":81,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54d.c\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-54d.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: snprintf\r\n * GoodSink: snwprintf with \"%s\" as the third argument and data as the fourth\r\n * BadSink : snwprintf with data as the third argument\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\n#ifdef _WIN32\r\n#define SNPRINTF _snwprintf\r\n#else\r\n#define SNPRINTF snprintf\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54e_badSink(wchar_t * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54d_badSink(wchar_t * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54e_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54e_goodG2BSink(wchar_t * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54d_goodG2BSink(wchar_t * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54e_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54e_goodB2GSink(wchar_t * data);\r\n\r\nvoid CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54d_goodB2GSink(wchar_t * data)\r\n{\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_54e_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_62b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_62b.cpp","cwe_id":"CWE134","file_extension":".cpp","language_hint":"C++","source_size_bytes":6214,"source_sha256":"1a3bba28ba8802b9260c588bd2fb88faecb72aed074a195f022796d3d44b1288","line_count":206,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_62b.cpp\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-62b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: snprintf\r\n * GoodSink: snwprintf with \"%s\" as the third argument and data as the fourth\r\n * BadSink : snwprintf with data as the third argument\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSource(wchar_t * &data)\r\n{\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(wchar_t * &data)\r\n{\r\n /* FIX: Use a fixed string that does not contain a format specifier */\r\n wcscpy(data, L\"fixedstringtest\");\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(wchar_t * &data)\r\n{\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n wchar_t *replace;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n size_t dataLen = wcslen(data);\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(connectSocket, (char *)(data + dataLen), sizeof(wchar_t) * (100 - dataLen - 1), 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* Append null terminator */\r\n data[dataLen + recvResult / sizeof(wchar_t)] = L'\\0';\r\n /* Eliminate CRLF */\r\n replace = wcschr(data, L'\\r');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n replace = wcschr(data, L'\\n');\r\n if (replace)\r\n {\r\n *replace = L'\\0';\r\n }\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84.h","cwe_id":"CWE134","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1959,"source_sha256":"4b149323c7fa39953d9fc36e13c7ad1e5350ddbf18724529763e4606618e02a6","line_count":65,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84.h\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-84.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: snprintf\r\n * GoodSink: snwprintf with \"%s\" as the third argument and data as the fourth\r\n * BadSink : snwprintf with data as the third argument\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef _WIN32\r\n#include \r\n#endif\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84_bad\r\n{\r\npublic:\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84_bad(wchar_t * dataCopy);\r\n ~CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84_bad();\r\n\r\nprivate:\r\n wchar_t * data;\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84_goodG2B\r\n{\r\npublic:\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84_goodG2B(wchar_t * dataCopy);\r\n ~CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84_goodG2B();\r\n\r\nprivate:\r\n wchar_t * data;\r\n};\r\n\r\nclass CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84_goodB2G\r\n{\r\npublic:\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84_goodB2G(wchar_t * dataCopy);\r\n ~CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84_goodB2G();\r\n\r\nprivate:\r\n wchar_t * data;\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE134_Uncontrolled_Format_String/s03/CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84a.cpp","cwe_id":"CWE134","file_extension":".cpp","language_hint":"C++","source_size_bytes":3003,"source_sha256":"8c257ade6041565beef93e5ffc08e76c3c24268fdf8b8b1964812c93262a8ee5","line_count":95,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84a.cpp\r\nLabel Definition File: CWE134_Uncontrolled_Format_String.label.xml\r\nTemplate File: sources-sinks-84a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 134 Uncontrolled Format String\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Copy a fixed string into data\r\n * Sinks: snprintf\r\n * GoodSink: snwprintf with \"%s\" as the third argument and data as the fourth\r\n * BadSink : snwprintf with data as the third argument\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84.h\"\r\n\r\nnamespace CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84_bad * badObject = new CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84_bad(data);\r\n delete badObject;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84_goodG2B * goodG2BObject = new CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84_goodG2B(data);\r\n delete goodG2BObject;\r\n}\r\n\r\n/* goodG2B uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n wchar_t dataBuffer[100] = L\"\";\r\n data = dataBuffer;\r\n CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84_goodB2G * goodB2GObject = new CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84_goodB2G(data);\r\n delete goodB2GObject;\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE134_Uncontrolled_Format_String__wchar_t_connect_socket_snprintf_84; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fgets_multiply_32.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fgets_multiply_32.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":4518,"source_sha256":"0c2e2f5bff2a96e5dd96c088d612131fe6eb45ff81297d261673ccd771c5840a","line_count":162,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fgets_multiply_32.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-32.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 32 Data flow using two pointers to the same value within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE190_Integer_Overflow__int_fgets_multiply_32_bad()\r\n{\r\n int data;\r\n int *dataPtr1 = &data;\r\n int *dataPtr2 = &data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n int data = *dataPtr1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read data from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n *dataPtr1 = data;\r\n }\r\n {\r\n int data = *dataPtr2;\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n int *dataPtr1 = &data;\r\n int *dataPtr2 = &data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n int data = *dataPtr1;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n *dataPtr1 = data;\r\n }\r\n {\r\n int data = *dataPtr2;\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n int *dataPtr1 = &data;\r\n int *dataPtr2 = &data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n int data = *dataPtr1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read data from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n *dataPtr1 = data;\r\n }\r\n {\r\n int data = *dataPtr2;\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < (INT_MAX/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_fgets_multiply_32_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_fgets_multiply_32_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_fgets_multiply_32_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fgets_multiply_65b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fgets_multiply_65b.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":2049,"source_sha256":"5f5ad2408013424b7a2feffffeeea18b69bfe44a7f507e6c752b2b7898cd22a5","line_count":67,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fgets_multiply_65b.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-65b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE190_Integer_Overflow__int_fgets_multiply_65b_badSink(int data)\r\n{\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE190_Integer_Overflow__int_fgets_multiply_65b_goodG2BSink(int data)\r\n{\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE190_Integer_Overflow__int_fgets_multiply_65b_goodB2GSink(int data)\r\n{\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < (INT_MAX/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fgets_multiply_67b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fgets_multiply_67b.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":2456,"source_sha256":"ba607616e650ef9288fd1e7ca12a491f4d388cb754da3fa9f17e096cda65968e","line_count":75,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fgets_multiply_67b.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-67b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\ntypedef struct _CWE190_Integer_Overflow__int_fgets_multiply_67_structType\r\n{\r\n int structFirst;\r\n} CWE190_Integer_Overflow__int_fgets_multiply_67_structType;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE190_Integer_Overflow__int_fgets_multiply_67b_badSink(CWE190_Integer_Overflow__int_fgets_multiply_67_structType myStruct)\r\n{\r\n int data = myStruct.structFirst;\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE190_Integer_Overflow__int_fgets_multiply_67b_goodG2BSink(CWE190_Integer_Overflow__int_fgets_multiply_67_structType myStruct)\r\n{\r\n int data = myStruct.structFirst;\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE190_Integer_Overflow__int_fgets_multiply_67b_goodB2GSink(CWE190_Integer_Overflow__int_fgets_multiply_67_structType myStruct)\r\n{\r\n int data = myStruct.structFirst;\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < (INT_MAX/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fgets_multiply_81_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fgets_multiply_81_bad.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":1180,"source_sha256":"e3fd38e7b79c16087ef81646d25ce8e3b95addaad7093c9f2a5e5287d02a9701","line_count":36,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fgets_multiply_81_bad.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-81_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE190_Integer_Overflow__int_fgets_multiply_81.h\"\r\n\r\nnamespace CWE190_Integer_Overflow__int_fgets_multiply_81\r\n{\r\n\r\nvoid CWE190_Integer_Overflow__int_fgets_multiply_81_bad::action(int data) const\r\n{\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fgets_multiply_82_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fgets_multiply_82_bad.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":1172,"source_sha256":"d550d7a925b29d032e254b50205e6f9ed350644f911d2fccf80e4a6787eb6beb","line_count":36,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fgets_multiply_82_bad.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-82_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE190_Integer_Overflow__int_fgets_multiply_82.h\"\r\n\r\nnamespace CWE190_Integer_Overflow__int_fgets_multiply_82\r\n{\r\n\r\nvoid CWE190_Integer_Overflow__int_fgets_multiply_82_bad::action(int data)\r\n{\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fgets_multiply_83_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fgets_multiply_83_bad.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":1794,"source_sha256":"f7c6f58134c6953d11a3dd3b3a5087faf15bba8648c00a9f3efef6150084a918","line_count":54,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fgets_multiply_83_bad.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-83_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE190_Integer_Overflow__int_fgets_multiply_83.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\nnamespace CWE190_Integer_Overflow__int_fgets_multiply_83\r\n{\r\nCWE190_Integer_Overflow__int_fgets_multiply_83_bad::CWE190_Integer_Overflow__int_fgets_multiply_83_bad(int dataCopy)\r\n{\r\n data = dataCopy;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read data from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n}\r\n\r\nCWE190_Integer_Overflow__int_fgets_multiply_83_bad::~CWE190_Integer_Overflow__int_fgets_multiply_83_bad()\r\n{\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fgets_multiply_84_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fgets_multiply_84_goodG2B.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":1583,"source_sha256":"2dffa85f14777e9047ca76ee70c750deb8c051a24c1cac7cb8b43d5a6b8fb210","line_count":43,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fgets_multiply_84_goodG2B.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-84_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE190_Integer_Overflow__int_fgets_multiply_84.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\nnamespace CWE190_Integer_Overflow__int_fgets_multiply_84\r\n{\r\nCWE190_Integer_Overflow__int_fgets_multiply_84_goodG2B::CWE190_Integer_Overflow__int_fgets_multiply_84_goodG2B(int dataCopy)\r\n{\r\n data = dataCopy;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n}\r\n\r\nCWE190_Integer_Overflow__int_fgets_multiply_84_goodG2B::~CWE190_Integer_Overflow__int_fgets_multiply_84_goodG2B()\r\n{\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fgets_square_53a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fgets_square_53a.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":3493,"source_sha256":"408395dd96e6b689a0dc39f65157dbeddbf82e5f0f95701da09177e6ba269a01","line_count":123,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fgets_square_53a.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-53a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE190_Integer_Overflow__int_fgets_square_53b_badSink(int data);\r\n\r\nvoid CWE190_Integer_Overflow__int_fgets_square_53_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read data from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n CWE190_Integer_Overflow__int_fgets_square_53b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE190_Integer_Overflow__int_fgets_square_53b_goodG2BSink(int data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n CWE190_Integer_Overflow__int_fgets_square_53b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE190_Integer_Overflow__int_fgets_square_53b_goodB2GSink(int data);\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read data from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n CWE190_Integer_Overflow__int_fgets_square_53b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_fgets_square_53_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_fgets_square_53_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_fgets_square_53_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fgets_square_65b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fgets_square_65b.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":1854,"source_sha256":"0366ac6f5da908543b249e618d716fc1074b544e34457e2006513b16ed44e55c","line_count":64,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fgets_square_65b.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-65b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE190_Integer_Overflow__int_fgets_square_65b_badSink(int data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE190_Integer_Overflow__int_fgets_square_65b_goodG2BSink(int data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE190_Integer_Overflow__int_fgets_square_65b_goodB2GSink(int data)\r\n{\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data > INT_MIN && abs(data) < (long)sqrt((double)INT_MAX))\r\n {\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fgets_square_67b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fgets_square_67b.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":2251,"source_sha256":"12af29679fc717aa70cf9c92309af165fc122401b97a19faf4279cc1d62bbefe","line_count":72,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fgets_square_67b.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-67b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#include \r\n\r\ntypedef struct _CWE190_Integer_Overflow__int_fgets_square_67_structType\r\n{\r\n int structFirst;\r\n} CWE190_Integer_Overflow__int_fgets_square_67_structType;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE190_Integer_Overflow__int_fgets_square_67b_badSink(CWE190_Integer_Overflow__int_fgets_square_67_structType myStruct)\r\n{\r\n int data = myStruct.structFirst;\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE190_Integer_Overflow__int_fgets_square_67b_goodG2BSink(CWE190_Integer_Overflow__int_fgets_square_67_structType myStruct)\r\n{\r\n int data = myStruct.structFirst;\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE190_Integer_Overflow__int_fgets_square_67b_goodB2GSink(CWE190_Integer_Overflow__int_fgets_square_67_structType myStruct)\r\n{\r\n int data = myStruct.structFirst;\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data > INT_MIN && abs(data) < (long)sqrt((double)INT_MAX))\r\n {\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fgets_square_74a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fgets_square_74a.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":3614,"source_sha256":"40f864c792d3ab306301796af447b74384d5524143df8f2e7e38d183e2ba296e","line_count":145,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fgets_square_74a.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-74a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 74 Data flow: data passed in a map from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\nusing namespace std;\r\n\r\nnamespace CWE190_Integer_Overflow__int_fgets_square_74\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(map dataMap);\r\n\r\nvoid bad()\r\n{\r\n int data;\r\n map dataMap;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read data from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n /* Put data in a map */\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n badSink(dataMap);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(map dataMap);\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n map dataMap;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n /* Put data in a map */\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n goodG2BSink(dataMap);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(map dataMap);\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n map dataMap;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read data from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n goodB2GSink(dataMap);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE190_Integer_Overflow__int_fgets_square_74; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_add_17.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_add_17.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":3290,"source_sha256":"7b4b807d89c36e5cfe0e67f5365cb5142c6a2595c2fb5ea16ce19fc20508b8f3","line_count":128,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fscanf_add_17.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-17.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: add\r\n * GoodSink: Ensure there will not be an overflow before adding 1 to data\r\n * BadSink : Add 1 to data, which can cause an overflow\r\n * Flow Variant: 17 Control flow: for loops\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE190_Integer_Overflow__int_fscanf_add_17_bad()\r\n{\r\n int i,j;\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n for(i = 0; i < 1; i++)\r\n {\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n }\r\n for(j = 0; j < 1; j++)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink in the for statements */\r\nstatic void goodB2G()\r\n{\r\n int i,k;\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n for(i = 0; i < 1; i++)\r\n {\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n }\r\n for(k = 0; k < 1; k++)\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < INT_MAX)\r\n {\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink in the for statements */\r\nstatic void goodG2B()\r\n{\r\n int h,j;\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n for(h = 0; h < 1; h++)\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n for(j = 0; j < 1; j++)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_fscanf_add_17_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_fscanf_add_17_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_fscanf_add_17_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_add_43.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_add_43.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":3171,"source_sha256":"f84982ffb0d5ef7123feb99491e253f2e6dbae7e6196df9adbebca52c768d900","line_count":129,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fscanf_add_43.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-43.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: add\r\n * GoodSink: Ensure there will not be an overflow before adding 1 to data\r\n * BadSink : Add 1 to data, which can cause an overflow\r\n * Flow Variant: 43 Data flow: data flows using a C++ reference from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE190_Integer_Overflow__int_fscanf_add_43\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSource(int &data)\r\n{\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n}\r\n\r\nvoid bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n badSource(data);\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSource(int &data)\r\n{\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n goodG2BSource(data);\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSource(int &data)\r\n{\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n goodB2GSource(data);\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < INT_MAX)\r\n {\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE190_Integer_Overflow__int_fscanf_add_43; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_add_82_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_add_82_bad.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":1068,"source_sha256":"5f3b8aa0a8ae83f668b2a79332677814d84c9e922656301681e59fedc9c1291d","line_count":35,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fscanf_add_82_bad.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-82_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: add\r\n * GoodSink: Ensure there will not be an overflow before adding 1 to data\r\n * BadSink : Add 1 to data, which can cause an overflow\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE190_Integer_Overflow__int_fscanf_add_82.h\"\r\n\r\nnamespace CWE190_Integer_Overflow__int_fscanf_add_82\r\n{\r\n\r\nvoid CWE190_Integer_Overflow__int_fscanf_add_82_bad::action(int data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_add_84.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_add_84.h","cwe_id":"CWE190","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1557,"source_sha256":"53a77a5b309d46ba0d82e15e255acb57ecb55926e25e97ae31e7b2f044f27df1","line_count":61,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fscanf_add_84.h\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-84.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: add\r\n * GoodSink: Ensure there will not be an overflow before adding 1 to data\r\n * BadSink : Add 1 to data, which can cause an overflow\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE190_Integer_Overflow__int_fscanf_add_84\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE190_Integer_Overflow__int_fscanf_add_84_bad\r\n{\r\npublic:\r\n CWE190_Integer_Overflow__int_fscanf_add_84_bad(int dataCopy);\r\n ~CWE190_Integer_Overflow__int_fscanf_add_84_bad();\r\n\r\nprivate:\r\n int data;\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE190_Integer_Overflow__int_fscanf_add_84_goodG2B\r\n{\r\npublic:\r\n CWE190_Integer_Overflow__int_fscanf_add_84_goodG2B(int dataCopy);\r\n ~CWE190_Integer_Overflow__int_fscanf_add_84_goodG2B();\r\n\r\nprivate:\r\n int data;\r\n};\r\n\r\nclass CWE190_Integer_Overflow__int_fscanf_add_84_goodB2G\r\n{\r\npublic:\r\n CWE190_Integer_Overflow__int_fscanf_add_84_goodB2G(int dataCopy);\r\n ~CWE190_Integer_Overflow__int_fscanf_add_84_goodB2G();\r\n\r\nprivate:\r\n int data;\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_add_84_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_add_84_goodG2B.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":1417,"source_sha256":"4d6cf74906bcf892740a527f1b95edf39c522ef382a253fe4f5e83f7cf54e9e7","line_count":40,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fscanf_add_84_goodG2B.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-84_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: add\r\n * GoodSink: Ensure there will not be an overflow before adding 1 to data\r\n * BadSink : Add 1 to data, which can cause an overflow\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE190_Integer_Overflow__int_fscanf_add_84.h\"\r\n\r\nnamespace CWE190_Integer_Overflow__int_fscanf_add_84\r\n{\r\nCWE190_Integer_Overflow__int_fscanf_add_84_goodG2B::CWE190_Integer_Overflow__int_fscanf_add_84_goodG2B(int dataCopy)\r\n{\r\n data = dataCopy;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n}\r\n\r\nCWE190_Integer_Overflow__int_fscanf_add_84_goodG2B::~CWE190_Integer_Overflow__int_fscanf_add_84_goodG2B()\r\n{\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_multiply_12.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_multiply_12.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":5451,"source_sha256":"962f9b93b80597536791fc2c6c4f687f50917625ebc80339c145d06a58e24f30","line_count":190,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fscanf_multiply_12.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-12.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE190_Integer_Overflow__int_fscanf_multiply_12_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n else\r\n {\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < (INT_MAX/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the first \"if\" so that\r\n both branches use the BadSource and the second \"if\" so that both branches\r\n use the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n }\r\n else\r\n {\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < (INT_MAX/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n }\r\n else\r\n {\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < (INT_MAX/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the first \"if\" so that\r\n both branches use the GoodSource and the second \"if\" so that both branches\r\n use the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n else\r\n {\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_fscanf_multiply_12_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_fscanf_multiply_12_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_fscanf_multiply_12_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_multiply_16.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_multiply_16.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":3630,"source_sha256":"2288329ccdce9845f0e7ea2863fe43136f3da7398dcacf4cd0ef4aba64c2b522","line_count":136,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fscanf_multiply_16.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-16.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 16 Control flow: while(1)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE190_Integer_Overflow__int_fscanf_multiply_16_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n break;\r\n }\r\n while(1)\r\n {\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the sinks in the second while statement */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n break;\r\n }\r\n while(1)\r\n {\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < (INT_MAX/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the sources in the first while statement */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n while(1)\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n break;\r\n }\r\n while(1)\r\n {\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_fscanf_multiply_16_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_fscanf_multiply_16_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_fscanf_multiply_16_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_multiply_33.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_multiply_33.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":3518,"source_sha256":"8752090e1df99de71cce85589fc2e3c608111d49f2c5c14c35c2c156b99b1fd2","line_count":130,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fscanf_multiply_33.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-33.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 33 Data flow: use of a C++ reference to data within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE190_Integer_Overflow__int_fscanf_multiply_33\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int data;\r\n int &dataRef = data;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n {\r\n int data = dataRef;\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n int &dataRef = data;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n {\r\n int data = dataRef;\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n int &dataRef = data;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n {\r\n int data = dataRef;\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < (INT_MAX/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE190_Integer_Overflow__int_fscanf_multiply_33; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_multiply_44.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_multiply_44.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":3640,"source_sha256":"1cc046ab44b7c77ea47b907f9b5873f4083c779da52035e14dce17a78d953d12","line_count":131,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fscanf_multiply_44.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-44.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * 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\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink(int data)\r\n{\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_fscanf_multiply_44_bad()\r\n{\r\n int data;\r\n /* define a function pointer */\r\n void (*funcPtr) (int) = badSink;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n /* use the function pointer */\r\n funcPtr(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink(int data)\r\n{\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n void (*funcPtr) (int) = goodG2BSink;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n funcPtr(data);\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink(int data)\r\n{\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < (INT_MAX/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n void (*funcPtr) (int) = goodB2GSink;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n funcPtr(data);\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_fscanf_multiply_44_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_fscanf_multiply_44_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_fscanf_multiply_44_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_multiply_52a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_multiply_52a.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":2920,"source_sha256":"c66c50ff97671ecded4a7b6ec40d393357ee4c3766912df2801c80826cbc161b","line_count":97,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fscanf_multiply_52a.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-52a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE190_Integer_Overflow__int_fscanf_multiply_52b_badSink(int data);\r\n\r\nvoid CWE190_Integer_Overflow__int_fscanf_multiply_52_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n CWE190_Integer_Overflow__int_fscanf_multiply_52b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE190_Integer_Overflow__int_fscanf_multiply_52b_goodG2BSink(int data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n CWE190_Integer_Overflow__int_fscanf_multiply_52b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE190_Integer_Overflow__int_fscanf_multiply_52b_goodB2GSink(int data);\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n CWE190_Integer_Overflow__int_fscanf_multiply_52b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_fscanf_multiply_52_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_fscanf_multiply_52_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_fscanf_multiply_52_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_multiply_54d.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_multiply_54d.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":1690,"source_sha256":"f3a4fad6a46d5ebdda8e9a444b7b7eb2d63e8c35b3744da00c044abc9c7bf107","line_count":50,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fscanf_multiply_54d.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-54d.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE190_Integer_Overflow__int_fscanf_multiply_54e_badSink(int data);\r\n\r\nvoid CWE190_Integer_Overflow__int_fscanf_multiply_54d_badSink(int data)\r\n{\r\n CWE190_Integer_Overflow__int_fscanf_multiply_54e_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE190_Integer_Overflow__int_fscanf_multiply_54e_goodG2BSink(int data);\r\n\r\nvoid CWE190_Integer_Overflow__int_fscanf_multiply_54d_goodG2BSink(int data)\r\n{\r\n CWE190_Integer_Overflow__int_fscanf_multiply_54e_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE190_Integer_Overflow__int_fscanf_multiply_54e_goodB2GSink(int data);\r\n\r\nvoid CWE190_Integer_Overflow__int_fscanf_multiply_54d_goodB2GSink(int data)\r\n{\r\n CWE190_Integer_Overflow__int_fscanf_multiply_54e_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_multiply_54e.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_multiply_54e.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":2008,"source_sha256":"b3cc5ed23323bbe717b02f1878bee144ac1b8bfdb4276d64e22fca891081bbc4","line_count":65,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fscanf_multiply_54e.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-54e.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE190_Integer_Overflow__int_fscanf_multiply_54e_badSink(int data)\r\n{\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE190_Integer_Overflow__int_fscanf_multiply_54e_goodG2BSink(int data)\r\n{\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE190_Integer_Overflow__int_fscanf_multiply_54e_goodB2GSink(int data)\r\n{\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < (INT_MAX/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_multiply_72b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_multiply_72b.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":2125,"source_sha256":"1eab19fb36a303ddf686a2f61a830761b7b7665587a2356963ad21e52d2a2f33","line_count":77,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fscanf_multiply_72b.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-72b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 72 Data flow: data passed in a vector from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE190_Integer_Overflow__int_fscanf_multiply_72\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(vector dataVector)\r\n{\r\n /* copy data out of dataVector */\r\n int data = dataVector[2];\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(vector dataVector)\r\n{\r\n int data = dataVector[2];\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(vector dataVector)\r\n{\r\n int data = dataVector[2];\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < (INT_MAX/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_multiply_83a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_multiply_83a.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":2429,"source_sha256":"843a8f5a9bcb58d4135449a98af679703492db3f66a38ef9701307e79404f0df","line_count":92,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fscanf_multiply_83a.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-83a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE190_Integer_Overflow__int_fscanf_multiply_83.h\"\r\n\r\nnamespace CWE190_Integer_Overflow__int_fscanf_multiply_83\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n CWE190_Integer_Overflow__int_fscanf_multiply_83_bad badObject(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n CWE190_Integer_Overflow__int_fscanf_multiply_83_goodG2B goodG2BObject(data);\r\n}\r\n\r\n/* goodG2B uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n CWE190_Integer_Overflow__int_fscanf_multiply_83_goodB2G goodB2GObject(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE190_Integer_Overflow__int_fscanf_multiply_83; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_square_12.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_square_12.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":5014,"source_sha256":"ec46324859c7f896b93146da820b1928eb46842ebca4e955e123adcae74c1f2f","line_count":180,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fscanf_square_12.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-12.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE190_Integer_Overflow__int_fscanf_square_12_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data > INT_MIN && abs(data) < (long)sqrt((double)INT_MAX))\r\n {\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the first \"if\" so that\r\n both branches use the BadSource and the second \"if\" so that both branches\r\n use the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n }\r\n else\r\n {\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data > INT_MIN && abs(data) < (long)sqrt((double)INT_MAX))\r\n {\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data > INT_MIN && abs(data) < (long)sqrt((double)INT_MAX))\r\n {\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the first \"if\" so that\r\n both branches use the GoodSource and the second \"if\" so that both branches\r\n use the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n }\r\n else\r\n {\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_fscanf_square_12_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_fscanf_square_12_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_fscanf_square_12_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_square_21.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_square_21.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":4817,"source_sha256":"193b8c511b4b3c6ed4b42c9a51a58e3afb65e40fd0fb2ff204a28db0f8a7d9fc","line_count":179,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fscanf_square_21.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-21.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 21 Control flow: Flow controlled by value of a static global variable. All functions contained in one file.\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* The static variable below is used to drive control flow in the sink function */\r\nstatic int badStatic = 0;\r\n\r\nstatic void badSink(int data)\r\n{\r\n if(badStatic)\r\n {\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_fscanf_square_21_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n badStatic = 1; /* true */\r\n badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* The static variables below are used to drive control flow in the sink functions. */\r\nstatic int goodB2G1Static = 0;\r\nstatic int goodB2G2Static = 0;\r\nstatic int goodG2BStatic = 0;\r\n\r\n/* goodB2G1() - use badsource and goodsink by setting the static variable to false instead of true */\r\nstatic void goodB2G1Sink(int data)\r\n{\r\n if(goodB2G1Static)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data > INT_MIN && abs(data) < (long)sqrt((double)INT_MAX))\r\n {\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\nstatic void goodB2G1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n goodB2G1Static = 0; /* false */\r\n goodB2G1Sink(data);\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */\r\nstatic void goodB2G2Sink(int data)\r\n{\r\n if(goodB2G2Static)\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data > INT_MIN && abs(data) < (long)sqrt((double)INT_MAX))\r\n {\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\nstatic void goodB2G2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n goodB2G2Static = 1; /* true */\r\n goodB2G2Sink(data);\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink */\r\nstatic void goodG2BSink(int data)\r\n{\r\n if(goodG2BStatic)\r\n {\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n goodG2BStatic = 1; /* true */\r\n goodG2BSink(data);\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_fscanf_square_21_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_fscanf_square_21_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_fscanf_square_21_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_square_53a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_square_53a.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":2912,"source_sha256":"936137daeac38d9ff74fc49f590174ff26209e0fbe0e0d09429923ea3c12038f","line_count":99,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fscanf_square_53a.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-53a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE190_Integer_Overflow__int_fscanf_square_53b_badSink(int data);\r\n\r\nvoid CWE190_Integer_Overflow__int_fscanf_square_53_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n CWE190_Integer_Overflow__int_fscanf_square_53b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE190_Integer_Overflow__int_fscanf_square_53b_goodG2BSink(int data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n CWE190_Integer_Overflow__int_fscanf_square_53b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE190_Integer_Overflow__int_fscanf_square_53b_goodB2GSink(int data);\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n CWE190_Integer_Overflow__int_fscanf_square_53b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_fscanf_square_53_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_fscanf_square_53_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_fscanf_square_53_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_square_54a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_square_54a.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":2913,"source_sha256":"2e6c1a2e09631342efb39811915713957bb5170dbbf5a0fdf80960b5d4f0cc40","line_count":99,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fscanf_square_54a.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-54a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE190_Integer_Overflow__int_fscanf_square_54b_badSink(int data);\r\n\r\nvoid CWE190_Integer_Overflow__int_fscanf_square_54_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n CWE190_Integer_Overflow__int_fscanf_square_54b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE190_Integer_Overflow__int_fscanf_square_54b_goodG2BSink(int data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n CWE190_Integer_Overflow__int_fscanf_square_54b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE190_Integer_Overflow__int_fscanf_square_54b_goodB2GSink(int data);\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n CWE190_Integer_Overflow__int_fscanf_square_54b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_fscanf_square_54_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_fscanf_square_54_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_fscanf_square_54_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_square_67b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_square_67b.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":2212,"source_sha256":"09eb0a554ddc29eb7aead8951e2aa1c06d128223f0cd2b09d6a872495bbfcca7","line_count":70,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fscanf_square_67b.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-67b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\ntypedef struct _CWE190_Integer_Overflow__int_fscanf_square_67_structType\r\n{\r\n int structFirst;\r\n} CWE190_Integer_Overflow__int_fscanf_square_67_structType;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE190_Integer_Overflow__int_fscanf_square_67b_badSink(CWE190_Integer_Overflow__int_fscanf_square_67_structType myStruct)\r\n{\r\n int data = myStruct.structFirst;\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE190_Integer_Overflow__int_fscanf_square_67b_goodG2BSink(CWE190_Integer_Overflow__int_fscanf_square_67_structType myStruct)\r\n{\r\n int data = myStruct.structFirst;\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE190_Integer_Overflow__int_fscanf_square_67b_goodB2GSink(CWE190_Integer_Overflow__int_fscanf_square_67_structType myStruct)\r\n{\r\n int data = myStruct.structFirst;\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data > INT_MIN && abs(data) < (long)sqrt((double)INT_MAX))\r\n {\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_square_72a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_square_72a.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":3295,"source_sha256":"67939e1e49977809905d3f2a4a6c8a087abaca38b594a58475a618838d763e35","line_count":121,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fscanf_square_72a.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-72a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 72 Data flow: data passed in a vector from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE190_Integer_Overflow__int_fscanf_square_72\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(vector dataVector);\r\n\r\nvoid bad()\r\n{\r\n int data;\r\n vector dataVector;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n /* Put data in a vector */\r\n dataVector.insert(dataVector.end(), 1, data);\r\n dataVector.insert(dataVector.end(), 1, data);\r\n dataVector.insert(dataVector.end(), 1, data);\r\n badSink(dataVector);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(vector dataVector);\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n vector dataVector;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n /* Put data in a vector */\r\n dataVector.insert(dataVector.end(), 1, data);\r\n dataVector.insert(dataVector.end(), 1, data);\r\n dataVector.insert(dataVector.end(), 1, data);\r\n goodG2BSink(dataVector);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(vector dataVector);\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n vector dataVector;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n dataVector.insert(dataVector.end(), 1, data);\r\n dataVector.insert(dataVector.end(), 1, data);\r\n dataVector.insert(dataVector.end(), 1, data);\r\n goodB2GSink(dataVector);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE190_Integer_Overflow__int_fscanf_square_72; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_square_83_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_square_83_goodB2G.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":1587,"source_sha256":"c09fb2ac31da4c874fe8b3ec7e13b75d646308c9545a3f3747f619dbb8e48def","line_count":47,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fscanf_square_83_goodB2G.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-83_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE190_Integer_Overflow__int_fscanf_square_83.h\"\r\n\r\n#include \r\n\r\nnamespace CWE190_Integer_Overflow__int_fscanf_square_83\r\n{\r\nCWE190_Integer_Overflow__int_fscanf_square_83_goodB2G::CWE190_Integer_Overflow__int_fscanf_square_83_goodB2G(int dataCopy)\r\n{\r\n data = dataCopy;\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n}\r\n\r\nCWE190_Integer_Overflow__int_fscanf_square_83_goodB2G::~CWE190_Integer_Overflow__int_fscanf_square_83_goodB2G()\r\n{\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data > INT_MIN && abs(data) < (long)sqrt((double)INT_MAX))\r\n {\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_square_84_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_fscanf_square_84_goodB2G.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":1612,"source_sha256":"6e8fd6ca58223aa91e9e96f5e6cb546180afbe66b173acfeb523e59720b43dbc","line_count":47,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_fscanf_square_84_goodB2G.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-84_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE190_Integer_Overflow__int_fscanf_square_84.h\"\r\n\r\n#include \r\n\r\nnamespace CWE190_Integer_Overflow__int_fscanf_square_84\r\n{\r\nCWE190_Integer_Overflow__int_fscanf_square_84_goodB2G::CWE190_Integer_Overflow__int_fscanf_square_84_goodB2G(int dataCopy)\r\n{\r\n data = dataCopy;\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n}\r\n\r\nCWE190_Integer_Overflow__int_fscanf_square_84_goodB2G::~CWE190_Integer_Overflow__int_fscanf_square_84_goodB2G()\r\n{\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data > INT_MIN && abs(data) < (long)sqrt((double)INT_MAX))\r\n {\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_add_06.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_add_06.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":12552,"source_sha256":"31c684863dae55428c2d4e64df30a5288c796b422928f43be42e8c86bd1a22df","line_count":412,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_listen_socket_add_06.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-06.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: add\r\n * GoodSink: Ensure there will not be an overflow before adding 1 to data\r\n * BadSink : Add 1 to data, which can cause an overflow\r\n * Flow Variant: 06 Control flow: if(STATIC_CONST_FIVE==5) and if(STATIC_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n/* The variable below is declared \"const\", so a tool should be able\r\n to identify that reads of this will always give its initialized\r\n value. */\r\nstatic const int STATIC_CONST_FIVE = 5;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_add_06_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodB2G1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < INT_MAX)\r\n {\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < INT_MAX)\r\n {\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_add_06_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_add_06_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_add_06_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_add_11.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_add_11.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":12347,"source_sha256":"f0ecc57652a02fee760f368e48c06a1903dbddb74494ecf40b22549b88632d48","line_count":407,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_listen_socket_add_11.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-11.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: add\r\n * GoodSink: Ensure there will not be an overflow before adding 1 to data\r\n * BadSink : Add 1 to data, which can cause an overflow\r\n * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_add_11_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < INT_MAX)\r\n {\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < INT_MAX)\r\n {\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_add_11_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_add_11_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_add_11_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_add_21.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_add_21.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":11344,"source_sha256":"895111046c09c9ea9b395867de488fa096b1e0e1f2a545c9e2bac5e26ba096bc","line_count":400,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_listen_socket_add_21.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-21.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: add\r\n * GoodSink: Ensure there will not be an overflow before adding 1 to data\r\n * BadSink : Add 1 to data, which can cause an overflow\r\n * Flow Variant: 21 Control flow: Flow controlled by value of a static global variable. All functions contained in one file.\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\n/* The static variable below is used to drive control flow in the sink function */\r\nstatic int badStatic = 0;\r\n\r\nstatic void badSink(int data)\r\n{\r\n if(badStatic)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_add_21_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n badStatic = 1; /* true */\r\n badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* The static variables below are used to drive control flow in the sink functions. */\r\nstatic int goodB2G1Static = 0;\r\nstatic int goodB2G2Static = 0;\r\nstatic int goodG2BStatic = 0;\r\n\r\n/* goodB2G1() - use badsource and goodsink by setting the static variable to false instead of true */\r\nstatic void goodB2G1Sink(int data)\r\n{\r\n if(goodB2G1Static)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < INT_MAX)\r\n {\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\nstatic void goodB2G1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n goodB2G1Static = 0; /* false */\r\n goodB2G1Sink(data);\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */\r\nstatic void goodB2G2Sink(int data)\r\n{\r\n if(goodB2G2Static)\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < INT_MAX)\r\n {\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\nstatic void goodB2G2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n goodB2G2Static = 1; /* true */\r\n goodB2G2Sink(data);\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink */\r\nstatic void goodG2BSink(int data)\r\n{\r\n if(goodG2BStatic)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n goodG2BStatic = 1; /* true */\r\n goodG2BSink(data);\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_add_21_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_add_21_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_add_21_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_add_22b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_add_22b.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":3330,"source_sha256":"d29b6c79c1df32ad6340c37ea2994878351820ee6f0b7740de7a2e1e137d5479","line_count":100,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_listen_socket_add_22b.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-22b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: add\r\n * GoodSink: Ensure there will not be an overflow before adding 1 to data\r\n * BadSink : Add 1 to data, which can cause an overflow\r\n * Flow Variant: 22 Control flow: Flow controlled by value of a global variable. Sink functions are in a separate file from sources.\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* The global variable below is used to drive control flow in the sink function */\r\nextern int CWE190_Integer_Overflow__int_listen_socket_add_22_badGlobal;\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_add_22_badSink(int data)\r\n{\r\n if(CWE190_Integer_Overflow__int_listen_socket_add_22_badGlobal)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* The global variables below are used to drive control flow in the sink functions. */\r\nextern int CWE190_Integer_Overflow__int_listen_socket_add_22_goodB2G1Global;\r\nextern int CWE190_Integer_Overflow__int_listen_socket_add_22_goodB2G2Global;\r\nextern int CWE190_Integer_Overflow__int_listen_socket_add_22_goodG2BGlobal;\r\n\r\n/* goodB2G1() - use badsource and goodsink by setting the static variable to false instead of true */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_add_22_goodB2G1Sink(int data)\r\n{\r\n if(CWE190_Integer_Overflow__int_listen_socket_add_22_goodB2G1Global)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < INT_MAX)\r\n {\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_add_22_goodB2G2Sink(int data)\r\n{\r\n if(CWE190_Integer_Overflow__int_listen_socket_add_22_goodB2G2Global)\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < INT_MAX)\r\n {\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_add_22_goodG2BSink(int data)\r\n{\r\n if(CWE190_Integer_Overflow__int_listen_socket_add_22_goodG2BGlobal)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_add_31.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_add_31.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":7829,"source_sha256":"5a95cf3b9b3a2303069fa2b6eb0eb4406cc363ad05b6ec765a495c58b37549be","line_count":274,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_listen_socket_add_31.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-31.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: add\r\n * GoodSink: Ensure there will not be an overflow before adding 1 to data\r\n * BadSink : Add 1 to data, which can cause an overflow\r\n * Flow Variant: 31 Data flow using a copy of data within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_add_31_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n {\r\n int dataCopy = data;\r\n int data = dataCopy;\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n {\r\n int dataCopy = data;\r\n int data = dataCopy;\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n {\r\n int dataCopy = data;\r\n int data = dataCopy;\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < INT_MAX)\r\n {\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_add_31_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_add_31_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_add_31_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_add_67a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_add_67a.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":8184,"source_sha256":"edc254b8c3d810552f543b030c6deb6aa8c7ea21c35079378531f7e510b9caff","line_count":264,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_listen_socket_add_67a.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-67a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: add\r\n * GoodSink: Ensure there will not be an overflow before adding 1 to data\r\n * BadSink : Add 1 to data, which can cause an overflow\r\n * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\ntypedef struct _CWE190_Integer_Overflow__int_listen_socket_add_67_structType\r\n{\r\n int structFirst;\r\n} CWE190_Integer_Overflow__int_listen_socket_add_67_structType;\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_add_67b_badSink(CWE190_Integer_Overflow__int_listen_socket_add_67_structType myStruct);\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_add_67_bad()\r\n{\r\n int data;\r\n CWE190_Integer_Overflow__int_listen_socket_add_67_structType myStruct;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n myStruct.structFirst = data;\r\n CWE190_Integer_Overflow__int_listen_socket_add_67b_badSink(myStruct);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_add_67b_goodG2BSink(CWE190_Integer_Overflow__int_listen_socket_add_67_structType myStruct);\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n CWE190_Integer_Overflow__int_listen_socket_add_67_structType myStruct;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n myStruct.structFirst = data;\r\n CWE190_Integer_Overflow__int_listen_socket_add_67b_goodG2BSink(myStruct);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_add_67b_goodB2GSink(CWE190_Integer_Overflow__int_listen_socket_add_67_structType myStruct);\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n CWE190_Integer_Overflow__int_listen_socket_add_67_structType myStruct;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n myStruct.structFirst = data;\r\n CWE190_Integer_Overflow__int_listen_socket_add_67b_goodB2GSink(myStruct);\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_add_67_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_add_67_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_add_67_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_add_68a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_add_68a.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":7917,"source_sha256":"65187546a5c6176d0102b69dd4a56bfea97f78f65611b6ab43a4bd524f5bb0b0","line_count":260,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_listen_socket_add_68a.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-68a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: add\r\n * GoodSink: Ensure there will not be an overflow before adding 1 to data\r\n * BadSink : Add 1 to data, which can cause an overflow\r\n * Flow Variant: 68 Data flow: data passed as a global variable from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\nint CWE190_Integer_Overflow__int_listen_socket_add_68_badData;\r\nint CWE190_Integer_Overflow__int_listen_socket_add_68_goodG2BData;\r\nint CWE190_Integer_Overflow__int_listen_socket_add_68_goodB2GData;\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_add_68b_badSink();\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_add_68_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE190_Integer_Overflow__int_listen_socket_add_68_badData = data;\r\n CWE190_Integer_Overflow__int_listen_socket_add_68b_badSink();\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_add_68b_goodG2BSink();\r\nvoid CWE190_Integer_Overflow__int_listen_socket_add_68b_goodB2GSink();\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n CWE190_Integer_Overflow__int_listen_socket_add_68_goodG2BData = data;\r\n CWE190_Integer_Overflow__int_listen_socket_add_68b_goodG2BSink();\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE190_Integer_Overflow__int_listen_socket_add_68_goodB2GData = data;\r\n CWE190_Integer_Overflow__int_listen_socket_add_68b_goodB2GSink();\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_add_68_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_add_68_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_add_68_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_add_82.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_add_82.h","cwe_id":"CWE190","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1519,"source_sha256":"db0aa585392f1888719c02afd305952e560321d0cedf26176e1d389ef96a8ec9","line_count":56,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_listen_socket_add_82.h\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-82.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: add\r\n * GoodSink: Ensure there will not be an overflow before adding 1 to data\r\n * BadSink : Add 1 to data, which can cause an overflow\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE190_Integer_Overflow__int_listen_socket_add_82\r\n{\r\n\r\nclass CWE190_Integer_Overflow__int_listen_socket_add_82_base\r\n{\r\npublic:\r\n /* pure virtual function */\r\n virtual void action(int data) = 0;\r\n};\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE190_Integer_Overflow__int_listen_socket_add_82_bad : public CWE190_Integer_Overflow__int_listen_socket_add_82_base\r\n{\r\npublic:\r\n void action(int data);\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE190_Integer_Overflow__int_listen_socket_add_82_goodG2B : public CWE190_Integer_Overflow__int_listen_socket_add_82_base\r\n{\r\npublic:\r\n void action(int data);\r\n};\r\n\r\nclass CWE190_Integer_Overflow__int_listen_socket_add_82_goodB2G : public CWE190_Integer_Overflow__int_listen_socket_add_82_base\r\n{\r\npublic:\r\n void action(int data);\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_add_82_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_add_82_goodB2G.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":1241,"source_sha256":"dd21a35039299e17beba7042a2379e94e2de358a6f9876393ee133682d2a9ecd","line_count":40,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_listen_socket_add_82_goodB2G.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-82_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: add\r\n * GoodSink: Ensure there will not be an overflow before adding 1 to data\r\n * BadSink : Add 1 to data, which can cause an overflow\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE190_Integer_Overflow__int_listen_socket_add_82.h\"\r\n\r\nnamespace CWE190_Integer_Overflow__int_listen_socket_add_82\r\n{\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_add_82_goodB2G::action(int data)\r\n{\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < INT_MAX)\r\n {\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_add_83a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_add_83a.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":2423,"source_sha256":"59b68ef8a2bf3eec733619de1746d13b9d72ce1749fdaf4f0f411cc81db6d1f7","line_count":92,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_listen_socket_add_83a.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-83a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: add\r\n * GoodSink: Ensure there will not be an overflow before adding 1 to data\r\n * BadSink : Add 1 to data, which can cause an overflow\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE190_Integer_Overflow__int_listen_socket_add_83.h\"\r\n\r\nnamespace CWE190_Integer_Overflow__int_listen_socket_add_83\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n CWE190_Integer_Overflow__int_listen_socket_add_83_bad badObject(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n CWE190_Integer_Overflow__int_listen_socket_add_83_goodG2B goodG2BObject(data);\r\n}\r\n\r\n/* goodG2B uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n CWE190_Integer_Overflow__int_listen_socket_add_83_goodB2G goodB2GObject(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE190_Integer_Overflow__int_listen_socket_add_83; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_multiply_14.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_multiply_14.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":12747,"source_sha256":"3feab5610eab5f79d03ee5c7ac02571e7de18d8194e69ac47140b80ca4585dc3","line_count":416,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_listen_socket_multiply_14.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-14.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 14 Control flow: if(globalFive==5) and if(globalFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_14_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalFive==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalFive==5)\r\n {\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalFive==5 to globalFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalFive==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < (INT_MAX/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalFive==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalFive==5)\r\n {\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < (INT_MAX/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalFive==5 to globalFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n if(globalFive==5)\r\n {\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalFive==5)\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n if(globalFive==5)\r\n {\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_14_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_14_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_14_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_multiply_22a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_multiply_22a.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":10968,"source_sha256":"82b3cae9e438383dde738b31d3b911a59cfbec2281d60614a623e775ad43dfd0","line_count":345,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_listen_socket_multiply_22a.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-22a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 22 Control flow: Flow controlled by value of a global variable. Sink functions are in a separate file from sources.\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\n/* The global variable below is used to drive control flow in the sink function */\r\nint CWE190_Integer_Overflow__int_listen_socket_multiply_22_badGlobal = 0;\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_22_badSink(int data);\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_22_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_22_badGlobal = 1; /* true */\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_22_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* The global variables below are used to drive control flow in the sink functions. */\r\nint CWE190_Integer_Overflow__int_listen_socket_multiply_22_goodB2G1Global = 0;\r\nint CWE190_Integer_Overflow__int_listen_socket_multiply_22_goodB2G2Global = 0;\r\nint CWE190_Integer_Overflow__int_listen_socket_multiply_22_goodG2BGlobal = 0;\r\n\r\n/* goodB2G1() - use badsource and goodsink by setting the static variable to false instead of true */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_22_goodB2G1Sink(int data);\r\n\r\nstatic void goodB2G1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_22_goodB2G1Global = 0; /* false */\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_22_goodB2G1Sink(data);\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_22_goodB2G2Sink(int data);\r\n\r\nstatic void goodB2G2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_22_goodB2G2Global = 1; /* true */\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_22_goodB2G2Sink(data);\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_22_goodG2BSink(int data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_22_goodG2BGlobal = 1; /* true */\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_22_goodG2BSink(data);\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_22_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_22_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_22_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_multiply_54a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_multiply_54a.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":7623,"source_sha256":"34408629e278a86e4998562017ef6093d21b53baf4b9a3411714ac6fe94e8580","line_count":253,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_listen_socket_multiply_54a.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-54a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_54b_badSink(int data);\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_54_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_54b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_54b_goodG2BSink(int data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_54b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_54b_goodB2GSink(int data);\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_54b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_54_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_54_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_54_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_multiply_54c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_multiply_54c.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":2292,"source_sha256":"2877498182d6c0270aa9eb5dc44491463b3b09b8069bb0ea1d4c4a764b3cd6c3","line_count":72,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_listen_socket_multiply_54c.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-54c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_54d_badSink(int data);\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_54c_badSink(int data)\r\n{\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_54d_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_54d_goodG2BSink(int data);\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_54c_goodG2BSink(int data)\r\n{\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_54d_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_54d_goodB2GSink(int data);\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_54c_goodB2GSink(int data)\r\n{\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_54d_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_multiply_63a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_multiply_63a.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":7586,"source_sha256":"d147a5c59b7e39a5de37cb175ff28027adea19bd64fd6cac81fc2569712322e1","line_count":253,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_listen_socket_multiply_63a.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-63a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_63b_badSink(int * dataPtr);\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_63_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_63b_badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_63b_goodG2BSink(int * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_63b_goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_63b_goodB2GSink(int * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_63b_goodB2GSink(&data);\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_63_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_63_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_63_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_multiply_65a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_multiply_65a.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":7807,"source_sha256":"af1e40c24084531d8979426584d9e9deb683f89b29f14f03a433587e5983ef20","line_count":258,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_listen_socket_multiply_65a.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-65a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_65b_badSink(int data);\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_65_bad()\r\n{\r\n int data;\r\n /* define a function pointer */\r\n void (*funcPtr) (int) = CWE190_Integer_Overflow__int_listen_socket_multiply_65b_badSink;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n /* use the function pointer */\r\n funcPtr(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_65b_goodG2BSink(int data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n void (*funcPtr) (int) = CWE190_Integer_Overflow__int_listen_socket_multiply_65b_goodG2BSink;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n funcPtr(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_65b_goodB2GSink(int data);\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n void (*funcPtr) (int) = CWE190_Integer_Overflow__int_listen_socket_multiply_65b_goodB2GSink;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n funcPtr(data);\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_65_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_65_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_multiply_65_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_multiply_81_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_multiply_81_goodB2G.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":1416,"source_sha256":"4f0736dc04a2acfc3e8234a8ea884483ddea78a73845d6ef947106719084f91a","line_count":43,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_listen_socket_multiply_81_goodB2G.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-81_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE190_Integer_Overflow__int_listen_socket_multiply_81.h\"\r\n\r\nnamespace CWE190_Integer_Overflow__int_listen_socket_multiply_81\r\n{\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_81_goodB2G::action(int data) const\r\n{\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < (INT_MAX/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_multiply_82_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_multiply_82_bad.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":1217,"source_sha256":"7e656238cfd518be32ac4ae3945d21e597098d03b9dacf729e6bf27b8be92fe0","line_count":36,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_listen_socket_multiply_82_bad.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-82_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE190_Integer_Overflow__int_listen_socket_multiply_82.h\"\r\n\r\nnamespace CWE190_Integer_Overflow__int_listen_socket_multiply_82\r\n{\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_multiply_82_bad::action(int data)\r\n{\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_square_09.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_square_09.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":12461,"source_sha256":"181c648ae1dea93ce5a2b0a3b887250a2fdf34af30e039063ffb94dc754d22d4","line_count":409,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_listen_socket_square_09.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-09.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_square_09_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data > INT_MIN && abs(data) < (long)sqrt((double)INT_MAX))\r\n {\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data > INT_MIN && abs(data) < (long)sqrt((double)INT_MAX))\r\n {\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_square_09_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_square_09_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_square_09_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_square_10.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_square_10.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":12349,"source_sha256":"ab891d5ea0d72e45cb27a084d7f84ef553601d02fd073060c9e512e477b5c227","line_count":409,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_listen_socket_square_10.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-10.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 10 Control flow: if(globalTrue) and if(globalFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_square_10_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalTrue)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalTrue)\r\n {\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalTrue to globalFalse */\r\nstatic void goodB2G1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalTrue)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data > INT_MIN && abs(data) < (long)sqrt((double)INT_MAX))\r\n {\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalTrue)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalTrue)\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data > INT_MIN && abs(data) < (long)sqrt((double)INT_MAX))\r\n {\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalTrue to globalFalse */\r\nstatic void goodG2B1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n if(globalTrue)\r\n {\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalTrue)\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n if(globalTrue)\r\n {\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_square_10_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_square_10_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_square_10_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_square_41.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_square_41.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":7846,"source_sha256":"93623249cb2e3ff32e378dc67d20b7124e896117ee27a26440b9c4699a049bd7","line_count":280,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_listen_socket_square_41.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-41.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 41 Data flow: data passed as an argument from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink(int data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_square_41_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink(int data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink(int data)\r\n{\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data > INT_MIN && abs(data) < (long)sqrt((double)INT_MAX))\r\n {\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n goodB2GSink(data);\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_square_41_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_square_41_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_square_41_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_square_53a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_square_53a.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":7587,"source_sha256":"eb3d98abc25fae8e4b2f6dd0e728d40fad9fbdb5afe187d89fa38cfea7c3d289","line_count":255,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_listen_socket_square_53a.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-53a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_square_53b_badSink(int data);\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_square_53_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE190_Integer_Overflow__int_listen_socket_square_53b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_square_53b_goodG2BSink(int data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n CWE190_Integer_Overflow__int_listen_socket_square_53b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE190_Integer_Overflow__int_listen_socket_square_53b_goodB2GSink(int data);\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE190_Integer_Overflow__int_listen_socket_square_53b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_listen_socket_square_53_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_square_53_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_listen_socket_square_53_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_square_82.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_square_82.h","cwe_id":"CWE190","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1543,"source_sha256":"99a3a641e0e8e89c86d9b3f872690c48df15d1a3f983bc8d2fdd03e723f5dfd3","line_count":56,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_listen_socket_square_82.h\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-82.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE190_Integer_Overflow__int_listen_socket_square_82\r\n{\r\n\r\nclass CWE190_Integer_Overflow__int_listen_socket_square_82_base\r\n{\r\npublic:\r\n /* pure virtual function */\r\n virtual void action(int data) = 0;\r\n};\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE190_Integer_Overflow__int_listen_socket_square_82_bad : public CWE190_Integer_Overflow__int_listen_socket_square_82_base\r\n{\r\npublic:\r\n void action(int data);\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE190_Integer_Overflow__int_listen_socket_square_82_goodG2B : public CWE190_Integer_Overflow__int_listen_socket_square_82_base\r\n{\r\npublic:\r\n void action(int data);\r\n};\r\n\r\nclass CWE190_Integer_Overflow__int_listen_socket_square_82_goodB2G : public CWE190_Integer_Overflow__int_listen_socket_square_82_base\r\n{\r\npublic:\r\n void action(int data);\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_square_83_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_listen_socket_square_83_bad.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":4016,"source_sha256":"d260ba3fc8a32096258fd27e5f5ffd8048755a5b6e95d51384bb8fa255f08480","line_count":131,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_listen_socket_square_83_bad.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-83_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE190_Integer_Overflow__int_listen_socket_square_83.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#include \r\n\r\nnamespace CWE190_Integer_Overflow__int_listen_socket_square_83\r\n{\r\nCWE190_Integer_Overflow__int_listen_socket_square_83_bad::CWE190_Integer_Overflow__int_listen_socket_square_83_bad(int dataCopy)\r\n{\r\n data = dataCopy;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n}\r\n\r\nCWE190_Integer_Overflow__int_listen_socket_square_83_bad::~CWE190_Integer_Overflow__int_listen_socket_square_83_bad()\r\n{\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n}\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_add_06.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_add_06.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":5074,"source_sha256":"c049fa37f84f5ca00d02ddc63bc3479ac5053bd4347ebd198c7cfb58d5489722","line_count":189,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_max_add_06.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-06.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: max Set data to the max value for int\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: add\r\n * GoodSink: Ensure there will not be an overflow before adding 1 to data\r\n * BadSink : Add 1 to data, which can cause an overflow\r\n * Flow Variant: 06 Control flow: if(STATIC_CONST_FIVE==5) and if(STATIC_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The variable below is declared \"const\", so a tool should be able\r\n to identify that reads of this will always give its initialized\r\n value. */\r\nstatic const int STATIC_CONST_FIVE = 5;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE190_Integer_Overflow__int_max_add_06_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodB2G1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n }\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < INT_MAX)\r\n {\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < INT_MAX)\r\n {\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_max_add_06_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_max_add_06_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_max_add_06_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_add_07.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_add_07.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":4991,"source_sha256":"500a17a54dd6b329c529fc902466397f4dec773e9d27af54293a1b2c293a4993","line_count":189,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_max_add_07.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-07.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: max Set data to the max value for int\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: add\r\n * GoodSink: Ensure there will not be an overflow before adding 1 to data\r\n * BadSink : Add 1 to data, which can cause an overflow\r\n * Flow Variant: 07 Control flow: if(staticFive==5) and if(staticFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The variable below is not declared \"const\", but is never assigned\r\n any other value so a tool should be able to identify that reads of\r\n this will always give its initialized value. */\r\nstatic int staticFive = 5;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE190_Integer_Overflow__int_max_add_07_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n }\r\n if(staticFive==5)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticFive==5 to staticFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n }\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < INT_MAX)\r\n {\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n }\r\n if(staticFive==5)\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < INT_MAX)\r\n {\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticFive==5 to staticFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n if(staticFive==5)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(staticFive==5)\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n if(staticFive==5)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_max_add_07_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_max_add_07_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_max_add_07_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_add_12.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_add_12.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":4742,"source_sha256":"7f1e2b94f5c62c17c3e1a7750e62be1c0a9c96887f95b8beda15349e942e8fb0","line_count":178,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_max_add_12.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-12.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: max Set data to the max value for int\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: add\r\n * GoodSink: Ensure there will not be an overflow before adding 1 to data\r\n * BadSink : Add 1 to data, which can cause an overflow\r\n * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE190_Integer_Overflow__int_max_add_12_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < INT_MAX)\r\n {\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the first \"if\" so that\r\n both branches use the BadSource and the second \"if\" so that both branches\r\n use the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n }\r\n else\r\n {\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < INT_MAX)\r\n {\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < INT_MAX)\r\n {\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the first \"if\" so that\r\n both branches use the GoodSource and the second \"if\" so that both branches\r\n use the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n }\r\n else\r\n {\r\n {\r\n /* POTENTIAL FLAW: Adding 1 to data could cause an overflow */\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_max_add_12_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_max_add_12_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_max_add_12_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_add_54c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_add_54c.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":1568,"source_sha256":"111c82ac81e5c4fe307185a0ce887ba8ee3f60d849f2b3aa8346d7ee75d587a1","line_count":50,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_max_add_54c.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-54c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: max Set data to the max value for int\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: add\r\n * GoodSink: Ensure there will not be an overflow before adding 1 to data\r\n * BadSink : Add 1 to data, which can cause an overflow\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE190_Integer_Overflow__int_max_add_54d_badSink(int data);\r\n\r\nvoid CWE190_Integer_Overflow__int_max_add_54c_badSink(int data)\r\n{\r\n CWE190_Integer_Overflow__int_max_add_54d_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE190_Integer_Overflow__int_max_add_54d_goodG2BSink(int data);\r\n\r\nvoid CWE190_Integer_Overflow__int_max_add_54c_goodG2BSink(int data)\r\n{\r\n CWE190_Integer_Overflow__int_max_add_54d_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE190_Integer_Overflow__int_max_add_54d_goodB2GSink(int data);\r\n\r\nvoid CWE190_Integer_Overflow__int_max_add_54c_goodB2GSink(int data)\r\n{\r\n CWE190_Integer_Overflow__int_max_add_54d_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_add_64a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_add_64a.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":2771,"source_sha256":"94ababeed60033d2f525541ab3cec3c5e9e21ea823447ef11b172830da744b54","line_count":97,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_max_add_64a.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-64a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: max Set data to the max value for int\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: add\r\n * GoodSink: Ensure there will not be an overflow before adding 1 to data\r\n * BadSink : Add 1 to data, which can cause an overflow\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE190_Integer_Overflow__int_max_add_64b_badSink(void * dataVoidPtr);\r\n\r\nvoid CWE190_Integer_Overflow__int_max_add_64_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n CWE190_Integer_Overflow__int_max_add_64b_badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE190_Integer_Overflow__int_max_add_64b_goodG2BSink(void * dataVoidPtr);\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n CWE190_Integer_Overflow__int_max_add_64b_goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE190_Integer_Overflow__int_max_add_64b_goodB2GSink(void * dataVoidPtr);\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n CWE190_Integer_Overflow__int_max_add_64b_goodB2GSink(&data);\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_max_add_64_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_max_add_64_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_max_add_64_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_add_81_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_add_81_goodB2G.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":1187,"source_sha256":"5e6f2b612a0132ce7bae981eedd0c0d6bbc3445535ce4aeb40cb5de2e40b6792","line_count":40,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_max_add_81_goodB2G.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-81_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: max Set data to the max value for int\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: add\r\n * GoodSink: Ensure there will not be an overflow before adding 1 to data\r\n * BadSink : Add 1 to data, which can cause an overflow\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE190_Integer_Overflow__int_max_add_81.h\"\r\n\r\nnamespace CWE190_Integer_Overflow__int_max_add_81\r\n{\r\n\r\nvoid CWE190_Integer_Overflow__int_max_add_81_goodB2G::action(int data) const\r\n{\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < INT_MAX)\r\n {\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_add_82_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_add_82_goodB2G.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":1179,"source_sha256":"3bc12624a089cbc2c38210fff878572fbd6da8c0090307142ab01d0de372d30d","line_count":40,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_max_add_82_goodB2G.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-82_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: max Set data to the max value for int\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: add\r\n * GoodSink: Ensure there will not be an overflow before adding 1 to data\r\n * BadSink : Add 1 to data, which can cause an overflow\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE190_Integer_Overflow__int_max_add_82.h\"\r\n\r\nnamespace CWE190_Integer_Overflow__int_max_add_82\r\n{\r\n\r\nvoid CWE190_Integer_Overflow__int_max_add_82_goodB2G::action(int data)\r\n{\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < INT_MAX)\r\n {\r\n int result = data + 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_multiply_01.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_multiply_01.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":3073,"source_sha256":"673f933e6a041dba34d6c6a161f8c547318a9581a9a58d24499a9f830fa935d2","line_count":112,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_max_multiply_01.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-01.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: max Set data to the max value for int\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 01 Baseline\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE190_Integer_Overflow__int_max_multiply_01_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < (INT_MAX/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_max_multiply_01_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_max_multiply_01_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_max_multiply_01_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_multiply_44.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_multiply_44.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":3578,"source_sha256":"5ef52f787022f45ad8b2f07ec18b2b55c590720c5ccc2a17b9b8c700962b496d","line_count":131,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_max_multiply_44.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-44.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: max Set data to the max value for int\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * 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\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink(int data)\r\n{\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_max_multiply_44_bad()\r\n{\r\n int data;\r\n /* define a function pointer */\r\n void (*funcPtr) (int) = badSink;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n /* use the function pointer */\r\n funcPtr(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink(int data)\r\n{\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n void (*funcPtr) (int) = goodG2BSink;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n funcPtr(data);\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink(int data)\r\n{\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < (INT_MAX/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n void (*funcPtr) (int) = goodB2GSink;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n funcPtr(data);\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_max_multiply_44_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_max_multiply_44_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_max_multiply_44_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_multiply_45.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_multiply_45.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":3959,"source_sha256":"6fea3b89d487ce6d0f3cc0532334f5e6f7b6c5ec2b8552b6b5e38e3955b016f4","line_count":136,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_max_multiply_45.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-45.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: max Set data to the max value for int\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 45 Data flow: data passed as a static global variable from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nstatic int CWE190_Integer_Overflow__int_max_multiply_45_badData;\r\nstatic int CWE190_Integer_Overflow__int_max_multiply_45_goodG2BData;\r\nstatic int CWE190_Integer_Overflow__int_max_multiply_45_goodB2GData;\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink()\r\n{\r\n int data = CWE190_Integer_Overflow__int_max_multiply_45_badData;\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_max_multiply_45_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n CWE190_Integer_Overflow__int_max_multiply_45_badData = data;\r\n badSink();\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink()\r\n{\r\n int data = CWE190_Integer_Overflow__int_max_multiply_45_goodG2BData;\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n CWE190_Integer_Overflow__int_max_multiply_45_goodG2BData = data;\r\n goodG2BSink();\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink()\r\n{\r\n int data = CWE190_Integer_Overflow__int_max_multiply_45_goodB2GData;\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < (INT_MAX/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n CWE190_Integer_Overflow__int_max_multiply_45_goodB2GData = data;\r\n goodB2GSink();\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_max_multiply_45_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_max_multiply_45_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_max_multiply_45_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_multiply_53b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_multiply_53b.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":1648,"source_sha256":"08e7241ca20c0f32927870b27bf6ae6b592499b59755a3716ff63aaa71185406","line_count":50,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_max_multiply_53b.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-53b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: max Set data to the max value for int\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE190_Integer_Overflow__int_max_multiply_53c_badSink(int data);\r\n\r\nvoid CWE190_Integer_Overflow__int_max_multiply_53b_badSink(int data)\r\n{\r\n CWE190_Integer_Overflow__int_max_multiply_53c_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE190_Integer_Overflow__int_max_multiply_53c_goodG2BSink(int data);\r\n\r\nvoid CWE190_Integer_Overflow__int_max_multiply_53b_goodG2BSink(int data)\r\n{\r\n CWE190_Integer_Overflow__int_max_multiply_53c_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE190_Integer_Overflow__int_max_multiply_53c_goodB2GSink(int data);\r\n\r\nvoid CWE190_Integer_Overflow__int_max_multiply_53b_goodB2GSink(int data)\r\n{\r\n CWE190_Integer_Overflow__int_max_multiply_53c_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_multiply_62b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_multiply_62b.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":1392,"source_sha256":"f87c8bb4f96586bb152733d413e7b5b0974ff00a8e0363b3397627c1d5964018","line_count":51,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_max_multiply_62b.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-62b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: max Set data to the max value for int\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE190_Integer_Overflow__int_max_multiply_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSource(int &data)\r\n{\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(int &data)\r\n{\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(int &data)\r\n{\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_multiply_64b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_multiply_64b.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":2499,"source_sha256":"428f90f9e4cb79e2def2a1ccbc62fc3cde1d8e9972a0187890306a8c6771ed9f","line_count":77,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_max_multiply_64b.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-64b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: max Set data to the max value for int\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE190_Integer_Overflow__int_max_multiply_64b_badSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n int * dataPtr = (int *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n int data = (*dataPtr);\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE190_Integer_Overflow__int_max_multiply_64b_goodG2BSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n int * dataPtr = (int *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n int data = (*dataPtr);\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE190_Integer_Overflow__int_max_multiply_64b_goodB2GSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n int * dataPtr = (int *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n int data = (*dataPtr);\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data < (INT_MAX/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_multiply_73a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_multiply_73a.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":3061,"source_sha256":"4cecf5b7f80c2f230a8ca3374589e856d25c379424359244b910f2debba10ab6","line_count":121,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_max_multiply_73a.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-73a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: max Set data to the max value for int\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE190_Integer_Overflow__int_max_multiply_73\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(list dataList);\r\n\r\nvoid bad()\r\n{\r\n int data;\r\n list dataList;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n /* Put data in a list */\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n badSink(dataList);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(list dataList);\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n list dataList;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n /* Put data in a list */\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n goodG2BSink(dataList);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(list dataList);\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n list dataList;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n goodB2GSink(dataList);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE190_Integer_Overflow__int_max_multiply_73; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_multiply_82_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_multiply_82_bad.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":1155,"source_sha256":"74a1adaadd404f0f3c63c0d647581c7244e21a41dfd13ffd1cb7d7c03ee7c037","line_count":36,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_max_multiply_82_bad.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-82_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: max Set data to the max value for int\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an overflow before multiplying data by 2\r\n * BadSink : If data is positive, multiply by 2, which can cause an overflow\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE190_Integer_Overflow__int_max_multiply_82.h\"\r\n\r\nnamespace CWE190_Integer_Overflow__int_max_multiply_82\r\n{\r\n\r\nvoid CWE190_Integer_Overflow__int_max_multiply_82_bad::action(int data)\r\n{\r\n if(data > 0) /* ensure we won't have an underflow */\r\n {\r\n /* POTENTIAL FLAW: if (data*2) > INT_MAX, this will overflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_square_11.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_square_11.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":5015,"source_sha256":"52462609f6ab1cde1f2634a042411ff1d56b4c6a08bdb79495a07844bd40b017","line_count":186,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_max_square_11.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-11.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: max Set data to the max value for int\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE190_Integer_Overflow__int_max_square_11_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n }\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data > INT_MIN && abs(data) < (long)sqrt((double)INT_MAX))\r\n {\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data > INT_MIN && abs(data) < (long)sqrt((double)INT_MAX))\r\n {\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_max_square_11_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_max_square_11_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_max_square_11_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_square_12.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_square_12.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":4934,"source_sha256":"a5043816515890cdc221dc9f96871b1e25001c59796e3dc077edaa6078dc69b5","line_count":180,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_max_square_12.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-12.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: max Set data to the max value for int\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE190_Integer_Overflow__int_max_square_12_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data > INT_MIN && abs(data) < (long)sqrt((double)INT_MAX))\r\n {\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the first \"if\" so that\r\n both branches use the BadSource and the second \"if\" so that both branches\r\n use the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n }\r\n else\r\n {\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data > INT_MIN && abs(data) < (long)sqrt((double)INT_MAX))\r\n {\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data > INT_MIN && abs(data) < (long)sqrt((double)INT_MAX))\r\n {\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the first \"if\" so that\r\n both branches use the GoodSource and the second \"if\" so that both branches\r\n use the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n }\r\n else\r\n {\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE190_Integer_Overflow__int_max_square_12_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE190_Integer_Overflow__int_max_square_12_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE190_Integer_Overflow__int_max_square_12_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_square_53c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_square_53c.c","cwe_id":"CWE190","file_extension":".c","language_hint":"C","source_size_bytes":1615,"source_sha256":"8ed3d3eec3826083f4bf43fdc9bce8e2cbf574231f10b2f2836064a650ed5d21","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_max_square_53c.c\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-53c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: max Set data to the max value for int\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE190_Integer_Overflow__int_max_square_53d_badSink(int data);\r\n\r\nvoid CWE190_Integer_Overflow__int_max_square_53c_badSink(int data)\r\n{\r\n CWE190_Integer_Overflow__int_max_square_53d_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE190_Integer_Overflow__int_max_square_53d_goodG2BSink(int data);\r\n\r\nvoid CWE190_Integer_Overflow__int_max_square_53c_goodG2BSink(int data)\r\n{\r\n CWE190_Integer_Overflow__int_max_square_53d_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE190_Integer_Overflow__int_max_square_53d_goodB2GSink(int data);\r\n\r\nvoid CWE190_Integer_Overflow__int_max_square_53c_goodB2GSink(int data)\r\n{\r\n CWE190_Integer_Overflow__int_max_square_53d_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_square_62a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_square_62a.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":2918,"source_sha256":"b4e5252dd0a8248c5f1c3ca81c5c7a22197f484fb63549622dfb7664991a4635","line_count":120,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_max_square_62a.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-62a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: max Set data to the max value for int\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE190_Integer_Overflow__int_max_square_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSource(int &data);\r\n\r\nvoid bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n badSource(data);\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(int &data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n goodG2BSource(data);\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(int &data);\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n goodB2GSource(data);\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data > INT_MIN && abs(data) < (long)sqrt((double)INT_MAX))\r\n {\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE190_Integer_Overflow__int_max_square_62; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_square_72b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_square_72b.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":1917,"source_sha256":"a8ac89e8066f81eb8d09a24f14f70e6a5fee41aa0a4e3a033aba25cd8101fbef","line_count":74,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_max_square_72b.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-72b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: max Set data to the max value for int\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 72 Data flow: data passed in a vector from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE190_Integer_Overflow__int_max_square_72\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(vector dataVector)\r\n{\r\n /* copy data out of dataVector */\r\n int data = dataVector[2];\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(vector dataVector)\r\n{\r\n int data = dataVector[2];\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(vector dataVector)\r\n{\r\n int data = dataVector[2];\r\n /* FIX: Add a check to prevent an overflow from occurring */\r\n if (data > INT_MIN && abs(data) < (long)sqrt((double)INT_MAX))\r\n {\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform arithmetic safely.\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_square_81_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_square_81_bad.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":1090,"source_sha256":"8466162b426caf8e635e75810b278077bee87585e630ea395ecb0e6028fba8c0","line_count":37,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_max_square_81_bad.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-81_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: max Set data to the max value for int\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE190_Integer_Overflow__int_max_square_81.h\"\r\n\r\n#include \r\n\r\nnamespace CWE190_Integer_Overflow__int_max_square_81\r\n{\r\n\r\nvoid CWE190_Integer_Overflow__int_max_square_81_bad::action(int data) const\r\n{\r\n {\r\n /* POTENTIAL FLAW: if (data*data) > INT_MAX, this will overflow */\r\n int result = data * data;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_square_81a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_square_81a.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":2853,"source_sha256":"6d6a2c327dab6ac224d4ad21c5438414433dff741c52f51dd75ab356cee8ad14","line_count":101,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_max_square_81a.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-81a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: max Set data to the max value for int\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE190_Integer_Overflow__int_max_square_81.h\"\r\n\r\nnamespace CWE190_Integer_Overflow__int_max_square_81\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n const CWE190_Integer_Overflow__int_max_square_81_base& baseObject = CWE190_Integer_Overflow__int_max_square_81_bad();\r\n baseObject.action(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n const CWE190_Integer_Overflow__int_max_square_81_base& baseObject = CWE190_Integer_Overflow__int_max_square_81_goodG2B();\r\n baseObject.action(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n const CWE190_Integer_Overflow__int_max_square_81_base& baseObject = CWE190_Integer_Overflow__int_max_square_81_goodB2G();\r\n baseObject.action(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE190_Integer_Overflow__int_max_square_81; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_square_82a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE190_Integer_Overflow/s03/CWE190_Integer_Overflow__int_max_square_82a.cpp","cwe_id":"CWE190","file_extension":".cpp","language_hint":"C++","source_size_bytes":2914,"source_sha256":"7e10d0e77c6097a9fe74dc3915042942b885bf2b09598d5909c49fe6d0474efd","line_count":104,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE190_Integer_Overflow__int_max_square_82a.cpp\r\nLabel Definition File: CWE190_Integer_Overflow__int.label.xml\r\nTemplate File: sources-sinks-82a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 190 Integer Overflow\r\n * BadSource: max Set data to the max value for int\r\n * GoodSource: Set data to a small, non-zero number (two)\r\n * Sinks: square\r\n * GoodSink: Ensure there will not be an overflow before squaring data\r\n * BadSink : Square data, which can lead to overflow\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE190_Integer_Overflow__int_max_square_82.h\"\r\n\r\nnamespace CWE190_Integer_Overflow__int_max_square_82\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n CWE190_Integer_Overflow__int_max_square_82_base* baseObject = new CWE190_Integer_Overflow__int_max_square_82_bad;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer overflow in the sinks */\r\n data = 2;\r\n CWE190_Integer_Overflow__int_max_square_82_base* baseObject = new CWE190_Integer_Overflow__int_max_square_82_goodG2B;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the maximum value for this type */\r\n data = INT_MAX;\r\n CWE190_Integer_Overflow__int_max_square_82_base* baseObject = new CWE190_Integer_Overflow__int_max_square_82_goodB2G;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE190_Integer_Overflow__int_max_square_82; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_multiply_01.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_multiply_01.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":3098,"source_sha256":"907d1af29a07a9cc45e9347e1bcb02302244acbdcfbcd77081c89db8684e6391","line_count":112,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__int_rand_multiply_01.c\r\nLabel Definition File: CWE191_Integer_Underflow__int.label.xml\r\nTemplate File: sources-sinks-01.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand(), which may be zero\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 01 Baseline\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__int_rand_multiply_01_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Set data to a random value */\r\n data = RAND32();\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < INT_MIN, this will underflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer underflow in the sinks */\r\n data = -2;\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < INT_MIN, this will underflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Set data to a random value */\r\n data = RAND32();\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (INT_MIN/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__int_rand_multiply_01_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__int_rand_multiply_01_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__int_rand_multiply_01_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_multiply_03.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_multiply_03.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":5143,"source_sha256":"758c12d949af3ab8c7893cc6051fc7a566418735c8c467956f360411794e7aee","line_count":193,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__int_rand_multiply_03.c\r\nLabel Definition File: CWE191_Integer_Underflow__int.label.xml\r\nTemplate File: sources-sinks-03.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand(), which may be zero\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 03 Control flow: if(5==5) and if(5!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__int_rand_multiply_03_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Set data to a random value */\r\n data = RAND32();\r\n }\r\n if(5==5)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < INT_MIN, this will underflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 5==5 to 5!=5 */\r\nstatic void goodB2G1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Set data to a random value */\r\n data = RAND32();\r\n }\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (INT_MIN/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Set data to a random value */\r\n data = RAND32();\r\n }\r\n if(5==5)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (INT_MIN/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 5==5 to 5!=5 */\r\nstatic void goodG2B1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer underflow in the sinks */\r\n data = -2;\r\n }\r\n if(5==5)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < INT_MIN, this will underflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(5==5)\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer underflow in the sinks */\r\n data = -2;\r\n }\r\n if(5==5)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < INT_MIN, this will underflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__int_rand_multiply_03_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__int_rand_multiply_03_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__int_rand_multiply_03_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_multiply_05.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_multiply_05.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":5535,"source_sha256":"af7dcbf096fcd4912ac91797cb6badb40aa0ad67415cf0e2ffb82221fe1823ce","line_count":199,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__int_rand_multiply_05.c\r\nLabel Definition File: CWE191_Integer_Underflow__int.label.xml\r\nTemplate File: sources-sinks-05.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand(), which may be zero\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 05 Control flow: if(staticTrue) and if(staticFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The two variables below are not defined as \"const\", but are never\r\n assigned any other value, so a tool should be able to identify that\r\n reads of these will always return their initialized values. */\r\nstatic int staticTrue = 1; /* true */\r\nstatic int staticFalse = 0; /* false */\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__int_rand_multiply_05_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Set data to a random value */\r\n data = RAND32();\r\n }\r\n if(staticTrue)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < INT_MIN, this will underflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticTrue to staticFalse */\r\nstatic void goodB2G1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Set data to a random value */\r\n data = RAND32();\r\n }\r\n if(staticFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (INT_MIN/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Set data to a random value */\r\n data = RAND32();\r\n }\r\n if(staticTrue)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (INT_MIN/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticTrue to staticFalse */\r\nstatic void goodG2B1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(staticFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer underflow in the sinks */\r\n data = -2;\r\n }\r\n if(staticTrue)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < INT_MIN, this will underflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n if(staticTrue)\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer underflow in the sinks */\r\n data = -2;\r\n }\r\n if(staticTrue)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < INT_MIN, this will underflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__int_rand_multiply_05_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__int_rand_multiply_05_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__int_rand_multiply_05_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_multiply_17.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_multiply_17.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":3563,"source_sha256":"8f541488a69eb63c87c6cca5161021cd3d2cfd3289624335686fd7e9a555b333","line_count":133,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__int_rand_multiply_17.c\r\nLabel Definition File: CWE191_Integer_Underflow__int.label.xml\r\nTemplate File: sources-sinks-17.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand(), which may be zero\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 17 Control flow: for loops\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__int_rand_multiply_17_bad()\r\n{\r\n int i,j;\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n for(i = 0; i < 1; i++)\r\n {\r\n /* POTENTIAL FLAW: Set data to a random value */\r\n data = RAND32();\r\n }\r\n for(j = 0; j < 1; j++)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < INT_MIN, this will underflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink in the for statements */\r\nstatic void goodB2G()\r\n{\r\n int i,k;\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n for(i = 0; i < 1; i++)\r\n {\r\n /* POTENTIAL FLAW: Set data to a random value */\r\n data = RAND32();\r\n }\r\n for(k = 0; k < 1; k++)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (INT_MIN/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink in the for statements */\r\nstatic void goodG2B()\r\n{\r\n int h,j;\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n for(h = 0; h < 1; h++)\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer underflow in the sinks */\r\n data = -2;\r\n }\r\n for(j = 0; j < 1; j++)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < INT_MIN, this will underflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__int_rand_multiply_17_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__int_rand_multiply_17_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__int_rand_multiply_17_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_multiply_67a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_multiply_67a.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":3527,"source_sha256":"2028ad245077330738da18ee59ead137a3eee4f11d6a22f16bdf2fb3737460f2","line_count":108,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__int_rand_multiply_67a.c\r\nLabel Definition File: CWE191_Integer_Underflow__int.label.xml\r\nTemplate File: sources-sinks-67a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand(), which may be zero\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\ntypedef struct _CWE191_Integer_Underflow__int_rand_multiply_67_structType\r\n{\r\n int structFirst;\r\n} CWE191_Integer_Underflow__int_rand_multiply_67_structType;\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE191_Integer_Underflow__int_rand_multiply_67b_badSink(CWE191_Integer_Underflow__int_rand_multiply_67_structType myStruct);\r\n\r\nvoid CWE191_Integer_Underflow__int_rand_multiply_67_bad()\r\n{\r\n int data;\r\n CWE191_Integer_Underflow__int_rand_multiply_67_structType myStruct;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Set data to a random value */\r\n data = RAND32();\r\n myStruct.structFirst = data;\r\n CWE191_Integer_Underflow__int_rand_multiply_67b_badSink(myStruct);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__int_rand_multiply_67b_goodG2BSink(CWE191_Integer_Underflow__int_rand_multiply_67_structType myStruct);\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n CWE191_Integer_Underflow__int_rand_multiply_67_structType myStruct;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer underflow in the sinks */\r\n data = -2;\r\n myStruct.structFirst = data;\r\n CWE191_Integer_Underflow__int_rand_multiply_67b_goodG2BSink(myStruct);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__int_rand_multiply_67b_goodB2GSink(CWE191_Integer_Underflow__int_rand_multiply_67_structType myStruct);\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n CWE191_Integer_Underflow__int_rand_multiply_67_structType myStruct;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Set data to a random value */\r\n data = RAND32();\r\n myStruct.structFirst = data;\r\n CWE191_Integer_Underflow__int_rand_multiply_67b_goodB2GSink(myStruct);\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__int_rand_multiply_67_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__int_rand_multiply_67_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__int_rand_multiply_67_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_multiply_73a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_multiply_73a.cpp","cwe_id":"CWE191","file_extension":".cpp","language_hint":"C++","source_size_bytes":3081,"source_sha256":"48cfbc5350529ca1b337e436778d021b6c6d38c47364ac114a95900b2aa19739","line_count":121,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__int_rand_multiply_73a.cpp\r\nLabel Definition File: CWE191_Integer_Underflow__int.label.xml\r\nTemplate File: sources-sinks-73a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand(), which may be zero\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE191_Integer_Underflow__int_rand_multiply_73\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(list dataList);\r\n\r\nvoid bad()\r\n{\r\n int data;\r\n list dataList;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Set data to a random value */\r\n data = RAND32();\r\n /* Put data in a list */\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n badSink(dataList);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(list dataList);\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n list dataList;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer underflow in the sinks */\r\n data = -2;\r\n /* Put data in a list */\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n goodG2BSink(dataList);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(list dataList);\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n list dataList;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Set data to a random value */\r\n data = RAND32();\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n goodB2GSink(dataList);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE191_Integer_Underflow__int_rand_multiply_73; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_multiply_74a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_multiply_74a.cpp","cwe_id":"CWE191","file_extension":".cpp","language_hint":"C++","source_size_bytes":3029,"source_sha256":"aac8c87d1dc78e7762b9faedbfee990f068482fc894704f6efc3392608243c10","line_count":121,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__int_rand_multiply_74a.cpp\r\nLabel Definition File: CWE191_Integer_Underflow__int.label.xml\r\nTemplate File: sources-sinks-74a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand(), which may be zero\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 74 Data flow: data passed in a map from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE191_Integer_Underflow__int_rand_multiply_74\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(map dataMap);\r\n\r\nvoid bad()\r\n{\r\n int data;\r\n map dataMap;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Set data to a random value */\r\n data = RAND32();\r\n /* Put data in a map */\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n badSink(dataMap);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(map dataMap);\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n map dataMap;\r\n /* Initialize data */\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an integer underflow in the sinks */\r\n data = -2;\r\n /* Put data in a map */\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n goodG2BSink(dataMap);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(map dataMap);\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n map dataMap;\r\n /* Initialize data */\r\n data = 0;\r\n /* POTENTIAL FLAW: Set data to a random value */\r\n data = RAND32();\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n goodB2GSink(dataMap);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE191_Integer_Underflow__int_rand_multiply_74; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_multiply_81_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_multiply_81_goodG2B.cpp","cwe_id":"CWE191","file_extension":".cpp","language_hint":"C++","source_size_bytes":1215,"source_sha256":"0679034660b5b57c995cb68f89c3b355e4b48dce1bca4f9e880f40d3649b6304","line_count":36,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__int_rand_multiply_81_goodG2B.cpp\r\nLabel Definition File: CWE191_Integer_Underflow__int.label.xml\r\nTemplate File: sources-sinks-81_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand(), which may be zero\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE191_Integer_Underflow__int_rand_multiply_81.h\"\r\n\r\nnamespace CWE191_Integer_Underflow__int_rand_multiply_81\r\n{\r\n\r\nvoid CWE191_Integer_Underflow__int_rand_multiply_81_goodG2B::action(int data) const\r\n{\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < INT_MIN, this will underflow */\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_multiply_83_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_multiply_83_goodB2G.cpp","cwe_id":"CWE191","file_extension":".cpp","language_hint":"C++","source_size_bytes":1665,"source_sha256":"acc6e2bac0d360276159385e8f2df0b30ed4cafddae6d49175142b75366baecd","line_count":48,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__int_rand_multiply_83_goodB2G.cpp\r\nLabel Definition File: CWE191_Integer_Underflow__int.label.xml\r\nTemplate File: sources-sinks-83_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand(), which may be zero\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE191_Integer_Underflow__int_rand_multiply_83.h\"\r\n\r\nnamespace CWE191_Integer_Underflow__int_rand_multiply_83\r\n{\r\nCWE191_Integer_Underflow__int_rand_multiply_83_goodB2G::CWE191_Integer_Underflow__int_rand_multiply_83_goodB2G(int dataCopy)\r\n{\r\n data = dataCopy;\r\n /* POTENTIAL FLAW: Set data to a random value */\r\n data = RAND32();\r\n}\r\n\r\nCWE191_Integer_Underflow__int_rand_multiply_83_goodB2G::~CWE191_Integer_Underflow__int_rand_multiply_83_goodB2G()\r\n{\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (INT_MIN/2))\r\n {\r\n int result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_sub_17.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_sub_17.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":3277,"source_sha256":"656c13f601fe730157e2a250b5a2c74ba350bceb92ac2dbca68cb7397b7f9836","line_count":128,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__int_rand_sub_17.c\r\nLabel Definition File: CWE191_Integer_Underflow__int.label.xml\r\nTemplate File: sources-sinks-17.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand(), which may be zero\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 17 Control flow: for loops\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__int_rand_sub_17_bad()\r\n{\r\n int i,j;\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n for(i = 0; i < 1; i++)\r\n {\r\n /* POTENTIAL FLAW: Set data to a random value */\r\n data = RAND32();\r\n }\r\n for(j = 0; j < 1; j++)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n int result = data - 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink in the for statements */\r\nstatic void goodB2G()\r\n{\r\n int i,k;\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n for(i = 0; i < 1; i++)\r\n {\r\n /* POTENTIAL FLAW: Set data to a random value */\r\n data = RAND32();\r\n }\r\n for(k = 0; k < 1; k++)\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > INT_MIN)\r\n {\r\n int result = data - 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink in the for statements */\r\nstatic void goodG2B()\r\n{\r\n int h,j;\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n for(h = 0; h < 1; h++)\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an integer underflow in the sinks */\r\n data = -2;\r\n }\r\n for(j = 0; j < 1; j++)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n int result = data - 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__int_rand_sub_17_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__int_rand_sub_17_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__int_rand_sub_17_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_sub_18.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_sub_18.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":3096,"source_sha256":"73ec740a1bd5553ef51e0b4cd283f8c742e5c577066832c9faf5036b9db43e5f","line_count":119,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__int_rand_sub_18.c\r\nLabel Definition File: CWE191_Integer_Underflow__int.label.xml\r\nTemplate File: sources-sinks-18.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand(), which may be zero\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 18 Control flow: goto statements\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__int_rand_sub_18_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n goto source;\r\nsource:\r\n /* POTENTIAL FLAW: Set data to a random value */\r\n data = RAND32();\r\n goto sink;\r\nsink:\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n int result = data - 1;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by reversing the blocks on the second goto statement */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n goto source;\r\nsource:\r\n /* POTENTIAL FLAW: Set data to a random value */\r\n data = RAND32();\r\n goto sink;\r\nsink:\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > INT_MIN)\r\n {\r\n int result = data - 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by reversing the blocks on the first goto statement */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = 0;\r\n goto source;\r\nsource:\r\n /* FIX: Use a small, non-zero value that will not cause an integer underflow in the sinks */\r\n data = -2;\r\n goto sink;\r\nsink:\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n int result = data - 1;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__int_rand_sub_18_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__int_rand_sub_18_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__int_rand_sub_18_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_sub_32.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_sub_32.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":3520,"source_sha256":"b1eb4df5dacfe0f06b1e87adcabd2aa46ad35afa9dc0740b180ffac728f8aece","line_count":133,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__int_rand_sub_32.c\r\nLabel Definition File: CWE191_Integer_Underflow__int.label.xml\r\nTemplate File: sources-sinks-32.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand(), which may be zero\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 32 Data flow using two pointers to the same value within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__int_rand_sub_32_bad()\r\n{\r\n int data;\r\n int *dataPtr1 = &data;\r\n int *dataPtr2 = &data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n int data = *dataPtr1;\r\n /* POTENTIAL FLAW: Set data to a random value */\r\n data = RAND32();\r\n *dataPtr1 = data;\r\n }\r\n {\r\n int data = *dataPtr2;\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n int result = data - 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n int *dataPtr1 = &data;\r\n int *dataPtr2 = &data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n int data = *dataPtr1;\r\n /* FIX: Use a small, non-zero value that will not cause an integer underflow in the sinks */\r\n data = -2;\r\n *dataPtr1 = data;\r\n }\r\n {\r\n int data = *dataPtr2;\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n int result = data - 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n int *dataPtr1 = &data;\r\n int *dataPtr2 = &data;\r\n /* Initialize data */\r\n data = 0;\r\n {\r\n int data = *dataPtr1;\r\n /* POTENTIAL FLAW: Set data to a random value */\r\n data = RAND32();\r\n *dataPtr1 = data;\r\n }\r\n {\r\n int data = *dataPtr2;\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > INT_MIN)\r\n {\r\n int result = data - 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__int_rand_sub_32_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__int_rand_sub_32_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__int_rand_sub_32_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_sub_65b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_sub_65b.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":1758,"source_sha256":"1aca629bbf89aae977282405597ddf6695a9bd37902732eef3340fd5da5e3ead","line_count":60,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__int_rand_sub_65b.c\r\nLabel Definition File: CWE191_Integer_Underflow__int.label.xml\r\nTemplate File: sources-sinks-65b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand(), which may be zero\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__int_rand_sub_65b_badSink(int data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n int result = data - 1;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__int_rand_sub_65b_goodG2BSink(int data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n int result = data - 1;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__int_rand_sub_65b_goodB2GSink(int data)\r\n{\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > INT_MIN)\r\n {\r\n int result = data - 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_sub_82_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__int_rand_sub_82_goodG2B.cpp","cwe_id":"CWE191","file_extension":".cpp","language_hint":"C++","source_size_bytes":1117,"source_sha256":"c30c29a334d8ceefdb6349290394559c3061b62f5e1cb6cbb452b4ba9e1e4b2a","line_count":35,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__int_rand_sub_82_goodG2B.cpp\r\nLabel Definition File: CWE191_Integer_Underflow__int.label.xml\r\nTemplate File: sources-sinks-82_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand(), which may be zero\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE191_Integer_Underflow__int_rand_sub_82.h\"\r\n\r\nnamespace CWE191_Integer_Underflow__int_rand_sub_82\r\n{\r\n\r\nvoid CWE191_Integer_Underflow__int_rand_sub_82_goodG2B::action(int data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n int result = data - 1;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_multiply_01.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_multiply_01.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":3077,"source_sha256":"e4d9dbf8ec3cd773157fbaa65f191ce4d0f346feaded29d40dc64f685ab77db2","line_count":109,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_fscanf_multiply_01.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-01.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 01 Baseline\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_multiply_01_bad()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n short data;\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (SHRT_MIN/2))\r\n {\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_multiply_01_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__short_fscanf_multiply_01_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__short_fscanf_multiply_01_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_multiply_43.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_multiply_43.cpp","cwe_id":"CWE191","file_extension":".cpp","language_hint":"C++","source_size_bytes":3393,"source_sha256":"726de14d8289c8c4ffbce24c531792784fd8ca3af58009ae8126b5192a935711","line_count":131,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_fscanf_multiply_43.cpp\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-43.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 43 Data flow: data flows using a C++ reference from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE191_Integer_Underflow__short_fscanf_multiply_43\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSource(short &data)\r\n{\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n}\r\n\r\nvoid bad()\r\n{\r\n short data;\r\n data = 0;\r\n badSource(data);\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSource(short &data)\r\n{\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n short data;\r\n data = 0;\r\n goodG2BSource(data);\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSource(short &data)\r\n{\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n short data;\r\n data = 0;\r\n goodB2GSource(data);\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (SHRT_MIN/2))\r\n {\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE191_Integer_Underflow__short_fscanf_multiply_43; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_multiply_63a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_multiply_63a.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":2867,"source_sha256":"39fa43e66eba3b9c43c7f0055f283c876de6480e666652d2981454b69be1ee81","line_count":94,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_fscanf_multiply_63a.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-63a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE191_Integer_Underflow__short_fscanf_multiply_63b_badSink(short * dataPtr);\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_multiply_63_bad()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n CWE191_Integer_Underflow__short_fscanf_multiply_63b_badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_multiply_63b_goodG2BSink(short * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n short data;\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n CWE191_Integer_Underflow__short_fscanf_multiply_63b_goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_multiply_63b_goodB2GSink(short * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n CWE191_Integer_Underflow__short_fscanf_multiply_63b_goodB2GSink(&data);\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_multiply_63_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__short_fscanf_multiply_63_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__short_fscanf_multiply_63_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_multiply_64a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_multiply_64a.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":2887,"source_sha256":"7e890e85aea096c2ce697fdbd0a37907ea368ebf6a7e68c71cf8988f934043d7","line_count":94,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_fscanf_multiply_64a.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-64a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE191_Integer_Underflow__short_fscanf_multiply_64b_badSink(void * dataVoidPtr);\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_multiply_64_bad()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n CWE191_Integer_Underflow__short_fscanf_multiply_64b_badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_multiply_64b_goodG2BSink(void * dataVoidPtr);\r\n\r\nstatic void goodG2B()\r\n{\r\n short data;\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n CWE191_Integer_Underflow__short_fscanf_multiply_64b_goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_multiply_64b_goodB2GSink(void * dataVoidPtr);\r\n\r\nstatic void goodB2G()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n CWE191_Integer_Underflow__short_fscanf_multiply_64b_goodB2GSink(&data);\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_multiply_64_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__short_fscanf_multiply_64_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__short_fscanf_multiply_64_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_multiply_67b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_multiply_67b.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":2470,"source_sha256":"5b02ae2c46d8104f33fc32e5c46ab4b812ff83be6e5970f6031ddce56a2a5049","line_count":73,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_fscanf_multiply_67b.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-67b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\ntypedef struct _CWE191_Integer_Underflow__short_fscanf_multiply_67_structType\r\n{\r\n short structFirst;\r\n} CWE191_Integer_Underflow__short_fscanf_multiply_67_structType;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_multiply_67b_badSink(CWE191_Integer_Underflow__short_fscanf_multiply_67_structType myStruct)\r\n{\r\n short data = myStruct.structFirst;\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_multiply_67b_goodG2BSink(CWE191_Integer_Underflow__short_fscanf_multiply_67_structType myStruct)\r\n{\r\n short data = myStruct.structFirst;\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_multiply_67b_goodB2GSink(CWE191_Integer_Underflow__short_fscanf_multiply_67_structType myStruct)\r\n{\r\n short data = myStruct.structFirst;\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (SHRT_MIN/2))\r\n {\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_multiply_81_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_multiply_81_goodB2G.cpp","cwe_id":"CWE191","file_extension":".cpp","language_hint":"C++","source_size_bytes":1399,"source_sha256":"2e06162b085b752756c3fa2a9ed61f7b898846697ef0cc03dccffc7ed67b9bce","line_count":43,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_fscanf_multiply_81_goodB2G.cpp\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-81_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE191_Integer_Underflow__short_fscanf_multiply_81.h\"\r\n\r\nnamespace CWE191_Integer_Underflow__short_fscanf_multiply_81\r\n{\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_multiply_81_goodB2G::action(short data) const\r\n{\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (SHRT_MIN/2))\r\n {\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_multiply_81a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_multiply_81a.cpp","cwe_id":"CWE191","file_extension":".cpp","language_hint":"C++","source_size_bytes":2931,"source_sha256":"3827af35326072e6c3d46f96c66073fe215109696161cfab467d0a650b4a525b","line_count":98,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_fscanf_multiply_81a.cpp\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-81a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE191_Integer_Underflow__short_fscanf_multiply_81.h\"\r\n\r\nnamespace CWE191_Integer_Underflow__short_fscanf_multiply_81\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n const CWE191_Integer_Underflow__short_fscanf_multiply_81_base& baseObject = CWE191_Integer_Underflow__short_fscanf_multiply_81_bad();\r\n baseObject.action(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n short data;\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n const CWE191_Integer_Underflow__short_fscanf_multiply_81_base& baseObject = CWE191_Integer_Underflow__short_fscanf_multiply_81_goodG2B();\r\n baseObject.action(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n const CWE191_Integer_Underflow__short_fscanf_multiply_81_base& baseObject = CWE191_Integer_Underflow__short_fscanf_multiply_81_goodB2G();\r\n baseObject.action(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE191_Integer_Underflow__short_fscanf_multiply_81; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_multiply_82_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_multiply_82_bad.cpp","cwe_id":"CWE191","file_extension":".cpp","language_hint":"C++","source_size_bytes":1205,"source_sha256":"ce8c1ed8a6dcd975e6ddcf63e9833bc0e1d055f275b914672ff785ff958c9a7c","line_count":36,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_fscanf_multiply_82_bad.cpp\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-82_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE191_Integer_Underflow__short_fscanf_multiply_82.h\"\r\n\r\nnamespace CWE191_Integer_Underflow__short_fscanf_multiply_82\r\n{\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_multiply_82_bad::action(short data)\r\n{\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_multiply_83_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_multiply_83_goodG2B.cpp","cwe_id":"CWE191","file_extension":".cpp","language_hint":"C++","source_size_bytes":1547,"source_sha256":"5e9ad0b4bd3d918840de1b6fe38c3778bef79da43b298a93ca2023f9fd28f8cd","line_count":41,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_fscanf_multiply_83_goodG2B.cpp\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-83_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE191_Integer_Underflow__short_fscanf_multiply_83.h\"\r\n\r\nnamespace CWE191_Integer_Underflow__short_fscanf_multiply_83\r\n{\r\nCWE191_Integer_Underflow__short_fscanf_multiply_83_goodG2B::CWE191_Integer_Underflow__short_fscanf_multiply_83_goodG2B(short dataCopy)\r\n{\r\n data = dataCopy;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n}\r\n\r\nCWE191_Integer_Underflow__short_fscanf_multiply_83_goodG2B::~CWE191_Integer_Underflow__short_fscanf_multiply_83_goodG2B()\r\n{\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_11.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_11.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":4860,"source_sha256":"6d92f7918e3d15953a3a05f5ae75536794cc75ae8ca64b146c8f19ed672c3e4e","line_count":179,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_fscanf_sub_11.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-11.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_11_bad()\r\n{\r\n short data;\r\n data = 0;\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n short data;\r\n data = 0;\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n }\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > SHRT_MIN)\r\n {\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n short data;\r\n data = 0;\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > SHRT_MIN)\r\n {\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n short data;\r\n data = 0;\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n short data;\r\n data = 0;\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_11_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__short_fscanf_sub_11_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__short_fscanf_sub_11_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_22a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_22a.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":4026,"source_sha256":"ac56a2cb5f9c074400b89d83b9894843902adfbfaed4eef5afeb5ce24e10ad4e","line_count":118,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_fscanf_sub_22a.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-22a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 22 Control flow: Flow controlled by value of a global variable. Sink functions are in a separate file from sources.\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* The global variable below is used to drive control flow in the sink function */\r\nint CWE191_Integer_Underflow__short_fscanf_sub_22_badGlobal = 0;\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_22_badSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_22_bad()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n CWE191_Integer_Underflow__short_fscanf_sub_22_badGlobal = 1; /* true */\r\n CWE191_Integer_Underflow__short_fscanf_sub_22_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* The global variables below are used to drive control flow in the sink functions. */\r\nint CWE191_Integer_Underflow__short_fscanf_sub_22_goodB2G1Global = 0;\r\nint CWE191_Integer_Underflow__short_fscanf_sub_22_goodB2G2Global = 0;\r\nint CWE191_Integer_Underflow__short_fscanf_sub_22_goodG2BGlobal = 0;\r\n\r\n/* goodB2G1() - use badsource and goodsink by setting the static variable to false instead of true */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_22_goodB2G1Sink(short data);\r\n\r\nstatic void goodB2G1()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n CWE191_Integer_Underflow__short_fscanf_sub_22_goodB2G1Global = 0; /* false */\r\n CWE191_Integer_Underflow__short_fscanf_sub_22_goodB2G1Sink(data);\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_22_goodB2G2Sink(short data);\r\n\r\nstatic void goodB2G2()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n CWE191_Integer_Underflow__short_fscanf_sub_22_goodB2G2Global = 1; /* true */\r\n CWE191_Integer_Underflow__short_fscanf_sub_22_goodB2G2Sink(data);\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_22_goodG2BSink(short data);\r\n\r\nstatic void goodG2B()\r\n{\r\n short data;\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n CWE191_Integer_Underflow__short_fscanf_sub_22_goodG2BGlobal = 1; /* true */\r\n CWE191_Integer_Underflow__short_fscanf_sub_22_goodG2BSink(data);\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_22_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__short_fscanf_sub_22_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__short_fscanf_sub_22_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_44.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_44.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":3326,"source_sha256":"17759070bc7d5d5901c33b742044821057b547c388c53c02d287f965c8bf0963","line_count":123,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_fscanf_sub_44.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-44.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * 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\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink(short data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_44_bad()\r\n{\r\n short data;\r\n /* define a function pointer */\r\n void (*funcPtr) (short) = badSink;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n /* use the function pointer */\r\n funcPtr(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink(short data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n short data;\r\n void (*funcPtr) (short) = goodG2BSink;\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n funcPtr(data);\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink(short data)\r\n{\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > SHRT_MIN)\r\n {\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n short data;\r\n void (*funcPtr) (short) = goodB2GSink;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n funcPtr(data);\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_44_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__short_fscanf_sub_44_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__short_fscanf_sub_44_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_51a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_51a.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":2787,"source_sha256":"2d0639c5440995641ae941f5039b75e3313ac339d94af99184698c64b5dcc29c","line_count":94,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_fscanf_sub_51a.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-51a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 51 Data flow: data passed as an argument from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_51b_badSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_51_bad()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n CWE191_Integer_Underflow__short_fscanf_sub_51b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_51b_goodG2BSink(short data);\r\n\r\nstatic void goodG2B()\r\n{\r\n short data;\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n CWE191_Integer_Underflow__short_fscanf_sub_51b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_51b_goodB2GSink(short data);\r\n\r\nstatic void goodB2G()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n CWE191_Integer_Underflow__short_fscanf_sub_51b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_51_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__short_fscanf_sub_51_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__short_fscanf_sub_51_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_52b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_52b.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":1645,"source_sha256":"208b67371490d9ad379348b15f2c3818eceadc950c199fa364906b7c389b269d","line_count":50,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_fscanf_sub_52b.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-52b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_52c_badSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_52b_badSink(short data)\r\n{\r\n CWE191_Integer_Underflow__short_fscanf_sub_52c_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_52c_goodG2BSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_52b_goodG2BSink(short data)\r\n{\r\n CWE191_Integer_Underflow__short_fscanf_sub_52c_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_52c_goodB2GSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_52b_goodB2GSink(short data)\r\n{\r\n CWE191_Integer_Underflow__short_fscanf_sub_52c_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_53d.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_53d.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":1780,"source_sha256":"69f673ce6ea7a0711ccf550ffffed9e8caf47a281f0bdc1eb9ee0c763f115ecd","line_count":60,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_fscanf_sub_53d.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-53d.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_53d_badSink(short data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_53d_goodG2BSink(short data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_53d_goodB2GSink(short data)\r\n{\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > SHRT_MIN)\r\n {\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_54c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_54c.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":1673,"source_sha256":"996a0c80533c92c2e8e47ac2cf5336376986778e06763829fca3bef419dd8433","line_count":50,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_fscanf_sub_54c.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-54c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_54d_badSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_54c_badSink(short data)\r\n{\r\n CWE191_Integer_Underflow__short_fscanf_sub_54d_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_54d_goodG2BSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_54c_goodG2BSink(short data)\r\n{\r\n CWE191_Integer_Underflow__short_fscanf_sub_54d_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_54d_goodB2GSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_54c_goodB2GSink(short data)\r\n{\r\n CWE191_Integer_Underflow__short_fscanf_sub_54d_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_54d.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_54d.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":1673,"source_sha256":"effdd6b9cd6aa607c0990c375c26a62eb7bed6ceb53d057ea387d13b28df3459","line_count":50,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_fscanf_sub_54d.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-54d.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_54e_badSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_54d_badSink(short data)\r\n{\r\n CWE191_Integer_Underflow__short_fscanf_sub_54e_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_54e_goodG2BSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_54d_goodG2BSink(short data)\r\n{\r\n CWE191_Integer_Underflow__short_fscanf_sub_54e_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_54e_goodB2GSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_54d_goodB2GSink(short data)\r\n{\r\n CWE191_Integer_Underflow__short_fscanf_sub_54e_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_54e.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_54e.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":1781,"source_sha256":"930dd6820282f518625b653e5cdbb625f663abc2b5e0ff62624354361ab744aa","line_count":60,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_fscanf_sub_54e.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-54e.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_54e_badSink(short data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_54e_goodG2BSink(short data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_54e_goodB2GSink(short data)\r\n{\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > SHRT_MIN)\r\n {\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_65a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_65a.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":3022,"source_sha256":"7059e82aa39320e6a8eb41cab385aa08ec0954ff8b99286df5b1626245162bfa","line_count":99,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_fscanf_sub_65a.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-65a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_65b_badSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_65_bad()\r\n{\r\n short data;\r\n /* define a function pointer */\r\n void (*funcPtr) (short) = CWE191_Integer_Underflow__short_fscanf_sub_65b_badSink;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n /* use the function pointer */\r\n funcPtr(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_65b_goodG2BSink(short data);\r\n\r\nstatic void goodG2B()\r\n{\r\n short data;\r\n void (*funcPtr) (short) = CWE191_Integer_Underflow__short_fscanf_sub_65b_goodG2BSink;\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n funcPtr(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_65b_goodB2GSink(short data);\r\n\r\nstatic void goodB2G()\r\n{\r\n short data;\r\n void (*funcPtr) (short) = CWE191_Integer_Underflow__short_fscanf_sub_65b_goodB2GSink;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n funcPtr(data);\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_65_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__short_fscanf_sub_65_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__short_fscanf_sub_65_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_65b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_65b.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":1778,"source_sha256":"c8891672f7f0f0af6506647688365286af32d3b64f7dd3de385f2fb32db62172","line_count":60,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_fscanf_sub_65b.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-65b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_65b_badSink(short data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_65b_goodG2BSink(short data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_65b_goodB2GSink(short data)\r\n{\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > SHRT_MIN)\r\n {\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_66a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_66a.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":3002,"source_sha256":"1f41be6ea1d53288478c86a4bd577672373240ff80c7eff0093974f1b58b3fdf","line_count":101,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_fscanf_sub_66a.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-66a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 66 Data flow: data passed in an array from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_66b_badSink(short dataArray[]);\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_66_bad()\r\n{\r\n short data;\r\n short dataArray[5];\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n /* put data in array */\r\n dataArray[2] = data;\r\n CWE191_Integer_Underflow__short_fscanf_sub_66b_badSink(dataArray);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_66b_goodG2BSink(short dataArray[]);\r\n\r\nstatic void goodG2B()\r\n{\r\n short data;\r\n short dataArray[5];\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n dataArray[2] = data;\r\n CWE191_Integer_Underflow__short_fscanf_sub_66b_goodG2BSink(dataArray);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_66b_goodB2GSink(short dataArray[]);\r\n\r\nstatic void goodB2G()\r\n{\r\n short data;\r\n short dataArray[5];\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n dataArray[2] = data;\r\n CWE191_Integer_Underflow__short_fscanf_sub_66b_goodB2GSink(dataArray);\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_fscanf_sub_66_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__short_fscanf_sub_66_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__short_fscanf_sub_66_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_82.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_82.h","cwe_id":"CWE191","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1502,"source_sha256":"68e3104f73ceb0ce72c634cca8a35b209515e8be2d124ea625f4af3eeee9dc1f","line_count":56,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_fscanf_sub_82.h\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-82.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE191_Integer_Underflow__short_fscanf_sub_82\r\n{\r\n\r\nclass CWE191_Integer_Underflow__short_fscanf_sub_82_base\r\n{\r\npublic:\r\n /* pure virtual function */\r\n virtual void action(short data) = 0;\r\n};\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE191_Integer_Underflow__short_fscanf_sub_82_bad : public CWE191_Integer_Underflow__short_fscanf_sub_82_base\r\n{\r\npublic:\r\n void action(short data);\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE191_Integer_Underflow__short_fscanf_sub_82_goodG2B : public CWE191_Integer_Underflow__short_fscanf_sub_82_base\r\n{\r\npublic:\r\n void action(short data);\r\n};\r\n\r\nclass CWE191_Integer_Underflow__short_fscanf_sub_82_goodB2G : public CWE191_Integer_Underflow__short_fscanf_sub_82_base\r\n{\r\npublic:\r\n void action(short data);\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_82a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_fscanf_sub_82a.cpp","cwe_id":"CWE191","file_extension":".cpp","language_hint":"C++","source_size_bytes":2925,"source_sha256":"46157b2aae513cacc9e0f06cf7b5564b6054d0e6a4d6f6782b65db6c2d3627f1","line_count":101,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_fscanf_sub_82a.cpp\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-82a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE191_Integer_Underflow__short_fscanf_sub_82.h\"\r\n\r\nnamespace CWE191_Integer_Underflow__short_fscanf_sub_82\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n CWE191_Integer_Underflow__short_fscanf_sub_82_base* baseObject = new CWE191_Integer_Underflow__short_fscanf_sub_82_bad;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n short data;\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n CWE191_Integer_Underflow__short_fscanf_sub_82_base* baseObject = new CWE191_Integer_Underflow__short_fscanf_sub_82_goodG2B;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a value input from the console */\r\n fscanf (stdin, \"%hd\", &data);\r\n CWE191_Integer_Underflow__short_fscanf_sub_82_base* baseObject = new CWE191_Integer_Underflow__short_fscanf_sub_82_goodB2G;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE191_Integer_Underflow__short_fscanf_sub_82; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_multiply_07.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_multiply_07.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":5404,"source_sha256":"fc390ccf23e73ae2ae63552b58c2459cca0e92a885e05a946030fe571686497b","line_count":193,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_min_multiply_07.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-07.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for short\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 07 Control flow: if(staticFive==5) and if(staticFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The variable below is not declared \"const\", but is never assigned\r\n any other value so a tool should be able to identify that reads of\r\n this will always give its initialized value. */\r\nstatic int staticFive = 5;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__short_min_multiply_07_bad()\r\n{\r\n short data;\r\n data = 0;\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = SHRT_MIN;\r\n }\r\n if(staticFive==5)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticFive==5 to staticFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n short data;\r\n data = 0;\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = SHRT_MIN;\r\n }\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (SHRT_MIN/2))\r\n {\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n short data;\r\n data = 0;\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = SHRT_MIN;\r\n }\r\n if(staticFive==5)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (SHRT_MIN/2))\r\n {\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticFive==5 to staticFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n short data;\r\n data = 0;\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n if(staticFive==5)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n short data;\r\n data = 0;\r\n if(staticFive==5)\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n if(staticFive==5)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_min_multiply_07_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__short_min_multiply_07_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__short_min_multiply_07_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_multiply_10.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_multiply_10.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":5138,"source_sha256":"33c7b9191ea3e6ac585baeb76b53280936c6e1d8d35ca190a889fae413a41cb3","line_count":188,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_min_multiply_10.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-10.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for short\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 10 Control flow: if(globalTrue) and if(globalFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__short_min_multiply_10_bad()\r\n{\r\n short data;\r\n data = 0;\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = SHRT_MIN;\r\n }\r\n if(globalTrue)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalTrue to globalFalse */\r\nstatic void goodB2G1()\r\n{\r\n short data;\r\n data = 0;\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = SHRT_MIN;\r\n }\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (SHRT_MIN/2))\r\n {\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n short data;\r\n data = 0;\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = SHRT_MIN;\r\n }\r\n if(globalTrue)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (SHRT_MIN/2))\r\n {\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalTrue to globalFalse */\r\nstatic void goodG2B1()\r\n{\r\n short data;\r\n data = 0;\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n if(globalTrue)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n short data;\r\n data = 0;\r\n if(globalTrue)\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n if(globalTrue)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_min_multiply_10_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__short_min_multiply_10_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__short_min_multiply_10_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_multiply_12.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_multiply_12.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":5327,"source_sha256":"fab97b5023453c676087ec00de089e7ad4cd32e43aca43fe93fbe593ad34c22f","line_count":187,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_min_multiply_12.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-12.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for short\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__short_min_multiply_12_bad()\r\n{\r\n short data;\r\n data = 0;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = SHRT_MIN;\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n else\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (SHRT_MIN/2))\r\n {\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the first \"if\" so that\r\n both branches use the BadSource and the second \"if\" so that both branches\r\n use the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n short data;\r\n data = 0;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = SHRT_MIN;\r\n }\r\n else\r\n {\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = SHRT_MIN;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (SHRT_MIN/2))\r\n {\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n }\r\n else\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (SHRT_MIN/2))\r\n {\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the first \"if\" so that\r\n both branches use the GoodSource and the second \"if\" so that both branches\r\n use the BadSink */\r\nstatic void goodG2B()\r\n{\r\n short data;\r\n data = 0;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n else\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_min_multiply_12_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__short_min_multiply_12_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__short_min_multiply_12_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_multiply_53a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_multiply_53a.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":2841,"source_sha256":"246f93d620bc32c25305fe1a195361a1ae93c3d146693028d8f79cd2ecdd1601","line_count":94,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_min_multiply_53a.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-53a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for short\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE191_Integer_Underflow__short_min_multiply_53b_badSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_min_multiply_53_bad()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = SHRT_MIN;\r\n CWE191_Integer_Underflow__short_min_multiply_53b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__short_min_multiply_53b_goodG2BSink(short data);\r\n\r\nstatic void goodG2B()\r\n{\r\n short data;\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n CWE191_Integer_Underflow__short_min_multiply_53b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__short_min_multiply_53b_goodB2GSink(short data);\r\n\r\nstatic void goodB2G()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = SHRT_MIN;\r\n CWE191_Integer_Underflow__short_min_multiply_53b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_min_multiply_53_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__short_min_multiply_53_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__short_min_multiply_53_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_multiply_54b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_multiply_54b.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":1701,"source_sha256":"890eb2467170740b2ba69bc74014172967050e124734fbfeba1ea5b739af3f93","line_count":50,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_min_multiply_54b.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-54b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for short\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE191_Integer_Underflow__short_min_multiply_54c_badSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_min_multiply_54b_badSink(short data)\r\n{\r\n CWE191_Integer_Underflow__short_min_multiply_54c_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__short_min_multiply_54c_goodG2BSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_min_multiply_54b_goodG2BSink(short data)\r\n{\r\n CWE191_Integer_Underflow__short_min_multiply_54c_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__short_min_multiply_54c_goodB2GSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_min_multiply_54b_goodB2GSink(short data)\r\n{\r\n CWE191_Integer_Underflow__short_min_multiply_54c_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_multiply_54c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_multiply_54c.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":1701,"source_sha256":"8f919d4370f11ece1b0dc2d8896fcbe710d23927ebcec590782f6af911683b4b","line_count":50,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_min_multiply_54c.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-54c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for short\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE191_Integer_Underflow__short_min_multiply_54d_badSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_min_multiply_54c_badSink(short data)\r\n{\r\n CWE191_Integer_Underflow__short_min_multiply_54d_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__short_min_multiply_54d_goodG2BSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_min_multiply_54c_goodG2BSink(short data)\r\n{\r\n CWE191_Integer_Underflow__short_min_multiply_54d_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__short_min_multiply_54d_goodB2GSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_min_multiply_54c_goodB2GSink(short data)\r\n{\r\n CWE191_Integer_Underflow__short_min_multiply_54d_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_multiply_65b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_multiply_65b.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":2020,"source_sha256":"027e8929a61d31b3c79b1e92680e2538d9d886d20eaf9285bf7491de49c0a2a8","line_count":65,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_min_multiply_65b.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-65b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for short\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__short_min_multiply_65b_badSink(short data)\r\n{\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__short_min_multiply_65b_goodG2BSink(short data)\r\n{\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__short_min_multiply_65b_goodB2GSink(short data)\r\n{\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (SHRT_MIN/2))\r\n {\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_multiply_83_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_multiply_83_goodB2G.cpp","cwe_id":"CWE191","file_extension":".cpp","language_hint":"C++","source_size_bytes":1670,"source_sha256":"3dfa296f47d07120cb9df62e93980ae0bff23260f845927a852f3ae11484154a","line_count":48,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_min_multiply_83_goodB2G.cpp\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-83_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for short\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE191_Integer_Underflow__short_min_multiply_83.h\"\r\n\r\nnamespace CWE191_Integer_Underflow__short_min_multiply_83\r\n{\r\nCWE191_Integer_Underflow__short_min_multiply_83_goodB2G::CWE191_Integer_Underflow__short_min_multiply_83_goodB2G(short dataCopy)\r\n{\r\n data = dataCopy;\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = SHRT_MIN;\r\n}\r\n\r\nCWE191_Integer_Underflow__short_min_multiply_83_goodB2G::~CWE191_Integer_Underflow__short_min_multiply_83_goodB2G()\r\n{\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (SHRT_MIN/2))\r\n {\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_sub_53a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_sub_53a.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":2769,"source_sha256":"94465b882f10cfa0c43e556779500d5ef087d975f44cff1d98b9ad1179f26dd4","line_count":94,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_min_sub_53a.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-53a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for short\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE191_Integer_Underflow__short_min_sub_53b_badSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_min_sub_53_bad()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = SHRT_MIN;\r\n CWE191_Integer_Underflow__short_min_sub_53b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__short_min_sub_53b_goodG2BSink(short data);\r\n\r\nstatic void goodG2B()\r\n{\r\n short data;\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n CWE191_Integer_Underflow__short_min_sub_53b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__short_min_sub_53b_goodB2GSink(short data);\r\n\r\nstatic void goodB2G()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = SHRT_MIN;\r\n CWE191_Integer_Underflow__short_min_sub_53b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_min_sub_53_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__short_min_sub_53_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__short_min_sub_53_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_sub_53c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_sub_53c.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":1633,"source_sha256":"97a7abde796d25077c30652a917e085b24dfd284382f050d4c49eabbf21a567e","line_count":50,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_min_sub_53c.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-53c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for short\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE191_Integer_Underflow__short_min_sub_53d_badSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_min_sub_53c_badSink(short data)\r\n{\r\n CWE191_Integer_Underflow__short_min_sub_53d_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__short_min_sub_53d_goodG2BSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_min_sub_53c_goodG2BSink(short data)\r\n{\r\n CWE191_Integer_Underflow__short_min_sub_53d_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__short_min_sub_53d_goodB2GSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_min_sub_53c_goodB2GSink(short data)\r\n{\r\n CWE191_Integer_Underflow__short_min_sub_53d_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_sub_73a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_sub_73a.cpp","cwe_id":"CWE191","file_extension":".cpp","language_hint":"C++","source_size_bytes":2985,"source_sha256":"deb09d946bb766c46cb96183b76f1dd2b1b712541ad3d1897d022f871472f023","line_count":118,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_min_sub_73a.cpp\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-73a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for short\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE191_Integer_Underflow__short_min_sub_73\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(list dataList);\r\n\r\nvoid bad()\r\n{\r\n short data;\r\n list dataList;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = SHRT_MIN;\r\n /* Put data in a list */\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n badSink(dataList);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(list dataList);\r\n\r\nstatic void goodG2B()\r\n{\r\n short data;\r\n list dataList;\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n /* Put data in a list */\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n goodG2BSink(dataList);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(list dataList);\r\n\r\nstatic void goodB2G()\r\n{\r\n short data;\r\n list dataList;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = SHRT_MIN;\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n goodB2GSink(dataList);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE191_Integer_Underflow__short_min_sub_73; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_sub_81_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_sub_81_goodB2G.cpp","cwe_id":"CWE191","file_extension":".cpp","language_hint":"C++","source_size_bytes":1223,"source_sha256":"a75fde842cdb678700b0b75813cefa93ea04e93a328c3267867a069e60f32262","line_count":40,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_min_sub_81_goodB2G.cpp\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-81_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for short\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE191_Integer_Underflow__short_min_sub_81.h\"\r\n\r\nnamespace CWE191_Integer_Underflow__short_min_sub_81\r\n{\r\n\r\nvoid CWE191_Integer_Underflow__short_min_sub_81_goodB2G::action(short data) const\r\n{\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > SHRT_MIN)\r\n {\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_sub_82_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_sub_82_goodB2G.cpp","cwe_id":"CWE191","file_extension":".cpp","language_hint":"C++","source_size_bytes":1215,"source_sha256":"86d91b8c763581500f9d8b834b40a5fef717dc9b198266508ea03a0befd75acb","line_count":40,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_min_sub_82_goodB2G.cpp\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-82_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for short\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE191_Integer_Underflow__short_min_sub_82.h\"\r\n\r\nnamespace CWE191_Integer_Underflow__short_min_sub_82\r\n{\r\n\r\nvoid CWE191_Integer_Underflow__short_min_sub_82_goodB2G::action(short data)\r\n{\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > SHRT_MIN)\r\n {\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_sub_82_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_min_sub_82_goodG2B.cpp","cwe_id":"CWE191","file_extension":".cpp","language_hint":"C++","source_size_bytes":1107,"source_sha256":"2ceaabe74c5f5352eba0c52f65455b0589671e6c7f055a112ba5e43178f5380f","line_count":35,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_min_sub_82_goodG2B.cpp\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-82_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for short\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE191_Integer_Underflow__short_min_sub_82.h\"\r\n\r\nnamespace CWE191_Integer_Underflow__short_min_sub_82\r\n{\r\n\r\nvoid CWE191_Integer_Underflow__short_min_sub_82_goodG2B::action(short data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_multiply_09.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_multiply_09.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":5213,"source_sha256":"eff74381f5b5076b50cad672826be8138ad697de9691dfb02879ab11a686bdf8","line_count":188,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_rand_multiply_09.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-09.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__short_rand_multiply_09_bad()\r\n{\r\n short data;\r\n data = 0;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n short data;\r\n data = 0;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n }\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (SHRT_MIN/2))\r\n {\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n short data;\r\n data = 0;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (SHRT_MIN/2))\r\n {\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n short data;\r\n data = 0;\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n short data;\r\n data = 0;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_rand_multiply_09_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__short_rand_multiply_09_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__short_rand_multiply_09_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_multiply_13.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_multiply_13.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":5256,"source_sha256":"6cccff28ed27c2eccb4a90f90d0649307fcb6515f8a07f3f90c8a55c96a60a1a","line_count":188,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_rand_multiply_13.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-13.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 13 Control flow: if(GLOBAL_CONST_FIVE==5) and if(GLOBAL_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__short_rand_multiply_13_bad()\r\n{\r\n short data;\r\n data = 0;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_FIVE==5 to GLOBAL_CONST_FIVE!=5 */\r\nstatic void goodB2G1()\r\n{\r\n short data;\r\n data = 0;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n }\r\n if(GLOBAL_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (SHRT_MIN/2))\r\n {\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n short data;\r\n data = 0;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (SHRT_MIN/2))\r\n {\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_FIVE==5 to GLOBAL_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n short data;\r\n data = 0;\r\n if(GLOBAL_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n short data;\r\n data = 0;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_rand_multiply_13_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__short_rand_multiply_13_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__short_rand_multiply_13_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_multiply_45.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_multiply_45.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":3942,"source_sha256":"d24da5d83c67f66becbc3bdbbd1f04407ee5f8096112e7b4b7d3de57109c3972","line_count":133,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_rand_multiply_45.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-45.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 45 Data flow: data passed as a static global variable from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nstatic short CWE191_Integer_Underflow__short_rand_multiply_45_badData;\r\nstatic short CWE191_Integer_Underflow__short_rand_multiply_45_goodG2BData;\r\nstatic short CWE191_Integer_Underflow__short_rand_multiply_45_goodB2GData;\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink()\r\n{\r\n short data = CWE191_Integer_Underflow__short_rand_multiply_45_badData;\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_rand_multiply_45_bad()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n CWE191_Integer_Underflow__short_rand_multiply_45_badData = data;\r\n badSink();\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink()\r\n{\r\n short data = CWE191_Integer_Underflow__short_rand_multiply_45_goodG2BData;\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* POTENTIAL FLAW: if (data * 2) < SHRT_MIN, this will underflow */\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n short data;\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n CWE191_Integer_Underflow__short_rand_multiply_45_goodG2BData = data;\r\n goodG2BSink();\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink()\r\n{\r\n short data = CWE191_Integer_Underflow__short_rand_multiply_45_goodB2GData;\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (SHRT_MIN/2))\r\n {\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n CWE191_Integer_Underflow__short_rand_multiply_45_goodB2GData = data;\r\n goodB2GSink();\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_rand_multiply_45_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__short_rand_multiply_45_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__short_rand_multiply_45_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_multiply_53c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_multiply_53c.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":1704,"source_sha256":"fbc22ac7db45af9ed4a9e107c652d31bb568eae3518923b603283388149c73cd","line_count":50,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_rand_multiply_53c.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-53c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE191_Integer_Underflow__short_rand_multiply_53d_badSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_rand_multiply_53c_badSink(short data)\r\n{\r\n CWE191_Integer_Underflow__short_rand_multiply_53d_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__short_rand_multiply_53d_goodG2BSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_rand_multiply_53c_goodG2BSink(short data)\r\n{\r\n CWE191_Integer_Underflow__short_rand_multiply_53d_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__short_rand_multiply_53d_goodB2GSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_rand_multiply_53c_goodB2GSink(short data)\r\n{\r\n CWE191_Integer_Underflow__short_rand_multiply_53d_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_multiply_72a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_multiply_72a.cpp","cwe_id":"CWE191","file_extension":".cpp","language_hint":"C++","source_size_bytes":3208,"source_sha256":"38aeb96e560360fd9938a2dad079649b40dc217f976460ceba3418d115ae6160","line_count":118,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_rand_multiply_72a.cpp\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-72a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 72 Data flow: data passed in a vector from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE191_Integer_Underflow__short_rand_multiply_72\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(vector dataVector);\r\n\r\nvoid bad()\r\n{\r\n short data;\r\n vector dataVector;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n /* Put data in a vector */\r\n dataVector.insert(dataVector.end(), 1, data);\r\n dataVector.insert(dataVector.end(), 1, data);\r\n dataVector.insert(dataVector.end(), 1, data);\r\n badSink(dataVector);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(vector dataVector);\r\n\r\nstatic void goodG2B()\r\n{\r\n short data;\r\n vector dataVector;\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n /* Put data in a vector */\r\n dataVector.insert(dataVector.end(), 1, data);\r\n dataVector.insert(dataVector.end(), 1, data);\r\n dataVector.insert(dataVector.end(), 1, data);\r\n goodG2BSink(dataVector);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(vector dataVector);\r\n\r\nstatic void goodB2G()\r\n{\r\n short data;\r\n vector dataVector;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n dataVector.insert(dataVector.end(), 1, data);\r\n dataVector.insert(dataVector.end(), 1, data);\r\n dataVector.insert(dataVector.end(), 1, data);\r\n goodB2GSink(dataVector);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE191_Integer_Underflow__short_rand_multiply_72; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_multiply_82a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_multiply_82a.cpp","cwe_id":"CWE191","file_extension":".cpp","language_hint":"C++","source_size_bytes":2913,"source_sha256":"ffee5b63ef470db780f0c0125bff55c5016cb09bddf4f2bb3244cc0e65262cd0","line_count":101,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_rand_multiply_82a.cpp\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-82a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE191_Integer_Underflow__short_rand_multiply_82.h\"\r\n\r\nnamespace CWE191_Integer_Underflow__short_rand_multiply_82\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n CWE191_Integer_Underflow__short_rand_multiply_82_base* baseObject = new CWE191_Integer_Underflow__short_rand_multiply_82_bad;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n short data;\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n CWE191_Integer_Underflow__short_rand_multiply_82_base* baseObject = new CWE191_Integer_Underflow__short_rand_multiply_82_goodG2B;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n CWE191_Integer_Underflow__short_rand_multiply_82_base* baseObject = new CWE191_Integer_Underflow__short_rand_multiply_82_goodB2G;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE191_Integer_Underflow__short_rand_multiply_82; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_multiply_83_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_multiply_83_goodB2G.cpp","cwe_id":"CWE191","file_extension":".cpp","language_hint":"C++","source_size_bytes":1659,"source_sha256":"b371657d637da4dc7ac3332d1bd1c9b5ed362a7bec201b4459487a6755334b91","line_count":48,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_rand_multiply_83_goodB2G.cpp\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-83_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: multiply\r\n * GoodSink: Ensure there will not be an underflow before multiplying data by 2\r\n * BadSink : If data is negative, multiply by 2, which can cause an underflow\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE191_Integer_Underflow__short_rand_multiply_83.h\"\r\n\r\nnamespace CWE191_Integer_Underflow__short_rand_multiply_83\r\n{\r\nCWE191_Integer_Underflow__short_rand_multiply_83_goodB2G::CWE191_Integer_Underflow__short_rand_multiply_83_goodB2G(short dataCopy)\r\n{\r\n data = dataCopy;\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n}\r\n\r\nCWE191_Integer_Underflow__short_rand_multiply_83_goodB2G::~CWE191_Integer_Underflow__short_rand_multiply_83_goodB2G()\r\n{\r\n if(data < 0) /* ensure we won't have an overflow */\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > (SHRT_MIN/2))\r\n {\r\n short result = data * 2;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too small to perform multiplication.\");\r\n }\r\n }\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_sub_02.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_sub_02.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":4476,"source_sha256":"3edd87dffe6941ad52592c1ff2e43fe0ee2dfcb32233b878d00b2624b55be0e3","line_count":179,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_rand_sub_02.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-02.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 02 Control flow: if(1) and if(0)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__short_rand_sub_02_bad()\r\n{\r\n short data;\r\n data = 0;\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n }\r\n if(1)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 1 to 0 */\r\nstatic void goodB2G1()\r\n{\r\n short data;\r\n data = 0;\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n }\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > SHRT_MIN)\r\n {\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n short data;\r\n data = 0;\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n }\r\n if(1)\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > SHRT_MIN)\r\n {\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 1 to 0 */\r\nstatic void goodG2B1()\r\n{\r\n short data;\r\n data = 0;\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n if(1)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n short data;\r\n data = 0;\r\n if(1)\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n if(1)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_rand_sub_02_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__short_rand_sub_02_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__short_rand_sub_02_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_sub_03.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_sub_03.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":4524,"source_sha256":"333333fc9adcb35942cca1d32c7e44de2e7bdabfee43cc7de50f6d55e68ba206","line_count":179,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_rand_sub_03.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-03.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 03 Control flow: if(5==5) and if(5!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__short_rand_sub_03_bad()\r\n{\r\n short data;\r\n data = 0;\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n }\r\n if(5==5)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 5==5 to 5!=5 */\r\nstatic void goodB2G1()\r\n{\r\n short data;\r\n data = 0;\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n }\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > SHRT_MIN)\r\n {\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n short data;\r\n data = 0;\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n }\r\n if(5==5)\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > SHRT_MIN)\r\n {\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 5==5 to 5!=5 */\r\nstatic void goodG2B1()\r\n{\r\n short data;\r\n data = 0;\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n if(5==5)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n short data;\r\n data = 0;\r\n if(5==5)\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n if(5==5)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_rand_sub_03_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__short_rand_sub_03_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__short_rand_sub_03_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_sub_06.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_sub_06.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":4974,"source_sha256":"1f73581200ff81f170b4954793ef4dda05e1d0550612d737608cf08a00457deb","line_count":184,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_rand_sub_06.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-06.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 06 Control flow: if(STATIC_CONST_FIVE==5) and if(STATIC_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The variable below is declared \"const\", so a tool should be able\r\n to identify that reads of this will always give its initialized\r\n value. */\r\nstatic const int STATIC_CONST_FIVE = 5;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__short_rand_sub_06_bad()\r\n{\r\n short data;\r\n data = 0;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodB2G1()\r\n{\r\n short data;\r\n data = 0;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n }\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > SHRT_MIN)\r\n {\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n short data;\r\n data = 0;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > SHRT_MIN)\r\n {\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n short data;\r\n data = 0;\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n short data;\r\n data = 0;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_rand_sub_06_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__short_rand_sub_06_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__short_rand_sub_06_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_sub_21.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_sub_21.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":4527,"source_sha256":"86b16151728c8bd7a713866b9578870ab13e6c33d164ad1929e032c924d015af","line_count":173,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_rand_sub_21.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-21.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 21 Control flow: Flow controlled by value of a static global variable. All functions contained in one file.\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* The static variable below is used to drive control flow in the sink function */\r\nstatic int badStatic = 0;\r\n\r\nstatic void badSink(short data)\r\n{\r\n if(badStatic)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_rand_sub_21_bad()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n badStatic = 1; /* true */\r\n badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* The static variables below are used to drive control flow in the sink functions. */\r\nstatic int goodB2G1Static = 0;\r\nstatic int goodB2G2Static = 0;\r\nstatic int goodG2BStatic = 0;\r\n\r\n/* goodB2G1() - use badsource and goodsink by setting the static variable to false instead of true */\r\nstatic void goodB2G1Sink(short data)\r\n{\r\n if(goodB2G1Static)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > SHRT_MIN)\r\n {\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n }\r\n}\r\n\r\nstatic void goodB2G1()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n goodB2G1Static = 0; /* false */\r\n goodB2G1Sink(data);\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */\r\nstatic void goodB2G2Sink(short data)\r\n{\r\n if(goodB2G2Static)\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > SHRT_MIN)\r\n {\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n }\r\n}\r\n\r\nstatic void goodB2G2()\r\n{\r\n short data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n goodB2G2Static = 1; /* true */\r\n goodB2G2Sink(data);\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink */\r\nstatic void goodG2BSink(short data)\r\n{\r\n if(goodG2BStatic)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n short data;\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n goodG2BStatic = 1; /* true */\r\n goodG2BSink(data);\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_rand_sub_21_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__short_rand_sub_21_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__short_rand_sub_21_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_sub_65a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_sub_65a.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":2941,"source_sha256":"853ec3c7082f5c560b3302e267d435ccc942875236d6f21fe3dd98ca2140a859","line_count":99,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_rand_sub_65a.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-65a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE191_Integer_Underflow__short_rand_sub_65b_badSink(short data);\r\n\r\nvoid CWE191_Integer_Underflow__short_rand_sub_65_bad()\r\n{\r\n short data;\r\n /* define a function pointer */\r\n void (*funcPtr) (short) = CWE191_Integer_Underflow__short_rand_sub_65b_badSink;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n /* use the function pointer */\r\n funcPtr(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__short_rand_sub_65b_goodG2BSink(short data);\r\n\r\nstatic void goodG2B()\r\n{\r\n short data;\r\n void (*funcPtr) (short) = CWE191_Integer_Underflow__short_rand_sub_65b_goodG2BSink;\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n funcPtr(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__short_rand_sub_65b_goodB2GSink(short data);\r\n\r\nstatic void goodB2G()\r\n{\r\n short data;\r\n void (*funcPtr) (short) = CWE191_Integer_Underflow__short_rand_sub_65b_goodB2GSink;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (short)RAND32();\r\n funcPtr(data);\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__short_rand_sub_65_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__short_rand_sub_65_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__short_rand_sub_65_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_sub_83_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__short_rand_sub_83_goodG2B.cpp","cwe_id":"CWE191","file_extension":".cpp","language_hint":"C++","source_size_bytes":1412,"source_sha256":"fe38644c41aeb969e1aa14241ff1db2ee041ba6733aef3ebfd01636199f95723","line_count":40,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__short_rand_sub_83_goodG2B.cpp\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-83_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE191_Integer_Underflow__short_rand_sub_83.h\"\r\n\r\nnamespace CWE191_Integer_Underflow__short_rand_sub_83\r\n{\r\nCWE191_Integer_Underflow__short_rand_sub_83_goodG2B::CWE191_Integer_Underflow__short_rand_sub_83_goodG2B(short dataCopy)\r\n{\r\n data = dataCopy;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n}\r\n\r\nCWE191_Integer_Underflow__short_rand_sub_83_goodG2B::~CWE191_Integer_Underflow__short_rand_sub_83_goodG2B()\r\n{\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n short result = data - 1;\r\n printIntLine(result);\r\n }\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_fscanf_sub_68b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_fscanf_sub_68b.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":2297,"source_sha256":"061ac9c59e70aa93302ebb81384f2deb519b11eb2df57d6103239d25a3e1189d","line_count":67,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__unsigned_int_fscanf_sub_68b.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-68b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 68 Data flow: data passed as a global variable from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nextern unsigned int CWE191_Integer_Underflow__unsigned_int_fscanf_sub_68_badData;\r\nextern unsigned int CWE191_Integer_Underflow__unsigned_int_fscanf_sub_68_goodG2BData;\r\nextern unsigned int CWE191_Integer_Underflow__unsigned_int_fscanf_sub_68_goodB2GData;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_fscanf_sub_68b_badSink()\r\n{\r\n unsigned int data = CWE191_Integer_Underflow__unsigned_int_fscanf_sub_68_badData;\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__unsigned_int_fscanf_sub_68b_goodG2BSink()\r\n{\r\n unsigned int data = CWE191_Integer_Underflow__unsigned_int_fscanf_sub_68_goodG2BData;\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__unsigned_int_fscanf_sub_68b_goodB2GSink()\r\n{\r\n unsigned int data = CWE191_Integer_Underflow__unsigned_int_fscanf_sub_68_goodB2GData;\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > 0)\r\n {\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_min_sub_07.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_min_sub_07.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":5030,"source_sha256":"2dd3933fe32ab50fdbff8599252c9545a4f9a0481325b0818ec456bfda7f06ad","line_count":184,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__unsigned_int_min_sub_07.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-07.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for unsigned int\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 07 Control flow: if(staticFive==5) and if(staticFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The variable below is not declared \"const\", but is never assigned\r\n any other value so a tool should be able to identify that reads of\r\n this will always give its initialized value. */\r\nstatic int staticFive = 5;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_07_bad()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = 0;\r\n }\r\n if(staticFive==5)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticFive==5 to staticFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = 0;\r\n }\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > 0)\r\n {\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = 0;\r\n }\r\n if(staticFive==5)\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > 0)\r\n {\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticFive==5 to staticFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n if(staticFive==5)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n if(staticFive==5)\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n if(staticFive==5)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_07_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_07_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_07_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_min_sub_08.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_min_sub_08.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":5186,"source_sha256":"2c42847f2f7fdd81cecab3153f37713bbcc7be1942230db20d813d8d72007333","line_count":192,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__unsigned_int_min_sub_08.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-08.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for unsigned int\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 08 Control flow: if(staticReturnsTrue()) and if(staticReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The two function below always return the same value, so a tool\r\n should be able to identify that calls to the functions will always\r\n return a fixed value. */\r\nstatic int staticReturnsTrue()\r\n{\r\n return 1;\r\n}\r\n\r\nstatic int staticReturnsFalse()\r\n{\r\n return 0;\r\n}\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_08_bad()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = 0;\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = 0;\r\n }\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > 0)\r\n {\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = 0;\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > 0)\r\n {\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_08_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_08_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_08_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_min_sub_12.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_min_sub_12.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":4814,"source_sha256":"f9cac5b2f5f6dc58a737a7dc6d6a592ede770fb4b3318847b0aa4ab80cd767ff","line_count":175,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__unsigned_int_min_sub_12.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-12.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for unsigned int\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_12_bad()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = 0;\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > 0)\r\n {\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the first \"if\" so that\r\n both branches use the BadSource and the second \"if\" so that both branches\r\n use the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = 0;\r\n }\r\n else\r\n {\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = 0;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > 0)\r\n {\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n }\r\n else\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > 0)\r\n {\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the first \"if\" so that\r\n both branches use the GoodSource and the second \"if\" so that both branches\r\n use the BadSink */\r\nstatic void goodG2B()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n else\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n }\r\n else\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_12_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_12_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_12_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_min_sub_16.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_min_sub_16.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":3318,"source_sha256":"895e0b7e9d8c3bd2e041846ef6fa37883931c18358247dd8bb5c8fb7adefbbaf","line_count":128,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__unsigned_int_min_sub_16.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-16.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for unsigned int\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 16 Control flow: while(1)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_16_bad()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = 0;\r\n break;\r\n }\r\n while(1)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the sinks in the second while statement */\r\nstatic void goodB2G()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = 0;\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > 0)\r\n {\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the sources in the first while statement */\r\nstatic void goodG2B()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n while(1)\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n break;\r\n }\r\n while(1)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_16_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_16_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_16_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_min_sub_42.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_min_sub_42.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":3243,"source_sha256":"5852ef597320b812fc6ba6f4928ccd6daffec2ee14e761698fc3840d2871c9e6","line_count":122,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__unsigned_int_min_sub_42.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-42.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for unsigned int\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 42 Data flow: data returned from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic unsigned int badSource(unsigned int data)\r\n{\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = 0;\r\n return data;\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_42_bad()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n data = badSource(data);\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic unsigned int goodG2BSource(unsigned int data)\r\n{\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n return data;\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n data = goodG2BSource(data);\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic unsigned int goodB2GSource(unsigned int data)\r\n{\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = 0;\r\n return data;\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n data = goodB2GSource(data);\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > 0)\r\n {\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_42_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_42_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_42_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_min_sub_44.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_min_sub_44.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":3402,"source_sha256":"3dbdb32e1a976edd5b44e9db78353263352e4e881a45e257d2c69ad72f062c2d","line_count":123,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__unsigned_int_min_sub_44.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-44.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for unsigned int\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * 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\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink(unsigned int data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_44_bad()\r\n{\r\n unsigned int data;\r\n /* define a function pointer */\r\n void (*funcPtr) (unsigned int) = badSink;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = 0;\r\n /* use the function pointer */\r\n funcPtr(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink(unsigned int data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n unsigned int data;\r\n void (*funcPtr) (unsigned int) = goodG2BSink;\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n funcPtr(data);\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink(unsigned int data)\r\n{\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > 0)\r\n {\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n unsigned int data;\r\n void (*funcPtr) (unsigned int) = goodB2GSink;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = 0;\r\n funcPtr(data);\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_44_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_44_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_44_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_min_sub_45.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_min_sub_45.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":3828,"source_sha256":"c587b2ee1b53e5384a9c1d30a872abdcc84e0aa4cc5cb71a2dc2e26306cba7ae","line_count":128,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__unsigned_int_min_sub_45.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-45.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for unsigned int\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 45 Data flow: data passed as a static global variable from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nstatic unsigned int CWE191_Integer_Underflow__unsigned_int_min_sub_45_badData;\r\nstatic unsigned int CWE191_Integer_Underflow__unsigned_int_min_sub_45_goodG2BData;\r\nstatic unsigned int CWE191_Integer_Underflow__unsigned_int_min_sub_45_goodB2GData;\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink()\r\n{\r\n unsigned int data = CWE191_Integer_Underflow__unsigned_int_min_sub_45_badData;\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_45_bad()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = 0;\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_45_badData = data;\r\n badSink();\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink()\r\n{\r\n unsigned int data = CWE191_Integer_Underflow__unsigned_int_min_sub_45_goodG2BData;\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_45_goodG2BData = data;\r\n goodG2BSink();\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink()\r\n{\r\n unsigned int data = CWE191_Integer_Underflow__unsigned_int_min_sub_45_goodB2GData;\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > 0)\r\n {\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = 0;\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_45_goodB2GData = data;\r\n goodB2GSink();\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_45_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_45_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_45_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_min_sub_53a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_min_sub_53a.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":2881,"source_sha256":"7ac229f7cbb3dc44ac1b19afa119a92e8f438a6c19683ab59cff3f7bd1e902d2","line_count":94,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__unsigned_int_min_sub_53a.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-53a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for unsigned int\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_53b_badSink(unsigned int data);\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_53_bad()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = 0;\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_53b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_53b_goodG2BSink(unsigned int data);\r\n\r\nstatic void goodG2B()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_53b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_53b_goodB2GSink(unsigned int data);\r\n\r\nstatic void goodB2G()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use the minimum size of the data type */\r\n data = 0;\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_53b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_53_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_53_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_53_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_min_sub_53c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_min_sub_53c.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":1752,"source_sha256":"36f5ddb65f8581d88f5625c2d56907dfba67489e709ed5021831016d14466c0b","line_count":50,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__unsigned_int_min_sub_53c.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-53c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for unsigned int\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_53d_badSink(unsigned int data);\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_53c_badSink(unsigned int data)\r\n{\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_53d_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_53d_goodG2BSink(unsigned int data);\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_53c_goodG2BSink(unsigned int data)\r\n{\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_53d_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_53d_goodB2GSink(unsigned int data);\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_53c_goodB2GSink(unsigned int data)\r\n{\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_53d_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_min_sub_61a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_min_sub_61a.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":3229,"source_sha256":"4779abe699125f5b4bfb4e38fceb2b75dc2b44081506517b5c4fd38d4ad47e29","line_count":108,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__unsigned_int_min_sub_61a.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-61a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for unsigned int\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 61 Data flow: data returned from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nunsigned int CWE191_Integer_Underflow__unsigned_int_min_sub_61b_badSource(unsigned int data);\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_61_bad()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n data = CWE191_Integer_Underflow__unsigned_int_min_sub_61b_badSource(data);\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nunsigned int CWE191_Integer_Underflow__unsigned_int_min_sub_61b_goodG2BSource(unsigned int data);\r\n\r\nstatic void goodG2B()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n data = CWE191_Integer_Underflow__unsigned_int_min_sub_61b_goodG2BSource(data);\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nunsigned int CWE191_Integer_Underflow__unsigned_int_min_sub_61b_goodB2GSource(unsigned int data);\r\n\r\nstatic void goodB2G()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n data = CWE191_Integer_Underflow__unsigned_int_min_sub_61b_goodB2GSource(data);\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > 0)\r\n {\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_61_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_61_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__unsigned_int_min_sub_61_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_min_sub_63b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_min_sub_63b.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":1916,"source_sha256":"4bc753a76891eff80f92ff68ecc01feaab892fa00c369205e350c44ea85cee3f","line_count":63,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__unsigned_int_min_sub_63b.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-63b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: min Set data to the min value for unsigned int\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_63b_badSink(unsigned int * dataPtr)\r\n{\r\n unsigned int data = *dataPtr;\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_63b_goodG2BSink(unsigned int * dataPtr)\r\n{\r\n unsigned int data = *dataPtr;\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__unsigned_int_min_sub_63b_goodB2GSink(unsigned int * dataPtr)\r\n{\r\n unsigned int data = *dataPtr;\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > 0)\r\n {\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_rand_sub_16.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_rand_sub_16.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":3314,"source_sha256":"225204f7e8fce1d8d176fb916fb1c30280a508c2971fd6439741fa674712d57a","line_count":128,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__unsigned_int_rand_sub_16.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-16.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 16 Control flow: while(1)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_rand_sub_16_bad()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (unsigned int)RAND32();\r\n break;\r\n }\r\n while(1)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the sinks in the second while statement */\r\nstatic void goodB2G()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (unsigned int)RAND32();\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > 0)\r\n {\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the sources in the first while statement */\r\nstatic void goodG2B()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n while(1)\r\n {\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n break;\r\n }\r\n while(1)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_rand_sub_16_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__unsigned_int_rand_sub_16_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__unsigned_int_rand_sub_16_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_rand_sub_51b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_rand_sub_51b.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":1791,"source_sha256":"850131169d1fe9da05dbe1d1317bba55ce1589280adf93294fc2ddc1a9861e38","line_count":60,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__unsigned_int_rand_sub_51b.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-51b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 51 Data flow: data passed as an argument from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_rand_sub_51b_badSink(unsigned int data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__unsigned_int_rand_sub_51b_goodG2BSink(unsigned int data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Subtracting 1 from data could cause an underflow */\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__unsigned_int_rand_sub_51b_goodB2GSink(unsigned int data)\r\n{\r\n /* FIX: Add a check to prevent an underflow from occurring */\r\n if (data > 0)\r\n {\r\n unsigned int result = data - 1;\r\n printUnsignedLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"data value is too large to perform subtraction.\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_rand_sub_64a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_rand_sub_64a.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":2846,"source_sha256":"41a96c21edae3f5a1248ed8bd3fce7f7f6a48fe47a58f1bd36f3f29fab06d6be","line_count":94,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__unsigned_int_rand_sub_64a.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-64a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE191_Integer_Underflow__unsigned_int_rand_sub_64b_badSink(void * dataVoidPtr);\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_rand_sub_64_bad()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (unsigned int)RAND32();\r\n CWE191_Integer_Underflow__unsigned_int_rand_sub_64b_badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__unsigned_int_rand_sub_64b_goodG2BSink(void * dataVoidPtr);\r\n\r\nstatic void goodG2B()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n CWE191_Integer_Underflow__unsigned_int_rand_sub_64b_goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__unsigned_int_rand_sub_64b_goodB2GSink(void * dataVoidPtr);\r\n\r\nstatic void goodB2G()\r\n{\r\n unsigned int data;\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (unsigned int)RAND32();\r\n CWE191_Integer_Underflow__unsigned_int_rand_sub_64b_goodB2GSink(&data);\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_rand_sub_64_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__unsigned_int_rand_sub_64_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__unsigned_int_rand_sub_64_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_rand_sub_66a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE191_Integer_Underflow/s03/CWE191_Integer_Underflow__unsigned_int_rand_sub_66a.c","cwe_id":"CWE191","file_extension":".c","language_hint":"C","source_size_bytes":3075,"source_sha256":"760fa5523ac1f3c9f6898b4ad9e0271bf5f17f10d0617c3a482295037e3386ef","line_count":101,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE191_Integer_Underflow__unsigned_int_rand_sub_66a.c\r\nLabel Definition File: CWE191_Integer_Underflow.label.xml\r\nTemplate File: sources-sinks-66a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 191 Integer Underflow\r\n * BadSource: rand Set data to result of rand()\r\n * GoodSource: Set data to a small, non-zero number (negative two)\r\n * Sinks: sub\r\n * GoodSink: Ensure there will not be an underflow before subtracting 1 from data\r\n * BadSink : Subtract 1 from data, which can cause an Underflow\r\n * Flow Variant: 66 Data flow: data passed in an array from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE191_Integer_Underflow__unsigned_int_rand_sub_66b_badSink(unsigned int dataArray[]);\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_rand_sub_66_bad()\r\n{\r\n unsigned int data;\r\n unsigned int dataArray[5];\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (unsigned int)RAND32();\r\n /* put data in array */\r\n dataArray[2] = data;\r\n CWE191_Integer_Underflow__unsigned_int_rand_sub_66b_badSink(dataArray);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE191_Integer_Underflow__unsigned_int_rand_sub_66b_goodG2BSink(unsigned int dataArray[]);\r\n\r\nstatic void goodG2B()\r\n{\r\n unsigned int data;\r\n unsigned int dataArray[5];\r\n data = 0;\r\n /* FIX: Use a small, non-zero value that will not cause an underflow in the sinks */\r\n data = -2;\r\n dataArray[2] = data;\r\n CWE191_Integer_Underflow__unsigned_int_rand_sub_66b_goodG2BSink(dataArray);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE191_Integer_Underflow__unsigned_int_rand_sub_66b_goodB2GSink(unsigned int dataArray[]);\r\n\r\nstatic void goodB2G()\r\n{\r\n unsigned int data;\r\n unsigned int dataArray[5];\r\n data = 0;\r\n /* POTENTIAL FLAW: Use a random value */\r\n data = (unsigned int)RAND32();\r\n dataArray[2] = data;\r\n CWE191_Integer_Underflow__unsigned_int_rand_sub_66b_goodB2GSink(dataArray);\r\n}\r\n\r\nvoid CWE191_Integer_Underflow__unsigned_int_rand_sub_66_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE191_Integer_Underflow__unsigned_int_rand_sub_66_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE191_Integer_Underflow__unsigned_int_rand_sub_66_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_connect_socket_16.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_connect_socket_16.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":7656,"source_sha256":"ba934d532bb15a026cfefac8410ec41a9b9d936f2d6e0c1718496ef11f6fa866","line_count":263,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_connect_socket_16.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-16.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 16 Control flow: while(1)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define CHAR_ARRAY_SIZE 20\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_16_bad()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n while(1)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n break;\r\n }\r\n while(1)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the sinks in the second while statement */\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n while(1)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the sources in the first while statement */\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n while(1)\r\n {\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n break;\r\n }\r\n while(1)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_16_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_connect_socket_16_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_connect_socket_16_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_connect_socket_32.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_connect_socket_32.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":7893,"source_sha256":"c7f3db8acbcefe9b82b21f708cf101af5352aa9692242965b14717115cee3769","line_count":265,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_connect_socket_32.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-32.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 32 Data flow using two pointers to the same value within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define CHAR_ARRAY_SIZE 20\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_32_bad()\r\n{\r\n float data;\r\n float *dataPtr1 = &data;\r\n float *dataPtr2 = &data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n float data = *dataPtr1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n *dataPtr1 = data;\r\n }\r\n {\r\n float data = *dataPtr2;\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n float *dataPtr1 = &data;\r\n float *dataPtr2 = &data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n float data = *dataPtr1;\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n *dataPtr1 = data;\r\n }\r\n {\r\n float data = *dataPtr2;\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n float *dataPtr1 = &data;\r\n float *dataPtr2 = &data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n float data = *dataPtr1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n *dataPtr1 = data;\r\n }\r\n {\r\n float data = *dataPtr2;\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_32_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_connect_socket_32_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_connect_socket_32_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_connect_socket_41.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_connect_socket_41.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":7078,"source_sha256":"e990936b7446ac910561777fc3a581e14fa01bbf41a25cd302d45025c0f4376f","line_count":254,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_connect_socket_41.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-41.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 41 Data flow: data passed as an argument from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define CHAR_ARRAY_SIZE 20\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink(float data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_41_bad()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink(float data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink(float data)\r\n{\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n goodB2GSink(data);\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_41_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_connect_socket_41_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_connect_socket_41_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_connect_socket_42.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_connect_socket_42.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":7155,"source_sha256":"f5d15779f20d18d970e716d011d12043100903ca3c034c8b8c183302ae84bc8d","line_count":257,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_connect_socket_42.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-42.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 42 Data flow: data returned from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define CHAR_ARRAY_SIZE 20\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic float badSource(float data)\r\n{\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n return data;\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_42_bad()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n data = badSource(data);\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic float goodG2BSource(float data)\r\n{\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n return data;\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n data = goodG2BSource(data);\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic float goodB2GSource(float data)\r\n{\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n return data;\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n data = goodB2GSource(data);\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_42_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_connect_socket_42_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_connect_socket_42_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_connect_socket_44.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_connect_socket_44.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":7314,"source_sha256":"55d4205da953b63d4a2d604613e605b6ae23bf94acde8ff990bc59fc2d36f123","line_count":258,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_connect_socket_44.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-44.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * 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\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define CHAR_ARRAY_SIZE 20\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink(float data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_44_bad()\r\n{\r\n float data;\r\n /* define a function pointer */\r\n void (*funcPtr) (float) = badSink;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n /* use the function pointer */\r\n funcPtr(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink(float data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n void (*funcPtr) (float) = goodG2BSink;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n funcPtr(data);\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink(float data)\r\n{\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n void (*funcPtr) (float) = goodB2GSink;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n funcPtr(data);\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_44_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_connect_socket_44_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_connect_socket_44_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_connect_socket_45.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_connect_socket_45.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":7713,"source_sha256":"f6e58979c6c158e0ae138835da888241c3934ffa314c429cd8a9b23443738ad8","line_count":263,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_connect_socket_45.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-45.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 45 Data flow: data passed as a static global variable from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define CHAR_ARRAY_SIZE 20\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\nstatic float CWE369_Divide_by_Zero__float_connect_socket_45_badData;\r\nstatic float CWE369_Divide_by_Zero__float_connect_socket_45_goodG2BData;\r\nstatic float CWE369_Divide_by_Zero__float_connect_socket_45_goodB2GData;\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink()\r\n{\r\n float data = CWE369_Divide_by_Zero__float_connect_socket_45_badData;\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_45_bad()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE369_Divide_by_Zero__float_connect_socket_45_badData = data;\r\n badSink();\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink()\r\n{\r\n float data = CWE369_Divide_by_Zero__float_connect_socket_45_goodG2BData;\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n CWE369_Divide_by_Zero__float_connect_socket_45_goodG2BData = data;\r\n goodG2BSink();\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink()\r\n{\r\n float data = CWE369_Divide_by_Zero__float_connect_socket_45_goodB2GData;\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE369_Divide_by_Zero__float_connect_socket_45_goodB2GData = data;\r\n goodB2GSink();\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_45_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_connect_socket_45_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_connect_socket_45_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_connect_socket_51a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_connect_socket_51a.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":6812,"source_sha256":"8265a75bd56e2db14f740b28a0f37ae8c7751d78fe01b4120a791f9af8faf72d","line_count":229,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_connect_socket_51a.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-51a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 51 Data flow: data passed as an argument from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define CHAR_ARRAY_SIZE 20\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_51b_badSink(float data);\r\n\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_51_bad()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE369_Divide_by_Zero__float_connect_socket_51b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_51b_goodG2BSink(float data);\r\n\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n CWE369_Divide_by_Zero__float_connect_socket_51b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_51b_goodB2GSink(float data);\r\n\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE369_Divide_by_Zero__float_connect_socket_51b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_51_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_connect_socket_51_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_connect_socket_51_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_connect_socket_51b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_connect_socket_51b.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":2196,"source_sha256":"a0c478bca6497d8132542d0f7f9a98df62803f56db1033d7b8200c86a63ad3f3","line_count":84,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_connect_socket_51b.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-51b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 51 Data flow: data passed as an argument from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define CHAR_ARRAY_SIZE 20\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_51b_badSink(float data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_51b_goodG2BSink(float data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_51b_goodB2GSink(float data)\r\n{\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_connect_socket_63b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_connect_socket_63b.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":2291,"source_sha256":"b5dcd4ea6b26ddff08d01aead219fa782c314fd2e3ef40c5c69f29d0be4e9dbd","line_count":87,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_connect_socket_63b.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-63b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define CHAR_ARRAY_SIZE 20\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_63b_badSink(float * dataPtr)\r\n{\r\n float data = *dataPtr;\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_63b_goodG2BSink(float * dataPtr)\r\n{\r\n float data = *dataPtr;\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_63b_goodB2GSink(float * dataPtr)\r\n{\r\n float data = *dataPtr;\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_connect_socket_65b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_connect_socket_65b.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":2238,"source_sha256":"a6e4c8a476eb7388b9fac1b863023dd036f880f5be55d748c716125b741caf3e","line_count":84,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_connect_socket_65b.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-65b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define CHAR_ARRAY_SIZE 20\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_65b_badSink(float data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_65b_goodG2BSink(float data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE369_Divide_by_Zero__float_connect_socket_65b_goodB2GSink(float data)\r\n{\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_connect_socket_82.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_connect_socket_82.h","cwe_id":"CWE369","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1474,"source_sha256":"e2db674f3e916a6237801242f4b80109bbe86e685358c7e12ae4cd6bc567dfbe","line_count":58,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_connect_socket_82.h\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-82.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE369_Divide_by_Zero__float_connect_socket_82\r\n{\r\n\r\nclass CWE369_Divide_by_Zero__float_connect_socket_82_base\r\n{\r\npublic:\r\n /* pure virtual function */\r\n virtual void action(float data) = 0;\r\n};\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE369_Divide_by_Zero__float_connect_socket_82_bad : public CWE369_Divide_by_Zero__float_connect_socket_82_base\r\n{\r\npublic:\r\n void action(float data);\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE369_Divide_by_Zero__float_connect_socket_82_goodG2B : public CWE369_Divide_by_Zero__float_connect_socket_82_base\r\n{\r\npublic:\r\n void action(float data);\r\n};\r\n\r\nclass CWE369_Divide_by_Zero__float_connect_socket_82_goodB2G : public CWE369_Divide_by_Zero__float_connect_socket_82_base\r\n{\r\npublic:\r\n void action(float data);\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_connect_socket_83_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_connect_socket_83_goodB2G.cpp","cwe_id":"CWE369","file_extension":".cpp","language_hint":"C++","source_size_bytes":3743,"source_sha256":"67ff7d12dc71d4395bb653e4473fab882a957d2024d1e3d265dc3b7285aab7f6","line_count":121,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_connect_socket_83_goodB2G.cpp\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-83_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE369_Divide_by_Zero__float_connect_socket_83.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define CHAR_ARRAY_SIZE 20\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n\r\nnamespace CWE369_Divide_by_Zero__float_connect_socket_83\r\n{\r\nCWE369_Divide_by_Zero__float_connect_socket_83_goodB2G::CWE369_Divide_by_Zero__float_connect_socket_83_goodB2G(float dataCopy)\r\n{\r\n data = dataCopy;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n}\r\n\r\nCWE369_Divide_by_Zero__float_connect_socket_83_goodB2G::~CWE369_Divide_by_Zero__float_connect_socket_83_goodB2G()\r\n{\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_fgets_06.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_fgets_06.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":6064,"source_sha256":"8a4f61959e85a18430537a20978ae24374b85333aafd238e59aa15468a77e4a4","line_count":226,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_fgets_06.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-06.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 06 Control flow: if(STATIC_CONST_FIVE==5) and if(STATIC_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#define CHAR_ARRAY_SIZE 20\r\n\r\n/* The variable below is declared \"const\", so a tool should be able\r\n to identify that reads of this will always give its initialized\r\n value. */\r\nstatic const int STATIC_CONST_FIVE = 5;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__float_fgets_06_bad()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n /* POTENTIAL FLAW: Use a value input from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodB2G1()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n /* POTENTIAL FLAW: Use a value input from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n }\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n /* POTENTIAL FLAW: Use a value input from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_fgets_06_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_fgets_06_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_fgets_06_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_fgets_11.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_fgets_11.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":5859,"source_sha256":"37286fc51a5281bf82b39b4851d92040bb347a9380afadc188d8c320550b8634","line_count":221,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_fgets_11.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-11.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#define CHAR_ARRAY_SIZE 20\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__float_fgets_11_bad()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n /* POTENTIAL FLAW: Use a value input from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n /* POTENTIAL FLAW: Use a value input from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n }\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n /* POTENTIAL FLAW: Use a value input from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_fgets_11_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_fgets_11_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_fgets_11_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_fgets_31.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_fgets_31.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":3726,"source_sha256":"523a0424b0a5664672c1dc43a61f59b9661031f1026bd1dfc268192be52c4337","line_count":144,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_fgets_31.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-31.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 31 Data flow using a copy of data within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#define CHAR_ARRAY_SIZE 20\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__float_fgets_31_bad()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n /* POTENTIAL FLAW: Use a value input from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n {\r\n float dataCopy = data;\r\n float data = dataCopy;\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n {\r\n float dataCopy = data;\r\n float data = dataCopy;\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n /* POTENTIAL FLAW: Use a value input from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n {\r\n float dataCopy = data;\r\n float data = dataCopy;\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_fgets_31_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_fgets_31_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_fgets_31_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_fgets_73b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_fgets_73b.cpp","cwe_id":"CWE369","file_extension":".cpp","language_hint":"C++","source_size_bytes":1797,"source_sha256":"603442df8aeeea72bfcfd020a87fb8e79618b1459f5b85822b21468fbe48ce50","line_count":74,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_fgets_73b.cpp\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-73b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE369_Divide_by_Zero__float_fgets_73\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(list dataList)\r\n{\r\n /* copy data out of dataList */\r\n float data = dataList.back();\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(list dataList)\r\n{\r\n float data = dataList.back();\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(list dataList)\r\n{\r\n float data = dataList.back();\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_fgets_81a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_fgets_81a.cpp","cwe_id":"CWE369","file_extension":".cpp","language_hint":"C++","source_size_bytes":3388,"source_sha256":"3d7db6dbad7744b661459fe168d0028eb94434b7263e30175bf91a56fa2cb74d","line_count":125,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_fgets_81a.cpp\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-81a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE369_Divide_by_Zero__float_fgets_81.h\"\r\n\r\n#define CHAR_ARRAY_SIZE 20\r\n\r\nnamespace CWE369_Divide_by_Zero__float_fgets_81\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n /* POTENTIAL FLAW: Use a value input from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n const CWE369_Divide_by_Zero__float_fgets_81_base& baseObject = CWE369_Divide_by_Zero__float_fgets_81_bad();\r\n baseObject.action(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n const CWE369_Divide_by_Zero__float_fgets_81_base& baseObject = CWE369_Divide_by_Zero__float_fgets_81_goodG2B();\r\n baseObject.action(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n /* POTENTIAL FLAW: Use a value input from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n const CWE369_Divide_by_Zero__float_fgets_81_base& baseObject = CWE369_Divide_by_Zero__float_fgets_81_goodB2G();\r\n baseObject.action(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE369_Divide_by_Zero__float_fgets_81; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_fgets_82a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_fgets_82a.cpp","cwe_id":"CWE369","file_extension":".cpp","language_hint":"C++","source_size_bytes":3449,"source_sha256":"1d2a961653a471068d148d2deda3ee63e6d36788a96ce396952ae520c8590da2","line_count":128,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_fgets_82a.cpp\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-82a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE369_Divide_by_Zero__float_fgets_82.h\"\r\n\r\n#define CHAR_ARRAY_SIZE 20\r\n\r\nnamespace CWE369_Divide_by_Zero__float_fgets_82\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n /* POTENTIAL FLAW: Use a value input from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n CWE369_Divide_by_Zero__float_fgets_82_base* baseObject = new CWE369_Divide_by_Zero__float_fgets_82_bad;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n CWE369_Divide_by_Zero__float_fgets_82_base* baseObject = new CWE369_Divide_by_Zero__float_fgets_82_goodG2B;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n /* POTENTIAL FLAW: Use a value input from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n CWE369_Divide_by_Zero__float_fgets_82_base* baseObject = new CWE369_Divide_by_Zero__float_fgets_82_goodB2G;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE369_Divide_by_Zero__float_fgets_82; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_fgets_84.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_fgets_84.h","cwe_id":"CWE369","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1484,"source_sha256":"ee4d38816025f7efee1133dd888305ef5f88ffe286c8667bb3f426aeb2590172","line_count":63,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_fgets_84.h\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-84.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE369_Divide_by_Zero__float_fgets_84\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE369_Divide_by_Zero__float_fgets_84_bad\r\n{\r\npublic:\r\n CWE369_Divide_by_Zero__float_fgets_84_bad(float dataCopy);\r\n ~CWE369_Divide_by_Zero__float_fgets_84_bad();\r\n\r\nprivate:\r\n float data;\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE369_Divide_by_Zero__float_fgets_84_goodG2B\r\n{\r\npublic:\r\n CWE369_Divide_by_Zero__float_fgets_84_goodG2B(float dataCopy);\r\n ~CWE369_Divide_by_Zero__float_fgets_84_goodG2B();\r\n\r\nprivate:\r\n float data;\r\n};\r\n\r\nclass CWE369_Divide_by_Zero__float_fgets_84_goodB2G\r\n{\r\npublic:\r\n CWE369_Divide_by_Zero__float_fgets_84_goodB2G(float dataCopy);\r\n ~CWE369_Divide_by_Zero__float_fgets_84_goodB2G();\r\n\r\nprivate:\r\n float data;\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_fscanf_01.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_fscanf_01.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":2790,"source_sha256":"8813a9a496e204462408d07f92829af404cd28876df1357aca797fa9f13f3bad","line_count":109,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_fscanf_01.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-01.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 01 Baseline\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__float_fscanf_01_bad()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* POTENTIAL FLAW: Use a value input from the console using fscanf() */\r\n fscanf (stdin, \"%f\", &data);\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* POTENTIAL FLAW: Use a value input from the console using fscanf() */\r\n fscanf (stdin, \"%f\", &data);\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_fscanf_01_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_fscanf_01_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_fscanf_01_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_fscanf_08.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_fscanf_08.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":5157,"source_sha256":"90fb53dac3922e148a172acc6e395302ba94abaf1e5f622e36cd31c2003c367f","line_count":199,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_fscanf_08.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-08.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 08 Control flow: if(staticReturnsTrue()) and if(staticReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The two function below always return the same value, so a tool\r\n should be able to identify that calls to the functions will always\r\n return a fixed value. */\r\nstatic int staticReturnsTrue()\r\n{\r\n return 1;\r\n}\r\n\r\nstatic int staticReturnsFalse()\r\n{\r\n return 0;\r\n}\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__float_fscanf_08_bad()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use a value input from the console using fscanf() */\r\n fscanf (stdin, \"%f\", &data);\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use a value input from the console using fscanf() */\r\n fscanf (stdin, \"%f\", &data);\r\n }\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use a value input from the console using fscanf() */\r\n fscanf (stdin, \"%f\", &data);\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_fscanf_08_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_fscanf_08_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_fscanf_08_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_fscanf_53c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_fscanf_53c.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":1552,"source_sha256":"b3252e48e21da94eb1e70ffabd6fc4286d3cda9a0d3acc8a8be5b2f6f8e5ab41","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_fscanf_53c.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-53c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE369_Divide_by_Zero__float_fscanf_53d_badSink(float data);\r\n\r\nvoid CWE369_Divide_by_Zero__float_fscanf_53c_badSink(float data)\r\n{\r\n CWE369_Divide_by_Zero__float_fscanf_53d_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE369_Divide_by_Zero__float_fscanf_53d_goodG2BSink(float data);\r\n\r\nvoid CWE369_Divide_by_Zero__float_fscanf_53c_goodG2BSink(float data)\r\n{\r\n CWE369_Divide_by_Zero__float_fscanf_53d_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE369_Divide_by_Zero__float_fscanf_53d_goodB2GSink(float data);\r\n\r\nvoid CWE369_Divide_by_Zero__float_fscanf_53c_goodB2GSink(float data)\r\n{\r\n CWE369_Divide_by_Zero__float_fscanf_53d_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_fscanf_66b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_fscanf_66b.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":1779,"source_sha256":"ddcda88c0f5fdc4435f81520be8c977349212b4edea9846acd2f16106dc68a64","line_count":66,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_fscanf_66b.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-66b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 66 Data flow: data passed in an array from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__float_fscanf_66b_badSink(float dataArray[])\r\n{\r\n /* copy data out of dataArray */\r\n float data = dataArray[2];\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE369_Divide_by_Zero__float_fscanf_66b_goodG2BSink(float dataArray[])\r\n{\r\n float data = dataArray[2];\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE369_Divide_by_Zero__float_fscanf_66b_goodB2GSink(float dataArray[])\r\n{\r\n float data = dataArray[2];\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_fscanf_74a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_fscanf_74a.cpp","cwe_id":"CWE369","file_extension":".cpp","language_hint":"C++","source_size_bytes":2997,"source_sha256":"a6dcb6e7d72c28c8ab7a81de5b79f04dc477072bc27d3d2cb28740243715ed1b","line_count":123,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_fscanf_74a.cpp\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-74a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 74 Data flow: data passed in a map from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE369_Divide_by_Zero__float_fscanf_74\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(map dataMap);\r\n\r\nvoid bad()\r\n{\r\n float data;\r\n map dataMap;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* POTENTIAL FLAW: Use a value input from the console using fscanf() */\r\n fscanf (stdin, \"%f\", &data);\r\n /* Put data in a map */\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n badSink(dataMap);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(map dataMap);\r\n\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n map dataMap;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n /* Put data in a map */\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n goodG2BSink(dataMap);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(map dataMap);\r\n\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n map dataMap;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* POTENTIAL FLAW: Use a value input from the console using fscanf() */\r\n fscanf (stdin, \"%f\", &data);\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n goodB2GSink(dataMap);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE369_Divide_by_Zero__float_fscanf_74; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_listenSocket_18.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_listenSocket_18.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":7659,"source_sha256":"819326d6566dbb73552e4a2baa5c0217744a5ed37e3bc8870c2cd26c456793ba","line_count":277,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_listenSocket_18.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-18.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: listenSocket Read data using a listen socket (server side)\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 18 Control flow: goto statements\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE 20\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__float_listenSocket_18_bad()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n goto source;\r\nsource:\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n goto sink;\r\nsink:\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by reversing the blocks on the second goto statement */\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n goto source;\r\nsource:\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n goto sink;\r\nsink:\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by reversing the blocks on the first goto statement */\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n goto source;\r\nsource:\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n goto sink;\r\nsink:\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_listenSocket_18_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_listenSocket_18_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_listenSocket_18_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_listenSocket_45.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_listenSocket_45.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":8293,"source_sha256":"d5e22cddc2820ce3cb81da097e976505283a0090c26ffe5be6cc9d67b3ee3100","line_count":289,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_listenSocket_45.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-45.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: listenSocket Read data using a listen socket (server side)\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 45 Data flow: data passed as a static global variable from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE 20\r\n\r\nstatic float CWE369_Divide_by_Zero__float_listenSocket_45_badData;\r\nstatic float CWE369_Divide_by_Zero__float_listenSocket_45_goodG2BData;\r\nstatic float CWE369_Divide_by_Zero__float_listenSocket_45_goodB2GData;\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink()\r\n{\r\n float data = CWE369_Divide_by_Zero__float_listenSocket_45_badData;\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_listenSocket_45_bad()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE369_Divide_by_Zero__float_listenSocket_45_badData = data;\r\n badSink();\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink()\r\n{\r\n float data = CWE369_Divide_by_Zero__float_listenSocket_45_goodG2BData;\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n CWE369_Divide_by_Zero__float_listenSocket_45_goodG2BData = data;\r\n goodG2BSink();\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink()\r\n{\r\n float data = CWE369_Divide_by_Zero__float_listenSocket_45_goodB2GData;\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE369_Divide_by_Zero__float_listenSocket_45_goodB2GData = data;\r\n goodB2GSink();\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_listenSocket_45_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_listenSocket_45_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_listenSocket_45_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_listenSocket_52a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_listenSocket_52a.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":7415,"source_sha256":"1f4536199c5758f643564fd84719fa438d9c5892d0262f91ec6ccfcf0d79747b","line_count":255,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_listenSocket_52a.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-52a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: listenSocket Read data using a listen socket (server side)\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE 20\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE369_Divide_by_Zero__float_listenSocket_52b_badSink(float data);\r\n\r\nvoid CWE369_Divide_by_Zero__float_listenSocket_52_bad()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE369_Divide_by_Zero__float_listenSocket_52b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE369_Divide_by_Zero__float_listenSocket_52b_goodG2BSink(float data);\r\n\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n CWE369_Divide_by_Zero__float_listenSocket_52b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE369_Divide_by_Zero__float_listenSocket_52b_goodB2GSink(float data);\r\n\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE369_Divide_by_Zero__float_listenSocket_52b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_listenSocket_52_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_listenSocket_52_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_listenSocket_52_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_listenSocket_61b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_listenSocket_61b.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":6033,"source_sha256":"e862cced18a9caf474cb228a6fd81af9f01d9b22504e6e8ff2b9906a15bf4c5d","line_count":207,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_listenSocket_61b.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-61b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: listenSocket Read data using a listen socket (server side)\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 61 Data flow: data returned from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE 20\r\n\r\n#ifndef OMITBAD\r\n\r\nfloat CWE369_Divide_by_Zero__float_listenSocket_61b_badSource(float data)\r\n{\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n return data;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nfloat CWE369_Divide_by_Zero__float_listenSocket_61b_goodG2BSource(float data)\r\n{\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n return data;\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nfloat CWE369_Divide_by_Zero__float_listenSocket_61b_goodB2GSource(float data)\r\n{\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n return data;\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_listenSocket_65a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_listenSocket_65a.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":7633,"source_sha256":"3124be4f1af02dd4762b8523f81b58def8e120f1d3f429248976acc251fc5dde","line_count":260,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_listenSocket_65a.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-65a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: listenSocket Read data using a listen socket (server side)\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE 20\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE369_Divide_by_Zero__float_listenSocket_65b_badSink(float data);\r\n\r\nvoid CWE369_Divide_by_Zero__float_listenSocket_65_bad()\r\n{\r\n float data;\r\n /* define a function pointer */\r\n void (*funcPtr) (float) = CWE369_Divide_by_Zero__float_listenSocket_65b_badSink;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n /* use the function pointer */\r\n funcPtr(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE369_Divide_by_Zero__float_listenSocket_65b_goodG2BSink(float data);\r\n\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n void (*funcPtr) (float) = CWE369_Divide_by_Zero__float_listenSocket_65b_goodG2BSink;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n funcPtr(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE369_Divide_by_Zero__float_listenSocket_65b_goodB2GSink(float data);\r\n\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n void (*funcPtr) (float) = CWE369_Divide_by_Zero__float_listenSocket_65b_goodB2GSink;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n funcPtr(data);\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_listenSocket_65_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_listenSocket_65_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_listenSocket_65_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_listenSocket_73b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_listenSocket_73b.cpp","cwe_id":"CWE369","file_extension":".cpp","language_hint":"C++","source_size_bytes":1823,"source_sha256":"57240fd1768cf429237d97e719840f239aabb7639310a816dfab18909e3feaf9","line_count":74,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_listenSocket_73b.cpp\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-73b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: listenSocket Read data using a listen socket (server side)\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE369_Divide_by_Zero__float_listenSocket_73\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(list dataList)\r\n{\r\n /* copy data out of dataList */\r\n float data = dataList.back();\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(list dataList)\r\n{\r\n float data = dataList.back();\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(list dataList)\r\n{\r\n float data = dataList.back();\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_listenSocket_74a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_listenSocket_74a.cpp","cwe_id":"CWE369","file_extension":".cpp","language_hint":"C++","source_size_bytes":7590,"source_sha256":"a107e8ba4f3a436ea1e1b37fe69cd8d7274d47db97238488ac4dd102fee7320f","line_count":279,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_listenSocket_74a.cpp\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-74a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: listenSocket Read data using a listen socket (server side)\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 74 Data flow: data passed in a map from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE 20\r\n\r\nusing namespace std;\r\n\r\nnamespace CWE369_Divide_by_Zero__float_listenSocket_74\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(map dataMap);\r\n\r\nvoid bad()\r\n{\r\n float data;\r\n map dataMap;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n /* Put data in a map */\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n badSink(dataMap);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(map dataMap);\r\n\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n map dataMap;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n /* Put data in a map */\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n goodG2BSink(dataMap);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(map dataMap);\r\n\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n map dataMap;\r\n /* Initialize data */\r\n data = 0.0F;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n goodB2GSink(dataMap);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE369_Divide_by_Zero__float_listenSocket_74; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_listenSocket_81_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_listenSocket_81_goodG2B.cpp","cwe_id":"CWE369","file_extension":".cpp","language_hint":"C++","source_size_bytes":1055,"source_sha256":"54c36baa6875952e3fcd5b8ae4f7e967fca1d55b40e28e7fa3eb5de97e94925d","line_count":35,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_listenSocket_81_goodG2B.cpp\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-81_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: listenSocket Read data using a listen socket (server side)\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE369_Divide_by_Zero__float_listenSocket_81.h\"\r\n\r\nnamespace CWE369_Divide_by_Zero__float_listenSocket_81\r\n{\r\n\r\nvoid CWE369_Divide_by_Zero__float_listenSocket_81_goodG2B::action(float data) const\r\n{\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_listenSocket_84_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_listenSocket_84_goodB2G.cpp","cwe_id":"CWE369","file_extension":".cpp","language_hint":"C++","source_size_bytes":4045,"source_sha256":"55e7c0846b42f8e0dc5900d3967693756b97a5acf898149420075fed1d179563","line_count":134,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_listenSocket_84_goodB2G.cpp\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-84_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: listenSocket Read data using a listen socket (server side)\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE369_Divide_by_Zero__float_listenSocket_84.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE 20\r\n\r\nnamespace CWE369_Divide_by_Zero__float_listenSocket_84\r\n{\r\nCWE369_Divide_by_Zero__float_listenSocket_84_goodB2G::CWE369_Divide_by_Zero__float_listenSocket_84_goodB2G(float dataCopy)\r\n{\r\n data = dataCopy;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n /* POTENTIAL FLAW: Use a value input from the network */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to float */\r\n data = (float)atof(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n}\r\n\r\nCWE369_Divide_by_Zero__float_listenSocket_84_goodB2G::~CWE369_Divide_by_Zero__float_listenSocket_84_goodB2G()\r\n{\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_10.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_10.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":4719,"source_sha256":"8f5a93529d6f8ad589081c116ac8b6ff3c0daa0ef5aec4b33193b56f57e40189","line_count":186,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_rand_10.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-10.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: rand Set data to result of RAND32(), which may be zero\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 10 Control flow: if(globalTrue) and if(globalFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__float_rand_10_bad()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Use a random number that could possibly equal zero */\r\n data = (float)RAND32();\r\n }\r\n if(globalTrue)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalTrue to globalFalse */\r\nstatic void goodB2G1()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Use a random number that could possibly equal zero */\r\n data = (float)RAND32();\r\n }\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Use a random number that could possibly equal zero */\r\n data = (float)RAND32();\r\n }\r\n if(globalTrue)\r\n {\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalTrue to globalFalse */\r\nstatic void goodG2B1()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n }\r\n if(globalTrue)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(globalTrue)\r\n {\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n }\r\n if(globalTrue)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_rand_10_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_rand_10_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_rand_10_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_12.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_12.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":4698,"source_sha256":"5707c909b4244a6f9eee73c81cdca38cf81fdbb2ff1f1d89f5c485304e008fed","line_count":180,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_rand_12.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-12.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: rand Set data to result of RAND32(), which may be zero\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__float_rand_12_bad()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Use a random number that could possibly equal zero */\r\n data = (float)RAND32();\r\n }\r\n else\r\n {\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n else\r\n {\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the first \"if\" so that\r\n both branches use the BadSource and the second \"if\" so that both branches\r\n use the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Use a random number that could possibly equal zero */\r\n data = (float)RAND32();\r\n }\r\n else\r\n {\r\n /* POTENTIAL FLAW: Use a random number that could possibly equal zero */\r\n data = (float)RAND32();\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n else\r\n {\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the first \"if\" so that\r\n both branches use the GoodSource and the second \"if\" so that both branches\r\n use the BadSink */\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n }\r\n else\r\n {\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n else\r\n {\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_rand_12_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_rand_12_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_rand_12_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_31.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_31.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":3141,"source_sha256":"9616964d7967e1f0829d28fbb4da5702d76f0d5d1b5576d2ca43915d319a0306","line_count":120,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_rand_31.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-31.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: rand Set data to result of RAND32(), which may be zero\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 31 Data flow using a copy of data within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__float_rand_31_bad()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* POTENTIAL FLAW: Use a random number that could possibly equal zero */\r\n data = (float)RAND32();\r\n {\r\n float dataCopy = data;\r\n float data = dataCopy;\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n {\r\n float dataCopy = data;\r\n float data = dataCopy;\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* POTENTIAL FLAW: Use a random number that could possibly equal zero */\r\n data = (float)RAND32();\r\n {\r\n float dataCopy = data;\r\n float data = dataCopy;\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_rand_31_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_rand_31_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_rand_31_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_34.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_34.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":3522,"source_sha256":"e820236a11efaeec2d0b57a822d75b56e83592fb7caef46396e0d51d4f83b161","line_count":129,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_rand_34.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-34.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: rand Set data to result of RAND32(), which may be zero\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 34 Data flow: use of a union containing two methods of accessing the same data (within the same function)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\ntypedef union\r\n{\r\n float unionFirst;\r\n float unionSecond;\r\n} CWE369_Divide_by_Zero__float_rand_34_unionType;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__float_rand_34_bad()\r\n{\r\n float data;\r\n CWE369_Divide_by_Zero__float_rand_34_unionType myUnion;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* POTENTIAL FLAW: Use a random number that could possibly equal zero */\r\n data = (float)RAND32();\r\n myUnion.unionFirst = data;\r\n {\r\n float data = myUnion.unionSecond;\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n CWE369_Divide_by_Zero__float_rand_34_unionType myUnion;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n myUnion.unionFirst = data;\r\n {\r\n float data = myUnion.unionSecond;\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n CWE369_Divide_by_Zero__float_rand_34_unionType myUnion;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* POTENTIAL FLAW: Use a random number that could possibly equal zero */\r\n data = (float)RAND32();\r\n myUnion.unionFirst = data;\r\n {\r\n float data = myUnion.unionSecond;\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_rand_34_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_rand_34_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_rand_34_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_44.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_44.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":3295,"source_sha256":"0bf967920c8cffb739fd6abd3c241218b8d577a40cd2f920efa09a9d8e7aa898","line_count":128,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_rand_44.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-44.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: rand Set data to result of RAND32(), which may be zero\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * 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\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink(float data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_rand_44_bad()\r\n{\r\n float data;\r\n /* define a function pointer */\r\n void (*funcPtr) (float) = badSink;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* POTENTIAL FLAW: Use a random number that could possibly equal zero */\r\n data = (float)RAND32();\r\n /* use the function pointer */\r\n funcPtr(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink(float data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n void (*funcPtr) (float) = goodG2BSink;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n funcPtr(data);\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink(float data)\r\n{\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n void (*funcPtr) (float) = goodB2GSink;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* POTENTIAL FLAW: Use a random number that could possibly equal zero */\r\n data = (float)RAND32();\r\n funcPtr(data);\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_rand_44_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_rand_44_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_rand_44_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_45.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_45.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":3604,"source_sha256":"bf9bd09cec8d1e5704e1476ea737d2f77d3dc37f98bf24f3642ed227da580dc7","line_count":133,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_rand_45.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-45.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: rand Set data to result of RAND32(), which may be zero\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 45 Data flow: data passed as a static global variable from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nstatic float CWE369_Divide_by_Zero__float_rand_45_badData;\r\nstatic float CWE369_Divide_by_Zero__float_rand_45_goodG2BData;\r\nstatic float CWE369_Divide_by_Zero__float_rand_45_goodB2GData;\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink()\r\n{\r\n float data = CWE369_Divide_by_Zero__float_rand_45_badData;\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_rand_45_bad()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* POTENTIAL FLAW: Use a random number that could possibly equal zero */\r\n data = (float)RAND32();\r\n CWE369_Divide_by_Zero__float_rand_45_badData = data;\r\n badSink();\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink()\r\n{\r\n float data = CWE369_Divide_by_Zero__float_rand_45_goodG2BData;\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n CWE369_Divide_by_Zero__float_rand_45_goodG2BData = data;\r\n goodG2BSink();\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink()\r\n{\r\n float data = CWE369_Divide_by_Zero__float_rand_45_goodB2GData;\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* POTENTIAL FLAW: Use a random number that could possibly equal zero */\r\n data = (float)RAND32();\r\n CWE369_Divide_by_Zero__float_rand_45_goodB2GData = data;\r\n goodB2GSink();\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_rand_45_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_rand_45_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_rand_45_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_51a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_51a.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":2733,"source_sha256":"a574719fccb9f73ddb91649523f55e21e281ca5834820b6acb4a972faa0515b2","line_count":99,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_rand_51a.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-51a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: rand Set data to result of RAND32(), which may be zero\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 51 Data flow: data passed as an argument from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE369_Divide_by_Zero__float_rand_51b_badSink(float data);\r\n\r\nvoid CWE369_Divide_by_Zero__float_rand_51_bad()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* POTENTIAL FLAW: Use a random number that could possibly equal zero */\r\n data = (float)RAND32();\r\n CWE369_Divide_by_Zero__float_rand_51b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE369_Divide_by_Zero__float_rand_51b_goodG2BSink(float data);\r\n\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n CWE369_Divide_by_Zero__float_rand_51b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE369_Divide_by_Zero__float_rand_51b_goodB2GSink(float data);\r\n\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* POTENTIAL FLAW: Use a random number that could possibly equal zero */\r\n data = (float)RAND32();\r\n CWE369_Divide_by_Zero__float_rand_51b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_rand_51_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_rand_51_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_rand_51_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_62b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_62b.cpp","cwe_id":"CWE369","file_extension":".cpp","language_hint":"C++","source_size_bytes":1359,"source_sha256":"956873025a2cb173fc023b1796bda9e870e0455161bf90330db58a0cc40814b2","line_count":53,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_rand_62b.cpp\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-62b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: rand Set data to result of RAND32(), which may be zero\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE369_Divide_by_Zero__float_rand_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSource(float &data)\r\n{\r\n /* POTENTIAL FLAW: Use a random number that could possibly equal zero */\r\n data = (float)RAND32();\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(float &data)\r\n{\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(float &data)\r\n{\r\n /* POTENTIAL FLAW: Use a random number that could possibly equal zero */\r\n data = (float)RAND32();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_66a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_66a.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":2948,"source_sha256":"eebf83cfb7fd83ee576f909464c7230820ad7f55c734b4f19ba71de55ed53fb3","line_count":106,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_rand_66a.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-66a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: rand Set data to result of RAND32(), which may be zero\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 66 Data flow: data passed in an array from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE369_Divide_by_Zero__float_rand_66b_badSink(float dataArray[]);\r\n\r\nvoid CWE369_Divide_by_Zero__float_rand_66_bad()\r\n{\r\n float data;\r\n float dataArray[5];\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* POTENTIAL FLAW: Use a random number that could possibly equal zero */\r\n data = (float)RAND32();\r\n /* put data in array */\r\n dataArray[2] = data;\r\n CWE369_Divide_by_Zero__float_rand_66b_badSink(dataArray);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE369_Divide_by_Zero__float_rand_66b_goodG2BSink(float dataArray[]);\r\n\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n float dataArray[5];\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n dataArray[2] = data;\r\n CWE369_Divide_by_Zero__float_rand_66b_goodG2BSink(dataArray);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE369_Divide_by_Zero__float_rand_66b_goodB2GSink(float dataArray[]);\r\n\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n float dataArray[5];\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* POTENTIAL FLAW: Use a random number that could possibly equal zero */\r\n data = (float)RAND32();\r\n dataArray[2] = data;\r\n CWE369_Divide_by_Zero__float_rand_66b_goodB2GSink(dataArray);\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_rand_66_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_rand_66_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_rand_66_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_68b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_68b.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":1991,"source_sha256":"ce59b43d2e7e76e21a9159d22b3eb9c9c5798886e733786fb2e81b6cde6d6320","line_count":69,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_rand_68b.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-68b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: rand Set data to result of RAND32(), which may be zero\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 68 Data flow: data passed as a global variable from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nextern float CWE369_Divide_by_Zero__float_rand_68_badData;\r\nextern float CWE369_Divide_by_Zero__float_rand_68_goodG2BData;\r\nextern float CWE369_Divide_by_Zero__float_rand_68_goodB2GData;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__float_rand_68b_badSink()\r\n{\r\n float data = CWE369_Divide_by_Zero__float_rand_68_badData;\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE369_Divide_by_Zero__float_rand_68b_goodG2BSink()\r\n{\r\n float data = CWE369_Divide_by_Zero__float_rand_68_goodG2BData;\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE369_Divide_by_Zero__float_rand_68b_goodB2GSink()\r\n{\r\n float data = CWE369_Divide_by_Zero__float_rand_68_goodB2GData;\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_74b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_74b.cpp","cwe_id":"CWE369","file_extension":".cpp","language_hint":"C++","source_size_bytes":1794,"source_sha256":"f6455f4aaf4daa18c9d5460f028e558ebd59da36de93b32289da4feb2b36c207","line_count":74,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_rand_74b.cpp\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-74b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: rand Set data to result of RAND32(), which may be zero\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 74 Data flow: data passed in a map from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE369_Divide_by_Zero__float_rand_74\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(map dataMap)\r\n{\r\n /* copy data out of dataMap */\r\n float data = dataMap[2];\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(map dataMap)\r\n{\r\n float data = dataMap[2];\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(map dataMap)\r\n{\r\n float data = dataMap[2];\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_81_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_81_goodB2G.cpp","cwe_id":"CWE369","file_extension":".cpp","language_hint":"C++","source_size_bytes":1144,"source_sha256":"7c255afc6cbf400b5ec372e5e966bf83352a1885e0f7c51cc0129a6993151c6e","line_count":40,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_rand_81_goodB2G.cpp\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-81_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: rand Set data to result of RAND32(), which may be zero\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE369_Divide_by_Zero__float_rand_81.h\"\r\n\r\nnamespace CWE369_Divide_by_Zero__float_rand_81\r\n{\r\n\r\nvoid CWE369_Divide_by_Zero__float_rand_81_goodB2G::action(float data) const\r\n{\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_81a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_rand_81a.cpp","cwe_id":"CWE369","file_extension":".cpp","language_hint":"C++","source_size_bytes":2798,"source_sha256":"66393b57a9a41cebb80519923f7721bb2d5af45a17b4c3acb8bd076224b6fb5f","line_count":101,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_rand_81a.cpp\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-81a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: rand Set data to result of RAND32(), which may be zero\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE369_Divide_by_Zero__float_rand_81.h\"\r\n\r\nnamespace CWE369_Divide_by_Zero__float_rand_81\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* POTENTIAL FLAW: Use a random number that could possibly equal zero */\r\n data = (float)RAND32();\r\n const CWE369_Divide_by_Zero__float_rand_81_base& baseObject = CWE369_Divide_by_Zero__float_rand_81_bad();\r\n baseObject.action(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n const CWE369_Divide_by_Zero__float_rand_81_base& baseObject = CWE369_Divide_by_Zero__float_rand_81_goodG2B();\r\n baseObject.action(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* POTENTIAL FLAW: Use a random number that could possibly equal zero */\r\n data = (float)RAND32();\r\n const CWE369_Divide_by_Zero__float_rand_81_base& baseObject = CWE369_Divide_by_Zero__float_rand_81_goodB2G();\r\n baseObject.action(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE369_Divide_by_Zero__float_rand_81; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_zero_07.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_zero_07.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":4820,"source_sha256":"bea0a242b844bef2facc81ed9fb1b50853351e75b037bca3a97daccd33ab21d0","line_count":191,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_zero_07.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-07.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: zero Fixed value of zero\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 07 Control flow: if(staticFive==5) and if(staticFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The variable below is not declared \"const\", but is never assigned\r\n any other value so a tool should be able to identify that reads of\r\n this will always give its initialized value. */\r\nstatic int staticFive = 5;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__float_zero_07_bad()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Set data to zero */\r\n data = 0.0F;\r\n }\r\n if(staticFive==5)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticFive==5 to staticFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Set data to zero */\r\n data = 0.0F;\r\n }\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Set data to zero */\r\n data = 0.0F;\r\n }\r\n if(staticFive==5)\r\n {\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticFive==5 to staticFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n }\r\n if(staticFive==5)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(staticFive==5)\r\n {\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n }\r\n if(staticFive==5)\r\n {\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_zero_07_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_zero_07_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_zero_07_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_zero_08.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_zero_08.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":4976,"source_sha256":"f28266ba3470360ff24aa943a6ea74a538d9fa7d9ce859ce6ea9da716b1eedcb","line_count":199,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_zero_08.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-08.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: zero Fixed value of zero\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 08 Control flow: if(staticReturnsTrue()) and if(staticReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The two function below always return the same value, so a tool\r\n should be able to identify that calls to the functions will always\r\n return a fixed value. */\r\nstatic int staticReturnsTrue()\r\n{\r\n return 1;\r\n}\r\n\r\nstatic int staticReturnsFalse()\r\n{\r\n return 0;\r\n}\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__float_zero_08_bad()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Set data to zero */\r\n data = 0.0F;\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Set data to zero */\r\n data = 0.0F;\r\n }\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Set data to zero */\r\n data = 0.0F;\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_zero_08_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_zero_08_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_zero_08_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_zero_52c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_zero_52c.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":1612,"source_sha256":"d147e296180303b9990501a1a1faaa993a5632a5e5b47e19f2dc04b8615847a5","line_count":62,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_zero_52c.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-52c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: zero Fixed value of zero\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__float_zero_52c_badSink(float data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE369_Divide_by_Zero__float_zero_52c_goodG2BSink(float data)\r\n{\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE369_Divide_by_Zero__float_zero_52c_goodB2GSink(float data)\r\n{\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_zero_53a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_zero_53a.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":2657,"source_sha256":"f960f4b74f4c98509ab4d2e3d962f23db9393fcfcb70853ae9214cf046d2d1ef","line_count":99,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_zero_53a.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-53a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: zero Fixed value of zero\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE369_Divide_by_Zero__float_zero_53b_badSink(float data);\r\n\r\nvoid CWE369_Divide_by_Zero__float_zero_53_bad()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* POTENTIAL FLAW: Set data to zero */\r\n data = 0.0F;\r\n CWE369_Divide_by_Zero__float_zero_53b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE369_Divide_by_Zero__float_zero_53b_goodG2BSink(float data);\r\n\r\nstatic void goodG2B()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* FIX: Use a hardcoded number that won't a divide by zero */\r\n data = 2.0F;\r\n CWE369_Divide_by_Zero__float_zero_53b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE369_Divide_by_Zero__float_zero_53b_goodB2GSink(float data);\r\n\r\nstatic void goodB2G()\r\n{\r\n float data;\r\n /* Initialize data */\r\n data = 0.0F;\r\n /* POTENTIAL FLAW: Set data to zero */\r\n data = 0.0F;\r\n CWE369_Divide_by_Zero__float_zero_53b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__float_zero_53_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__float_zero_53_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__float_zero_53_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_zero_54d.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_zero_54d.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":1509,"source_sha256":"df39faee895cd48d981af0733d3b3de5a41c0576aeba895b65195c4d41fa53be","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_zero_54d.c\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-54d.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: zero Fixed value of zero\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE369_Divide_by_Zero__float_zero_54e_badSink(float data);\r\n\r\nvoid CWE369_Divide_by_Zero__float_zero_54d_badSink(float data)\r\n{\r\n CWE369_Divide_by_Zero__float_zero_54e_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE369_Divide_by_Zero__float_zero_54e_goodG2BSink(float data);\r\n\r\nvoid CWE369_Divide_by_Zero__float_zero_54d_goodG2BSink(float data)\r\n{\r\n CWE369_Divide_by_Zero__float_zero_54e_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE369_Divide_by_Zero__float_zero_54e_goodB2GSink(float data);\r\n\r\nvoid CWE369_Divide_by_Zero__float_zero_54d_goodB2GSink(float data)\r\n{\r\n CWE369_Divide_by_Zero__float_zero_54e_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_zero_81_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_zero_81_bad.cpp","cwe_id":"CWE369","file_extension":".cpp","language_hint":"C++","source_size_bytes":975,"source_sha256":"2a2f3db6f4d841d69550e1367d8e08134d85556486e5470138f263db56087250","line_count":35,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_zero_81_bad.cpp\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-81_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: zero Fixed value of zero\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE369_Divide_by_Zero__float_zero_81.h\"\r\n\r\nnamespace CWE369_Divide_by_Zero__float_zero_81\r\n{\r\n\r\nvoid CWE369_Divide_by_Zero__float_zero_81_bad::action(float data) const\r\n{\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_zero_81_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__float_zero_81_goodB2G.cpp","cwe_id":"CWE369","file_extension":".cpp","language_hint":"C++","source_size_bytes":1114,"source_sha256":"fbdc4851887258595904885769147865847322c26ffdb8d38002836b4da5a270","line_count":40,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__float_zero_81_goodB2G.cpp\r\nLabel Definition File: CWE369_Divide_by_Zero__float.label.xml\r\nTemplate File: sources-sinks-81_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: zero Fixed value of zero\r\n * GoodSource: A hardcoded non-zero number (two)\r\n * Sinks:\r\n * GoodSink: Check value of or near zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE369_Divide_by_Zero__float_zero_81.h\"\r\n\r\nnamespace CWE369_Divide_by_Zero__float_zero_81\r\n{\r\n\r\nvoid CWE369_Divide_by_Zero__float_zero_81_goodB2G::action(float data) const\r\n{\r\n /* FIX: Check for value of or near zero before dividing */\r\n if(fabs(data) > 0.000001)\r\n {\r\n int result = (int)(100.0 / data);\r\n printIntLine(result);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_connect_socket_divide_06.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_connect_socket_divide_06.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":10919,"source_sha256":"41e91793755a3a17c33fccfc0cbf5b38b50c15bac5348e21683861173f00954a","line_count":362,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_connect_socket_divide_06.c\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-06.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Non-zero\r\n * Sinks: divide\r\n * GoodSink: Check for zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 06 Control flow: if(STATIC_CONST_FIVE==5) and if(STATIC_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n/* The variable below is declared \"const\", so a tool should be able\r\n to identify that reads of this will always give its initialized\r\n value. */\r\nstatic const int STATIC_CONST_FIVE = 5;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_divide_06_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 / data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodB2G1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: test for a zero denominator */\r\n if( data != 0 )\r\n {\r\n printIntLine(100 / data);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: test for a zero denominator */\r\n if( data != 0 )\r\n {\r\n printIntLine(100 / data);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a value not equal to zero */\r\n data = 7;\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 / data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: Use a value not equal to zero */\r\n data = 7;\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 / data);\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_divide_06_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__int_connect_socket_divide_06_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__int_connect_socket_divide_06_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_connect_socket_divide_12.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_connect_socket_divide_12.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":10460,"source_sha256":"07832a408c6e99c16b8b4ecc4df3de5d2b6454c2dd7cd33d49514312484bffd9","line_count":350,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_connect_socket_divide_12.c\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-12.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Non-zero\r\n * Sinks: divide\r\n * GoodSink: Check for zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_divide_12_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n else\r\n {\r\n /* FIX: Use a value not equal to zero */\r\n data = 7;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 / data);\r\n }\r\n else\r\n {\r\n /* FIX: test for a zero denominator */\r\n if( data != 0 )\r\n {\r\n printIntLine(100 / data);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the first \"if\" so that\r\n both branches use the BadSource and the second \"if\" so that both branches\r\n use the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n else\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: test for a zero denominator */\r\n if( data != 0 )\r\n {\r\n printIntLine(100 / data);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n else\r\n {\r\n /* FIX: test for a zero denominator */\r\n if( data != 0 )\r\n {\r\n printIntLine(100 / data);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the first \"if\" so that\r\n both branches use the GoodSource and the second \"if\" so that both branches\r\n use the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Use a value not equal to zero */\r\n data = 7;\r\n }\r\n else\r\n {\r\n /* FIX: Use a value not equal to zero */\r\n data = 7;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 / data);\r\n }\r\n else\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 / data);\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_divide_12_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__int_connect_socket_divide_12_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__int_connect_socket_divide_12_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_connect_socket_divide_15.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_connect_socket_divide_15.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":11393,"source_sha256":"c317452cec97938b237284e1bf66da39d3cc8706a81f4461c23fcdf7e37e6ccd","line_count":407,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_connect_socket_divide_15.c\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-15.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Non-zero\r\n * Sinks: divide\r\n * GoodSink: Check for zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 15 Control flow: switch(6) and switch(7)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_divide_15_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n switch(6)\r\n {\r\n case 6:\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 / data);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second switch to switch(8) */\r\nstatic void goodB2G1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n switch(6)\r\n {\r\n case 6:\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(8)\r\n {\r\n case 7:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: test for a zero denominator */\r\n if( data != 0 )\r\n {\r\n printIntLine(100 / data);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n break;\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second switch */\r\nstatic void goodB2G2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n switch(6)\r\n {\r\n case 6:\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* FIX: test for a zero denominator */\r\n if( data != 0 )\r\n {\r\n printIntLine(100 / data);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first switch to switch(5) */\r\nstatic void goodG2B1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n switch(5)\r\n {\r\n case 6:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Use a value not equal to zero */\r\n data = 7;\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 / data);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first switch */\r\nstatic void goodG2B2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n switch(6)\r\n {\r\n case 6:\r\n /* FIX: Use a value not equal to zero */\r\n data = 7;\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 / data);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_divide_15_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__int_connect_socket_divide_15_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__int_connect_socket_divide_15_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_connect_socket_divide_22b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_connect_socket_divide_22b.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":2970,"source_sha256":"47b43d0ed87913b7a35ee9125b66596bdeacb5bd06150f59fd8a18737e76fec5","line_count":92,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_connect_socket_divide_22b.c\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-22b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Non-zero\r\n * Sinks: divide\r\n * GoodSink: Check for zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 22 Control flow: Flow controlled by value of a global variable. Sink functions are in a separate file from sources.\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* The global variable below is used to drive control flow in the sink function */\r\nextern int CWE369_Divide_by_Zero__int_connect_socket_divide_22_badGlobal;\r\n\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_divide_22_badSink(int data)\r\n{\r\n if(CWE369_Divide_by_Zero__int_connect_socket_divide_22_badGlobal)\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 / data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* The global variables below are used to drive control flow in the sink functions. */\r\nextern int CWE369_Divide_by_Zero__int_connect_socket_divide_22_goodB2G1Global;\r\nextern int CWE369_Divide_by_Zero__int_connect_socket_divide_22_goodB2G2Global;\r\nextern int CWE369_Divide_by_Zero__int_connect_socket_divide_22_goodG2BGlobal;\r\n\r\n/* goodB2G1() - use badsource and goodsink by setting the static variable to false instead of true */\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_divide_22_goodB2G1Sink(int data)\r\n{\r\n if(CWE369_Divide_by_Zero__int_connect_socket_divide_22_goodB2G1Global)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: test for a zero denominator */\r\n if( data != 0 )\r\n {\r\n printIntLine(100 / data);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_divide_22_goodB2G2Sink(int data)\r\n{\r\n if(CWE369_Divide_by_Zero__int_connect_socket_divide_22_goodB2G2Global)\r\n {\r\n /* FIX: test for a zero denominator */\r\n if( data != 0 )\r\n {\r\n printIntLine(100 / data);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink */\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_divide_22_goodG2BSink(int data)\r\n{\r\n if(CWE369_Divide_by_Zero__int_connect_socket_divide_22_goodG2BGlobal)\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 / data);\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_connect_socket_divide_33.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_connect_socket_divide_33.cpp","cwe_id":"CWE369","file_extension":".cpp","language_hint":"C++","source_size_bytes":6874,"source_sha256":"c0dec31b322d7132169cafdd36fc2152cae8a5c70ea2d7de5b40e1e32ee32ab4","line_count":248,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_connect_socket_divide_33.cpp\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-33.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Non-zero\r\n * Sinks: divide\r\n * GoodSink: Check for zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 33 Data flow: use of a C++ reference to data within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\nnamespace CWE369_Divide_by_Zero__int_connect_socket_divide_33\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int data;\r\n int &dataRef = data;\r\n /* Initialize data */\r\n data = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n {\r\n int data = dataRef;\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 / data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n int &dataRef = data;\r\n /* Initialize data */\r\n data = -1;\r\n /* FIX: Use a value not equal to zero */\r\n data = 7;\r\n {\r\n int data = dataRef;\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 / data);\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n int &dataRef = data;\r\n /* Initialize data */\r\n data = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n {\r\n int data = dataRef;\r\n /* FIX: test for a zero denominator */\r\n if( data != 0 )\r\n {\r\n printIntLine(100 / data);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE369_Divide_by_Zero__int_connect_socket_divide_33; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_connect_socket_divide_68a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_connect_socket_divide_68a.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":7214,"source_sha256":"73156d6a976dc10150b902b7775dfad9330481aa1efcdf724277b4da72b6c816","line_count":234,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_connect_socket_divide_68a.c\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-68a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Non-zero\r\n * Sinks: divide\r\n * GoodSink: Check for zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 68 Data flow: data passed as a global variable from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\nint CWE369_Divide_by_Zero__int_connect_socket_divide_68_badData;\r\nint CWE369_Divide_by_Zero__int_connect_socket_divide_68_goodG2BData;\r\nint CWE369_Divide_by_Zero__int_connect_socket_divide_68_goodB2GData;\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_divide_68b_badSink();\r\n\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_divide_68_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE369_Divide_by_Zero__int_connect_socket_divide_68_badData = data;\r\n CWE369_Divide_by_Zero__int_connect_socket_divide_68b_badSink();\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_divide_68b_goodG2BSink();\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_divide_68b_goodB2GSink();\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n /* FIX: Use a value not equal to zero */\r\n data = 7;\r\n CWE369_Divide_by_Zero__int_connect_socket_divide_68_goodG2BData = data;\r\n CWE369_Divide_by_Zero__int_connect_socket_divide_68b_goodG2BSink();\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE369_Divide_by_Zero__int_connect_socket_divide_68_goodB2GData = data;\r\n CWE369_Divide_by_Zero__int_connect_socket_divide_68b_goodB2GSink();\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_divide_68_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__int_connect_socket_divide_68_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__int_connect_socket_divide_68_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_connect_socket_divide_84a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_connect_socket_divide_84a.cpp","cwe_id":"CWE369","file_extension":".cpp","language_hint":"C++","source_size_bytes":2662,"source_sha256":"a6754673eefac59ac9068af9aa0bc4bf58d415474e6d573fc322ab1a48e95794","line_count":95,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_connect_socket_divide_84a.cpp\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-84a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Non-zero\r\n * Sinks: divide\r\n * GoodSink: Check for zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE369_Divide_by_Zero__int_connect_socket_divide_84.h\"\r\n\r\nnamespace CWE369_Divide_by_Zero__int_connect_socket_divide_84\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n CWE369_Divide_by_Zero__int_connect_socket_divide_84_bad * badObject = new CWE369_Divide_by_Zero__int_connect_socket_divide_84_bad(data);\r\n delete badObject;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n CWE369_Divide_by_Zero__int_connect_socket_divide_84_goodG2B * goodG2BObject = new CWE369_Divide_by_Zero__int_connect_socket_divide_84_goodG2B(data);\r\n delete goodG2BObject;\r\n}\r\n\r\n/* goodG2B uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n CWE369_Divide_by_Zero__int_connect_socket_divide_84_goodB2G * goodB2GObject = new CWE369_Divide_by_Zero__int_connect_socket_divide_84_goodB2G(data);\r\n delete goodB2GObject;\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE369_Divide_by_Zero__int_connect_socket_divide_84; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_connect_socket_modulo_22a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_connect_socket_modulo_22a.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":9822,"source_sha256":"de8c720f636c170166063bb7e91b080895c8f9a98899e165bf2eff8341f0b739","line_count":306,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_connect_socket_modulo_22a.c\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-22a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Non-zero\r\n * Sinks: modulo\r\n * GoodSink: Check for zero before modulo\r\n * BadSink : Modulo a constant with data\r\n * Flow Variant: 22 Control flow: Flow controlled by value of a global variable. Sink functions are in a separate file from sources.\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\n/* The global variable below is used to drive control flow in the sink function */\r\nint CWE369_Divide_by_Zero__int_connect_socket_modulo_22_badGlobal = 0;\r\n\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_modulo_22_badSink(int data);\r\n\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_modulo_22_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE369_Divide_by_Zero__int_connect_socket_modulo_22_badGlobal = 1; /* true */\r\n CWE369_Divide_by_Zero__int_connect_socket_modulo_22_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* The global variables below are used to drive control flow in the sink functions. */\r\nint CWE369_Divide_by_Zero__int_connect_socket_modulo_22_goodB2G1Global = 0;\r\nint CWE369_Divide_by_Zero__int_connect_socket_modulo_22_goodB2G2Global = 0;\r\nint CWE369_Divide_by_Zero__int_connect_socket_modulo_22_goodG2BGlobal = 0;\r\n\r\n/* goodB2G1() - use badsource and goodsink by setting the static variable to false instead of true */\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_modulo_22_goodB2G1Sink(int data);\r\n\r\nstatic void goodB2G1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE369_Divide_by_Zero__int_connect_socket_modulo_22_goodB2G1Global = 0; /* false */\r\n CWE369_Divide_by_Zero__int_connect_socket_modulo_22_goodB2G1Sink(data);\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_modulo_22_goodB2G2Sink(int data);\r\n\r\nstatic void goodB2G2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE369_Divide_by_Zero__int_connect_socket_modulo_22_goodB2G2Global = 1; /* true */\r\n CWE369_Divide_by_Zero__int_connect_socket_modulo_22_goodB2G2Sink(data);\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink */\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_modulo_22_goodG2BSink(int data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n /* FIX: Use a value not equal to zero */\r\n data = 7;\r\n CWE369_Divide_by_Zero__int_connect_socket_modulo_22_goodG2BGlobal = 1; /* true */\r\n CWE369_Divide_by_Zero__int_connect_socket_modulo_22_goodG2BSink(data);\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_modulo_22_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__int_connect_socket_modulo_22_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__int_connect_socket_modulo_22_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_connect_socket_modulo_63a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_connect_socket_modulo_63a.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":6785,"source_sha256":"3ca8c622ccf5b5baa19ee5e903071f0a44f51cde0effa93181fb44fdebcbf1fb","line_count":227,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_connect_socket_modulo_63a.c\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-63a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Non-zero\r\n * Sinks: modulo\r\n * GoodSink: Check for zero before modulo\r\n * BadSink : Modulo a constant with data\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_modulo_63b_badSink(int * dataPtr);\r\n\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_modulo_63_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE369_Divide_by_Zero__int_connect_socket_modulo_63b_badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_modulo_63b_goodG2BSink(int * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n /* FIX: Use a value not equal to zero */\r\n data = 7;\r\n CWE369_Divide_by_Zero__int_connect_socket_modulo_63b_goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_modulo_63b_goodB2GSink(int * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE369_Divide_by_Zero__int_connect_socket_modulo_63b_goodB2GSink(&data);\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__int_connect_socket_modulo_63_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__int_connect_socket_modulo_63_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__int_connect_socket_modulo_63_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_connect_socket_modulo_83_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_connect_socket_modulo_83_goodB2G.cpp","cwe_id":"CWE369","file_extension":".cpp","language_hint":"C++","source_size_bytes":3687,"source_sha256":"6a20dc4dbd9c2ca0f10de85f41215c7c6f922f3239b6a7970864913892307a53","line_count":120,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_connect_socket_modulo_83_goodB2G.cpp\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-83_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Non-zero\r\n * Sinks: modulo\r\n * GoodSink: Check for zero before modulo\r\n * BadSink : Modulo a constant with data\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE369_Divide_by_Zero__int_connect_socket_modulo_83.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\nnamespace CWE369_Divide_by_Zero__int_connect_socket_modulo_83\r\n{\r\nCWE369_Divide_by_Zero__int_connect_socket_modulo_83_goodB2G::CWE369_Divide_by_Zero__int_connect_socket_modulo_83_goodB2G(int dataCopy)\r\n{\r\n data = dataCopy;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read data using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n}\r\n\r\nCWE369_Divide_by_Zero__int_connect_socket_modulo_83_goodB2G::~CWE369_Divide_by_Zero__int_connect_socket_modulo_83_goodB2G()\r\n{\r\n /* FIX: test for a zero denominator */\r\n if( data != 0 )\r\n {\r\n printIntLine(100 % data);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_divide_41.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_divide_41.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":3378,"source_sha256":"3f1d68dd9009981309367933cf375211ae1ba6c20b715d4df171d0e31315f155","line_count":139,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_fgets_divide_41.c\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-41.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Non-zero\r\n * Sinks: divide\r\n * GoodSink: Check for zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 41 Data flow: data passed as an argument from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink(int data)\r\n{\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 / data);\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__int_fgets_divide_41_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read data from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink(int data)\r\n{\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 / data);\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n /* FIX: Use a value not equal to zero */\r\n data = 7;\r\n goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink(int data)\r\n{\r\n /* FIX: test for a zero denominator */\r\n if( data != 0 )\r\n {\r\n printIntLine(100 / data);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read data from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n goodB2GSink(data);\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__int_fgets_divide_41_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__int_fgets_divide_41_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__int_fgets_divide_41_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_divide_42.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_divide_42.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":3449,"source_sha256":"e1f6eee3b7eca89fa1579e96c942fe55f86db325bc29fad7210ae9d43e4fcd55","line_count":142,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_fgets_divide_42.c\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-42.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Non-zero\r\n * Sinks: divide\r\n * GoodSink: Check for zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 42 Data flow: data returned from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic int badSource(int data)\r\n{\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read data from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n return data;\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__int_fgets_divide_42_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n data = badSource(data);\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 / data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic int goodG2BSource(int data)\r\n{\r\n /* FIX: Use a value not equal to zero */\r\n data = 7;\r\n return data;\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n data = goodG2BSource(data);\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 / data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic int goodB2GSource(int data)\r\n{\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read data from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n return data;\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n data = goodB2GSource(data);\r\n /* FIX: test for a zero denominator */\r\n if( data != 0 )\r\n {\r\n printIntLine(100 / data);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__int_fgets_divide_42_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__int_fgets_divide_42_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__int_fgets_divide_42_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_divide_52c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_divide_52c.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":1457,"source_sha256":"4b07e90b6be7f0fbf94a6a345658768577f6832c31f6d9ceadf81623a4c4469b","line_count":55,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_fgets_divide_52c.c\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-52c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Non-zero\r\n * Sinks: divide\r\n * GoodSink: Check for zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__int_fgets_divide_52c_badSink(int data)\r\n{\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 / data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE369_Divide_by_Zero__int_fgets_divide_52c_goodG2BSink(int data)\r\n{\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 / data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE369_Divide_by_Zero__int_fgets_divide_52c_goodB2GSink(int data)\r\n{\r\n /* FIX: test for a zero denominator */\r\n if( data != 0 )\r\n {\r\n printIntLine(100 / data);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_divide_53b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_divide_53b.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":1574,"source_sha256":"9d5660cb51bda013b358b461f87ded7a80b6a815f8c6e6182ccd0df1c30815b2","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_fgets_divide_53b.c\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-53b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Non-zero\r\n * Sinks: divide\r\n * GoodSink: Check for zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE369_Divide_by_Zero__int_fgets_divide_53c_badSink(int data);\r\n\r\nvoid CWE369_Divide_by_Zero__int_fgets_divide_53b_badSink(int data)\r\n{\r\n CWE369_Divide_by_Zero__int_fgets_divide_53c_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE369_Divide_by_Zero__int_fgets_divide_53c_goodG2BSink(int data);\r\n\r\nvoid CWE369_Divide_by_Zero__int_fgets_divide_53b_goodG2BSink(int data)\r\n{\r\n CWE369_Divide_by_Zero__int_fgets_divide_53c_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE369_Divide_by_Zero__int_fgets_divide_53c_goodB2GSink(int data);\r\n\r\nvoid CWE369_Divide_by_Zero__int_fgets_divide_53b_goodB2GSink(int data)\r\n{\r\n CWE369_Divide_by_Zero__int_fgets_divide_53c_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_divide_54d.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_divide_54d.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":1575,"source_sha256":"09778bcdaa8fb081621d0527b95838bd69c47963e3ac845cc0eeae0e0811de85","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_fgets_divide_54d.c\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-54d.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Non-zero\r\n * Sinks: divide\r\n * GoodSink: Check for zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE369_Divide_by_Zero__int_fgets_divide_54e_badSink(int data);\r\n\r\nvoid CWE369_Divide_by_Zero__int_fgets_divide_54d_badSink(int data)\r\n{\r\n CWE369_Divide_by_Zero__int_fgets_divide_54e_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE369_Divide_by_Zero__int_fgets_divide_54e_goodG2BSink(int data);\r\n\r\nvoid CWE369_Divide_by_Zero__int_fgets_divide_54d_goodG2BSink(int data)\r\n{\r\n CWE369_Divide_by_Zero__int_fgets_divide_54e_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE369_Divide_by_Zero__int_fgets_divide_54e_goodB2GSink(int data);\r\n\r\nvoid CWE369_Divide_by_Zero__int_fgets_divide_54d_goodB2GSink(int data)\r\n{\r\n CWE369_Divide_by_Zero__int_fgets_divide_54e_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_divide_83a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_divide_83a.cpp","cwe_id":"CWE369","file_extension":".cpp","language_hint":"C++","source_size_bytes":2282,"source_sha256":"655543e0bbfc856233b5029f643ed3dff94b8481da6f50f5bdd5a762f2c807d9","line_count":92,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_fgets_divide_83a.cpp\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-83a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Non-zero\r\n * Sinks: divide\r\n * GoodSink: Check for zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE369_Divide_by_Zero__int_fgets_divide_83.h\"\r\n\r\nnamespace CWE369_Divide_by_Zero__int_fgets_divide_83\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n CWE369_Divide_by_Zero__int_fgets_divide_83_bad badObject(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n CWE369_Divide_by_Zero__int_fgets_divide_83_goodG2B goodG2BObject(data);\r\n}\r\n\r\n/* goodG2B uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n CWE369_Divide_by_Zero__int_fgets_divide_83_goodB2G goodB2GObject(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE369_Divide_by_Zero__int_fgets_divide_83; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_divide_84a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_divide_84a.cpp","cwe_id":"CWE369","file_extension":".cpp","language_hint":"C++","source_size_bytes":2557,"source_sha256":"cf02a559ede581218f9ba41a4a2470c3444d0729b811e09ab39ce0b8e69e1c6b","line_count":95,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_fgets_divide_84a.cpp\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-84a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Non-zero\r\n * Sinks: divide\r\n * GoodSink: Check for zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE369_Divide_by_Zero__int_fgets_divide_84.h\"\r\n\r\nnamespace CWE369_Divide_by_Zero__int_fgets_divide_84\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n CWE369_Divide_by_Zero__int_fgets_divide_84_bad * badObject = new CWE369_Divide_by_Zero__int_fgets_divide_84_bad(data);\r\n delete badObject;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n CWE369_Divide_by_Zero__int_fgets_divide_84_goodG2B * goodG2BObject = new CWE369_Divide_by_Zero__int_fgets_divide_84_goodG2B(data);\r\n delete goodG2BObject;\r\n}\r\n\r\n/* goodG2B uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n CWE369_Divide_by_Zero__int_fgets_divide_84_goodB2G * goodB2GObject = new CWE369_Divide_by_Zero__int_fgets_divide_84_goodB2G(data);\r\n delete goodB2GObject;\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE369_Divide_by_Zero__int_fgets_divide_84; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_modulo_03.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_modulo_03.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":5136,"source_sha256":"90a3d7a029c4912a4b2c4eb493cd2f002f485e9fb41cede7efd7fa4ac647188f","line_count":208,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_fgets_modulo_03.c\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-03.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Non-zero\r\n * Sinks: modulo\r\n * GoodSink: Check for zero before modulo\r\n * BadSink : Modulo a constant with data\r\n * Flow Variant: 03 Control flow: if(5==5) and if(5!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__int_fgets_modulo_03_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n if(5==5)\r\n {\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read data from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 % data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 5==5 to 5!=5 */\r\nstatic void goodB2G1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n if(5==5)\r\n {\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read data from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n }\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: test for a zero denominator */\r\n if( data != 0 )\r\n {\r\n printIntLine(100 % data);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n if(5==5)\r\n {\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read data from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n }\r\n if(5==5)\r\n {\r\n /* FIX: test for a zero denominator */\r\n if( data != 0 )\r\n {\r\n printIntLine(100 % data);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 5==5 to 5!=5 */\r\nstatic void goodG2B1()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a value not equal to zero */\r\n data = 7;\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 % data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n if(5==5)\r\n {\r\n /* FIX: Use a value not equal to zero */\r\n data = 7;\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 % data);\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__int_fgets_modulo_03_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__int_fgets_modulo_03_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__int_fgets_modulo_03_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_modulo_16.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_modulo_16.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":3638,"source_sha256":"9adc6a6f7f2b99e0014e98a0ecc6351e1c7a45ef7be2994f534ec11cc5a813d1","line_count":148,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_fgets_modulo_16.c\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-16.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Non-zero\r\n * Sinks: modulo\r\n * GoodSink: Check for zero before modulo\r\n * BadSink : Modulo a constant with data\r\n * Flow Variant: 16 Control flow: while(1)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__int_fgets_modulo_16_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n while(1)\r\n {\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read data from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 % data);\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the sinks in the second while statement */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n while(1)\r\n {\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read data from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* FIX: test for a zero denominator */\r\n if( data != 0 )\r\n {\r\n printIntLine(100 % data);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the sources in the first while statement */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n while(1)\r\n {\r\n /* FIX: Use a value not equal to zero */\r\n data = 7;\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 % data);\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__int_fgets_modulo_16_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__int_fgets_modulo_16_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__int_fgets_modulo_16_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_modulo_17.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_modulo_17.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":3608,"source_sha256":"aef7e9965927ab94ddd518f0646421f5629690cae8ca165c88f6b39a941100cf","line_count":145,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_fgets_modulo_17.c\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-17.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Non-zero\r\n * Sinks: modulo\r\n * GoodSink: Check for zero before modulo\r\n * BadSink : Modulo a constant with data\r\n * Flow Variant: 17 Control flow: for loops\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__int_fgets_modulo_17_bad()\r\n{\r\n int i,j;\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n for(i = 0; i < 1; i++)\r\n {\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read data from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n }\r\n for(j = 0; j < 1; j++)\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 % data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink in the for statements */\r\nstatic void goodB2G()\r\n{\r\n int i,k;\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n for(i = 0; i < 1; i++)\r\n {\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read data from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n }\r\n for(k = 0; k < 1; k++)\r\n {\r\n /* FIX: test for a zero denominator */\r\n if( data != 0 )\r\n {\r\n printIntLine(100 % data);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink in the for statements */\r\nstatic void goodG2B()\r\n{\r\n int h,j;\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n for(h = 0; h < 1; h++)\r\n {\r\n /* FIX: Use a value not equal to zero */\r\n data = 7;\r\n }\r\n for(j = 0; j < 1; j++)\r\n {\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 % data);\r\n }\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__int_fgets_modulo_17_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__int_fgets_modulo_17_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__int_fgets_modulo_17_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_modulo_45.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_modulo_45.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":3971,"source_sha256":"551bbbf914b5ce11dec5b1cbc95b2353da7939d57de8079da00835b248d7aadf","line_count":148,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_fgets_modulo_45.c\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-45.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Non-zero\r\n * Sinks: modulo\r\n * GoodSink: Check for zero before modulo\r\n * BadSink : Modulo a constant with data\r\n * Flow Variant: 45 Data flow: data passed as a static global variable from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\nstatic int CWE369_Divide_by_Zero__int_fgets_modulo_45_badData;\r\nstatic int CWE369_Divide_by_Zero__int_fgets_modulo_45_goodG2BData;\r\nstatic int CWE369_Divide_by_Zero__int_fgets_modulo_45_goodB2GData;\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink()\r\n{\r\n int data = CWE369_Divide_by_Zero__int_fgets_modulo_45_badData;\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 % data);\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__int_fgets_modulo_45_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read data from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n CWE369_Divide_by_Zero__int_fgets_modulo_45_badData = data;\r\n badSink();\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink()\r\n{\r\n int data = CWE369_Divide_by_Zero__int_fgets_modulo_45_goodG2BData;\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 % data);\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n /* FIX: Use a value not equal to zero */\r\n data = 7;\r\n CWE369_Divide_by_Zero__int_fgets_modulo_45_goodG2BData = data;\r\n goodG2BSink();\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink()\r\n{\r\n int data = CWE369_Divide_by_Zero__int_fgets_modulo_45_goodB2GData;\r\n /* FIX: test for a zero denominator */\r\n if( data != 0 )\r\n {\r\n printIntLine(100 % data);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read data from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n CWE369_Divide_by_Zero__int_fgets_modulo_45_goodB2GData = data;\r\n goodB2GSink();\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__int_fgets_modulo_45_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__int_fgets_modulo_45_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__int_fgets_modulo_45_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_modulo_51a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_modulo_51a.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":3279,"source_sha256":"0d587485ff7681722a61ab2af6598440d65172371f53803f090febfa89bd3f71","line_count":121,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_fgets_modulo_51a.c\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-51a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Non-zero\r\n * Sinks: modulo\r\n * GoodSink: Check for zero before modulo\r\n * BadSink : Modulo a constant with data\r\n * Flow Variant: 51 Data flow: data passed as an argument from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE369_Divide_by_Zero__int_fgets_modulo_51b_badSink(int data);\r\n\r\nvoid CWE369_Divide_by_Zero__int_fgets_modulo_51_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read data from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n CWE369_Divide_by_Zero__int_fgets_modulo_51b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE369_Divide_by_Zero__int_fgets_modulo_51b_goodG2BSink(int data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n /* FIX: Use a value not equal to zero */\r\n data = 7;\r\n CWE369_Divide_by_Zero__int_fgets_modulo_51b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE369_Divide_by_Zero__int_fgets_modulo_51b_goodB2GSink(int data);\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read data from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n CWE369_Divide_by_Zero__int_fgets_modulo_51b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__int_fgets_modulo_51_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__int_fgets_modulo_51_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__int_fgets_modulo_51_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_modulo_68a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_modulo_68a.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":3662,"source_sha256":"1f1c7f565ec515dac6e527c9943ec2c76072f3e95b924226ac1344d5fdfa493f","line_count":128,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_fgets_modulo_68a.c\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-68a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Non-zero\r\n * Sinks: modulo\r\n * GoodSink: Check for zero before modulo\r\n * BadSink : Modulo a constant with data\r\n * Flow Variant: 68 Data flow: data passed as a global variable from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(data) + 2)\r\n\r\nint CWE369_Divide_by_Zero__int_fgets_modulo_68_badData;\r\nint CWE369_Divide_by_Zero__int_fgets_modulo_68_goodG2BData;\r\nint CWE369_Divide_by_Zero__int_fgets_modulo_68_goodB2GData;\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE369_Divide_by_Zero__int_fgets_modulo_68b_badSink();\r\n\r\nvoid CWE369_Divide_by_Zero__int_fgets_modulo_68_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read data from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n CWE369_Divide_by_Zero__int_fgets_modulo_68_badData = data;\r\n CWE369_Divide_by_Zero__int_fgets_modulo_68b_badSink();\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\nvoid CWE369_Divide_by_Zero__int_fgets_modulo_68b_goodG2BSink();\r\nvoid CWE369_Divide_by_Zero__int_fgets_modulo_68b_goodB2GSink();\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n /* FIX: Use a value not equal to zero */\r\n data = 7;\r\n CWE369_Divide_by_Zero__int_fgets_modulo_68_goodG2BData = data;\r\n CWE369_Divide_by_Zero__int_fgets_modulo_68b_goodG2BSink();\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read data from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n data = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n CWE369_Divide_by_Zero__int_fgets_modulo_68_goodB2GData = data;\r\n CWE369_Divide_by_Zero__int_fgets_modulo_68b_goodB2GSink();\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__int_fgets_modulo_68_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__int_fgets_modulo_68_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__int_fgets_modulo_68_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_modulo_81_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fgets_modulo_81_bad.cpp","cwe_id":"CWE369","file_extension":".cpp","language_hint":"C++","source_size_bytes":925,"source_sha256":"a56ec25e71448f38d709060298b09e37da37ed2689ed2dc365b9d9f9467038d1","line_count":32,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_fgets_modulo_81_bad.cpp\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-81_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Non-zero\r\n * Sinks: modulo\r\n * GoodSink: Check for zero before modulo\r\n * BadSink : Modulo a constant with data\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE369_Divide_by_Zero__int_fgets_modulo_81.h\"\r\n\r\nnamespace CWE369_Divide_by_Zero__int_fgets_modulo_81\r\n{\r\n\r\nvoid CWE369_Divide_by_Zero__int_fgets_modulo_81_bad::action(int data) const\r\n{\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 % data);\r\n}\r\n\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fscanf_divide_52a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fscanf_divide_52a.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":2715,"source_sha256":"8e49e7186803fdd004856c8363b29a71550e4ec3e39c58ca47ba60a24371e2d9","line_count":97,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_fscanf_divide_52a.c\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-52a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Non-zero\r\n * Sinks: divide\r\n * GoodSink: Check for zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE369_Divide_by_Zero__int_fscanf_divide_52b_badSink(int data);\r\n\r\nvoid CWE369_Divide_by_Zero__int_fscanf_divide_52_bad()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n CWE369_Divide_by_Zero__int_fscanf_divide_52b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE369_Divide_by_Zero__int_fscanf_divide_52b_goodG2BSink(int data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n /* FIX: Use a value not equal to zero */\r\n data = 7;\r\n CWE369_Divide_by_Zero__int_fscanf_divide_52b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE369_Divide_by_Zero__int_fscanf_divide_52b_goodB2GSink(int data);\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* Initialize data */\r\n data = -1;\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n CWE369_Divide_by_Zero__int_fscanf_divide_52b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE369_Divide_by_Zero__int_fscanf_divide_52_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE369_Divide_by_Zero__int_fscanf_divide_52_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE369_Divide_by_Zero__int_fscanf_divide_52_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fscanf_divide_65b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fscanf_divide_65b.c","cwe_id":"CWE369","file_extension":".c","language_hint":"C","source_size_bytes":1438,"source_sha256":"daa662d498c9e197e0525955c786de439ffdfb77a9f82ab1440f795d2d760b62","line_count":53,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_fscanf_divide_65b.c\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-65b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Non-zero\r\n * Sinks: divide\r\n * GoodSink: Check for zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE369_Divide_by_Zero__int_fscanf_divide_65b_badSink(int data)\r\n{\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 / data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE369_Divide_by_Zero__int_fscanf_divide_65b_goodG2BSink(int data)\r\n{\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 / data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE369_Divide_by_Zero__int_fscanf_divide_65b_goodB2GSink(int data)\r\n{\r\n /* FIX: test for a zero denominator */\r\n if( data != 0 )\r\n {\r\n printIntLine(100 / data);\r\n }\r\n else\r\n {\r\n printLine(\"This would result in a divide by zero\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fscanf_divide_83_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE369_Divide_by_Zero/s01/CWE369_Divide_by_Zero__int_fscanf_divide_83_bad.cpp","cwe_id":"CWE369","file_extension":".cpp","language_hint":"C++","source_size_bytes":1214,"source_sha256":"c9df2dc8a0f427c4d93cbc85e84f84fd2d1bebad04697c5aa440d8f8f96ec81c","line_count":37,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE369_Divide_by_Zero__int_fscanf_divide_83_bad.cpp\r\nLabel Definition File: CWE369_Divide_by_Zero__int.label.xml\r\nTemplate File: sources-sinks-83_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 369 Divide by Zero\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Non-zero\r\n * Sinks: divide\r\n * GoodSink: Check for zero before dividing\r\n * BadSink : Divide a constant by data\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE369_Divide_by_Zero__int_fscanf_divide_83.h\"\r\n\r\nnamespace CWE369_Divide_by_Zero__int_fscanf_divide_83\r\n{\r\nCWE369_Divide_by_Zero__int_fscanf_divide_83_bad::CWE369_Divide_by_Zero__int_fscanf_divide_83_bad(int dataCopy)\r\n{\r\n data = dataCopy;\r\n /* POTENTIAL FLAW: Read data from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &data);\r\n}\r\n\r\nCWE369_Divide_by_Zero__int_fscanf_divide_83_bad::~CWE369_Divide_by_Zero__int_fscanf_divide_83_bad()\r\n{\r\n /* POTENTIAL FLAW: Possibly divide by zero */\r\n printIntLine(100 / data);\r\n}\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_for_loop_11.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_for_loop_11.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":11553,"source_sha256":"6c2bdce7dd359720265f9097f0cb71886cd49a5702ea30e95b4dae8eba9e9f5c","line_count":379,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_for_loop_11.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-11.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_for_loop_11_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n {\r\n size_t i = 0;\r\n /* FIX: Validate count before using it as the for loop variant */\r\n if (count > 0 && count <= 20)\r\n {\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n size_t i = 0;\r\n /* FIX: Validate count before using it as the for loop variant */\r\n if (count > 0 && count <= 20)\r\n {\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_for_loop_11_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_11_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_11_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_for_loop_22a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_for_loop_22a.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":10009,"source_sha256":"bc32a40a35a9680ed64386a057bc86c2b7d4b25fb4c067c0e3705127717738b8","line_count":306,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_for_loop_22a.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-22a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 22 Control flow: Flow controlled by value of a global variable. Sink functions are in a separate file from sources.\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\n/* The global variable below is used to drive control flow in the sink function */\r\nint CWE400_Resource_Exhaustion__connect_socket_for_loop_22_badGlobal = 0;\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_for_loop_22_badSink(int count);\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_for_loop_22_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_22_badGlobal = 1; /* true */\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_22_badSink(count);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* The global variables below are used to drive control flow in the sink functions. */\r\nint CWE400_Resource_Exhaustion__connect_socket_for_loop_22_goodB2G1Global = 0;\r\nint CWE400_Resource_Exhaustion__connect_socket_for_loop_22_goodB2G2Global = 0;\r\nint CWE400_Resource_Exhaustion__connect_socket_for_loop_22_goodG2BGlobal = 0;\r\n\r\n/* goodB2G1() - use badsource and goodsink by setting the static variable to false instead of true */\r\nvoid CWE400_Resource_Exhaustion__connect_socket_for_loop_22_goodB2G1Sink(int count);\r\n\r\nstatic void goodB2G1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_22_goodB2G1Global = 0; /* false */\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_22_goodB2G1Sink(count);\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */\r\nvoid CWE400_Resource_Exhaustion__connect_socket_for_loop_22_goodB2G2Sink(int count);\r\n\r\nstatic void goodB2G2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_22_goodB2G2Global = 1; /* true */\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_22_goodB2G2Sink(count);\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink */\r\nvoid CWE400_Resource_Exhaustion__connect_socket_for_loop_22_goodG2BSink(int count);\r\n\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_22_goodG2BGlobal = 1; /* true */\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_22_goodG2BSink(count);\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_for_loop_22_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_22_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_22_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_for_loop_54a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_for_loop_54a.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":6972,"source_sha256":"670b896f63846ed93a2853ace4cd35ec393be1e49ea585faf64bec5f3b5a12ad","line_count":227,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_for_loop_54a.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-54a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE400_Resource_Exhaustion__connect_socket_for_loop_54b_badSink(int count);\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_for_loop_54_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_54b_badSink(count);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE400_Resource_Exhaustion__connect_socket_for_loop_54b_goodG2BSink(int count);\r\n\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_54b_goodG2BSink(count);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE400_Resource_Exhaustion__connect_socket_for_loop_54b_goodB2GSink(int count);\r\n\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_54b_goodB2GSink(count);\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_for_loop_54_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_54_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_54_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_for_loop_54b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_for_loop_54b.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":2309,"source_sha256":"856b2057db29797c76639a7f4a119264ac950e938256614965eaeb78fe2c774b","line_count":72,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_for_loop_54b.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-54b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE400_Resource_Exhaustion__connect_socket_for_loop_54c_badSink(int count);\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_for_loop_54b_badSink(int count)\r\n{\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_54c_badSink(count);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE400_Resource_Exhaustion__connect_socket_for_loop_54c_goodG2BSink(int count);\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_for_loop_54b_goodG2BSink(int count)\r\n{\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_54c_goodG2BSink(count);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE400_Resource_Exhaustion__connect_socket_for_loop_54c_goodB2GSink(int count);\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_for_loop_54b_goodB2GSink(int count)\r\n{\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_54c_goodB2GSink(count);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_for_loop_54e.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_for_loop_54e.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":2521,"source_sha256":"2f9fd7a8d31356753c6782c91cecd368ef9c5eded77e519dc048157c1a40a2ac","line_count":89,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_for_loop_54e.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-54e.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_for_loop_54e_badSink(int count)\r\n{\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE400_Resource_Exhaustion__connect_socket_for_loop_54e_goodG2BSink(int count)\r\n{\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE400_Resource_Exhaustion__connect_socket_for_loop_54e_goodB2GSink(int count)\r\n{\r\n {\r\n size_t i = 0;\r\n /* FIX: Validate count before using it as the for loop variant */\r\n if (count > 0 && count <= 20)\r\n {\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_for_loop_62b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_for_loop_62b.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":5391,"source_sha256":"ba1b032bd7efb19c3894dd06f1d244814426ff6b4c3807e0f8e97c5107d03349","line_count":181,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_for_loop_62b.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-62b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\nnamespace CWE400_Resource_Exhaustion__connect_socket_for_loop_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSource(int &count)\r\n{\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(int &count)\r\n{\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(int &count)\r\n{\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_for_loop_82_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_for_loop_82_goodB2G.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":1297,"source_sha256":"2193391e4da306aa3fbfcc78e97ae4ee3ddc03c56908ba86aec874dc334284c4","line_count":41,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_for_loop_82_goodB2G.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-82_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE400_Resource_Exhaustion__connect_socket_for_loop_82.h\"\r\n\r\nnamespace CWE400_Resource_Exhaustion__connect_socket_for_loop_82\r\n{\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_for_loop_82_goodB2G::action(int count)\r\n{\r\n {\r\n size_t i = 0;\r\n /* FIX: Validate count before using it as the for loop variant */\r\n if (count > 0 && count <= 20)\r\n {\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_for_loop_82_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_for_loop_82_goodG2B.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":1235,"source_sha256":"b723664976c03c3471121e938e1ab386b11e205536b9c209cf5aa50ad86f814a","line_count":38,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_for_loop_82_goodG2B.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-82_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE400_Resource_Exhaustion__connect_socket_for_loop_82.h\"\r\n\r\nnamespace CWE400_Resource_Exhaustion__connect_socket_for_loop_82\r\n{\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_for_loop_82_goodG2B::action(int count)\r\n{\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_for_loop_82a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_for_loop_82a.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":7059,"source_sha256":"09c021f0c192cc5c52ff4d7432fba986fe4d7d8dc7cfd32b0f4a6f06b60fd47b","line_count":234,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_for_loop_82a.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-82a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE400_Resource_Exhaustion__connect_socket_for_loop_82.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\nnamespace CWE400_Resource_Exhaustion__connect_socket_for_loop_82\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_82_base* baseObject = new CWE400_Resource_Exhaustion__connect_socket_for_loop_82_bad;\r\n baseObject->action(count);\r\n delete baseObject;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_82_base* baseObject = new CWE400_Resource_Exhaustion__connect_socket_for_loop_82_goodG2B;\r\n baseObject->action(count);\r\n delete baseObject;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_82_base* baseObject = new CWE400_Resource_Exhaustion__connect_socket_for_loop_82_goodB2G;\r\n baseObject->action(count);\r\n delete baseObject;\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE400_Resource_Exhaustion__connect_socket_for_loop_82; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_for_loop_83.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_for_loop_83.h","cwe_id":"CWE400","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1696,"source_sha256":"843b3bba595632312c41869e1c7e9960f555eb6a0bf7bea332fa66961cccc2f9","line_count":61,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_for_loop_83.h\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-83.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE400_Resource_Exhaustion__connect_socket_for_loop_83\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE400_Resource_Exhaustion__connect_socket_for_loop_83_bad\r\n{\r\npublic:\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_83_bad(int countCopy);\r\n ~CWE400_Resource_Exhaustion__connect_socket_for_loop_83_bad();\r\n\r\nprivate:\r\n int count;\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE400_Resource_Exhaustion__connect_socket_for_loop_83_goodG2B\r\n{\r\npublic:\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_83_goodG2B(int countCopy);\r\n ~CWE400_Resource_Exhaustion__connect_socket_for_loop_83_goodG2B();\r\n\r\nprivate:\r\n int count;\r\n};\r\n\r\nclass CWE400_Resource_Exhaustion__connect_socket_for_loop_83_goodB2G\r\n{\r\npublic:\r\n CWE400_Resource_Exhaustion__connect_socket_for_loop_83_goodB2G(int countCopy);\r\n ~CWE400_Resource_Exhaustion__connect_socket_for_loop_83_goodB2G();\r\n\r\nprivate:\r\n int count;\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_for_loop_83_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_for_loop_83_bad.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":3811,"source_sha256":"9b1fd6e55990814daf20575cdf1260e96911b917b45010da3cd301105f68a8ff","line_count":119,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_for_loop_83_bad.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-83_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE400_Resource_Exhaustion__connect_socket_for_loop_83.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\nnamespace CWE400_Resource_Exhaustion__connect_socket_for_loop_83\r\n{\r\nCWE400_Resource_Exhaustion__connect_socket_for_loop_83_bad::CWE400_Resource_Exhaustion__connect_socket_for_loop_83_bad(int countCopy)\r\n{\r\n count = countCopy;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n}\r\n\r\nCWE400_Resource_Exhaustion__connect_socket_for_loop_83_bad::~CWE400_Resource_Exhaustion__connect_socket_for_loop_83_bad()\r\n{\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n}\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_fwrite_03.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_fwrite_03.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":13703,"source_sha256":"fc21685c815a058aac1db3dc009a64d218bfd81aa4fd21bcc7f32425f87cd902","line_count":448,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_fwrite_03.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-03.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 03 Control flow: if(5==5) and if(5!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#define SENTENCE \"This is the sentence we are printing to the file. \"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_fwrite_03_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(5==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(5==5)\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 5==5 to 5!=5 */\r\nstatic void goodB2G1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(5==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_good.txt\";\r\n /* FIX: Validate count before using it as the for loop variant to write to a file */\r\n if (count > 0 && count <= 20)\r\n {\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile)) exit(1);\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(5==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(5==5)\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_good.txt\";\r\n /* FIX: Validate count before using it as the for loop variant to write to a file */\r\n if (count > 0 && count <= 20)\r\n {\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile)) exit(1);\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 5==5 to 5!=5 */\r\nstatic void goodG2B1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(5==5)\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(5==5)\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(5==5)\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_fwrite_03_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__connect_socket_fwrite_03_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__connect_socket_fwrite_03_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_fwrite_09.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_fwrite_09.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":13916,"source_sha256":"4f3d0656f74d3955d14a58ef49bf97a796b98a83a991ff246caf7fdeae70f7b9","line_count":448,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_fwrite_09.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-09.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#define SENTENCE \"This is the sentence we are printing to the file. \"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_fwrite_09_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_good.txt\";\r\n /* FIX: Validate count before using it as the for loop variant to write to a file */\r\n if (count > 0 && count <= 20)\r\n {\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile)) exit(1);\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_good.txt\";\r\n /* FIX: Validate count before using it as the for loop variant to write to a file */\r\n if (count > 0 && count <= 20)\r\n {\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile)) exit(1);\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_fwrite_09_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__connect_socket_fwrite_09_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__connect_socket_fwrite_09_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_fwrite_10.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_fwrite_10.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":13804,"source_sha256":"05db5589431c9d2428937deb1bc6fb20c229c2bfcd0f327a796ff1f75d2d039e","line_count":448,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_fwrite_10.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-10.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 10 Control flow: if(globalTrue) and if(globalFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#define SENTENCE \"This is the sentence we are printing to the file. \"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_fwrite_10_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalTrue)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalTrue)\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalTrue to globalFalse */\r\nstatic void goodB2G1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalTrue)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_good.txt\";\r\n /* FIX: Validate count before using it as the for loop variant to write to a file */\r\n if (count > 0 && count <= 20)\r\n {\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile)) exit(1);\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalTrue)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalTrue)\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_good.txt\";\r\n /* FIX: Validate count before using it as the for loop variant to write to a file */\r\n if (count > 0 && count <= 20)\r\n {\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile)) exit(1);\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalTrue to globalFalse */\r\nstatic void goodG2B1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(globalTrue)\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalTrue)\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(globalTrue)\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_fwrite_10_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__connect_socket_fwrite_10_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__connect_socket_fwrite_10_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_fwrite_41.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_fwrite_41.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":8641,"source_sha256":"ef2effd8395c0b6fedaaf7665ee3350e2d95e6fd15f045316b142cba2ab2fac1","line_count":302,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_fwrite_41.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-41.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 41 Data flow: data passed as an argument from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#define SENTENCE \"This is the sentence we are printing to the file. \"\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink(int count)\r\n{\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_fwrite_41_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n badSink(count);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink(int count)\r\n{\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n goodG2BSink(count);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink(int count)\r\n{\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_good.txt\";\r\n /* FIX: Validate count before using it as the for loop variant to write to a file */\r\n if (count > 0 && count <= 20)\r\n {\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile)) exit(1);\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n goodB2GSink(count);\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_fwrite_41_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__connect_socket_fwrite_41_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__connect_socket_fwrite_41_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_fwrite_42.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_fwrite_42.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":8718,"source_sha256":"9a798e6c37fcf9c4f038c1fc819c61eced7f522069921b53c855c396dc31e54f","line_count":305,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_fwrite_42.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-42.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 42 Data flow: data returned from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#define SENTENCE \"This is the sentence we are printing to the file. \"\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic int badSource(int count)\r\n{\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n return count;\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_fwrite_42_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n count = badSource(count);\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic int goodG2BSource(int count)\r\n{\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n return count;\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n count = goodG2BSource(count);\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic int goodB2GSource(int count)\r\n{\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n return count;\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n count = goodB2GSource(count);\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_good.txt\";\r\n /* FIX: Validate count before using it as the for loop variant to write to a file */\r\n if (count > 0 && count <= 20)\r\n {\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile)) exit(1);\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_fwrite_42_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__connect_socket_fwrite_42_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__connect_socket_fwrite_42_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_fwrite_43.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_fwrite_43.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":8672,"source_sha256":"ac4d565dee96cfed94844a7a8578ab03e12006004b645ac4ad7909c84952477b","line_count":309,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_fwrite_43.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-43.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 43 Data flow: data flows using a C++ reference from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#define SENTENCE \"This is the sentence we are printing to the file. \"\r\n\r\nnamespace CWE400_Resource_Exhaustion__connect_socket_fwrite_43\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSource(int &count)\r\n{\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n}\r\n\r\nvoid bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n badSource(count);\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSource(int &count)\r\n{\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n goodG2BSource(count);\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSource(int &count)\r\n{\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n goodB2GSource(count);\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_good.txt\";\r\n /* FIX: Validate count before using it as the for loop variant to write to a file */\r\n if (count > 0 && count <= 20)\r\n {\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile)) exit(1);\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE400_Resource_Exhaustion__connect_socket_fwrite_43; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_fwrite_81.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_fwrite_81.h","cwe_id":"CWE400","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1582,"source_sha256":"8dc29c4555f993d154b0bbd6764088d818c8d5d17fc3b5fce69a5974de564c68","line_count":56,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_fwrite_81.h\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-81.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE400_Resource_Exhaustion__connect_socket_fwrite_81\r\n{\r\n\r\nclass CWE400_Resource_Exhaustion__connect_socket_fwrite_81_base\r\n{\r\npublic:\r\n /* pure virtual function */\r\n virtual void action(int count) const = 0;\r\n};\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE400_Resource_Exhaustion__connect_socket_fwrite_81_bad : public CWE400_Resource_Exhaustion__connect_socket_fwrite_81_base\r\n{\r\npublic:\r\n void action(int count) const;\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE400_Resource_Exhaustion__connect_socket_fwrite_81_goodG2B : public CWE400_Resource_Exhaustion__connect_socket_fwrite_81_base\r\n{\r\npublic:\r\n void action(int count) const;\r\n};\r\n\r\nclass CWE400_Resource_Exhaustion__connect_socket_fwrite_81_goodB2G : public CWE400_Resource_Exhaustion__connect_socket_fwrite_81_base\r\n{\r\npublic:\r\n void action(int count) const;\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_fwrite_83.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_fwrite_83.h","cwe_id":"CWE400","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1665,"source_sha256":"570b52cc81eb7c3dd8cf8992ab2d9ea9ddd61843d5429eeb3d7fc332581c2a28","line_count":61,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_fwrite_83.h\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-83.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE400_Resource_Exhaustion__connect_socket_fwrite_83\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE400_Resource_Exhaustion__connect_socket_fwrite_83_bad\r\n{\r\npublic:\r\n CWE400_Resource_Exhaustion__connect_socket_fwrite_83_bad(int countCopy);\r\n ~CWE400_Resource_Exhaustion__connect_socket_fwrite_83_bad();\r\n\r\nprivate:\r\n int count;\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE400_Resource_Exhaustion__connect_socket_fwrite_83_goodG2B\r\n{\r\npublic:\r\n CWE400_Resource_Exhaustion__connect_socket_fwrite_83_goodG2B(int countCopy);\r\n ~CWE400_Resource_Exhaustion__connect_socket_fwrite_83_goodG2B();\r\n\r\nprivate:\r\n int count;\r\n};\r\n\r\nclass CWE400_Resource_Exhaustion__connect_socket_fwrite_83_goodB2G\r\n{\r\npublic:\r\n CWE400_Resource_Exhaustion__connect_socket_fwrite_83_goodB2G(int countCopy);\r\n ~CWE400_Resource_Exhaustion__connect_socket_fwrite_83_goodB2G();\r\n\r\nprivate:\r\n int count;\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_fwrite_83a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_fwrite_83a.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":2465,"source_sha256":"b08d72b70e8528d692d243e80173dff622dcf5eadecbe0ca1111bb0edc5c6579","line_count":92,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_fwrite_83a.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-83a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE400_Resource_Exhaustion__connect_socket_fwrite_83.h\"\r\n\r\nnamespace CWE400_Resource_Exhaustion__connect_socket_fwrite_83\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n CWE400_Resource_Exhaustion__connect_socket_fwrite_83_bad badObject(count);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n CWE400_Resource_Exhaustion__connect_socket_fwrite_83_goodG2B goodG2BObject(count);\r\n}\r\n\r\n/* goodG2B uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n CWE400_Resource_Exhaustion__connect_socket_fwrite_83_goodB2G goodB2GObject(count);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE400_Resource_Exhaustion__connect_socket_fwrite_83; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_sleep_18.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_sleep_18.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":7233,"source_sha256":"fc5d046a33dfe1577bb6e137627089bea9d233be8c1689fef9b3657b06eb395b","line_count":251,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_sleep_18.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-18.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: sleep\r\n * GoodSink: Validate count before using it as a parameter in sleep function\r\n * BadSink : Use count as the parameter for sleep withhout checking it's size first\r\n * Flow Variant: 18 Control flow: goto statements\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#ifdef _WIN32\r\n#define SLEEP Sleep\r\n#else\r\n#define SLEEP usleep\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_sleep_18_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n goto source;\r\nsource:\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Sleep function using count as the parameter with no validation */\r\n SLEEP(count);\r\n printLine(\"Sleep time possibly too long\");\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by reversing the blocks on the second goto statement */\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n goto source;\r\nsource:\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n goto sink;\r\nsink:\r\n /* FIX: Validate count before using it as a parameter in the sleep function */\r\n if (count > 0 && count <= 2000)\r\n {\r\n SLEEP(count);\r\n printLine(\"Sleep time OK\");\r\n }\r\n else\r\n {\r\n printLine(\"Sleep time too long\");\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by reversing the blocks on the first goto statement */\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n goto source;\r\nsource:\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Sleep function using count as the parameter with no validation */\r\n SLEEP(count);\r\n printLine(\"Sleep time possibly too long\");\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_sleep_18_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__connect_socket_sleep_18_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__connect_socket_sleep_18_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_sleep_52a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_sleep_52a.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":7009,"source_sha256":"14a1db6e79368223913f2d97f02228fed135aae2d16425d5f0e62518653d2d1e","line_count":233,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_sleep_52a.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-52a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: sleep\r\n * GoodSink: Validate count before using it as a parameter in sleep function\r\n * BadSink : Use count as the parameter for sleep withhout checking it's size first\r\n * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#ifdef _WIN32\r\n#define SLEEP Sleep\r\n#else\r\n#define SLEEP usleep\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE400_Resource_Exhaustion__connect_socket_sleep_52b_badSink(int count);\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_sleep_52_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE400_Resource_Exhaustion__connect_socket_sleep_52b_badSink(count);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE400_Resource_Exhaustion__connect_socket_sleep_52b_goodG2BSink(int count);\r\n\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n CWE400_Resource_Exhaustion__connect_socket_sleep_52b_goodG2BSink(count);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE400_Resource_Exhaustion__connect_socket_sleep_52b_goodB2GSink(int count);\r\n\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE400_Resource_Exhaustion__connect_socket_sleep_52b_goodB2GSink(count);\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_sleep_52_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__connect_socket_sleep_52_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__connect_socket_sleep_52_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_sleep_53a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_sleep_53a.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":7036,"source_sha256":"93c7edbfd87b70bd6af9bb5686474c642d9dc0a114e26dd8e2ff99b961aa68d8","line_count":233,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_sleep_53a.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-53a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: sleep\r\n * GoodSink: Validate count before using it as a parameter in sleep function\r\n * BadSink : Use count as the parameter for sleep withhout checking it's size first\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else /* NOT _WIN32 */\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define IP_ADDRESS \"127.0.0.1\"\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#ifdef _WIN32\r\n#define SLEEP Sleep\r\n#else\r\n#define SLEEP usleep\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE400_Resource_Exhaustion__connect_socket_sleep_53b_badSink(int count);\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_sleep_53_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE400_Resource_Exhaustion__connect_socket_sleep_53b_badSink(count);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE400_Resource_Exhaustion__connect_socket_sleep_53b_goodG2BSink(int count);\r\n\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n CWE400_Resource_Exhaustion__connect_socket_sleep_53b_goodG2BSink(count);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE400_Resource_Exhaustion__connect_socket_sleep_53b_goodB2GSink(int count);\r\n\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET connectSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a connect socket */\r\n connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (connectSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = inet_addr(IP_ADDRESS);\r\n service.sin_port = htons(TCP_PORT);\r\n if (connect(connectSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed, make sure to recv one\r\n * less char than is in the recv_buf in order to append a terminator */\r\n recvResult = recv(connectSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (connectSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(connectSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE400_Resource_Exhaustion__connect_socket_sleep_53b_goodB2GSink(count);\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__connect_socket_sleep_53_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__connect_socket_sleep_53_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__connect_socket_sleep_53_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_sleep_83a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_sleep_83a.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":2490,"source_sha256":"8f1dcc1c0d625ce2bf0e15ffeae9b2f1612e2452fd01e2e8411be65352fbac93","line_count":92,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_sleep_83a.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-83a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: sleep\r\n * GoodSink: Validate count before using it as a parameter in sleep function\r\n * BadSink : Use count as the parameter for sleep withhout checking it's size first\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE400_Resource_Exhaustion__connect_socket_sleep_83.h\"\r\n\r\nnamespace CWE400_Resource_Exhaustion__connect_socket_sleep_83\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n CWE400_Resource_Exhaustion__connect_socket_sleep_83_bad badObject(count);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n CWE400_Resource_Exhaustion__connect_socket_sleep_83_goodG2B goodG2BObject(count);\r\n}\r\n\r\n/* goodG2B uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n CWE400_Resource_Exhaustion__connect_socket_sleep_83_goodB2G goodB2GObject(count);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE400_Resource_Exhaustion__connect_socket_sleep_83; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_sleep_84.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__connect_socket_sleep_84.h","cwe_id":"CWE400","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1711,"source_sha256":"0666b2c376e32cd525a17abae5bd5662ec3b7a0c8fa71505eb0e2162cb530107","line_count":61,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__connect_socket_sleep_84.h\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-84.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: connect_socket Read data using a connect socket (client side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: sleep\r\n * GoodSink: Validate count before using it as a parameter in sleep function\r\n * BadSink : Use count as the parameter for sleep withhout checking it's size first\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE400_Resource_Exhaustion__connect_socket_sleep_84\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE400_Resource_Exhaustion__connect_socket_sleep_84_bad\r\n{\r\npublic:\r\n CWE400_Resource_Exhaustion__connect_socket_sleep_84_bad(int countCopy);\r\n ~CWE400_Resource_Exhaustion__connect_socket_sleep_84_bad();\r\n\r\nprivate:\r\n int count;\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE400_Resource_Exhaustion__connect_socket_sleep_84_goodG2B\r\n{\r\npublic:\r\n CWE400_Resource_Exhaustion__connect_socket_sleep_84_goodG2B(int countCopy);\r\n ~CWE400_Resource_Exhaustion__connect_socket_sleep_84_goodG2B();\r\n\r\nprivate:\r\n int count;\r\n};\r\n\r\nclass CWE400_Resource_Exhaustion__connect_socket_sleep_84_goodB2G\r\n{\r\npublic:\r\n CWE400_Resource_Exhaustion__connect_socket_sleep_84_goodB2G(int countCopy);\r\n ~CWE400_Resource_Exhaustion__connect_socket_sleep_84_goodB2G();\r\n\r\nprivate:\r\n int count;\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_for_loop_01.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_for_loop_01.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":3609,"source_sha256":"468dfa19657f3a783727bdd7f385f446c44fee3962844e9b0f0242766c7d81af","line_count":138,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fgets_for_loop_01.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-01.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 01 Baseline\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__fgets_for_loop_01_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n {\r\n size_t i = 0;\r\n /* FIX: Validate count before using it as the for loop variant */\r\n if (count > 0 && count <= 20)\r\n {\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__fgets_for_loop_01_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__fgets_for_loop_01_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__fgets_for_loop_01_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_for_loop_32.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_for_loop_32.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":4443,"source_sha256":"65e9bcf54fc98af4d8ceb22897fe8caa4324da88e4883af201d2a42402e4af44","line_count":164,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fgets_for_loop_32.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-32.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 32 Data flow using two pointers to the same value within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__fgets_for_loop_32_bad()\r\n{\r\n int count;\r\n int *countPtr1 = &count;\r\n int *countPtr2 = &count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n int count = *countPtr1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n *countPtr1 = count;\r\n }\r\n {\r\n int count = *countPtr2;\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n int *countPtr1 = &count;\r\n int *countPtr2 = &count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n int count = *countPtr1;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n *countPtr1 = count;\r\n }\r\n {\r\n int count = *countPtr2;\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n int *countPtr1 = &count;\r\n int *countPtr2 = &count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n int count = *countPtr1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n *countPtr1 = count;\r\n }\r\n {\r\n int count = *countPtr2;\r\n {\r\n size_t i = 0;\r\n /* FIX: Validate count before using it as the for loop variant */\r\n if (count > 0 && count <= 20)\r\n {\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__fgets_for_loop_32_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__fgets_for_loop_32_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__fgets_for_loop_32_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_for_loop_43.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_for_loop_43.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":3935,"source_sha256":"2b8e2b3a42c1bdac89f39c58c90992e47441657266c7e9b397a0201b82f68ca2","line_count":160,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fgets_for_loop_43.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-43.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 43 Data flow: data flows using a C++ reference from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\nnamespace CWE400_Resource_Exhaustion__fgets_for_loop_43\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSource(int &count)\r\n{\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n}\r\n\r\nvoid bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n badSource(count);\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSource(int &count)\r\n{\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n goodG2BSource(count);\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSource(int &count)\r\n{\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n goodB2GSource(count);\r\n {\r\n size_t i = 0;\r\n /* FIX: Validate count before using it as the for loop variant */\r\n if (count > 0 && count <= 20)\r\n {\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE400_Resource_Exhaustion__fgets_for_loop_43; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_for_loop_45.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_for_loop_45.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":4510,"source_sha256":"87f4d082debbf86f318c8162d48c32a4469c854ce0a53ef5afaec53e48478b88","line_count":162,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fgets_for_loop_45.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-45.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 45 Data flow: data passed as a static global variable from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\nstatic int CWE400_Resource_Exhaustion__fgets_for_loop_45_badData;\r\nstatic int CWE400_Resource_Exhaustion__fgets_for_loop_45_goodG2BData;\r\nstatic int CWE400_Resource_Exhaustion__fgets_for_loop_45_goodB2GData;\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink()\r\n{\r\n int count = CWE400_Resource_Exhaustion__fgets_for_loop_45_badData;\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__fgets_for_loop_45_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n CWE400_Resource_Exhaustion__fgets_for_loop_45_badData = count;\r\n badSink();\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink()\r\n{\r\n int count = CWE400_Resource_Exhaustion__fgets_for_loop_45_goodG2BData;\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n CWE400_Resource_Exhaustion__fgets_for_loop_45_goodG2BData = count;\r\n goodG2BSink();\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink()\r\n{\r\n int count = CWE400_Resource_Exhaustion__fgets_for_loop_45_goodB2GData;\r\n {\r\n size_t i = 0;\r\n /* FIX: Validate count before using it as the for loop variant */\r\n if (count > 0 && count <= 20)\r\n {\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n CWE400_Resource_Exhaustion__fgets_for_loop_45_goodB2GData = count;\r\n goodB2GSink();\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__fgets_for_loop_45_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__fgets_for_loop_45_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__fgets_for_loop_45_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_for_loop_54e.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_for_loop_54e.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":1976,"source_sha256":"195e8cc307e0aa4e8bcc6c27e6a859295265929992365532900258c884a512d9","line_count":69,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fgets_for_loop_54e.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-54e.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__fgets_for_loop_54e_badSink(int count)\r\n{\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE400_Resource_Exhaustion__fgets_for_loop_54e_goodG2BSink(int count)\r\n{\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE400_Resource_Exhaustion__fgets_for_loop_54e_goodB2GSink(int count)\r\n{\r\n {\r\n size_t i = 0;\r\n /* FIX: Validate count before using it as the for loop variant */\r\n if (count > 0 && count <= 20)\r\n {\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_for_loop_63b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_for_loop_63b.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":2026,"source_sha256":"3e9a852627e8d4958b0a6ccf87b3ce8d2b143d4be40ae49da82fe27ba1870d7e","line_count":72,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fgets_for_loop_63b.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-63b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__fgets_for_loop_63b_badSink(int * countPtr)\r\n{\r\n int count = *countPtr;\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE400_Resource_Exhaustion__fgets_for_loop_63b_goodG2BSink(int * countPtr)\r\n{\r\n int count = *countPtr;\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE400_Resource_Exhaustion__fgets_for_loop_63b_goodB2GSink(int * countPtr)\r\n{\r\n int count = *countPtr;\r\n {\r\n size_t i = 0;\r\n /* FIX: Validate count before using it as the for loop variant */\r\n if (count > 0 && count <= 20)\r\n {\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_for_loop_68a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_for_loop_68a.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":3827,"source_sha256":"90cc13f434800b650a91dfe725f501bacca0d930a84fc1d110731b26444fe82b","line_count":128,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fgets_for_loop_68a.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-68a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 68 Data flow: data passed as a global variable from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\nint CWE400_Resource_Exhaustion__fgets_for_loop_68_badData;\r\nint CWE400_Resource_Exhaustion__fgets_for_loop_68_goodG2BData;\r\nint CWE400_Resource_Exhaustion__fgets_for_loop_68_goodB2GData;\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE400_Resource_Exhaustion__fgets_for_loop_68b_badSink();\r\n\r\nvoid CWE400_Resource_Exhaustion__fgets_for_loop_68_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n CWE400_Resource_Exhaustion__fgets_for_loop_68_badData = count;\r\n CWE400_Resource_Exhaustion__fgets_for_loop_68b_badSink();\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\nvoid CWE400_Resource_Exhaustion__fgets_for_loop_68b_goodG2BSink();\r\nvoid CWE400_Resource_Exhaustion__fgets_for_loop_68b_goodB2GSink();\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n CWE400_Resource_Exhaustion__fgets_for_loop_68_goodG2BData = count;\r\n CWE400_Resource_Exhaustion__fgets_for_loop_68b_goodG2BSink();\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n CWE400_Resource_Exhaustion__fgets_for_loop_68_goodB2GData = count;\r\n CWE400_Resource_Exhaustion__fgets_for_loop_68b_goodB2GSink();\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__fgets_for_loop_68_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__fgets_for_loop_68_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__fgets_for_loop_68_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_for_loop_82_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_for_loop_82_goodB2G.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":1246,"source_sha256":"3135d27f9a1a6de48fa1515736a50fe63eb7cfe6a2de5d5367e4dda365ff52de","line_count":41,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fgets_for_loop_82_goodB2G.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-82_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE400_Resource_Exhaustion__fgets_for_loop_82.h\"\r\n\r\nnamespace CWE400_Resource_Exhaustion__fgets_for_loop_82\r\n{\r\n\r\nvoid CWE400_Resource_Exhaustion__fgets_for_loop_82_goodB2G::action(int count)\r\n{\r\n {\r\n size_t i = 0;\r\n /* FIX: Validate count before using it as the for loop variant */\r\n if (count > 0 && count <= 20)\r\n {\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_for_loop_82a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_for_loop_82a.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":3570,"source_sha256":"83479cddc823b9b7333de136de29976c44cf779c0f662fca6f70fe7068309c3e","line_count":128,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fgets_for_loop_82a.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-82a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE400_Resource_Exhaustion__fgets_for_loop_82.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\nnamespace CWE400_Resource_Exhaustion__fgets_for_loop_82\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n CWE400_Resource_Exhaustion__fgets_for_loop_82_base* baseObject = new CWE400_Resource_Exhaustion__fgets_for_loop_82_bad;\r\n baseObject->action(count);\r\n delete baseObject;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n CWE400_Resource_Exhaustion__fgets_for_loop_82_base* baseObject = new CWE400_Resource_Exhaustion__fgets_for_loop_82_goodG2B;\r\n baseObject->action(count);\r\n delete baseObject;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n CWE400_Resource_Exhaustion__fgets_for_loop_82_base* baseObject = new CWE400_Resource_Exhaustion__fgets_for_loop_82_goodB2G;\r\n baseObject->action(count);\r\n delete baseObject;\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE400_Resource_Exhaustion__fgets_for_loop_82; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_for_loop_84_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_for_loop_84_goodG2B.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":1532,"source_sha256":"563b39c21f366ec7294ee02551316b72c4f54a34c15d5eed158d11c2021d72b0","line_count":45,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fgets_for_loop_84_goodG2B.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-84_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE400_Resource_Exhaustion__fgets_for_loop_84.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\nnamespace CWE400_Resource_Exhaustion__fgets_for_loop_84\r\n{\r\nCWE400_Resource_Exhaustion__fgets_for_loop_84_goodG2B::CWE400_Resource_Exhaustion__fgets_for_loop_84_goodG2B(int countCopy)\r\n{\r\n count = countCopy;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n}\r\n\r\nCWE400_Resource_Exhaustion__fgets_for_loop_84_goodG2B::~CWE400_Resource_Exhaustion__fgets_for_loop_84_goodG2B()\r\n{\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_fwrite_05.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_fwrite_05.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":8762,"source_sha256":"b5b3eb7b42d89d538d708efa7db7e6e44837d058dfc3ac65f9416d1f94757446","line_count":305,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fgets_fwrite_05.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-05.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 05 Control flow: if(staticTrue) and if(staticFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#define SENTENCE \"This is the sentence we are printing to the file. \"\r\n\r\n/* The two variables below are not defined as \"const\", but are never\r\n assigned any other value, so a tool should be able to identify that\r\n reads of these will always return their initialized values. */\r\nstatic int staticTrue = 1; /* true */\r\nstatic int staticFalse = 0; /* false */\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__fgets_fwrite_05_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(staticTrue)\r\n {\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n }\r\n if(staticTrue)\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticTrue to staticFalse */\r\nstatic void goodB2G1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(staticTrue)\r\n {\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n }\r\n if(staticFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_good.txt\";\r\n /* FIX: Validate count before using it as the for loop variant to write to a file */\r\n if (count > 0 && count <= 20)\r\n {\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile)) exit(1);\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(staticTrue)\r\n {\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n }\r\n if(staticTrue)\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_good.txt\";\r\n /* FIX: Validate count before using it as the for loop variant to write to a file */\r\n if (count > 0 && count <= 20)\r\n {\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile)) exit(1);\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticTrue to staticFalse */\r\nstatic void goodG2B1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(staticFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(staticTrue)\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(staticTrue)\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(staticTrue)\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__fgets_fwrite_05_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__fgets_fwrite_05_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__fgets_fwrite_05_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_fwrite_16.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_fwrite_16.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":5671,"source_sha256":"6ad12e6e472e15d5abbb43dd20d803f9d0f14a0edddbeab6a0775cb05a418506","line_count":205,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fgets_fwrite_16.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-16.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 16 Control flow: while(1)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#define SENTENCE \"This is the sentence we are printing to the file. \"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__fgets_fwrite_16_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n while(1)\r\n {\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n break;\r\n }\r\n while(1)\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the sinks in the second while statement */\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n while(1)\r\n {\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n break;\r\n }\r\n while(1)\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_good.txt\";\r\n /* FIX: Validate count before using it as the for loop variant to write to a file */\r\n if (count > 0 && count <= 20)\r\n {\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile)) exit(1);\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the sources in the first while statement */\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n while(1)\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n break;\r\n }\r\n while(1)\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__fgets_fwrite_16_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__fgets_fwrite_16_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__fgets_fwrite_16_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_fwrite_63b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_fwrite_63b.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":3335,"source_sha256":"2b7238e83fb2e1f21680548cf90c925717edaa633fd98abf94e6afc54e8dc85d","line_count":115,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fgets_fwrite_63b.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-63b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#define SENTENCE \"This is the sentence we are printing to the file. \"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__fgets_fwrite_63b_badSink(int * countPtr)\r\n{\r\n int count = *countPtr;\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE400_Resource_Exhaustion__fgets_fwrite_63b_goodG2BSink(int * countPtr)\r\n{\r\n int count = *countPtr;\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE400_Resource_Exhaustion__fgets_fwrite_63b_goodB2GSink(int * countPtr)\r\n{\r\n int count = *countPtr;\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_good.txt\";\r\n /* FIX: Validate count before using it as the for loop variant to write to a file */\r\n if (count > 0 && count <= 20)\r\n {\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile)) exit(1);\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_fwrite_72a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_fwrite_72a.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":3894,"source_sha256":"2af4e884bb85ea43e4adb1617edba50d21856b4fdeda1288b951700479b74c04","line_count":145,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fgets_fwrite_72a.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-72a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 72 Data flow: data passed in a vector from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\nusing namespace std;\r\n\r\nnamespace CWE400_Resource_Exhaustion__fgets_fwrite_72\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(vector countVector);\r\n\r\nvoid bad()\r\n{\r\n int count;\r\n vector countVector;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n /* Put count in a vector */\r\n countVector.insert(countVector.end(), 1, count);\r\n countVector.insert(countVector.end(), 1, count);\r\n countVector.insert(countVector.end(), 1, count);\r\n badSink(countVector);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(vector countVector);\r\n\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n vector countVector;\r\n /* Initialize count */\r\n count = -1;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n /* Put count in a vector */\r\n countVector.insert(countVector.end(), 1, count);\r\n countVector.insert(countVector.end(), 1, count);\r\n countVector.insert(countVector.end(), 1, count);\r\n goodG2BSink(countVector);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(vector countVector);\r\n\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n vector countVector;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n countVector.insert(countVector.end(), 1, count);\r\n countVector.insert(countVector.end(), 1, count);\r\n countVector.insert(countVector.end(), 1, count);\r\n goodB2GSink(countVector);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE400_Resource_Exhaustion__fgets_fwrite_72; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_fwrite_81a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_fwrite_81a.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":3480,"source_sha256":"927a614e56632a29b6d9ae2cad226e6506f0371fbb36ec3121fd8807b0687c18","line_count":125,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fgets_fwrite_81a.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-81a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE400_Resource_Exhaustion__fgets_fwrite_81.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\nnamespace CWE400_Resource_Exhaustion__fgets_fwrite_81\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n const CWE400_Resource_Exhaustion__fgets_fwrite_81_base& baseObject = CWE400_Resource_Exhaustion__fgets_fwrite_81_bad();\r\n baseObject.action(count);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n const CWE400_Resource_Exhaustion__fgets_fwrite_81_base& baseObject = CWE400_Resource_Exhaustion__fgets_fwrite_81_goodG2B();\r\n baseObject.action(count);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n const CWE400_Resource_Exhaustion__fgets_fwrite_81_base& baseObject = CWE400_Resource_Exhaustion__fgets_fwrite_81_goodB2G();\r\n baseObject.action(count);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE400_Resource_Exhaustion__fgets_fwrite_81; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_fwrite_83a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_fwrite_83a.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":2387,"source_sha256":"492bb2a60a53211720ff2609b0617946da52441e569aee84ff91f2a4ae0c7735","line_count":92,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fgets_fwrite_83a.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-83a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE400_Resource_Exhaustion__fgets_fwrite_83.h\"\r\n\r\nnamespace CWE400_Resource_Exhaustion__fgets_fwrite_83\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n CWE400_Resource_Exhaustion__fgets_fwrite_83_bad badObject(count);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n CWE400_Resource_Exhaustion__fgets_fwrite_83_goodG2B goodG2BObject(count);\r\n}\r\n\r\n/* goodG2B uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n CWE400_Resource_Exhaustion__fgets_fwrite_83_goodB2G goodB2GObject(count);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE400_Resource_Exhaustion__fgets_fwrite_83; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_fwrite_84_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_fwrite_84_goodG2B.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":2015,"source_sha256":"eb528ff6e4302dc4f7769f7574b7dc392c8ed2866c4bbd23e707619a9f36ace6","line_count":62,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fgets_fwrite_84_goodG2B.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-84_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE400_Resource_Exhaustion__fgets_fwrite_84.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#define SENTENCE \"This is the sentence we are printing to the file. \"\r\n\r\nnamespace CWE400_Resource_Exhaustion__fgets_fwrite_84\r\n{\r\nCWE400_Resource_Exhaustion__fgets_fwrite_84_goodG2B::CWE400_Resource_Exhaustion__fgets_fwrite_84_goodG2B(int countCopy)\r\n{\r\n count = countCopy;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n}\r\n\r\nCWE400_Resource_Exhaustion__fgets_fwrite_84_goodG2B::~CWE400_Resource_Exhaustion__fgets_fwrite_84_goodG2B()\r\n{\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_sleep_11.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_sleep_11.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":6014,"source_sha256":"df3a62bea4d98670c00b9089b8fe7fddd53076238b276769b49b4d70cd4c20b0","line_count":221,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fgets_sleep_11.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-11.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: sleep\r\n * GoodSink: Validate count before using it as a parameter in sleep function\r\n * BadSink : Use count as the parameter for sleep withhout checking it's size first\r\n * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#ifdef _WIN32\r\n#include \r\n#define SLEEP Sleep\r\n#else\r\n#include \r\n#define SLEEP usleep\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__fgets_sleep_11_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Sleep function using count as the parameter with no validation */\r\n SLEEP(count);\r\n printLine(\"Sleep time possibly too long\");\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n }\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Validate count before using it as a parameter in the sleep function */\r\n if (count > 0 && count <= 2000)\r\n {\r\n SLEEP(count);\r\n printLine(\"Sleep time OK\");\r\n }\r\n else\r\n {\r\n printLine(\"Sleep time too long\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Validate count before using it as a parameter in the sleep function */\r\n if (count > 0 && count <= 2000)\r\n {\r\n SLEEP(count);\r\n printLine(\"Sleep time OK\");\r\n }\r\n else\r\n {\r\n printLine(\"Sleep time too long\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Sleep function using count as the parameter with no validation */\r\n SLEEP(count);\r\n printLine(\"Sleep time possibly too long\");\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Sleep function using count as the parameter with no validation */\r\n SLEEP(count);\r\n printLine(\"Sleep time possibly too long\");\r\n }\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__fgets_sleep_11_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__fgets_sleep_11_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__fgets_sleep_11_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_sleep_22a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_sleep_22a.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":4936,"source_sha256":"7bbd7e58cae62f43abbf271a151135b3e0e7e81841200b7ab662ce03c2293d4f","line_count":157,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fgets_sleep_22a.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-22a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: sleep\r\n * GoodSink: Validate count before using it as a parameter in sleep function\r\n * BadSink : Use count as the parameter for sleep withhout checking it's size first\r\n * Flow Variant: 22 Control flow: Flow controlled by value of a global variable. Sink functions are in a separate file from sources.\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\n/* The global variable below is used to drive control flow in the sink function */\r\nint CWE400_Resource_Exhaustion__fgets_sleep_22_badGlobal = 0;\r\n\r\nvoid CWE400_Resource_Exhaustion__fgets_sleep_22_badSink(int count);\r\n\r\nvoid CWE400_Resource_Exhaustion__fgets_sleep_22_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n CWE400_Resource_Exhaustion__fgets_sleep_22_badGlobal = 1; /* true */\r\n CWE400_Resource_Exhaustion__fgets_sleep_22_badSink(count);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* The global variables below are used to drive control flow in the sink functions. */\r\nint CWE400_Resource_Exhaustion__fgets_sleep_22_goodB2G1Global = 0;\r\nint CWE400_Resource_Exhaustion__fgets_sleep_22_goodB2G2Global = 0;\r\nint CWE400_Resource_Exhaustion__fgets_sleep_22_goodG2BGlobal = 0;\r\n\r\n/* goodB2G1() - use badsource and goodsink by setting the static variable to false instead of true */\r\nvoid CWE400_Resource_Exhaustion__fgets_sleep_22_goodB2G1Sink(int count);\r\n\r\nstatic void goodB2G1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n CWE400_Resource_Exhaustion__fgets_sleep_22_goodB2G1Global = 0; /* false */\r\n CWE400_Resource_Exhaustion__fgets_sleep_22_goodB2G1Sink(count);\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */\r\nvoid CWE400_Resource_Exhaustion__fgets_sleep_22_goodB2G2Sink(int count);\r\n\r\nstatic void goodB2G2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n CWE400_Resource_Exhaustion__fgets_sleep_22_goodB2G2Global = 1; /* true */\r\n CWE400_Resource_Exhaustion__fgets_sleep_22_goodB2G2Sink(count);\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink */\r\nvoid CWE400_Resource_Exhaustion__fgets_sleep_22_goodG2BSink(int count);\r\n\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n CWE400_Resource_Exhaustion__fgets_sleep_22_goodG2BGlobal = 1; /* true */\r\n CWE400_Resource_Exhaustion__fgets_sleep_22_goodG2BSink(count);\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__fgets_sleep_22_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__fgets_sleep_22_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__fgets_sleep_22_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_sleep_65a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_sleep_65a.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":3766,"source_sha256":"974f085a54a929a0814c4100cea1572c1f038a4964a5091b955145990d6f3a95","line_count":134,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fgets_sleep_65a.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-65a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: sleep\r\n * GoodSink: Validate count before using it as a parameter in sleep function\r\n * BadSink : Use count as the parameter for sleep withhout checking it's size first\r\n * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#ifdef _WIN32\r\n#include \r\n#define SLEEP Sleep\r\n#else\r\n#include \r\n#define SLEEP usleep\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE400_Resource_Exhaustion__fgets_sleep_65b_badSink(int count);\r\n\r\nvoid CWE400_Resource_Exhaustion__fgets_sleep_65_bad()\r\n{\r\n int count;\r\n /* define a function pointer */\r\n void (*funcPtr) (int) = CWE400_Resource_Exhaustion__fgets_sleep_65b_badSink;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n /* use the function pointer */\r\n funcPtr(count);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE400_Resource_Exhaustion__fgets_sleep_65b_goodG2BSink(int count);\r\n\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n void (*funcPtr) (int) = CWE400_Resource_Exhaustion__fgets_sleep_65b_goodG2BSink;\r\n /* Initialize count */\r\n count = -1;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n funcPtr(count);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE400_Resource_Exhaustion__fgets_sleep_65b_goodB2GSink(int count);\r\n\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n void (*funcPtr) (int) = CWE400_Resource_Exhaustion__fgets_sleep_65b_goodB2GSink;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n char inputBuffer[CHAR_ARRAY_SIZE] = \"\";\r\n /* POTENTIAL FLAW: Read count from the console using fgets() */\r\n if (fgets(inputBuffer, CHAR_ARRAY_SIZE, stdin) != NULL)\r\n {\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n else\r\n {\r\n printLine(\"fgets() failed.\");\r\n }\r\n }\r\n funcPtr(count);\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__fgets_sleep_65_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__fgets_sleep_65_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__fgets_sleep_65_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_sleep_81.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fgets_sleep_81.h","cwe_id":"CWE400","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1509,"source_sha256":"5f06dad547ebf0a6817f89f1811aa4c0c215cde3ca9c3bedd4d6f334a2af29fb","line_count":56,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fgets_sleep_81.h\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-81.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fgets Read data from the console using fgets()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: sleep\r\n * GoodSink: Validate count before using it as a parameter in sleep function\r\n * BadSink : Use count as the parameter for sleep withhout checking it's size first\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE400_Resource_Exhaustion__fgets_sleep_81\r\n{\r\n\r\nclass CWE400_Resource_Exhaustion__fgets_sleep_81_base\r\n{\r\npublic:\r\n /* pure virtual function */\r\n virtual void action(int count) const = 0;\r\n};\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE400_Resource_Exhaustion__fgets_sleep_81_bad : public CWE400_Resource_Exhaustion__fgets_sleep_81_base\r\n{\r\npublic:\r\n void action(int count) const;\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE400_Resource_Exhaustion__fgets_sleep_81_goodG2B : public CWE400_Resource_Exhaustion__fgets_sleep_81_base\r\n{\r\npublic:\r\n void action(int count) const;\r\n};\r\n\r\nclass CWE400_Resource_Exhaustion__fgets_sleep_81_goodB2G : public CWE400_Resource_Exhaustion__fgets_sleep_81_base\r\n{\r\npublic:\r\n void action(int count) const;\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_for_loop_03.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_for_loop_03.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":4983,"source_sha256":"024f93d527666c407e540ffd4daaccaac192698875afabe69c1f899b943718e5","line_count":195,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fscanf_for_loop_03.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-03.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 03 Control flow: if(5==5) and if(5!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_for_loop_03_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n }\r\n if(5==5)\r\n {\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 5==5 to 5!=5 */\r\nstatic void goodB2G1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n }\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n {\r\n size_t i = 0;\r\n /* FIX: Validate count before using it as the for loop variant */\r\n if (count > 0 && count <= 20)\r\n {\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n }\r\n if(5==5)\r\n {\r\n {\r\n size_t i = 0;\r\n /* FIX: Validate count before using it as the for loop variant */\r\n if (count > 0 && count <= 20)\r\n {\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 5==5 to 5!=5 */\r\nstatic void goodG2B1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(5==5)\r\n {\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(5==5)\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(5==5)\r\n {\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_for_loop_03_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__fscanf_for_loop_03_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__fscanf_for_loop_03_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_for_loop_22a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_for_loop_22a.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":4132,"source_sha256":"b1627092fb7b3ec45954fbe5509dcb55530afe26ad37038a63fc20047c7a4162","line_count":122,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fscanf_for_loop_22a.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-22a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 22 Control flow: Flow controlled by value of a global variable. Sink functions are in a separate file from sources.\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* The global variable below is used to drive control flow in the sink function */\r\nint CWE400_Resource_Exhaustion__fscanf_for_loop_22_badGlobal = 0;\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_for_loop_22_badSink(int count);\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_for_loop_22_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n CWE400_Resource_Exhaustion__fscanf_for_loop_22_badGlobal = 1; /* true */\r\n CWE400_Resource_Exhaustion__fscanf_for_loop_22_badSink(count);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* The global variables below are used to drive control flow in the sink functions. */\r\nint CWE400_Resource_Exhaustion__fscanf_for_loop_22_goodB2G1Global = 0;\r\nint CWE400_Resource_Exhaustion__fscanf_for_loop_22_goodB2G2Global = 0;\r\nint CWE400_Resource_Exhaustion__fscanf_for_loop_22_goodG2BGlobal = 0;\r\n\r\n/* goodB2G1() - use badsource and goodsink by setting the static variable to false instead of true */\r\nvoid CWE400_Resource_Exhaustion__fscanf_for_loop_22_goodB2G1Sink(int count);\r\n\r\nstatic void goodB2G1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n CWE400_Resource_Exhaustion__fscanf_for_loop_22_goodB2G1Global = 0; /* false */\r\n CWE400_Resource_Exhaustion__fscanf_for_loop_22_goodB2G1Sink(count);\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */\r\nvoid CWE400_Resource_Exhaustion__fscanf_for_loop_22_goodB2G2Sink(int count);\r\n\r\nstatic void goodB2G2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n CWE400_Resource_Exhaustion__fscanf_for_loop_22_goodB2G2Global = 1; /* true */\r\n CWE400_Resource_Exhaustion__fscanf_for_loop_22_goodB2G2Sink(count);\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink */\r\nvoid CWE400_Resource_Exhaustion__fscanf_for_loop_22_goodG2BSink(int count);\r\n\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n CWE400_Resource_Exhaustion__fscanf_for_loop_22_goodG2BGlobal = 1; /* true */\r\n CWE400_Resource_Exhaustion__fscanf_for_loop_22_goodG2BSink(count);\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_for_loop_22_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__fscanf_for_loop_22_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__fscanf_for_loop_22_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_for_loop_53b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_for_loop_53b.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":1670,"source_sha256":"27eee04ba312a9784d43e18633c86f3b827bdb702f83e1ebdb4a340c21295f59","line_count":50,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fscanf_for_loop_53b.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-53b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE400_Resource_Exhaustion__fscanf_for_loop_53c_badSink(int count);\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_for_loop_53b_badSink(int count)\r\n{\r\n CWE400_Resource_Exhaustion__fscanf_for_loop_53c_badSink(count);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE400_Resource_Exhaustion__fscanf_for_loop_53c_goodG2BSink(int count);\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_for_loop_53b_goodG2BSink(int count)\r\n{\r\n CWE400_Resource_Exhaustion__fscanf_for_loop_53c_goodG2BSink(count);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE400_Resource_Exhaustion__fscanf_for_loop_53c_goodB2GSink(int count);\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_for_loop_53b_goodB2GSink(int count)\r\n{\r\n CWE400_Resource_Exhaustion__fscanf_for_loop_53c_goodB2GSink(count);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_for_loop_54c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_for_loop_54c.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":1671,"source_sha256":"d5d7fd0f05623afe5c02899ee8d151ad58afeb9ebc2758df5f01f3c670d8a61a","line_count":50,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fscanf_for_loop_54c.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-54c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE400_Resource_Exhaustion__fscanf_for_loop_54d_badSink(int count);\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_for_loop_54c_badSink(int count)\r\n{\r\n CWE400_Resource_Exhaustion__fscanf_for_loop_54d_badSink(count);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE400_Resource_Exhaustion__fscanf_for_loop_54d_goodG2BSink(int count);\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_for_loop_54c_goodG2BSink(int count)\r\n{\r\n CWE400_Resource_Exhaustion__fscanf_for_loop_54d_goodG2BSink(count);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE400_Resource_Exhaustion__fscanf_for_loop_54d_goodB2GSink(int count);\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_for_loop_54c_goodB2GSink(int count)\r\n{\r\n CWE400_Resource_Exhaustion__fscanf_for_loop_54d_goodB2GSink(count);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_for_loop_62a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_for_loop_62a.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":3070,"source_sha256":"729734983ed0479cbc03435829c9362d74b91b2699e914eb620f1ca4e82fda00","line_count":125,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fscanf_for_loop_62a.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-62a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE400_Resource_Exhaustion__fscanf_for_loop_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSource(int &count);\r\n\r\nvoid bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n badSource(count);\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(int &count);\r\n\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n goodG2BSource(count);\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(int &count);\r\n\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n goodB2GSource(count);\r\n {\r\n size_t i = 0;\r\n /* FIX: Validate count before using it as the for loop variant */\r\n if (count > 0 && count <= 20)\r\n {\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE400_Resource_Exhaustion__fscanf_for_loop_62; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_for_loop_72a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_for_loop_72a.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":3319,"source_sha256":"cc681c967d4fccb760af8da3dbec3dec92e0e26b3eda517dd1e86530b5a38165","line_count":121,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fscanf_for_loop_72a.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-72a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 72 Data flow: data passed in a vector from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE400_Resource_Exhaustion__fscanf_for_loop_72\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(vector countVector);\r\n\r\nvoid bad()\r\n{\r\n int count;\r\n vector countVector;\r\n /* Initialize count */\r\n count = -1;\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n /* Put count in a vector */\r\n countVector.insert(countVector.end(), 1, count);\r\n countVector.insert(countVector.end(), 1, count);\r\n countVector.insert(countVector.end(), 1, count);\r\n badSink(countVector);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(vector countVector);\r\n\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n vector countVector;\r\n /* Initialize count */\r\n count = -1;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n /* Put count in a vector */\r\n countVector.insert(countVector.end(), 1, count);\r\n countVector.insert(countVector.end(), 1, count);\r\n countVector.insert(countVector.end(), 1, count);\r\n goodG2BSink(countVector);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(vector countVector);\r\n\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n vector countVector;\r\n /* Initialize count */\r\n count = -1;\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n countVector.insert(countVector.end(), 1, count);\r\n countVector.insert(countVector.end(), 1, count);\r\n countVector.insert(countVector.end(), 1, count);\r\n goodB2GSink(countVector);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE400_Resource_Exhaustion__fscanf_for_loop_72; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_for_loop_81a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_for_loop_81a.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":2926,"source_sha256":"60df0efb7be6454c9580c484d451022c19d7f3c9d2c010abd27b4073dfcc5a28","line_count":101,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fscanf_for_loop_81a.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-81a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE400_Resource_Exhaustion__fscanf_for_loop_81.h\"\r\n\r\nnamespace CWE400_Resource_Exhaustion__fscanf_for_loop_81\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n const CWE400_Resource_Exhaustion__fscanf_for_loop_81_base& baseObject = CWE400_Resource_Exhaustion__fscanf_for_loop_81_bad();\r\n baseObject.action(count);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n const CWE400_Resource_Exhaustion__fscanf_for_loop_81_base& baseObject = CWE400_Resource_Exhaustion__fscanf_for_loop_81_goodG2B();\r\n baseObject.action(count);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n const CWE400_Resource_Exhaustion__fscanf_for_loop_81_base& baseObject = CWE400_Resource_Exhaustion__fscanf_for_loop_81_goodB2G();\r\n baseObject.action(count);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE400_Resource_Exhaustion__fscanf_for_loop_81; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_fwrite_68b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_fwrite_68b.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":3603,"source_sha256":"917b51a31ab490cccafe4abc2c2c53d4672851068e0038788c7ce44a8f41362b","line_count":117,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fscanf_fwrite_68b.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-68b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 68 Data flow: data passed as a global variable from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#define SENTENCE \"This is the sentence we are printing to the file. \"\r\n\r\nextern int CWE400_Resource_Exhaustion__fscanf_fwrite_68_badData;\r\nextern int CWE400_Resource_Exhaustion__fscanf_fwrite_68_goodG2BData;\r\nextern int CWE400_Resource_Exhaustion__fscanf_fwrite_68_goodB2GData;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_fwrite_68b_badSink()\r\n{\r\n int count = CWE400_Resource_Exhaustion__fscanf_fwrite_68_badData;\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE400_Resource_Exhaustion__fscanf_fwrite_68b_goodG2BSink()\r\n{\r\n int count = CWE400_Resource_Exhaustion__fscanf_fwrite_68_goodG2BData;\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE400_Resource_Exhaustion__fscanf_fwrite_68b_goodB2GSink()\r\n{\r\n int count = CWE400_Resource_Exhaustion__fscanf_fwrite_68_goodB2GData;\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_good.txt\";\r\n /* FIX: Validate count before using it as the for loop variant to write to a file */\r\n if (count > 0 && count <= 20)\r\n {\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile)) exit(1);\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_fwrite_82_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_fwrite_82_goodB2G.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":1695,"source_sha256":"64797c799aaf8e67c4b8ce393b226d817535c528b6323cb78ae2f01e9793d379","line_count":54,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fscanf_fwrite_82_goodB2G.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-82_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE400_Resource_Exhaustion__fscanf_fwrite_82.h\"\r\n\r\n#define SENTENCE \"This is the sentence we are printing to the file. \"\r\n\r\nnamespace CWE400_Resource_Exhaustion__fscanf_fwrite_82\r\n{\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_fwrite_82_goodB2G::action(int count)\r\n{\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_good.txt\";\r\n /* FIX: Validate count before using it as the for loop variant to write to a file */\r\n if (count > 0 && count <= 20)\r\n {\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile)) exit(1);\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_sleep_06.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_sleep_06.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":5227,"source_sha256":"0d5d278da35f9eca4bf9e319dc9b5fb20290ac363ba6fe0c5c2fb19c9830cc45","line_count":191,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fscanf_sleep_06.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-06.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: sleep\r\n * GoodSink: Validate count before using it as a parameter in sleep function\r\n * BadSink : Use count as the parameter for sleep withhout checking it's size first\r\n * Flow Variant: 06 Control flow: if(STATIC_CONST_FIVE==5) and if(STATIC_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#define SLEEP Sleep\r\n#else\r\n#include \r\n#define SLEEP usleep\r\n#endif\r\n\r\n/* The variable below is declared \"const\", so a tool should be able\r\n to identify that reads of this will always give its initialized\r\n value. */\r\nstatic const int STATIC_CONST_FIVE = 5;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_sleep_06_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Sleep function using count as the parameter with no validation */\r\n SLEEP(count);\r\n printLine(\"Sleep time possibly too long\");\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodB2G1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n }\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Validate count before using it as a parameter in the sleep function */\r\n if (count > 0 && count <= 2000)\r\n {\r\n SLEEP(count);\r\n printLine(\"Sleep time OK\");\r\n }\r\n else\r\n {\r\n printLine(\"Sleep time too long\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: Validate count before using it as a parameter in the sleep function */\r\n if (count > 0 && count <= 2000)\r\n {\r\n SLEEP(count);\r\n printLine(\"Sleep time OK\");\r\n }\r\n else\r\n {\r\n printLine(\"Sleep time too long\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Sleep function using count as the parameter with no validation */\r\n SLEEP(count);\r\n printLine(\"Sleep time possibly too long\");\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Sleep function using count as the parameter with no validation */\r\n SLEEP(count);\r\n printLine(\"Sleep time possibly too long\");\r\n }\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_sleep_06_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__fscanf_sleep_06_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__fscanf_sleep_06_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_sleep_07.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_sleep_07.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":5144,"source_sha256":"e82f25e1509ba8b1f6b6149e052604bd5dfd12f8ee43fdef86e408189300efc2","line_count":191,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fscanf_sleep_07.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-07.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: sleep\r\n * GoodSink: Validate count before using it as a parameter in sleep function\r\n * BadSink : Use count as the parameter for sleep withhout checking it's size first\r\n * Flow Variant: 07 Control flow: if(staticFive==5) and if(staticFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#define SLEEP Sleep\r\n#else\r\n#include \r\n#define SLEEP usleep\r\n#endif\r\n\r\n/* The variable below is not declared \"const\", but is never assigned\r\n any other value so a tool should be able to identify that reads of\r\n this will always give its initialized value. */\r\nstatic int staticFive = 5;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_sleep_07_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Sleep function using count as the parameter with no validation */\r\n SLEEP(count);\r\n printLine(\"Sleep time possibly too long\");\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticFive==5 to staticFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n }\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Validate count before using it as a parameter in the sleep function */\r\n if (count > 0 && count <= 2000)\r\n {\r\n SLEEP(count);\r\n printLine(\"Sleep time OK\");\r\n }\r\n else\r\n {\r\n printLine(\"Sleep time too long\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n }\r\n if(staticFive==5)\r\n {\r\n /* FIX: Validate count before using it as a parameter in the sleep function */\r\n if (count > 0 && count <= 2000)\r\n {\r\n SLEEP(count);\r\n printLine(\"Sleep time OK\");\r\n }\r\n else\r\n {\r\n printLine(\"Sleep time too long\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticFive==5 to staticFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Sleep function using count as the parameter with no validation */\r\n SLEEP(count);\r\n printLine(\"Sleep time possibly too long\");\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(staticFive==5)\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Sleep function using count as the parameter with no validation */\r\n SLEEP(count);\r\n printLine(\"Sleep time possibly too long\");\r\n }\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_sleep_07_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__fscanf_sleep_07_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__fscanf_sleep_07_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_sleep_10.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_sleep_10.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":4878,"source_sha256":"a92b60b14144bcb5c1f426de9bed77b70ef1fcbac10716e04ea80e374b6051e5","line_count":186,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fscanf_sleep_10.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-10.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: sleep\r\n * GoodSink: Validate count before using it as a parameter in sleep function\r\n * BadSink : Use count as the parameter for sleep withhout checking it's size first\r\n * Flow Variant: 10 Control flow: if(globalTrue) and if(globalFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#define SLEEP Sleep\r\n#else\r\n#include \r\n#define SLEEP usleep\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_sleep_10_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Sleep function using count as the parameter with no validation */\r\n SLEEP(count);\r\n printLine(\"Sleep time possibly too long\");\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalTrue to globalFalse */\r\nstatic void goodB2G1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n }\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Validate count before using it as a parameter in the sleep function */\r\n if (count > 0 && count <= 2000)\r\n {\r\n SLEEP(count);\r\n printLine(\"Sleep time OK\");\r\n }\r\n else\r\n {\r\n printLine(\"Sleep time too long\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n }\r\n if(globalTrue)\r\n {\r\n /* FIX: Validate count before using it as a parameter in the sleep function */\r\n if (count > 0 && count <= 2000)\r\n {\r\n SLEEP(count);\r\n printLine(\"Sleep time OK\");\r\n }\r\n else\r\n {\r\n printLine(\"Sleep time too long\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalTrue to globalFalse */\r\nstatic void goodG2B1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Sleep function using count as the parameter with no validation */\r\n SLEEP(count);\r\n printLine(\"Sleep time possibly too long\");\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalTrue)\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Sleep function using count as the parameter with no validation */\r\n SLEEP(count);\r\n printLine(\"Sleep time possibly too long\");\r\n }\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_sleep_10_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__fscanf_sleep_10_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__fscanf_sleep_10_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_sleep_34.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_sleep_34.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":3732,"source_sha256":"0b59d7e09e3b2eacf73fe53d2dfafc78044474a4e0aa8fe59779ab085ce2c970","line_count":131,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fscanf_sleep_34.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-34.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: sleep\r\n * GoodSink: Validate count before using it as a parameter in sleep function\r\n * BadSink : Use count as the parameter for sleep withhout checking it's size first\r\n * Flow Variant: 34 Data flow: use of a union containing two methods of accessing the same data (within the same function)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#define SLEEP Sleep\r\n#else\r\n#include \r\n#define SLEEP usleep\r\n#endif\r\n\r\ntypedef union\r\n{\r\n int unionFirst;\r\n int unionSecond;\r\n} CWE400_Resource_Exhaustion__fscanf_sleep_34_unionType;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_sleep_34_bad()\r\n{\r\n int count;\r\n CWE400_Resource_Exhaustion__fscanf_sleep_34_unionType myUnion;\r\n /* Initialize count */\r\n count = -1;\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n myUnion.unionFirst = count;\r\n {\r\n int count = myUnion.unionSecond;\r\n /* POTENTIAL FLAW: Sleep function using count as the parameter with no validation */\r\n SLEEP(count);\r\n printLine(\"Sleep time possibly too long\");\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n CWE400_Resource_Exhaustion__fscanf_sleep_34_unionType myUnion;\r\n /* Initialize count */\r\n count = -1;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n myUnion.unionFirst = count;\r\n {\r\n int count = myUnion.unionSecond;\r\n /* POTENTIAL FLAW: Sleep function using count as the parameter with no validation */\r\n SLEEP(count);\r\n printLine(\"Sleep time possibly too long\");\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n CWE400_Resource_Exhaustion__fscanf_sleep_34_unionType myUnion;\r\n /* Initialize count */\r\n count = -1;\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n myUnion.unionFirst = count;\r\n {\r\n int count = myUnion.unionSecond;\r\n /* FIX: Validate count before using it as a parameter in the sleep function */\r\n if (count > 0 && count <= 2000)\r\n {\r\n SLEEP(count);\r\n printLine(\"Sleep time OK\");\r\n }\r\n else\r\n {\r\n printLine(\"Sleep time too long\");\r\n }\r\n }\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_sleep_34_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__fscanf_sleep_34_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__fscanf_sleep_34_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_sleep_54b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_sleep_54b.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":1782,"source_sha256":"064470e636a1dd3185727f7d395bc1422d12125bc95fdc4fea5ee3efce557256","line_count":58,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fscanf_sleep_54b.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-54b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: sleep\r\n * GoodSink: Validate count before using it as a parameter in sleep function\r\n * BadSink : Use count as the parameter for sleep withhout checking it's size first\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#define SLEEP Sleep\r\n#else\r\n#include \r\n#define SLEEP usleep\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE400_Resource_Exhaustion__fscanf_sleep_54c_badSink(int count);\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_sleep_54b_badSink(int count)\r\n{\r\n CWE400_Resource_Exhaustion__fscanf_sleep_54c_badSink(count);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE400_Resource_Exhaustion__fscanf_sleep_54c_goodG2BSink(int count);\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_sleep_54b_goodG2BSink(int count)\r\n{\r\n CWE400_Resource_Exhaustion__fscanf_sleep_54c_goodG2BSink(count);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE400_Resource_Exhaustion__fscanf_sleep_54c_goodB2GSink(int count);\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_sleep_54b_goodB2GSink(int count)\r\n{\r\n CWE400_Resource_Exhaustion__fscanf_sleep_54c_goodB2GSink(count);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_sleep_64a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_sleep_64a.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":2989,"source_sha256":"47e9e949b9e40198a6b0631fa1a6f51ec346db9f6fc93dc48cf9c10ea9691747","line_count":105,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fscanf_sleep_64a.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-64a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: sleep\r\n * GoodSink: Validate count before using it as a parameter in sleep function\r\n * BadSink : Use count as the parameter for sleep withhout checking it's size first\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#define SLEEP Sleep\r\n#else\r\n#include \r\n#define SLEEP usleep\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE400_Resource_Exhaustion__fscanf_sleep_64b_badSink(void * countVoidPtr);\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_sleep_64_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n CWE400_Resource_Exhaustion__fscanf_sleep_64b_badSink(&count);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE400_Resource_Exhaustion__fscanf_sleep_64b_goodG2BSink(void * countVoidPtr);\r\n\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n CWE400_Resource_Exhaustion__fscanf_sleep_64b_goodG2BSink(&count);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE400_Resource_Exhaustion__fscanf_sleep_64b_goodB2GSink(void * countVoidPtr);\r\n\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n CWE400_Resource_Exhaustion__fscanf_sleep_64b_goodB2GSink(&count);\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_sleep_64_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__fscanf_sleep_64_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__fscanf_sleep_64_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_sleep_66a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_sleep_66a.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":3174,"source_sha256":"5a6c5dd4e71bfb226d2b34ce98434aa4781837946be74b3c3f38eb5cfe2ce7fe","line_count":112,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fscanf_sleep_66a.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-66a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: sleep\r\n * GoodSink: Validate count before using it as a parameter in sleep function\r\n * BadSink : Use count as the parameter for sleep withhout checking it's size first\r\n * Flow Variant: 66 Data flow: data passed in an array from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#define SLEEP Sleep\r\n#else\r\n#include \r\n#define SLEEP usleep\r\n#endif\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE400_Resource_Exhaustion__fscanf_sleep_66b_badSink(int countArray[]);\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_sleep_66_bad()\r\n{\r\n int count;\r\n int countArray[5];\r\n /* Initialize count */\r\n count = -1;\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n /* put count in array */\r\n countArray[2] = count;\r\n CWE400_Resource_Exhaustion__fscanf_sleep_66b_badSink(countArray);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE400_Resource_Exhaustion__fscanf_sleep_66b_goodG2BSink(int countArray[]);\r\n\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n int countArray[5];\r\n /* Initialize count */\r\n count = -1;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n countArray[2] = count;\r\n CWE400_Resource_Exhaustion__fscanf_sleep_66b_goodG2BSink(countArray);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE400_Resource_Exhaustion__fscanf_sleep_66b_goodB2GSink(int countArray[]);\r\n\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n int countArray[5];\r\n /* Initialize count */\r\n count = -1;\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n countArray[2] = count;\r\n CWE400_Resource_Exhaustion__fscanf_sleep_66b_goodB2GSink(countArray);\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__fscanf_sleep_66_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__fscanf_sleep_66_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__fscanf_sleep_66_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_sleep_72a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_sleep_72a.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":3333,"source_sha256":"027f9ea15c558029f5f069a166daf71daa733570cffd5fbcddb7f7da77fe5716","line_count":121,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fscanf_sleep_72a.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-72a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: sleep\r\n * GoodSink: Validate count before using it as a parameter in sleep function\r\n * BadSink : Use count as the parameter for sleep withhout checking it's size first\r\n * Flow Variant: 72 Data flow: data passed in a vector from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE400_Resource_Exhaustion__fscanf_sleep_72\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(vector countVector);\r\n\r\nvoid bad()\r\n{\r\n int count;\r\n vector countVector;\r\n /* Initialize count */\r\n count = -1;\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n /* Put count in a vector */\r\n countVector.insert(countVector.end(), 1, count);\r\n countVector.insert(countVector.end(), 1, count);\r\n countVector.insert(countVector.end(), 1, count);\r\n badSink(countVector);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(vector countVector);\r\n\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n vector countVector;\r\n /* Initialize count */\r\n count = -1;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n /* Put count in a vector */\r\n countVector.insert(countVector.end(), 1, count);\r\n countVector.insert(countVector.end(), 1, count);\r\n countVector.insert(countVector.end(), 1, count);\r\n goodG2BSink(countVector);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(vector countVector);\r\n\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n vector countVector;\r\n /* Initialize count */\r\n count = -1;\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n countVector.insert(countVector.end(), 1, count);\r\n countVector.insert(countVector.end(), 1, count);\r\n countVector.insert(countVector.end(), 1, count);\r\n goodB2GSink(countVector);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE400_Resource_Exhaustion__fscanf_sleep_72; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_sleep_72b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_sleep_72b.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":2026,"source_sha256":"ef110c5605bc8cd463dba86a6ce5e4563a23257ad7c2871b2e62274969982605","line_count":76,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fscanf_sleep_72b.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-72b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: sleep\r\n * GoodSink: Validate count before using it as a parameter in sleep function\r\n * BadSink : Use count as the parameter for sleep withhout checking it's size first\r\n * Flow Variant: 72 Data flow: data passed in a vector from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#ifdef _WIN32\r\n#include \r\n#define SLEEP Sleep\r\n#else\r\n#include \r\n#define SLEEP usleep\r\n#endif\r\n\r\nusing namespace std;\r\n\r\nnamespace CWE400_Resource_Exhaustion__fscanf_sleep_72\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(vector countVector)\r\n{\r\n /* copy count out of countVector */\r\n int count = countVector[2];\r\n /* POTENTIAL FLAW: Sleep function using count as the parameter with no validation */\r\n SLEEP(count);\r\n printLine(\"Sleep time possibly too long\");\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(vector countVector)\r\n{\r\n int count = countVector[2];\r\n /* POTENTIAL FLAW: Sleep function using count as the parameter with no validation */\r\n SLEEP(count);\r\n printLine(\"Sleep time possibly too long\");\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(vector countVector)\r\n{\r\n int count = countVector[2];\r\n /* FIX: Validate count before using it as a parameter in the sleep function */\r\n if (count > 0 && count <= 2000)\r\n {\r\n SLEEP(count);\r\n printLine(\"Sleep time OK\");\r\n }\r\n else\r\n {\r\n printLine(\"Sleep time too long\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_sleep_81.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_sleep_81.h","cwe_id":"CWE400","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1520,"source_sha256":"734b22ab8b41531cebe9635732a01c489edf67a227c089491b05cd501815285d","line_count":56,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fscanf_sleep_81.h\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-81.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: sleep\r\n * GoodSink: Validate count before using it as a parameter in sleep function\r\n * BadSink : Use count as the parameter for sleep withhout checking it's size first\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE400_Resource_Exhaustion__fscanf_sleep_81\r\n{\r\n\r\nclass CWE400_Resource_Exhaustion__fscanf_sleep_81_base\r\n{\r\npublic:\r\n /* pure virtual function */\r\n virtual void action(int count) const = 0;\r\n};\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE400_Resource_Exhaustion__fscanf_sleep_81_bad : public CWE400_Resource_Exhaustion__fscanf_sleep_81_base\r\n{\r\npublic:\r\n void action(int count) const;\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE400_Resource_Exhaustion__fscanf_sleep_81_goodG2B : public CWE400_Resource_Exhaustion__fscanf_sleep_81_base\r\n{\r\npublic:\r\n void action(int count) const;\r\n};\r\n\r\nclass CWE400_Resource_Exhaustion__fscanf_sleep_81_goodB2G : public CWE400_Resource_Exhaustion__fscanf_sleep_81_base\r\n{\r\npublic:\r\n void action(int count) const;\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_sleep_84_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__fscanf_sleep_84_goodB2G.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":1686,"source_sha256":"63a7fdcf881b745cc0a160023418414d1e877f2cbd143e55383215ede5b61221","line_count":53,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__fscanf_sleep_84_goodB2G.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-84_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: fscanf Read data from the console using fscanf()\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: sleep\r\n * GoodSink: Validate count before using it as a parameter in sleep function\r\n * BadSink : Use count as the parameter for sleep withhout checking it's size first\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE400_Resource_Exhaustion__fscanf_sleep_84.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#define SLEEP Sleep\r\n#else\r\n#include \r\n#define SLEEP usleep\r\n#endif\r\n\r\nnamespace CWE400_Resource_Exhaustion__fscanf_sleep_84\r\n{\r\nCWE400_Resource_Exhaustion__fscanf_sleep_84_goodB2G::CWE400_Resource_Exhaustion__fscanf_sleep_84_goodB2G(int countCopy)\r\n{\r\n count = countCopy;\r\n /* POTENTIAL FLAW: Read count from the console using fscanf() */\r\n fscanf(stdin, \"%d\", &count);\r\n}\r\n\r\nCWE400_Resource_Exhaustion__fscanf_sleep_84_goodB2G::~CWE400_Resource_Exhaustion__fscanf_sleep_84_goodB2G()\r\n{\r\n /* FIX: Validate count before using it as a parameter in the sleep function */\r\n if (count > 0 && count <= 2000)\r\n {\r\n SLEEP(count);\r\n printLine(\"Sleep time OK\");\r\n }\r\n else\r\n {\r\n printLine(\"Sleep time too long\");\r\n }\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_for_loop_07.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_for_loop_07.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":12749,"source_sha256":"8499257440b634aa58306f0d3d3e8453223a1421db5603d0225acab9fd09791f","line_count":423,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__listen_socket_for_loop_07.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-07.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 07 Control flow: if(staticFive==5) and if(staticFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n/* The variable below is not declared \"const\", but is never assigned\r\n any other value so a tool should be able to identify that reads of\r\n this will always give its initialized value. */\r\nstatic int staticFive = 5;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__listen_socket_for_loop_07_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(staticFive==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(staticFive==5)\r\n {\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticFive==5 to staticFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(staticFive==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n {\r\n size_t i = 0;\r\n /* FIX: Validate count before using it as the for loop variant */\r\n if (count > 0 && count <= 20)\r\n {\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(staticFive==5)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(staticFive==5)\r\n {\r\n {\r\n size_t i = 0;\r\n /* FIX: Validate count before using it as the for loop variant */\r\n if (count > 0 && count <= 20)\r\n {\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticFive==5 to staticFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(staticFive==5)\r\n {\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(staticFive==5)\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(staticFive==5)\r\n {\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__listen_socket_for_loop_07_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__listen_socket_for_loop_07_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__listen_socket_for_loop_07_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_for_loop_51b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_for_loop_51b.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":2447,"source_sha256":"03972465a5864f6a423c4747a139bccef81869bea586755659cb11d7f54f5317","line_count":89,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__listen_socket_for_loop_51b.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-51b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 51 Data flow: data passed as an argument from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__listen_socket_for_loop_51b_badSink(int count)\r\n{\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE400_Resource_Exhaustion__listen_socket_for_loop_51b_goodG2BSink(int count)\r\n{\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE400_Resource_Exhaustion__listen_socket_for_loop_51b_goodB2GSink(int count)\r\n{\r\n {\r\n size_t i = 0;\r\n /* FIX: Validate count before using it as the for loop variant */\r\n if (count > 0 && count <= 20)\r\n {\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_for_loop_52c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_for_loop_52c.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":2464,"source_sha256":"b67b92b58eb46ac45de5c3784c0d56ab6c38752547da851a767879ccb634a666","line_count":89,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__listen_socket_for_loop_52c.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-52c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__listen_socket_for_loop_52c_badSink(int count)\r\n{\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE400_Resource_Exhaustion__listen_socket_for_loop_52c_goodG2BSink(int count)\r\n{\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE400_Resource_Exhaustion__listen_socket_for_loop_52c_goodB2GSink(int count)\r\n{\r\n {\r\n size_t i = 0;\r\n /* FIX: Validate count before using it as the for loop variant */\r\n if (count > 0 && count <= 20)\r\n {\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_for_loop_81_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_for_loop_81_bad.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":1223,"source_sha256":"a19dee97604f21cb14f6653c018b07b53b50a3743536d0bf655b246840065002","line_count":38,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__listen_socket_for_loop_81_bad.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-81_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE400_Resource_Exhaustion__listen_socket_for_loop_81.h\"\r\n\r\nnamespace CWE400_Resource_Exhaustion__listen_socket_for_loop_81\r\n{\r\n\r\nvoid CWE400_Resource_Exhaustion__listen_socket_for_loop_81_bad::action(int count) const\r\n{\r\n {\r\n size_t i = 0;\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_for_loop_81_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_for_loop_81_goodB2G.cpp","cwe_id":"CWE400","file_extension":".cpp","language_hint":"C++","source_size_bytes":1299,"source_sha256":"daef906e50f03745ba0727340e8fd30db8649245927c51249876ab673ab8795c","line_count":41,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__listen_socket_for_loop_81_goodB2G.cpp\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-81_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: for_loop\r\n * GoodSink: Validate count before using it as the loop variant in a for loop\r\n * BadSink : Use count as the loop variant in a for loop\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE400_Resource_Exhaustion__listen_socket_for_loop_81.h\"\r\n\r\nnamespace CWE400_Resource_Exhaustion__listen_socket_for_loop_81\r\n{\r\n\r\nvoid CWE400_Resource_Exhaustion__listen_socket_for_loop_81_goodB2G::action(int count) const\r\n{\r\n {\r\n size_t i = 0;\r\n /* FIX: Validate count before using it as the for loop variant */\r\n if (count > 0 && count <= 20)\r\n {\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n printLine(\"Hello\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_fwrite_10.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_fwrite_10.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":14878,"source_sha256":"acbe71e44e7660cef735d99c251f64874bb0001bcdba0cb440bfc240ddee4a39","line_count":487,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__listen_socket_fwrite_10.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-10.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 10 Control flow: if(globalTrue) and if(globalFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#define SENTENCE \"This is the sentence we are printing to the file. \"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_10_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalTrue)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalTrue)\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalTrue to globalFalse */\r\nstatic void goodB2G1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalTrue)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_good.txt\";\r\n /* FIX: Validate count before using it as the for loop variant to write to a file */\r\n if (count > 0 && count <= 20)\r\n {\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile)) exit(1);\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalTrue)\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalTrue)\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_good.txt\";\r\n /* FIX: Validate count before using it as the for loop variant to write to a file */\r\n if (count > 0 && count <= 20)\r\n {\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile)) exit(1);\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalTrue to globalFalse */\r\nstatic void goodG2B1()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(globalTrue)\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalTrue)\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(globalTrue)\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_10_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__listen_socket_fwrite_10_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__listen_socket_fwrite_10_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_fwrite_12.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_fwrite_12.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":15293,"source_sha256":"9ad17f734d6a0e3997ecc2c5ef18489c7fcc584402d82121cc90920530de15e0","line_count":493,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__listen_socket_fwrite_12.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-12.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#define SENTENCE \"This is the sentence we are printing to the file. \"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_12_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n else\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n else\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_good.txt\";\r\n /* FIX: Validate count before using it as the for loop variant to write to a file */\r\n if (count > 0 && count <= 20)\r\n {\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile)) exit(1);\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the first \"if\" so that\r\n both branches use the BadSource and the second \"if\" so that both branches\r\n use the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n else\r\n {\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_good.txt\";\r\n /* FIX: Validate count before using it as the for loop variant to write to a file */\r\n if (count > 0 && count <= 20)\r\n {\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile)) exit(1);\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n }\r\n else\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_good.txt\";\r\n /* FIX: Validate count before using it as the for loop variant to write to a file */\r\n if (count > 0 && count <= 20)\r\n {\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile)) exit(1);\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the first \"if\" so that\r\n both branches use the GoodSource and the second \"if\" so that both branches\r\n use the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n else\r\n {\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n else\r\n {\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_12_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__listen_socket_fwrite_12_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__listen_socket_fwrite_12_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_fwrite_18.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_fwrite_18.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":9226,"source_sha256":"310a1cfd8f235e8d4a0916bdaf6e35730b5821a6f9c43d3fdfcd2813ca7902bb","line_count":325,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__listen_socket_fwrite_18.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-18.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 18 Control flow: goto statements\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#define SENTENCE \"This is the sentence we are printing to the file. \"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_18_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n goto source;\r\nsource:\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n goto sink;\r\nsink:\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by reversing the blocks on the second goto statement */\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n goto source;\r\nsource:\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n goto sink;\r\nsink:\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_good.txt\";\r\n /* FIX: Validate count before using it as the for loop variant to write to a file */\r\n if (count > 0 && count <= 20)\r\n {\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile)) exit(1);\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by reversing the blocks on the first goto statement */\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n goto source;\r\nsource:\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n goto sink;\r\nsink:\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_18_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__listen_socket_fwrite_18_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__listen_socket_fwrite_18_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_fwrite_42.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_fwrite_42.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":9320,"source_sha256":"2e47931dcb4b91aefe5d9bcd9857badc8a155a6106008b801c27ff99bc8244d7","line_count":331,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__listen_socket_fwrite_42.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-42.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 42 Data flow: data returned from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#define SENTENCE \"This is the sentence we are printing to the file. \"\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic int badSource(int count)\r\n{\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n return count;\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_42_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n count = badSource(count);\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic int goodG2BSource(int count)\r\n{\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n return count;\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n count = goodG2BSource(count);\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic int goodB2GSource(int count)\r\n{\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n return count;\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n count = goodB2GSource(count);\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_good.txt\";\r\n /* FIX: Validate count before using it as the for loop variant to write to a file */\r\n if (count > 0 && count <= 20)\r\n {\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile)) exit(1);\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_42_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__listen_socket_fwrite_42_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__listen_socket_fwrite_42_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_fwrite_52a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_fwrite_52a.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":7582,"source_sha256":"cd84003fcbf2b598c530883b79b1c025fa1d6ca51db6d126cd5d8555d16bc730","line_count":255,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__listen_socket_fwrite_52a.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-52a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#define SENTENCE \"This is the sentence we are printing to the file. \"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_52b_badSink(int count);\r\n\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_52_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE400_Resource_Exhaustion__listen_socket_fwrite_52b_badSink(count);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_52b_goodG2BSink(int count);\r\n\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n CWE400_Resource_Exhaustion__listen_socket_fwrite_52b_goodG2BSink(count);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_52b_goodB2GSink(int count);\r\n\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE400_Resource_Exhaustion__listen_socket_fwrite_52b_goodB2GSink(count);\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_52_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__listen_socket_fwrite_52_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__listen_socket_fwrite_52_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_fwrite_52b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_fwrite_52b.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":2290,"source_sha256":"cedcfa1f02404dcfad61b2e2e3956a85bbb09368c363d7f33281eb129b7f6b71","line_count":74,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__listen_socket_fwrite_52b.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-52b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#define SENTENCE \"This is the sentence we are printing to the file. \"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_52c_badSink(int count);\r\n\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_52b_badSink(int count)\r\n{\r\n CWE400_Resource_Exhaustion__listen_socket_fwrite_52c_badSink(count);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_52c_goodG2BSink(int count);\r\n\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_52b_goodG2BSink(int count)\r\n{\r\n CWE400_Resource_Exhaustion__listen_socket_fwrite_52c_goodG2BSink(count);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_52c_goodB2GSink(int count);\r\n\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_52b_goodB2GSink(int count)\r\n{\r\n CWE400_Resource_Exhaustion__listen_socket_fwrite_52c_goodB2GSink(count);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_fwrite_54a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_fwrite_54a.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":7610,"source_sha256":"303cb7401944d893f7a17cad42515b5e6f6cbcb93543a25f1772c04842b31de6","line_count":255,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__listen_socket_fwrite_54a.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-54a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#define SENTENCE \"This is the sentence we are printing to the file. \"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_54b_badSink(int count);\r\n\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_54_bad()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE400_Resource_Exhaustion__listen_socket_fwrite_54b_badSink(count);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_54b_goodG2BSink(int count);\r\n\r\nstatic void goodG2B()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n /* FIX: Use a relatively small number */\r\n count = 20;\r\n CWE400_Resource_Exhaustion__listen_socket_fwrite_54b_goodG2BSink(count);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_54b_goodB2GSink(int count);\r\n\r\nstatic void goodB2G()\r\n{\r\n int count;\r\n /* Initialize count */\r\n count = -1;\r\n {\r\n#ifdef _WIN32\r\n WSADATA wsaData;\r\n int wsaDataInit = 0;\r\n#endif\r\n int recvResult;\r\n struct sockaddr_in service;\r\n SOCKET listenSocket = INVALID_SOCKET;\r\n SOCKET acceptSocket = INVALID_SOCKET;\r\n char inputBuffer[CHAR_ARRAY_SIZE];\r\n do\r\n {\r\n#ifdef _WIN32\r\n if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)\r\n {\r\n break;\r\n }\r\n wsaDataInit = 1;\r\n#endif\r\n /* POTENTIAL FLAW: Read count using a listen socket */\r\n listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);\r\n if (listenSocket == INVALID_SOCKET)\r\n {\r\n break;\r\n }\r\n memset(&service, 0, sizeof(service));\r\n service.sin_family = AF_INET;\r\n service.sin_addr.s_addr = INADDR_ANY;\r\n service.sin_port = htons(TCP_PORT);\r\n if (bind(listenSocket, (struct sockaddr*)&service, sizeof(service)) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n if (listen(listenSocket, LISTEN_BACKLOG) == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n acceptSocket = accept(listenSocket, NULL, NULL);\r\n if (acceptSocket == SOCKET_ERROR)\r\n {\r\n break;\r\n }\r\n /* Abort on error or the connection was closed */\r\n recvResult = recv(acceptSocket, inputBuffer, CHAR_ARRAY_SIZE - 1, 0);\r\n if (recvResult == SOCKET_ERROR || recvResult == 0)\r\n {\r\n break;\r\n }\r\n /* NUL-terminate the string */\r\n inputBuffer[recvResult] = '\\0';\r\n /* Convert to int */\r\n count = atoi(inputBuffer);\r\n }\r\n while (0);\r\n if (listenSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(listenSocket);\r\n }\r\n if (acceptSocket != INVALID_SOCKET)\r\n {\r\n CLOSE_SOCKET(acceptSocket);\r\n }\r\n#ifdef _WIN32\r\n if (wsaDataInit)\r\n {\r\n WSACleanup();\r\n }\r\n#endif\r\n }\r\n CWE400_Resource_Exhaustion__listen_socket_fwrite_54b_goodB2GSink(count);\r\n}\r\n\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_54_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE400_Resource_Exhaustion__listen_socket_fwrite_54_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE400_Resource_Exhaustion__listen_socket_fwrite_54_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_fwrite_63b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_fwrite_63b.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":3851,"source_sha256":"9cdaf8d4f0c67cb42691e4fba0f157b1199215d4212366db5e40c480caddb305","line_count":135,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__listen_socket_fwrite_63b.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-63b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#define SENTENCE \"This is the sentence we are printing to the file. \"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_63b_badSink(int * countPtr)\r\n{\r\n int count = *countPtr;\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_63b_goodG2BSink(int * countPtr)\r\n{\r\n int count = *countPtr;\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_63b_goodB2GSink(int * countPtr)\r\n{\r\n int count = *countPtr;\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_good.txt\";\r\n /* FIX: Validate count before using it as the for loop variant to write to a file */\r\n if (count > 0 && count <= 20)\r\n {\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile)) exit(1);\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_fwrite_67b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE400_Resource_Exhaustion/s01/CWE400_Resource_Exhaustion__listen_socket_fwrite_67b.c","cwe_id":"CWE400","file_extension":".c","language_hint":"C","source_size_bytes":4230,"source_sha256":"a3027856eb5f5985658fcee40727164f4f97abf9d784571079aa0b1271f941ce","line_count":140,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE400_Resource_Exhaustion__listen_socket_fwrite_67b.c\r\nLabel Definition File: CWE400_Resource_Exhaustion.label.xml\r\nTemplate File: sources-sinks-67b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 400 Resource Exhaustion\r\n * BadSource: listen_socket Read data using a listen socket (server side)\r\n * GoodSource: Assign count to be a relatively small number\r\n * Sinks: fwrite\r\n * GoodSink: Write to a file count number of times, but first validate count\r\n * BadSink : Write to a file count number of times\r\n * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifdef _WIN32\r\n#include \r\n#include \r\n#include \r\n#pragma comment(lib, \"ws2_32\") /* include ws2_32.lib when linking */\r\n#define CLOSE_SOCKET closesocket\r\n#else\r\n#include \r\n#include \r\n#include \r\n#include \r\n#include \r\n#define INVALID_SOCKET -1\r\n#define SOCKET_ERROR -1\r\n#define CLOSE_SOCKET close\r\n#define SOCKET int\r\n#endif\r\n\r\n#define TCP_PORT 27015\r\n#define LISTEN_BACKLOG 5\r\n#define CHAR_ARRAY_SIZE (3 * sizeof(count) + 2)\r\n\r\n#define SENTENCE \"This is the sentence we are printing to the file. \"\r\n\r\ntypedef struct _CWE400_Resource_Exhaustion__listen_socket_fwrite_67_structType\r\n{\r\n int structFirst;\r\n} CWE400_Resource_Exhaustion__listen_socket_fwrite_67_structType;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_67b_badSink(CWE400_Resource_Exhaustion__listen_socket_fwrite_67_structType myStruct)\r\n{\r\n int count = myStruct.structFirst;\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_67b_goodG2BSink(CWE400_Resource_Exhaustion__listen_socket_fwrite_67_structType myStruct)\r\n{\r\n int count = myStruct.structFirst;\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_bad.txt\";\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n /* POTENTIAL FLAW: For loop using count as the loop variant and no validation\r\n * This can cause a file to become very large */\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile))\r\n {\r\n exit(1);\r\n }\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE400_Resource_Exhaustion__listen_socket_fwrite_67b_goodB2GSink(CWE400_Resource_Exhaustion__listen_socket_fwrite_67_structType myStruct)\r\n{\r\n int count = myStruct.structFirst;\r\n {\r\n size_t i = 0;\r\n FILE *pFile = NULL;\r\n const char *filename = \"output_good.txt\";\r\n /* FIX: Validate count before using it as the for loop variant to write to a file */\r\n if (count > 0 && count <= 20)\r\n {\r\n pFile = fopen(filename, \"w+\");\r\n if (pFile == NULL)\r\n {\r\n exit(1);\r\n }\r\n for (i = 0; i < (size_t)count; i++)\r\n {\r\n if (strlen(SENTENCE) != fwrite(SENTENCE, sizeof(char), strlen(SENTENCE), pFile)) exit(1);\r\n }\r\n if (pFile)\r\n {\r\n fclose(pFile);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__strdup_wchar_t_08.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__strdup_wchar_t_08.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":5264,"source_sha256":"a4e891915116da2bcfe472e702128b49f9eaf638efe108e63446c671110726f0","line_count":193,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__strdup_wchar_t_08.c\r\nLabel Definition File: CWE401_Memory_Leak__strdup.label.xml\r\nTemplate File: sources-sinks-08.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: Allocate data using wcsdup()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 08 Control flow: if(staticReturnsTrue()) and if(staticReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The two function below always return the same value, so a tool\r\n should be able to identify that calls to the functions will always\r\n return a fixed value. */\r\nstatic int staticReturnsTrue()\r\n{\r\n return 1;\r\n}\r\n\r\nstatic int staticReturnsFalse()\r\n{\r\n return 0;\r\n}\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__strdup_wchar_t_08_bad()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n {\r\n wchar_t myString[] = L\"myString\";\r\n /* POTENTIAL FLAW: Allocate memory from the heap using a function that requires free() for deallocation */\r\n data = wcsdup(myString);\r\n /* Use data */\r\n printWLine(data);\r\n }\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: No deallocation of memory */\r\n /* no deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n {\r\n wchar_t myString[] = L\"myString\";\r\n /* POTENTIAL FLAW: Allocate memory from the heap using a function that requires free() for deallocation */\r\n data = wcsdup(myString);\r\n /* Use data */\r\n printWLine(data);\r\n }\r\n }\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Deallocate memory initialized in the source */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n {\r\n wchar_t myString[] = L\"myString\";\r\n /* POTENTIAL FLAW: Allocate memory from the heap using a function that requires free() for deallocation */\r\n data = wcsdup(myString);\r\n /* Use data */\r\n printWLine(data);\r\n }\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Deallocate memory initialized in the source */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize then use data */\r\n wcscpy(data, L\"a string\");\r\n printWLine(data);\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: No deallocation of memory */\r\n /* no deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize then use data */\r\n wcscpy(data, L\"a string\");\r\n printWLine(data);\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: No deallocation of memory */\r\n /* no deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\nvoid CWE401_Memory_Leak__strdup_wchar_t_08_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__strdup_wchar_t_08_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__strdup_wchar_t_08_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__strdup_wchar_t_12.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__strdup_wchar_t_12.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":4776,"source_sha256":"65f98590f867b17e8d61592b45e6590d9fc5f0975ceed6299126bc7743032297","line_count":171,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__strdup_wchar_t_12.c\r\nLabel Definition File: CWE401_Memory_Leak__strdup.label.xml\r\nTemplate File: sources-sinks-12.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: Allocate data using wcsdup()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__strdup_wchar_t_12_bad()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n wchar_t myString[] = L\"myString\";\r\n /* POTENTIAL FLAW: Allocate memory from the heap using a function that requires free() for deallocation */\r\n data = wcsdup(myString);\r\n /* Use data */\r\n printWLine(data);\r\n }\r\n }\r\n else\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize then use data */\r\n wcscpy(data, L\"a string\");\r\n printWLine(data);\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: No deallocation of memory */\r\n /* no deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n else\r\n {\r\n /* FIX: Deallocate memory initialized in the source */\r\n free(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the first \"if\" so that\r\n both branches use the BadSource and the second \"if\" so that both branches\r\n use the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n wchar_t myString[] = L\"myString\";\r\n /* POTENTIAL FLAW: Allocate memory from the heap using a function that requires free() for deallocation */\r\n data = wcsdup(myString);\r\n /* Use data */\r\n printWLine(data);\r\n }\r\n }\r\n else\r\n {\r\n {\r\n wchar_t myString[] = L\"myString\";\r\n /* POTENTIAL FLAW: Allocate memory from the heap using a function that requires free() for deallocation */\r\n data = wcsdup(myString);\r\n /* Use data */\r\n printWLine(data);\r\n }\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Deallocate memory initialized in the source */\r\n free(data);\r\n }\r\n else\r\n {\r\n /* FIX: Deallocate memory initialized in the source */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the first \"if\" so that\r\n both branches use the GoodSource and the second \"if\" so that both branches\r\n use the BadSink */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize then use data */\r\n wcscpy(data, L\"a string\");\r\n printWLine(data);\r\n }\r\n else\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize then use data */\r\n wcscpy(data, L\"a string\");\r\n printWLine(data);\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: No deallocation of memory */\r\n /* no deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n else\r\n {\r\n /* POTENTIAL FLAW: No deallocation of memory */\r\n /* no deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\nvoid CWE401_Memory_Leak__strdup_wchar_t_12_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__strdup_wchar_t_12_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__strdup_wchar_t_12_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__strdup_wchar_t_13.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__strdup_wchar_t_13.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":4997,"source_sha256":"3be477cf7f20ec5cc8a81327f71de1a7afb5237b3c1d0107d119db93b2860477","line_count":180,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__strdup_wchar_t_13.c\r\nLabel Definition File: CWE401_Memory_Leak__strdup.label.xml\r\nTemplate File: sources-sinks-13.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: Allocate data using wcsdup()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 13 Control flow: if(GLOBAL_CONST_FIVE==5) and if(GLOBAL_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__strdup_wchar_t_13_bad()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n {\r\n wchar_t myString[] = L\"myString\";\r\n /* POTENTIAL FLAW: Allocate memory from the heap using a function that requires free() for deallocation */\r\n data = wcsdup(myString);\r\n /* Use data */\r\n printWLine(data);\r\n }\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: No deallocation of memory */\r\n /* no deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_FIVE==5 to GLOBAL_CONST_FIVE!=5 */\r\nstatic void goodB2G1()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n {\r\n wchar_t myString[] = L\"myString\";\r\n /* POTENTIAL FLAW: Allocate memory from the heap using a function that requires free() for deallocation */\r\n data = wcsdup(myString);\r\n /* Use data */\r\n printWLine(data);\r\n }\r\n }\r\n if(GLOBAL_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Deallocate memory initialized in the source */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n {\r\n wchar_t myString[] = L\"myString\";\r\n /* POTENTIAL FLAW: Allocate memory from the heap using a function that requires free() for deallocation */\r\n data = wcsdup(myString);\r\n /* Use data */\r\n printWLine(data);\r\n }\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* FIX: Deallocate memory initialized in the source */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_FIVE==5 to GLOBAL_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize then use data */\r\n wcscpy(data, L\"a string\");\r\n printWLine(data);\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: No deallocation of memory */\r\n /* no deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize then use data */\r\n wcscpy(data, L\"a string\");\r\n printWLine(data);\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: No deallocation of memory */\r\n /* no deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\nvoid CWE401_Memory_Leak__strdup_wchar_t_13_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__strdup_wchar_t_13_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__strdup_wchar_t_13_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__strdup_wchar_t_14.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__strdup_wchar_t_14.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":4885,"source_sha256":"28378ee35f9672cd6fbd18658da3dfe2a1ed1b4684b126bad17be78827a39fda","line_count":180,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__strdup_wchar_t_14.c\r\nLabel Definition File: CWE401_Memory_Leak__strdup.label.xml\r\nTemplate File: sources-sinks-14.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: Allocate data using wcsdup()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 14 Control flow: if(globalFive==5) and if(globalFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__strdup_wchar_t_14_bad()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(globalFive==5)\r\n {\r\n {\r\n wchar_t myString[] = L\"myString\";\r\n /* POTENTIAL FLAW: Allocate memory from the heap using a function that requires free() for deallocation */\r\n data = wcsdup(myString);\r\n /* Use data */\r\n printWLine(data);\r\n }\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: No deallocation of memory */\r\n /* no deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalFive==5 to globalFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(globalFive==5)\r\n {\r\n {\r\n wchar_t myString[] = L\"myString\";\r\n /* POTENTIAL FLAW: Allocate memory from the heap using a function that requires free() for deallocation */\r\n data = wcsdup(myString);\r\n /* Use data */\r\n printWLine(data);\r\n }\r\n }\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Deallocate memory initialized in the source */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(globalFive==5)\r\n {\r\n {\r\n wchar_t myString[] = L\"myString\";\r\n /* POTENTIAL FLAW: Allocate memory from the heap using a function that requires free() for deallocation */\r\n data = wcsdup(myString);\r\n /* Use data */\r\n printWLine(data);\r\n }\r\n }\r\n if(globalFive==5)\r\n {\r\n /* FIX: Deallocate memory initialized in the source */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalFive==5 to globalFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize then use data */\r\n wcscpy(data, L\"a string\");\r\n printWLine(data);\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: No deallocation of memory */\r\n /* no deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(globalFive==5)\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize then use data */\r\n wcscpy(data, L\"a string\");\r\n printWLine(data);\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: No deallocation of memory */\r\n /* no deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\nvoid CWE401_Memory_Leak__strdup_wchar_t_14_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__strdup_wchar_t_14_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__strdup_wchar_t_14_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__strdup_wchar_t_32.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__strdup_wchar_t_32.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":3639,"source_sha256":"9c543e1267a8eff0d5709b8f4acd76460e3edd4fa74999b865090156578a2e9f","line_count":133,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__strdup_wchar_t_32.c\r\nLabel Definition File: CWE401_Memory_Leak__strdup.label.xml\r\nTemplate File: sources-sinks-32.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: Allocate data using wcsdup()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 32 Data flow using two pointers to the same value within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__strdup_wchar_t_32_bad()\r\n{\r\n wchar_t * data;\r\n wchar_t * *dataPtr1 = &data;\r\n wchar_t * *dataPtr2 = &data;\r\n data = NULL;\r\n {\r\n wchar_t * data = *dataPtr1;\r\n {\r\n wchar_t myString[] = L\"myString\";\r\n /* POTENTIAL FLAW: Allocate memory from the heap using a function that requires free() for deallocation */\r\n data = wcsdup(myString);\r\n /* Use data */\r\n printWLine(data);\r\n }\r\n *dataPtr1 = data;\r\n }\r\n {\r\n wchar_t * data = *dataPtr2;\r\n /* POTENTIAL FLAW: No deallocation of memory */\r\n /* no deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n wchar_t * *dataPtr1 = &data;\r\n wchar_t * *dataPtr2 = &data;\r\n data = NULL;\r\n {\r\n wchar_t * data = *dataPtr1;\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize then use data */\r\n wcscpy(data, L\"a string\");\r\n printWLine(data);\r\n *dataPtr1 = data;\r\n }\r\n {\r\n wchar_t * data = *dataPtr2;\r\n /* POTENTIAL FLAW: No deallocation of memory */\r\n /* no deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n wchar_t * *dataPtr1 = &data;\r\n wchar_t * *dataPtr2 = &data;\r\n data = NULL;\r\n {\r\n wchar_t * data = *dataPtr1;\r\n {\r\n wchar_t myString[] = L\"myString\";\r\n /* POTENTIAL FLAW: Allocate memory from the heap using a function that requires free() for deallocation */\r\n data = wcsdup(myString);\r\n /* Use data */\r\n printWLine(data);\r\n }\r\n *dataPtr1 = data;\r\n }\r\n {\r\n wchar_t * data = *dataPtr2;\r\n /* FIX: Deallocate memory initialized in the source */\r\n free(data);\r\n }\r\n}\r\n\r\nvoid CWE401_Memory_Leak__strdup_wchar_t_32_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__strdup_wchar_t_32_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__strdup_wchar_t_32_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__strdup_wchar_t_63a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__strdup_wchar_t_63a.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":3055,"source_sha256":"ef7dcc441091c4db2824c5c831aca7034b6ddb846ca14c8d735f5f1333d6ebeb","line_count":109,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__strdup_wchar_t_63a.c\r\nLabel Definition File: CWE401_Memory_Leak__strdup.label.xml\r\nTemplate File: sources-sinks-63a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: Allocate data using wcsdup()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE401_Memory_Leak__strdup_wchar_t_63b_badSink(wchar_t * * dataPtr);\r\n\r\nvoid CWE401_Memory_Leak__strdup_wchar_t_63_bad()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n {\r\n wchar_t myString[] = L\"myString\";\r\n /* POTENTIAL FLAW: Allocate memory from the heap using a function that requires free() for deallocation */\r\n data = wcsdup(myString);\r\n /* Use data */\r\n printWLine(data);\r\n }\r\n CWE401_Memory_Leak__strdup_wchar_t_63b_badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__strdup_wchar_t_63b_goodG2BSink(wchar_t * * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize then use data */\r\n wcscpy(data, L\"a string\");\r\n printWLine(data);\r\n CWE401_Memory_Leak__strdup_wchar_t_63b_goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__strdup_wchar_t_63b_goodB2GSink(wchar_t * * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n {\r\n wchar_t myString[] = L\"myString\";\r\n /* POTENTIAL FLAW: Allocate memory from the heap using a function that requires free() for deallocation */\r\n data = wcsdup(myString);\r\n /* Use data */\r\n printWLine(data);\r\n }\r\n CWE401_Memory_Leak__strdup_wchar_t_63b_goodB2GSink(&data);\r\n}\r\n\r\nvoid CWE401_Memory_Leak__strdup_wchar_t_63_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__strdup_wchar_t_63_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__strdup_wchar_t_63_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_calloc_02.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_calloc_02.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":4981,"source_sha256":"da3b1f72d17e189128b236e6154669ab40348b83e7afeaa7bf3b91e29e8289e1","line_count":179,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_calloc_02.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-02.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 02 Control flow: if(1) and if(0)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_02_bad()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)calloc(100, sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 1 to 0 */\r\nstatic void goodB2G1()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)calloc(100, sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n }\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)calloc(100, sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n }\r\n if(1)\r\n {\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 1 to 0 */\r\nstatic void goodG2B1()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (struct _twoIntsStruct *)ALLOCA(100*sizeof(struct _twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n if(1)\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (struct _twoIntsStruct *)ALLOCA(100*sizeof(struct _twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_02_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__struct_twoIntsStruct_calloc_02_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__struct_twoIntsStruct_calloc_02_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_calloc_15.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_calloc_15.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":6565,"source_sha256":"74b910ca191f7647245fbec1b04739f50c6ae81c4a0b675cf6acd09d80daca86","line_count":229,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_calloc_15.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-15.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 15 Control flow: switch(6) and switch(7)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_15_bad()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)calloc(100, sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second switch to switch(8) */\r\nstatic void goodB2G1()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)calloc(100, sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(8)\r\n {\r\n case 7:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n break;\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second switch */\r\nstatic void goodB2G2()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)calloc(100, sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first switch to switch(5) */\r\nstatic void goodG2B1()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n switch(5)\r\n {\r\n case 6:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (struct _twoIntsStruct *)ALLOCA(100*sizeof(struct _twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first switch */\r\nstatic void goodG2B2()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (struct _twoIntsStruct *)ALLOCA(100*sizeof(struct _twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_15_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__struct_twoIntsStruct_calloc_15_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__struct_twoIntsStruct_calloc_15_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_calloc_18.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_calloc_18.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":3333,"source_sha256":"0cbc5a971ed0e19624b89b1d93a25b8c94adcd36e2609f0b901b81c11b429e29","line_count":118,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_calloc_18.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-18.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 18 Control flow: goto statements\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_18_bad()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n goto source;\r\nsource:\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)calloc(100, sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by reversing the blocks on the second goto statement */\r\nstatic void goodB2G()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n goto source;\r\nsource:\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)calloc(100, sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n goto sink;\r\nsink:\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by reversing the blocks on the first goto statement */\r\nstatic void goodG2B()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n goto source;\r\nsource:\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (struct _twoIntsStruct *)ALLOCA(100*sizeof(struct _twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_18_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__struct_twoIntsStruct_calloc_18_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__struct_twoIntsStruct_calloc_18_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_calloc_52b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_calloc_52b.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":1697,"source_sha256":"fd2cc97ca91570792b81cc8a80324654dd7a561ba0ba6b4383293092e26a1f16","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_calloc_52b.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-52b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_52c_badSink(struct _twoIntsStruct * data);\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_52b_badSink(struct _twoIntsStruct * data)\r\n{\r\n CWE401_Memory_Leak__struct_twoIntsStruct_calloc_52c_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_52c_goodG2BSink(struct _twoIntsStruct * data);\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_52b_goodG2BSink(struct _twoIntsStruct * data)\r\n{\r\n CWE401_Memory_Leak__struct_twoIntsStruct_calloc_52c_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_52c_goodB2GSink(struct _twoIntsStruct * data);\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_52b_goodB2GSink(struct _twoIntsStruct * data)\r\n{\r\n CWE401_Memory_Leak__struct_twoIntsStruct_calloc_52c_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_calloc_53c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_calloc_53c.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":1724,"source_sha256":"dd92ffb12e10cc7f316962969bdc25372940d637baf5e43702da789d694ca37f","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_calloc_53c.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-53c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_53d_badSink(struct _twoIntsStruct * data);\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_53c_badSink(struct _twoIntsStruct * data)\r\n{\r\n CWE401_Memory_Leak__struct_twoIntsStruct_calloc_53d_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_53d_goodG2BSink(struct _twoIntsStruct * data);\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_53c_goodG2BSink(struct _twoIntsStruct * data)\r\n{\r\n CWE401_Memory_Leak__struct_twoIntsStruct_calloc_53d_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_53d_goodB2GSink(struct _twoIntsStruct * data);\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_53c_goodB2GSink(struct _twoIntsStruct * data)\r\n{\r\n CWE401_Memory_Leak__struct_twoIntsStruct_calloc_53d_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_calloc_53d.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_calloc_53d.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":1418,"source_sha256":"35adf05d55a7631026ae4c511859647c57ea527afa82a0c3fbb35ecfafe2d3eb","line_count":48,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_calloc_53d.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-53d.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_53d_badSink(struct _twoIntsStruct * data)\r\n{\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_53d_goodG2BSink(struct _twoIntsStruct * data)\r\n{\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_53d_goodB2GSink(struct _twoIntsStruct * data)\r\n{\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54a.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":3506,"source_sha256":"3b509a4e484eb430b66fa09d296c64d7ae597622422c050e8306cccb3ec3e8fa","line_count":110,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54a.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-54a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54b_badSink(struct _twoIntsStruct * data);\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54_bad()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)calloc(100, sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54b_goodG2BSink(struct _twoIntsStruct * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (struct _twoIntsStruct *)ALLOCA(100*sizeof(struct _twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54b_goodB2GSink(struct _twoIntsStruct * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)calloc(100, sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54c.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":1725,"source_sha256":"6516cd6ccf9cff881d0fec4536f6ab9fe0ec9526adf6eb692b2afbcb85a84e8d","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54c.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-54c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54d_badSink(struct _twoIntsStruct * data);\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54c_badSink(struct _twoIntsStruct * data)\r\n{\r\n CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54d_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54d_goodG2BSink(struct _twoIntsStruct * data);\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54c_goodG2BSink(struct _twoIntsStruct * data)\r\n{\r\n CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54d_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54d_goodB2GSink(struct _twoIntsStruct * data);\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54c_goodB2GSink(struct _twoIntsStruct * data)\r\n{\r\n CWE401_Memory_Leak__struct_twoIntsStruct_calloc_54d_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_calloc_67b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_calloc_67b.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":1863,"source_sha256":"651ab74d37e51107658027d5e9885249dab5f6a26c41f0d040ac197ff7e97d8d","line_count":56,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_calloc_67b.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-67b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\ntypedef struct _CWE401_Memory_Leak__struct_twoIntsStruct_calloc_67_structType\r\n{\r\n struct _twoIntsStruct * structFirst;\r\n} CWE401_Memory_Leak__struct_twoIntsStruct_calloc_67_structType;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_67b_badSink(CWE401_Memory_Leak__struct_twoIntsStruct_calloc_67_structType myStruct)\r\n{\r\n struct _twoIntsStruct * data = myStruct.structFirst;\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_67b_goodG2BSink(CWE401_Memory_Leak__struct_twoIntsStruct_calloc_67_structType myStruct)\r\n{\r\n struct _twoIntsStruct * data = myStruct.structFirst;\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_calloc_67b_goodB2GSink(CWE401_Memory_Leak__struct_twoIntsStruct_calloc_67_structType myStruct)\r\n{\r\n struct _twoIntsStruct * data = myStruct.structFirst;\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_calloc_81.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_calloc_81.h","cwe_id":"CWE401","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1539,"source_sha256":"91b968c101fc6c48e4114611164699d23977a7a925f4a27569be3f4417564e57","line_count":58,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_calloc_81.h\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-81.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE401_Memory_Leak__struct_twoIntsStruct_calloc_81\r\n{\r\n\r\nclass CWE401_Memory_Leak__struct_twoIntsStruct_calloc_81_base\r\n{\r\npublic:\r\n /* pure virtual function */\r\n virtual void action(struct _twoIntsStruct * data) const = 0;\r\n};\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE401_Memory_Leak__struct_twoIntsStruct_calloc_81_bad : public CWE401_Memory_Leak__struct_twoIntsStruct_calloc_81_base\r\n{\r\npublic:\r\n void action(struct _twoIntsStruct * data) const;\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE401_Memory_Leak__struct_twoIntsStruct_calloc_81_goodG2B : public CWE401_Memory_Leak__struct_twoIntsStruct_calloc_81_base\r\n{\r\npublic:\r\n void action(struct _twoIntsStruct * data) const;\r\n};\r\n\r\nclass CWE401_Memory_Leak__struct_twoIntsStruct_calloc_81_goodB2G : public CWE401_Memory_Leak__struct_twoIntsStruct_calloc_81_base\r\n{\r\npublic:\r\n void action(struct _twoIntsStruct * data) const;\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_calloc_82a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_calloc_82a.cpp","cwe_id":"CWE401","file_extension":".cpp","language_hint":"C++","source_size_bytes":3518,"source_sha256":"2de33b08e0a68d301171e718e4db4315a16c75b8fff1db02469f4091ecdffda7","line_count":115,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_calloc_82a.cpp\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-82a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE401_Memory_Leak__struct_twoIntsStruct_calloc_82.h\"\r\n\r\nnamespace CWE401_Memory_Leak__struct_twoIntsStruct_calloc_82\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)calloc(100, sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n CWE401_Memory_Leak__struct_twoIntsStruct_calloc_82_base* baseObject = new CWE401_Memory_Leak__struct_twoIntsStruct_calloc_82_bad;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (struct _twoIntsStruct *)ALLOCA(100*sizeof(struct _twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n CWE401_Memory_Leak__struct_twoIntsStruct_calloc_82_base* baseObject = new CWE401_Memory_Leak__struct_twoIntsStruct_calloc_82_goodG2B;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)calloc(100, sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n CWE401_Memory_Leak__struct_twoIntsStruct_calloc_82_base* baseObject = new CWE401_Memory_Leak__struct_twoIntsStruct_calloc_82_goodB2G;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE401_Memory_Leak__struct_twoIntsStruct_calloc_82; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_malloc_04.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_malloc_04.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":5510,"source_sha256":"24eca8a10412fb48cd8f896d96562f5f71153578efe588d9d06f3114212602f6","line_count":185,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_malloc_04.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-04.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: malloc Allocate data using malloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 04 Control flow: if(STATIC_CONST_TRUE) and if(STATIC_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The two variables below are declared \"const\", so a tool should\r\n be able to identify that reads of these will always return their\r\n initialized values. */\r\nstatic const int STATIC_CONST_TRUE = 1; /* true */\r\nstatic const int STATIC_CONST_FALSE = 0; /* false */\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_malloc_04_bad()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)malloc(100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second STATIC_CONST_TRUE to STATIC_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)malloc(100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n }\r\n if(STATIC_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)malloc(100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first STATIC_CONST_TRUE to STATIC_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n if(STATIC_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (struct _twoIntsStruct *)ALLOCA(100*sizeof(struct _twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (struct _twoIntsStruct *)ALLOCA(100*sizeof(struct _twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_malloc_04_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__struct_twoIntsStruct_malloc_04_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__struct_twoIntsStruct_malloc_04_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_malloc_31.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_malloc_31.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":3482,"source_sha256":"0f7e559b61298ebaa8b0c293bb25c6a7edb4de856df2677ac431dd5712beaa29","line_count":117,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_malloc_31.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-31.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: malloc Allocate data using malloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 31 Data flow using a copy of data within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_malloc_31_bad()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)malloc(100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n {\r\n struct _twoIntsStruct * dataCopy = data;\r\n struct _twoIntsStruct * data = dataCopy;\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (struct _twoIntsStruct *)ALLOCA(100*sizeof(struct _twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n {\r\n struct _twoIntsStruct * dataCopy = data;\r\n struct _twoIntsStruct * data = dataCopy;\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)malloc(100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n {\r\n struct _twoIntsStruct * dataCopy = data;\r\n struct _twoIntsStruct * data = dataCopy;\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n}\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_malloc_31_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__struct_twoIntsStruct_malloc_31_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__struct_twoIntsStruct_malloc_31_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_malloc_43.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_malloc_43.cpp","cwe_id":"CWE401","file_extension":".cpp","language_hint":"C++","source_size_bytes":3437,"source_sha256":"d0ae1feb9784f2a32182b8237734c688d720cc2cb1172e33c3f25e3f5e7384e3","line_count":128,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_malloc_43.cpp\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-43.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: malloc Allocate data using malloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 43 Data flow: data flows using a C++ reference from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE401_Memory_Leak__struct_twoIntsStruct_malloc_43\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSource(struct _twoIntsStruct * &data)\r\n{\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)malloc(100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n}\r\n\r\nvoid bad()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n badSource(data);\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSource(struct _twoIntsStruct * &data)\r\n{\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (struct _twoIntsStruct *)ALLOCA(100*sizeof(struct _twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n goodG2BSource(data);\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSource(struct _twoIntsStruct * &data)\r\n{\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)malloc(100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n goodB2GSource(data);\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE401_Memory_Leak__struct_twoIntsStruct_malloc_43; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_malloc_51a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_malloc_51a.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":3459,"source_sha256":"b7f6a29796faa7fa9a2fa7832a08b14e9ee64d0087872030a3967378191418ff","line_count":110,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_malloc_51a.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-51a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: malloc Allocate data using malloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 51 Data flow: data passed as an argument from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_malloc_51b_badSink(struct _twoIntsStruct * data);\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_malloc_51_bad()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)malloc(100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n CWE401_Memory_Leak__struct_twoIntsStruct_malloc_51b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_malloc_51b_goodG2BSink(struct _twoIntsStruct * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (struct _twoIntsStruct *)ALLOCA(100*sizeof(struct _twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n CWE401_Memory_Leak__struct_twoIntsStruct_malloc_51b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_malloc_51b_goodB2GSink(struct _twoIntsStruct * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)malloc(100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n CWE401_Memory_Leak__struct_twoIntsStruct_malloc_51b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_malloc_51_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__struct_twoIntsStruct_malloc_51_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__struct_twoIntsStruct_malloc_51_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_malloc_65a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_malloc_65a.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":3748,"source_sha256":"adc7eeb89c3afc246119cac9ebf36b1ecf1b2eb1603f73cd96b52853f3431fb7","line_count":115,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_malloc_65a.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-65a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: malloc Allocate data using malloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_malloc_65b_badSink(struct _twoIntsStruct * data);\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_malloc_65_bad()\r\n{\r\n struct _twoIntsStruct * data;\r\n /* define a function pointer */\r\n void (*funcPtr) (struct _twoIntsStruct *) = CWE401_Memory_Leak__struct_twoIntsStruct_malloc_65b_badSink;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)malloc(100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n /* use the function pointer */\r\n funcPtr(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_malloc_65b_goodG2BSink(struct _twoIntsStruct * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n struct _twoIntsStruct * data;\r\n void (*funcPtr) (struct _twoIntsStruct *) = CWE401_Memory_Leak__struct_twoIntsStruct_malloc_65b_goodG2BSink;\r\n data = NULL;\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (struct _twoIntsStruct *)ALLOCA(100*sizeof(struct _twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n funcPtr(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_malloc_65b_goodB2GSink(struct _twoIntsStruct * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n struct _twoIntsStruct * data;\r\n void (*funcPtr) (struct _twoIntsStruct *) = CWE401_Memory_Leak__struct_twoIntsStruct_malloc_65b_goodB2GSink;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)malloc(100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n funcPtr(data);\r\n}\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_malloc_65_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__struct_twoIntsStruct_malloc_65_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__struct_twoIntsStruct_malloc_65_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_malloc_82.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_malloc_82.h","cwe_id":"CWE401","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1513,"source_sha256":"2d69265a62fdc85c0e54a5da0e7be1a1a017aa2003bb075bb92e851f30daabbd","line_count":58,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_malloc_82.h\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-82.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: malloc Allocate data using malloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE401_Memory_Leak__struct_twoIntsStruct_malloc_82\r\n{\r\n\r\nclass CWE401_Memory_Leak__struct_twoIntsStruct_malloc_82_base\r\n{\r\npublic:\r\n /* pure virtual function */\r\n virtual void action(struct _twoIntsStruct * data) = 0;\r\n};\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE401_Memory_Leak__struct_twoIntsStruct_malloc_82_bad : public CWE401_Memory_Leak__struct_twoIntsStruct_malloc_82_base\r\n{\r\npublic:\r\n void action(struct _twoIntsStruct * data);\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE401_Memory_Leak__struct_twoIntsStruct_malloc_82_goodG2B : public CWE401_Memory_Leak__struct_twoIntsStruct_malloc_82_base\r\n{\r\npublic:\r\n void action(struct _twoIntsStruct * data);\r\n};\r\n\r\nclass CWE401_Memory_Leak__struct_twoIntsStruct_malloc_82_goodB2G : public CWE401_Memory_Leak__struct_twoIntsStruct_malloc_82_base\r\n{\r\npublic:\r\n void action(struct _twoIntsStruct * data);\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_malloc_82a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_malloc_82a.cpp","cwe_id":"CWE401","file_extension":".cpp","language_hint":"C++","source_size_bytes":3516,"source_sha256":"c4c8cd9360abf7e8a93743bf8409f43a29cbf8786d9481dac6dc5c6c85aa0803","line_count":115,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_malloc_82a.cpp\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-82a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: malloc Allocate data using malloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE401_Memory_Leak__struct_twoIntsStruct_malloc_82.h\"\r\n\r\nnamespace CWE401_Memory_Leak__struct_twoIntsStruct_malloc_82\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)malloc(100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n CWE401_Memory_Leak__struct_twoIntsStruct_malloc_82_base* baseObject = new CWE401_Memory_Leak__struct_twoIntsStruct_malloc_82_bad;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (struct _twoIntsStruct *)ALLOCA(100*sizeof(struct _twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n CWE401_Memory_Leak__struct_twoIntsStruct_malloc_82_base* baseObject = new CWE401_Memory_Leak__struct_twoIntsStruct_malloc_82_goodG2B;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)malloc(100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n CWE401_Memory_Leak__struct_twoIntsStruct_malloc_82_base* baseObject = new CWE401_Memory_Leak__struct_twoIntsStruct_malloc_82_goodB2G;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE401_Memory_Leak__struct_twoIntsStruct_malloc_82; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_realloc_01.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_realloc_01.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":3088,"source_sha256":"2a0687355ebde327652ed1ff43575578dca78c7339b2ba7ad95dc4d9981438e5","line_count":106,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_realloc_01.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-01.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 01 Baseline\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_realloc_01_bad()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)realloc(data, 100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (struct _twoIntsStruct *)ALLOCA(100*sizeof(struct _twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)realloc(data, 100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n}\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_realloc_01_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__struct_twoIntsStruct_realloc_01_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__struct_twoIntsStruct_realloc_01_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_realloc_09.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_realloc_09.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":5267,"source_sha256":"9707ff511c2de9213bd3a38e825df397cbafd78ddeace2581dc7cd4ac6b0a340","line_count":179,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_realloc_09.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-09.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_realloc_09_bad()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)realloc(data, 100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)realloc(data, 100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n }\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)realloc(data, 100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (struct _twoIntsStruct *)ALLOCA(100*sizeof(struct _twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (struct _twoIntsStruct *)ALLOCA(100*sizeof(struct _twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_realloc_09_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__struct_twoIntsStruct_realloc_09_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__struct_twoIntsStruct_realloc_09_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_realloc_15.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_realloc_15.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":6590,"source_sha256":"fe25ffbe54a8fd446213a0af1c6ecc389b4c01ffd0e4a77f1cab99bce7a31274","line_count":229,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_realloc_15.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-15.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 15 Control flow: switch(6) and switch(7)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_realloc_15_bad()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)realloc(data, 100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second switch to switch(8) */\r\nstatic void goodB2G1()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)realloc(data, 100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(8)\r\n {\r\n case 7:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n break;\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second switch */\r\nstatic void goodB2G2()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)realloc(data, 100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first switch to switch(5) */\r\nstatic void goodG2B1()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n switch(5)\r\n {\r\n case 6:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (struct _twoIntsStruct *)ALLOCA(100*sizeof(struct _twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first switch */\r\nstatic void goodG2B2()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (struct _twoIntsStruct *)ALLOCA(100*sizeof(struct _twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_realloc_15_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__struct_twoIntsStruct_realloc_15_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__struct_twoIntsStruct_realloc_15_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_realloc_16.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_realloc_16.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":3563,"source_sha256":"4edb3c02d3c33441a4f98c5249d8afb46057208bcc8e49069e4cd545e0db3487","line_count":130,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_realloc_16.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-16.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 16 Control flow: while(1)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_realloc_16_bad()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)realloc(data, 100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the sinks in the second while statement */\r\nstatic void goodB2G()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)realloc(data, 100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the sources in the first while statement */\r\nstatic void goodG2B()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n while(1)\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (struct _twoIntsStruct *)ALLOCA(100*sizeof(struct _twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_realloc_16_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__struct_twoIntsStruct_realloc_16_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__struct_twoIntsStruct_realloc_16_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_realloc_43.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_realloc_43.cpp","cwe_id":"CWE401","file_extension":".cpp","language_hint":"C++","source_size_bytes":3456,"source_sha256":"80225c42133afdaf1f305577fd11541588870083618504c1342293d707adcf83","line_count":128,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_realloc_43.cpp\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-43.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 43 Data flow: data flows using a C++ reference from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE401_Memory_Leak__struct_twoIntsStruct_realloc_43\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSource(struct _twoIntsStruct * &data)\r\n{\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)realloc(data, 100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n}\r\n\r\nvoid bad()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n badSource(data);\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSource(struct _twoIntsStruct * &data)\r\n{\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (struct _twoIntsStruct *)ALLOCA(100*sizeof(struct _twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n goodG2BSource(data);\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSource(struct _twoIntsStruct * &data)\r\n{\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (struct _twoIntsStruct *)realloc(data, 100*sizeof(struct _twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine((twoIntsStruct *)&data[0]);\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n struct _twoIntsStruct * data;\r\n data = NULL;\r\n goodB2GSource(data);\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE401_Memory_Leak__struct_twoIntsStruct_realloc_43; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_realloc_54d.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_realloc_54d.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":1737,"source_sha256":"20a10a9219ca45119a3e36852a1ff085bf8fec0ff0b74c8fef5711bf688d6c64","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_realloc_54d.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-54d.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_realloc_54e_badSink(struct _twoIntsStruct * data);\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_realloc_54d_badSink(struct _twoIntsStruct * data)\r\n{\r\n CWE401_Memory_Leak__struct_twoIntsStruct_realloc_54e_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_realloc_54e_goodG2BSink(struct _twoIntsStruct * data);\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_realloc_54d_goodG2BSink(struct _twoIntsStruct * data)\r\n{\r\n CWE401_Memory_Leak__struct_twoIntsStruct_realloc_54e_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_realloc_54e_goodB2GSink(struct _twoIntsStruct * data);\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_realloc_54d_goodB2GSink(struct _twoIntsStruct * data)\r\n{\r\n CWE401_Memory_Leak__struct_twoIntsStruct_realloc_54e_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_realloc_67b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_realloc_67b.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":1874,"source_sha256":"a90e2e84e44a679feacbee8c9e116543f1a1266b695b98caa256de9b325de856","line_count":56,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_realloc_67b.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-67b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\ntypedef struct _CWE401_Memory_Leak__struct_twoIntsStruct_realloc_67_structType\r\n{\r\n struct _twoIntsStruct * structFirst;\r\n} CWE401_Memory_Leak__struct_twoIntsStruct_realloc_67_structType;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_realloc_67b_badSink(CWE401_Memory_Leak__struct_twoIntsStruct_realloc_67_structType myStruct)\r\n{\r\n struct _twoIntsStruct * data = myStruct.structFirst;\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_realloc_67b_goodG2BSink(CWE401_Memory_Leak__struct_twoIntsStruct_realloc_67_structType myStruct)\r\n{\r\n struct _twoIntsStruct * data = myStruct.structFirst;\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_realloc_67b_goodB2GSink(CWE401_Memory_Leak__struct_twoIntsStruct_realloc_67_structType myStruct)\r\n{\r\n struct _twoIntsStruct * data = myStruct.structFirst;\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_realloc_82.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_realloc_82.h","cwe_id":"CWE401","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1524,"source_sha256":"3ac7712270402ab2d72211cc4e7e8de747acea9fa5b94f7c4f47fa179814bd55","line_count":58,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_realloc_82.h\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-82.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE401_Memory_Leak__struct_twoIntsStruct_realloc_82\r\n{\r\n\r\nclass CWE401_Memory_Leak__struct_twoIntsStruct_realloc_82_base\r\n{\r\npublic:\r\n /* pure virtual function */\r\n virtual void action(struct _twoIntsStruct * data) = 0;\r\n};\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE401_Memory_Leak__struct_twoIntsStruct_realloc_82_bad : public CWE401_Memory_Leak__struct_twoIntsStruct_realloc_82_base\r\n{\r\npublic:\r\n void action(struct _twoIntsStruct * data);\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE401_Memory_Leak__struct_twoIntsStruct_realloc_82_goodG2B : public CWE401_Memory_Leak__struct_twoIntsStruct_realloc_82_base\r\n{\r\npublic:\r\n void action(struct _twoIntsStruct * data);\r\n};\r\n\r\nclass CWE401_Memory_Leak__struct_twoIntsStruct_realloc_82_goodB2G : public CWE401_Memory_Leak__struct_twoIntsStruct_realloc_82_base\r\n{\r\npublic:\r\n void action(struct _twoIntsStruct * data);\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_realloc_82_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__struct_twoIntsStruct_realloc_82_bad.cpp","cwe_id":"CWE401","file_extension":".cpp","language_hint":"C++","source_size_bytes":973,"source_sha256":"cd351ca4331db00c0294db17b2f0297bd1a461993cfcd181594d84946ec812a4","line_count":32,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__struct_twoIntsStruct_realloc_82_bad.cpp\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-82_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE401_Memory_Leak__struct_twoIntsStruct_realloc_82.h\"\r\n\r\nnamespace CWE401_Memory_Leak__struct_twoIntsStruct_realloc_82\r\n{\r\n\r\nvoid CWE401_Memory_Leak__struct_twoIntsStruct_realloc_82_bad::action(struct _twoIntsStruct * data)\r\n{\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_calloc_02.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_calloc_02.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":4741,"source_sha256":"c306074f4f9df33c48573b3732c839e18737fbaefccc0f5aa9bf2e8b6a5d6eb3","line_count":179,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_calloc_02.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-02.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 02 Control flow: if(1) and if(0)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_calloc_02_bad()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)calloc(100, sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 1 to 0 */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)calloc(100, sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n }\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)calloc(100, sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n }\r\n if(1)\r\n {\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 1 to 0 */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (twoIntsStruct *)ALLOCA(100*sizeof(twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n if(1)\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (twoIntsStruct *)ALLOCA(100*sizeof(twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_calloc_02_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__twoIntsStruct_calloc_02_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__twoIntsStruct_calloc_02_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_calloc_04.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_calloc_04.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":5273,"source_sha256":"e050b68c226e3cdcc53bee9e972724cfeeb66635a16883f1634f250d7d494042","line_count":185,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_calloc_04.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-04.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 04 Control flow: if(STATIC_CONST_TRUE) and if(STATIC_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The two variables below are declared \"const\", so a tool should\r\n be able to identify that reads of these will always return their\r\n initialized values. */\r\nstatic const int STATIC_CONST_TRUE = 1; /* true */\r\nstatic const int STATIC_CONST_FALSE = 0; /* false */\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_calloc_04_bad()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)calloc(100, sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second STATIC_CONST_TRUE to STATIC_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)calloc(100, sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n }\r\n if(STATIC_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)calloc(100, sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first STATIC_CONST_TRUE to STATIC_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n if(STATIC_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (twoIntsStruct *)ALLOCA(100*sizeof(twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (twoIntsStruct *)ALLOCA(100*sizeof(twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_calloc_04_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__twoIntsStruct_calloc_04_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__twoIntsStruct_calloc_04_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_calloc_33.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_calloc_33.cpp","cwe_id":"CWE401","file_extension":".cpp","language_hint":"C++","source_size_bytes":3298,"source_sha256":"8f6807f97a4f17a654bd07745b7996be8def4ceb10ee667131f526022b78e2bf","line_count":124,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_calloc_33.cpp\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-33.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 33 Data flow: use of a C++ reference to data within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE401_Memory_Leak__twoIntsStruct_calloc_33\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n twoIntsStruct * data;\r\n twoIntsStruct * &dataRef = data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)calloc(100, sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n {\r\n twoIntsStruct * data = dataRef;\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n twoIntsStruct * data;\r\n twoIntsStruct * &dataRef = data;\r\n data = NULL;\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (twoIntsStruct *)ALLOCA(100*sizeof(twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n {\r\n twoIntsStruct * data = dataRef;\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n twoIntsStruct * data;\r\n twoIntsStruct * &dataRef = data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)calloc(100, sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n {\r\n twoIntsStruct * data = dataRef;\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE401_Memory_Leak__twoIntsStruct_calloc_33; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_calloc_63b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_calloc_63b.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":1447,"source_sha256":"a3a70c6542fc0bbe1410ebf1bdb30bb49636c87dd6c931608f441eea3d893823","line_count":51,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_calloc_63b.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-63b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_calloc_63b_badSink(twoIntsStruct * * dataPtr)\r\n{\r\n twoIntsStruct * data = *dataPtr;\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_calloc_63b_goodG2BSink(twoIntsStruct * * dataPtr)\r\n{\r\n twoIntsStruct * data = *dataPtr;\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_calloc_63b_goodB2GSink(twoIntsStruct * * dataPtr)\r\n{\r\n twoIntsStruct * data = *dataPtr;\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_calloc_65a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_calloc_65a.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":3502,"source_sha256":"657098701e4b1e879ad15493a9ca8992898e4b9dc042ad7ed00fb35c79e12c5a","line_count":115,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_calloc_65a.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-65a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_calloc_65b_badSink(twoIntsStruct * data);\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_calloc_65_bad()\r\n{\r\n twoIntsStruct * data;\r\n /* define a function pointer */\r\n void (*funcPtr) (twoIntsStruct *) = CWE401_Memory_Leak__twoIntsStruct_calloc_65b_badSink;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)calloc(100, sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n /* use the function pointer */\r\n funcPtr(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_calloc_65b_goodG2BSink(twoIntsStruct * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n twoIntsStruct * data;\r\n void (*funcPtr) (twoIntsStruct *) = CWE401_Memory_Leak__twoIntsStruct_calloc_65b_goodG2BSink;\r\n data = NULL;\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (twoIntsStruct *)ALLOCA(100*sizeof(twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n funcPtr(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_calloc_65b_goodB2GSink(twoIntsStruct * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n twoIntsStruct * data;\r\n void (*funcPtr) (twoIntsStruct *) = CWE401_Memory_Leak__twoIntsStruct_calloc_65b_goodB2GSink;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)calloc(100, sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n funcPtr(data);\r\n}\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_calloc_65_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__twoIntsStruct_calloc_65_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__twoIntsStruct_calloc_65_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_calloc_84a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_calloc_84a.cpp","cwe_id":"CWE401","file_extension":".cpp","language_hint":"C++","source_size_bytes":2506,"source_sha256":"7d2c32a0a7482bd84fdcd7da184db23fac7c01a377db138ceb63a060f7a58ea4","line_count":92,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_calloc_84a.cpp\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-84a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE401_Memory_Leak__twoIntsStruct_calloc_84.h\"\r\n\r\nnamespace CWE401_Memory_Leak__twoIntsStruct_calloc_84\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n CWE401_Memory_Leak__twoIntsStruct_calloc_84_bad * badObject = new CWE401_Memory_Leak__twoIntsStruct_calloc_84_bad(data);\r\n delete badObject;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n CWE401_Memory_Leak__twoIntsStruct_calloc_84_goodG2B * goodG2BObject = new CWE401_Memory_Leak__twoIntsStruct_calloc_84_goodG2B(data);\r\n delete goodG2BObject;\r\n}\r\n\r\n/* goodG2B uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n CWE401_Memory_Leak__twoIntsStruct_calloc_84_goodB2G * goodB2GObject = new CWE401_Memory_Leak__twoIntsStruct_calloc_84_goodB2G(data);\r\n delete goodB2GObject;\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE401_Memory_Leak__twoIntsStruct_calloc_84; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_malloc_12.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_malloc_12.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":4833,"source_sha256":"555d9f4b02b64f8f392c370ea4ef07cf7a15893a19cd86d57b09d0dac7b450ab","line_count":171,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_malloc_12.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-12.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: malloc Allocate data using malloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_malloc_12_bad()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n }\r\n else\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (twoIntsStruct *)ALLOCA(100*sizeof(twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n else\r\n {\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the first \"if\" so that\r\n both branches use the BadSource and the second \"if\" so that both branches\r\n use the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n }\r\n else\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n else\r\n {\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the first \"if\" so that\r\n both branches use the GoodSource and the second \"if\" so that both branches\r\n use the BadSink */\r\nstatic void goodG2B()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (twoIntsStruct *)ALLOCA(100*sizeof(twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n }\r\n else\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (twoIntsStruct *)ALLOCA(100*sizeof(twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n else\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_malloc_12_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__twoIntsStruct_malloc_12_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__twoIntsStruct_malloc_12_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_malloc_22b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_malloc_22b.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":2587,"source_sha256":"baeccef5dd3b623b4682cbca8774ff113d1e891560d0000fff8d0523684ec557","line_count":80,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_malloc_22b.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-22b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: malloc Allocate data using malloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 22 Control flow: Flow controlled by value of a global variable. Sink functions are in a separate file from sources.\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* The global variable below is used to drive control flow in the sink function */\r\nextern int CWE401_Memory_Leak__twoIntsStruct_malloc_22_badGlobal;\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_malloc_22_badSink(twoIntsStruct * data)\r\n{\r\n if(CWE401_Memory_Leak__twoIntsStruct_malloc_22_badGlobal)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* The global variables below are used to drive control flow in the sink functions. */\r\nextern int CWE401_Memory_Leak__twoIntsStruct_malloc_22_goodB2G1Global;\r\nextern int CWE401_Memory_Leak__twoIntsStruct_malloc_22_goodB2G2Global;\r\nextern int CWE401_Memory_Leak__twoIntsStruct_malloc_22_goodG2BGlobal;\r\n\r\n/* goodB2G1() - use badsource and goodsink by setting the static variable to false instead of true */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_malloc_22_goodB2G1Sink(twoIntsStruct * data)\r\n{\r\n if(CWE401_Memory_Leak__twoIntsStruct_malloc_22_goodB2G1Global)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_malloc_22_goodB2G2Sink(twoIntsStruct * data)\r\n{\r\n if(CWE401_Memory_Leak__twoIntsStruct_malloc_22_goodB2G2Global)\r\n {\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_malloc_22_goodG2BSink(twoIntsStruct * data)\r\n{\r\n if(CWE401_Memory_Leak__twoIntsStruct_malloc_22_goodG2BGlobal)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_malloc_51b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_malloc_51b.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":1322,"source_sha256":"8b7200bd4930918613dc83d1099f319cfeec0b9dbd8c4bd0d7ed3c8fa28dbf0d","line_count":48,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_malloc_51b.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-51b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: malloc Allocate data using malloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 51 Data flow: data passed as an argument from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_malloc_51b_badSink(twoIntsStruct * data)\r\n{\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_malloc_51b_goodG2BSink(twoIntsStruct * data)\r\n{\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_malloc_51b_goodB2GSink(twoIntsStruct * data)\r\n{\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_malloc_52b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_malloc_52b.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":1579,"source_sha256":"1344a897d34e596cb402d3f65ddbc39e07ca98c8b3760b7d0ae5f723ad45ce02","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_malloc_52b.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-52b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: malloc Allocate data using malloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_malloc_52c_badSink(twoIntsStruct * data);\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_malloc_52b_badSink(twoIntsStruct * data)\r\n{\r\n CWE401_Memory_Leak__twoIntsStruct_malloc_52c_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_malloc_52c_goodG2BSink(twoIntsStruct * data);\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_malloc_52b_goodG2BSink(twoIntsStruct * data)\r\n{\r\n CWE401_Memory_Leak__twoIntsStruct_malloc_52c_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_malloc_52c_goodB2GSink(twoIntsStruct * data);\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_malloc_52b_goodB2GSink(twoIntsStruct * data)\r\n{\r\n CWE401_Memory_Leak__twoIntsStruct_malloc_52c_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_malloc_54e.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_malloc_54e.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":1367,"source_sha256":"ab864085bcafee8e23402b4906494e0cb1e2583d119d608bfecdb5011539be16","line_count":48,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_malloc_54e.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-54e.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: malloc Allocate data using malloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_malloc_54e_badSink(twoIntsStruct * data)\r\n{\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_malloc_54e_goodG2BSink(twoIntsStruct * data)\r\n{\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_malloc_54e_goodB2GSink(twoIntsStruct * data)\r\n{\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_malloc_67b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_malloc_67b.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":1768,"source_sha256":"b3c8b401df19a7bffeaa027b3ba470d5bb3db44390a6f0a7e233efc3607e7221","line_count":56,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_malloc_67b.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-67b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: malloc Allocate data using malloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\ntypedef struct _CWE401_Memory_Leak__twoIntsStruct_malloc_67_structType\r\n{\r\n twoIntsStruct * structFirst;\r\n} CWE401_Memory_Leak__twoIntsStruct_malloc_67_structType;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_malloc_67b_badSink(CWE401_Memory_Leak__twoIntsStruct_malloc_67_structType myStruct)\r\n{\r\n twoIntsStruct * data = myStruct.structFirst;\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_malloc_67b_goodG2BSink(CWE401_Memory_Leak__twoIntsStruct_malloc_67_structType myStruct)\r\n{\r\n twoIntsStruct * data = myStruct.structFirst;\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_malloc_67b_goodB2GSink(CWE401_Memory_Leak__twoIntsStruct_malloc_67_structType myStruct)\r\n{\r\n twoIntsStruct * data = myStruct.structFirst;\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_malloc_81_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_malloc_81_goodG2B.cpp","cwe_id":"CWE401","file_extension":".cpp","language_hint":"C++","source_size_bytes":953,"source_sha256":"f62c4f5924377b91a38da20f385db3b839ffa156134042ac08b01fd2d53388ac","line_count":32,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_malloc_81_goodG2B.cpp\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-81_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: malloc Allocate data using malloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE401_Memory_Leak__twoIntsStruct_malloc_81.h\"\r\n\r\nnamespace CWE401_Memory_Leak__twoIntsStruct_malloc_81\r\n{\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_malloc_81_goodG2B::action(twoIntsStruct * data) const\r\n{\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_malloc_83.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_malloc_83.h","cwe_id":"CWE401","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1531,"source_sha256":"76bb9841fdee75464f380d0247a93274a3997b275e6f844351e8a1091b8731c0","line_count":63,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_malloc_83.h\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-83.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: malloc Allocate data using malloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE401_Memory_Leak__twoIntsStruct_malloc_83\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE401_Memory_Leak__twoIntsStruct_malloc_83_bad\r\n{\r\npublic:\r\n CWE401_Memory_Leak__twoIntsStruct_malloc_83_bad(twoIntsStruct * dataCopy);\r\n ~CWE401_Memory_Leak__twoIntsStruct_malloc_83_bad();\r\n\r\nprivate:\r\n twoIntsStruct * data;\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE401_Memory_Leak__twoIntsStruct_malloc_83_goodG2B\r\n{\r\npublic:\r\n CWE401_Memory_Leak__twoIntsStruct_malloc_83_goodG2B(twoIntsStruct * dataCopy);\r\n ~CWE401_Memory_Leak__twoIntsStruct_malloc_83_goodG2B();\r\n\r\nprivate:\r\n twoIntsStruct * data;\r\n};\r\n\r\nclass CWE401_Memory_Leak__twoIntsStruct_malloc_83_goodB2G\r\n{\r\npublic:\r\n CWE401_Memory_Leak__twoIntsStruct_malloc_83_goodB2G(twoIntsStruct * dataCopy);\r\n ~CWE401_Memory_Leak__twoIntsStruct_malloc_83_goodB2G();\r\n\r\nprivate:\r\n twoIntsStruct * data;\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_malloc_84_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_malloc_84_goodB2G.cpp","cwe_id":"CWE401","file_extension":".cpp","language_hint":"C++","source_size_bytes":1398,"source_sha256":"a806f973581509b93f44b8c548b8d463c0034849f137450bff201f6d42cfa48a","line_count":42,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_malloc_84_goodB2G.cpp\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-84_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: malloc Allocate data using malloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE401_Memory_Leak__twoIntsStruct_malloc_84.h\"\r\n\r\nnamespace CWE401_Memory_Leak__twoIntsStruct_malloc_84\r\n{\r\nCWE401_Memory_Leak__twoIntsStruct_malloc_84_goodB2G::CWE401_Memory_Leak__twoIntsStruct_malloc_84_goodB2G(twoIntsStruct * dataCopy)\r\n{\r\n data = dataCopy;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n}\r\n\r\nCWE401_Memory_Leak__twoIntsStruct_malloc_84_goodB2G::~CWE401_Memory_Leak__twoIntsStruct_malloc_84_goodB2G()\r\n{\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_realloc_01.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_realloc_01.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":2930,"source_sha256":"0d0fe12f7d28095be2451a98304c0881052efb69540374b9e76d41540cec2dce","line_count":106,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_realloc_01.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-01.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 01 Baseline\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_01_bad()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)realloc(data, 100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (twoIntsStruct *)ALLOCA(100*sizeof(twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)realloc(data, 100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n}\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_01_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_01_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_01_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_realloc_09.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_realloc_09.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":5027,"source_sha256":"c142a025b1903d821e28f892ec738dfd66b8a95825f18dd6d154faa29751dc9a","line_count":179,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_realloc_09.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-09.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_09_bad()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)realloc(data, 100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)realloc(data, 100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n }\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)realloc(data, 100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (twoIntsStruct *)ALLOCA(100*sizeof(twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (twoIntsStruct *)ALLOCA(100*sizeof(twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_09_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_09_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_09_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_realloc_34.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_realloc_34.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":3700,"source_sha256":"c90ae837b35ab6989e1a7d968f07eba4024c95687dcb4439e925935e5e2f4f3f","line_count":126,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_realloc_34.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-34.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 34 Data flow: use of a union containing two methods of accessing the same data (within the same function)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\ntypedef union\r\n{\r\n twoIntsStruct * unionFirst;\r\n twoIntsStruct * unionSecond;\r\n} CWE401_Memory_Leak__twoIntsStruct_realloc_34_unionType;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_34_bad()\r\n{\r\n twoIntsStruct * data;\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_34_unionType myUnion;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)realloc(data, 100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n myUnion.unionFirst = data;\r\n {\r\n twoIntsStruct * data = myUnion.unionSecond;\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n twoIntsStruct * data;\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_34_unionType myUnion;\r\n data = NULL;\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (twoIntsStruct *)ALLOCA(100*sizeof(twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n myUnion.unionFirst = data;\r\n {\r\n twoIntsStruct * data = myUnion.unionSecond;\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n twoIntsStruct * data;\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_34_unionType myUnion;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)realloc(data, 100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n myUnion.unionFirst = data;\r\n {\r\n twoIntsStruct * data = myUnion.unionSecond;\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n}\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_34_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_34_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_34_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_realloc_42.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_realloc_42.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":3348,"source_sha256":"00464483b9a5f0ba43896495371e0f3eef1ccc4c95d42fd7f9983db06ead7e06","line_count":124,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_realloc_42.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-42.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 42 Data flow: data returned from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nstatic twoIntsStruct * badSource(twoIntsStruct * data)\r\n{\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)realloc(data, 100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n return data;\r\n}\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_42_bad()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n data = badSource(data);\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic twoIntsStruct * goodG2BSource(twoIntsStruct * data)\r\n{\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (twoIntsStruct *)ALLOCA(100*sizeof(twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n return data;\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n data = goodG2BSource(data);\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic twoIntsStruct * goodB2GSource(twoIntsStruct * data)\r\n{\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)realloc(data, 100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n return data;\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n data = goodB2GSource(data);\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n}\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_42_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_42_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_42_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_realloc_52b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_realloc_52b.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":1591,"source_sha256":"65a8f81a9c5276f8080cb0ff0daa939584473a173cfa362a08441a3870950e58","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_realloc_52b.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-52b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_52c_badSink(twoIntsStruct * data);\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_52b_badSink(twoIntsStruct * data)\r\n{\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_52c_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_52c_goodG2BSink(twoIntsStruct * data);\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_52b_goodG2BSink(twoIntsStruct * data)\r\n{\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_52c_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_52c_goodB2GSink(twoIntsStruct * data);\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_52b_goodB2GSink(twoIntsStruct * data)\r\n{\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_52c_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_realloc_54a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_realloc_54a.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":3307,"source_sha256":"b81ddf194d594aa55c3f428be7c3941f2b606eae7614bb34435510b043fa735d","line_count":110,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_realloc_54a.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-54a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_54b_badSink(twoIntsStruct * data);\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_54_bad()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)realloc(data, 100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_54b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_54b_goodG2BSink(twoIntsStruct * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (twoIntsStruct *)ALLOCA(100*sizeof(twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_54b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_54b_goodB2GSink(twoIntsStruct * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)realloc(data, 100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_54b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_54_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_54_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_54_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_realloc_63a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_realloc_63a.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":3270,"source_sha256":"964e5c91e9b4ce59941ddc65c16e074c5955bda6bd0ea753c401a1b83a8fbd2b","line_count":110,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_realloc_63a.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-63a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_63b_badSink(twoIntsStruct * * dataPtr);\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_63_bad()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)realloc(data, 100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_63b_badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_63b_goodG2BSink(twoIntsStruct * * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (twoIntsStruct *)ALLOCA(100*sizeof(twoIntsStruct));\r\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_63b_goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_63b_goodB2GSink(twoIntsStruct * * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n twoIntsStruct * data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)realloc(data, 100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_63b_goodB2GSink(&data);\r\n}\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_63_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_63_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_63_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_realloc_64b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_realloc_64b.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":1959,"source_sha256":"33546090d154c464edd1f0d1116819615ab035d9ebbd519841597c6a0e7cfe99","line_count":60,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_realloc_64b.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-64b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_64b_badSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n twoIntsStruct * * dataPtr = (twoIntsStruct * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n twoIntsStruct * data = (*dataPtr);\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_64b_goodG2BSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n twoIntsStruct * * dataPtr = (twoIntsStruct * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n twoIntsStruct * data = (*dataPtr);\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__twoIntsStruct_realloc_64b_goodB2GSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n twoIntsStruct * * dataPtr = (twoIntsStruct * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n twoIntsStruct * data = (*dataPtr);\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_realloc_73b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_realloc_73b.cpp","cwe_id":"CWE401","file_extension":".cpp","language_hint":"C++","source_size_bytes":1520,"source_sha256":"c8afd654c0055727e4e8fc92057fafa3416a06d7add6823a290491013c5c04c5","line_count":60,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_realloc_73b.cpp\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-73b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE401_Memory_Leak__twoIntsStruct_realloc_73\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(list dataList)\r\n{\r\n /* copy data out of dataList */\r\n twoIntsStruct * data = dataList.back();\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(list dataList)\r\n{\r\n twoIntsStruct * data = dataList.back();\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(list dataList)\r\n{\r\n twoIntsStruct * data = dataList.back();\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_realloc_81.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_realloc_81.h","cwe_id":"CWE401","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1455,"source_sha256":"8063b11d7f859fbf00d6680c6b052e2498abd24b475380258c71d6f87b7f6dfb","line_count":58,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_realloc_81.h\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-81.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE401_Memory_Leak__twoIntsStruct_realloc_81\r\n{\r\n\r\nclass CWE401_Memory_Leak__twoIntsStruct_realloc_81_base\r\n{\r\npublic:\r\n /* pure virtual function */\r\n virtual void action(twoIntsStruct * data) const = 0;\r\n};\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE401_Memory_Leak__twoIntsStruct_realloc_81_bad : public CWE401_Memory_Leak__twoIntsStruct_realloc_81_base\r\n{\r\npublic:\r\n void action(twoIntsStruct * data) const;\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE401_Memory_Leak__twoIntsStruct_realloc_81_goodG2B : public CWE401_Memory_Leak__twoIntsStruct_realloc_81_base\r\n{\r\npublic:\r\n void action(twoIntsStruct * data) const;\r\n};\r\n\r\nclass CWE401_Memory_Leak__twoIntsStruct_realloc_81_goodB2G : public CWE401_Memory_Leak__twoIntsStruct_realloc_81_base\r\n{\r\npublic:\r\n void action(twoIntsStruct * data) const;\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_realloc_83_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_realloc_83_goodB2G.cpp","cwe_id":"CWE401","file_extension":".cpp","language_hint":"C++","source_size_bytes":1389,"source_sha256":"101408270b46048d9ef5b87a09b4b27a0459e303075a664b86fc07f00b0b5713","line_count":42,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_realloc_83_goodB2G.cpp\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-83_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE401_Memory_Leak__twoIntsStruct_realloc_83.h\"\r\n\r\nnamespace CWE401_Memory_Leak__twoIntsStruct_realloc_83\r\n{\r\nCWE401_Memory_Leak__twoIntsStruct_realloc_83_goodB2G::CWE401_Memory_Leak__twoIntsStruct_realloc_83_goodB2G(twoIntsStruct * dataCopy)\r\n{\r\n data = dataCopy;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (twoIntsStruct *)realloc(data, 100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n data[0].intOne = 0;\r\n data[0].intTwo = 0;\r\n printStructLine(&data[0]);\r\n}\r\n\r\nCWE401_Memory_Leak__twoIntsStruct_realloc_83_goodB2G::~CWE401_Memory_Leak__twoIntsStruct_realloc_83_goodB2G()\r\n{\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_realloc_84.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__twoIntsStruct_realloc_84.h","cwe_id":"CWE401","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1569,"source_sha256":"c9e4d1843d77d4cebc584eda192ad77f8ab4bacae19695d26ddbbafa07f73640","line_count":63,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__twoIntsStruct_realloc_84.h\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-84.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE401_Memory_Leak__twoIntsStruct_realloc_84\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE401_Memory_Leak__twoIntsStruct_realloc_84_bad\r\n{\r\npublic:\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_84_bad(twoIntsStruct * dataCopy);\r\n ~CWE401_Memory_Leak__twoIntsStruct_realloc_84_bad();\r\n\r\nprivate:\r\n twoIntsStruct * data;\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE401_Memory_Leak__twoIntsStruct_realloc_84_goodG2B\r\n{\r\npublic:\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_84_goodG2B(twoIntsStruct * dataCopy);\r\n ~CWE401_Memory_Leak__twoIntsStruct_realloc_84_goodG2B();\r\n\r\nprivate:\r\n twoIntsStruct * data;\r\n};\r\n\r\nclass CWE401_Memory_Leak__twoIntsStruct_realloc_84_goodB2G\r\n{\r\npublic:\r\n CWE401_Memory_Leak__twoIntsStruct_realloc_84_goodB2G(twoIntsStruct * dataCopy);\r\n ~CWE401_Memory_Leak__twoIntsStruct_realloc_84_goodB2G();\r\n\r\nprivate:\r\n twoIntsStruct * data;\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_calloc_12.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_calloc_12.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":4530,"source_sha256":"aba553ac762f09cdbe13df154e4235eb394d7cb12940a03ca30a51b0358edbda","line_count":165,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__wchar_t_calloc_12.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-12.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_calloc_12_bad()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (wchar_t *)calloc(100, sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n }\r\n else\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n else\r\n {\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the first \"if\" so that\r\n both branches use the BadSource and the second \"if\" so that both branches\r\n use the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (wchar_t *)calloc(100, sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n }\r\n else\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (wchar_t *)calloc(100, sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n else\r\n {\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the first \"if\" so that\r\n both branches use the GoodSource and the second \"if\" so that both branches\r\n use the BadSink */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n }\r\n else\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n else\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_calloc_12_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__wchar_t_calloc_12_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__wchar_t_calloc_12_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_calloc_52b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_calloc_52b.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":1483,"source_sha256":"122f770a4d9b890eb3aec38e72a42ad097f04a90ce34bc30cdc5919ac8281bbc","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__wchar_t_calloc_52b.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-52b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE401_Memory_Leak__wchar_t_calloc_52c_badSink(wchar_t * data);\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_calloc_52b_badSink(wchar_t * data)\r\n{\r\n CWE401_Memory_Leak__wchar_t_calloc_52c_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__wchar_t_calloc_52c_goodG2BSink(wchar_t * data);\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_calloc_52b_goodG2BSink(wchar_t * data)\r\n{\r\n CWE401_Memory_Leak__wchar_t_calloc_52c_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__wchar_t_calloc_52c_goodB2GSink(wchar_t * data);\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_calloc_52b_goodB2GSink(wchar_t * data)\r\n{\r\n CWE401_Memory_Leak__wchar_t_calloc_52c_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_calloc_53a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_calloc_53a.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":3062,"source_sha256":"e36e8c59d201cf72cbe9061900f2375b9a7956978fcd10758d9eda019df17dfc","line_count":107,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__wchar_t_calloc_53a.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-53a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE401_Memory_Leak__wchar_t_calloc_53b_badSink(wchar_t * data);\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_calloc_53_bad()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (wchar_t *)calloc(100, sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n CWE401_Memory_Leak__wchar_t_calloc_53b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__wchar_t_calloc_53b_goodG2BSink(wchar_t * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n CWE401_Memory_Leak__wchar_t_calloc_53b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__wchar_t_calloc_53b_goodB2GSink(wchar_t * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (wchar_t *)calloc(100, sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n CWE401_Memory_Leak__wchar_t_calloc_53b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_calloc_53_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__wchar_t_calloc_53_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__wchar_t_calloc_53_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_calloc_54e.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_calloc_54e.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":1325,"source_sha256":"6c2a07db7118810fb858ac850fbf6fd9b723529e1c5510833e7acdd4d3a2696a","line_count":48,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__wchar_t_calloc_54e.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-54e.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_calloc_54e_badSink(wchar_t * data)\r\n{\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__wchar_t_calloc_54e_goodG2BSink(wchar_t * data)\r\n{\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__wchar_t_calloc_54e_goodB2GSink(wchar_t * data)\r\n{\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_calloc_62b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_calloc_62b.cpp","cwe_id":"CWE401","file_extension":".cpp","language_hint":"C++","source_size_bytes":1704,"source_sha256":"59a308446602ecece963cb9753908541fcdd2f5a50e9c673bf1d2870cba90a52","line_count":64,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__wchar_t_calloc_62b.cpp\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-62b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE401_Memory_Leak__wchar_t_calloc_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSource(wchar_t * &data)\r\n{\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (wchar_t *)calloc(100, sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(wchar_t * &data)\r\n{\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(wchar_t * &data)\r\n{\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (wchar_t *)calloc(100, sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_calloc_81_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_calloc_81_bad.cpp","cwe_id":"CWE401","file_extension":".cpp","language_hint":"C++","source_size_bytes":909,"source_sha256":"f5f849e0fc7f1061de2aefddd0c226ad96be27d1d7ea1c0ab58302dc3634a864","line_count":32,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__wchar_t_calloc_81_bad.cpp\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-81_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE401_Memory_Leak__wchar_t_calloc_81.h\"\r\n\r\nnamespace CWE401_Memory_Leak__wchar_t_calloc_81\r\n{\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_calloc_81_bad::action(wchar_t * data) const\r\n{\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_calloc_83a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_calloc_83a.cpp","cwe_id":"CWE401","file_extension":".cpp","language_hint":"C++","source_size_bytes":2168,"source_sha256":"8c97a55bc9d49507c8993c81fc78c8c926baa9629e4ac4abe1afbe24bd6d4ab0","line_count":89,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__wchar_t_calloc_83a.cpp\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-83a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE401_Memory_Leak__wchar_t_calloc_83.h\"\r\n\r\nnamespace CWE401_Memory_Leak__wchar_t_calloc_83\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n CWE401_Memory_Leak__wchar_t_calloc_83_bad badObject(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n CWE401_Memory_Leak__wchar_t_calloc_83_goodG2B goodG2BObject(data);\r\n}\r\n\r\n/* goodG2B uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n CWE401_Memory_Leak__wchar_t_calloc_83_goodB2G goodB2GObject(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE401_Memory_Leak__wchar_t_calloc_83; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_calloc_84_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_calloc_84_goodG2B.cpp","cwe_id":"CWE401","file_extension":".cpp","language_hint":"C++","source_size_bytes":1335,"source_sha256":"be2dc2fc46dd9dfd5d0e22ff673372530233289d8637bccf0aea3e61ed6b0930","line_count":40,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__wchar_t_calloc_84_goodG2B.cpp\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-84_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: calloc Allocate data using calloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE401_Memory_Leak__wchar_t_calloc_84.h\"\r\n\r\nnamespace CWE401_Memory_Leak__wchar_t_calloc_84\r\n{\r\nCWE401_Memory_Leak__wchar_t_calloc_84_goodG2B::CWE401_Memory_Leak__wchar_t_calloc_84_goodG2B(wchar_t * dataCopy)\r\n{\r\n data = dataCopy;\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n}\r\n\r\nCWE401_Memory_Leak__wchar_t_calloc_84_goodG2B::~CWE401_Memory_Leak__wchar_t_calloc_84_goodG2B()\r\n{\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_malloc_42.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_malloc_42.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":3126,"source_sha256":"70fff7181de946cc9bfdd81cc6344b038e9c5e8056ea31b7a63fffbcecf89d01","line_count":121,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__wchar_t_malloc_42.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-42.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: malloc Allocate data using malloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 42 Data flow: data returned from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nstatic wchar_t * badSource(wchar_t * data)\r\n{\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n return data;\r\n}\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_malloc_42_bad()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n data = badSource(data);\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic wchar_t * goodG2BSource(wchar_t * data)\r\n{\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n return data;\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n data = goodG2BSource(data);\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic wchar_t * goodB2GSource(wchar_t * data)\r\n{\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n return data;\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n data = goodB2GSource(data);\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n}\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_malloc_42_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__wchar_t_malloc_42_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__wchar_t_malloc_42_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_malloc_53c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_malloc_53c.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":1510,"source_sha256":"e8024f986bc9bc81cdd3b7f37127c095607564f8af360fb9d1c5bc0cd4b87546","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__wchar_t_malloc_53c.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-53c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: malloc Allocate data using malloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE401_Memory_Leak__wchar_t_malloc_53d_badSink(wchar_t * data);\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_malloc_53c_badSink(wchar_t * data)\r\n{\r\n CWE401_Memory_Leak__wchar_t_malloc_53d_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__wchar_t_malloc_53d_goodG2BSink(wchar_t * data);\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_malloc_53c_goodG2BSink(wchar_t * data)\r\n{\r\n CWE401_Memory_Leak__wchar_t_malloc_53d_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__wchar_t_malloc_53d_goodB2GSink(wchar_t * data);\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_malloc_53c_goodB2GSink(wchar_t * data)\r\n{\r\n CWE401_Memory_Leak__wchar_t_malloc_53d_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_malloc_83_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_malloc_83_goodG2B.cpp","cwe_id":"CWE401","file_extension":".cpp","language_hint":"C++","source_size_bytes":1310,"source_sha256":"32b4c583edc0e755916531d1c04b2451820e1b980479c73bcb29a2399a11438c","line_count":40,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__wchar_t_malloc_83_goodG2B.cpp\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-83_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: malloc Allocate data using malloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE401_Memory_Leak__wchar_t_malloc_83.h\"\r\n\r\nnamespace CWE401_Memory_Leak__wchar_t_malloc_83\r\n{\r\nCWE401_Memory_Leak__wchar_t_malloc_83_goodG2B::CWE401_Memory_Leak__wchar_t_malloc_83_goodG2B(wchar_t * dataCopy)\r\n{\r\n data = dataCopy;\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n}\r\n\r\nCWE401_Memory_Leak__wchar_t_malloc_83_goodG2B::~CWE401_Memory_Leak__wchar_t_malloc_83_goodG2B()\r\n{\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_malloc_84_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_malloc_84_goodG2B.cpp","cwe_id":"CWE401","file_extension":".cpp","language_hint":"C++","source_size_bytes":1335,"source_sha256":"7d552eb607fb5571e92947a197c9fca139bdefb8fcfbe6cadb11c1a502eceea5","line_count":40,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__wchar_t_malloc_84_goodG2B.cpp\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-84_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: malloc Allocate data using malloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE401_Memory_Leak__wchar_t_malloc_84.h\"\r\n\r\nnamespace CWE401_Memory_Leak__wchar_t_malloc_84\r\n{\r\nCWE401_Memory_Leak__wchar_t_malloc_84_goodG2B::CWE401_Memory_Leak__wchar_t_malloc_84_goodG2B(wchar_t * dataCopy)\r\n{\r\n data = dataCopy;\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n}\r\n\r\nCWE401_Memory_Leak__wchar_t_malloc_84_goodG2B::~CWE401_Memory_Leak__wchar_t_malloc_84_goodG2B()\r\n{\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_realloc_13.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_realloc_13.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":4795,"source_sha256":"7082afab487a8c570674ecce7e5aeeb81ab49d59a206389751cca8305043d4d7","line_count":174,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__wchar_t_realloc_13.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-13.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 13 Control flow: if(GLOBAL_CONST_FIVE==5) and if(GLOBAL_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_realloc_13_bad()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (wchar_t *)realloc(data, 100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_FIVE==5 to GLOBAL_CONST_FIVE!=5 */\r\nstatic void goodB2G1()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (wchar_t *)realloc(data, 100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n }\r\n if(GLOBAL_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (wchar_t *)realloc(data, 100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_FIVE==5 to GLOBAL_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_realloc_13_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__wchar_t_realloc_13_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__wchar_t_realloc_13_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_realloc_53c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_realloc_53c.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":1522,"source_sha256":"5da9438b908f204d19eb5cd8124e5fa862173e9a4188119dcd79b44ec334a6e2","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__wchar_t_realloc_53c.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-53c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE401_Memory_Leak__wchar_t_realloc_53d_badSink(wchar_t * data);\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_realloc_53c_badSink(wchar_t * data)\r\n{\r\n CWE401_Memory_Leak__wchar_t_realloc_53d_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__wchar_t_realloc_53d_goodG2BSink(wchar_t * data);\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_realloc_53c_goodG2BSink(wchar_t * data)\r\n{\r\n CWE401_Memory_Leak__wchar_t_realloc_53d_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__wchar_t_realloc_53d_goodB2GSink(wchar_t * data);\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_realloc_53c_goodB2GSink(wchar_t * data)\r\n{\r\n CWE401_Memory_Leak__wchar_t_realloc_53d_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_realloc_54a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_realloc_54a.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":3088,"source_sha256":"fa0e54ebdbb4ab181c5b89a92b21cf3972a6979403a1c154dd4c8500808643bb","line_count":107,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__wchar_t_realloc_54a.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-54a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE401_Memory_Leak__wchar_t_realloc_54b_badSink(wchar_t * data);\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_realloc_54_bad()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (wchar_t *)realloc(data, 100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n CWE401_Memory_Leak__wchar_t_realloc_54b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__wchar_t_realloc_54b_goodG2BSink(wchar_t * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n CWE401_Memory_Leak__wchar_t_realloc_54b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__wchar_t_realloc_54b_goodB2GSink(wchar_t * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (wchar_t *)realloc(data, 100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n CWE401_Memory_Leak__wchar_t_realloc_54b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_realloc_54_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__wchar_t_realloc_54_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__wchar_t_realloc_54_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_realloc_54c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_realloc_54c.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":1523,"source_sha256":"9653522828c98a3e8cd2edd642b3ef4088bb5960606f1fb9cfd304f2508dd1df","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__wchar_t_realloc_54c.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-54c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE401_Memory_Leak__wchar_t_realloc_54d_badSink(wchar_t * data);\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_realloc_54c_badSink(wchar_t * data)\r\n{\r\n CWE401_Memory_Leak__wchar_t_realloc_54d_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__wchar_t_realloc_54d_goodG2BSink(wchar_t * data);\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_realloc_54c_goodG2BSink(wchar_t * data)\r\n{\r\n CWE401_Memory_Leak__wchar_t_realloc_54d_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__wchar_t_realloc_54d_goodB2GSink(wchar_t * data);\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_realloc_54c_goodB2GSink(wchar_t * data)\r\n{\r\n CWE401_Memory_Leak__wchar_t_realloc_54d_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_realloc_61a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_realloc_61a.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":2629,"source_sha256":"99d5e4b2e0de3b26a00747cba2f2b93230788d2e7d11f28008fe0ee27c23ee6e","line_count":96,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__wchar_t_realloc_61a.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-61a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 61 Data flow: data returned from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nwchar_t * CWE401_Memory_Leak__wchar_t_realloc_61b_badSource(wchar_t * data);\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_realloc_61_bad()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n data = CWE401_Memory_Leak__wchar_t_realloc_61b_badSource(data);\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nwchar_t * CWE401_Memory_Leak__wchar_t_realloc_61b_goodG2BSource(wchar_t * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n data = CWE401_Memory_Leak__wchar_t_realloc_61b_goodG2BSource(data);\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nwchar_t * CWE401_Memory_Leak__wchar_t_realloc_61b_goodB2GSource(wchar_t * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n data = CWE401_Memory_Leak__wchar_t_realloc_61b_goodB2GSource(data);\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n}\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_realloc_61_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE401_Memory_Leak__wchar_t_realloc_61_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE401_Memory_Leak__wchar_t_realloc_61_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_realloc_64b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_realloc_64b.c","cwe_id":"CWE401","file_extension":".c","language_hint":"C","source_size_bytes":1881,"source_sha256":"088e7e1f69ac019dbeb72c8ca1aa9ff7d02d2547fc402f719ada14fdb09aaa1a","line_count":60,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__wchar_t_realloc_64b.c\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-64b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE401_Memory_Leak__wchar_t_realloc_64b_badSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n wchar_t * * dataPtr = (wchar_t * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n wchar_t * data = (*dataPtr);\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE401_Memory_Leak__wchar_t_realloc_64b_goodG2BSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n wchar_t * * dataPtr = (wchar_t * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n wchar_t * data = (*dataPtr);\r\n /* POTENTIAL FLAW: No deallocation */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE401_Memory_Leak__wchar_t_realloc_64b_goodB2GSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n wchar_t * * dataPtr = (wchar_t * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n wchar_t * data = (*dataPtr);\r\n /* FIX: Deallocate memory */\r\n free(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_realloc_73a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_realloc_73a.cpp","cwe_id":"CWE401","file_extension":".cpp","language_hint":"C++","source_size_bytes":3347,"source_sha256":"7531173d37777144d7870cd23e5b0616d130596f6bc5c9313665a6bdb8d599e2","line_count":131,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__wchar_t_realloc_73a.cpp\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-73a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE401_Memory_Leak__wchar_t_realloc_73\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(list dataList);\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n list dataList;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (wchar_t *)realloc(data, 100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n /* Put data in a list */\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n badSink(dataList);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(list dataList);\r\n\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n list dataList;\r\n data = NULL;\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n /* Put data in a list */\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n goodG2BSink(dataList);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(list dataList);\r\n\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n list dataList;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (wchar_t *)realloc(data, 100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n goodB2GSink(dataList);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE401_Memory_Leak__wchar_t_realloc_73; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_realloc_81a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE401_Memory_Leak/s03/CWE401_Memory_Leak__wchar_t_realloc_81a.cpp","cwe_id":"CWE401","file_extension":".cpp","language_hint":"C++","source_size_bytes":3093,"source_sha256":"51715dbdaa61881d7bf4aaf850364c2fe7eac528ad3b4b133dd440aff306a2ca","line_count":109,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE401_Memory_Leak__wchar_t_realloc_81a.cpp\r\nLabel Definition File: CWE401_Memory_Leak.c.label.xml\r\nTemplate File: sources-sinks-81a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 401 Memory Leak\r\n * BadSource: realloc Allocate data using realloc()\r\n * GoodSource: Allocate data on the stack\r\n * Sinks:\r\n * GoodSink: call free() on data\r\n * BadSink : no deallocation of data\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE401_Memory_Leak__wchar_t_realloc_81.h\"\r\n\r\nnamespace CWE401_Memory_Leak__wchar_t_realloc_81\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (wchar_t *)realloc(data, 100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n const CWE401_Memory_Leak__wchar_t_realloc_81_base& baseObject = CWE401_Memory_Leak__wchar_t_realloc_81_bad();\r\n baseObject.action(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n /* FIX: Use memory allocated on the stack with ALLOCA */\r\n data = (wchar_t *)ALLOCA(100*sizeof(wchar_t));\r\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n const CWE401_Memory_Leak__wchar_t_realloc_81_base& baseObject = CWE401_Memory_Leak__wchar_t_realloc_81_goodG2B();\r\n baseObject.action(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n data = NULL;\r\n /* POTENTIAL FLAW: Allocate memory on the heap */\r\n data = (wchar_t *)realloc(data, 100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* Initialize and make use of data */\r\n wcscpy(data, L\"A String\");\r\n printWLine(data);\r\n const CWE401_Memory_Leak__wchar_t_realloc_81_base& baseObject = CWE401_Memory_Leak__wchar_t_realloc_81_goodB2G();\r\n baseObject.action(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE401_Memory_Leak__wchar_t_realloc_81; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_char_13.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_char_13.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":4686,"source_sha256":"469273b971ec999fb21c7d87b96134114aa00743466a965ca61448ee5027c7e1","line_count":171,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_char_13.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-13.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 13 Control flow: if(GLOBAL_CONST_FIVE==5) and if(GLOBAL_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE415_Double_Free__malloc_free_char_13_bad()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_FIVE==5 to GLOBAL_CONST_FIVE!=5 */\r\nstatic void goodB2G1()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n }\r\n if(GLOBAL_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_FIVE==5 to GLOBAL_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_char_13_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE415_Double_Free__malloc_free_char_13_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE415_Double_Free__malloc_free_char_13_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_char_17.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_char_17.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":3125,"source_sha256":"254b56d925c4b9249ba6dcf877edc259733548a19f6e7d4f095c5e0cd79f190c","line_count":122,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_char_17.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-17.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 17 Control flow: for loops\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE415_Double_Free__malloc_free_char_17_bad()\r\n{\r\n int i,j;\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n for(i = 0; i < 1; i++)\r\n {\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n }\r\n for(j = 0; j < 1; j++)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink in the for statements */\r\nstatic void goodB2G()\r\n{\r\n int i,k;\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n for(i = 0; i < 1; i++)\r\n {\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n }\r\n for(k = 0; k < 1; k++)\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink in the for statements */\r\nstatic void goodG2B()\r\n{\r\n int h,j;\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n for(h = 0; h < 1; h++)\r\n {\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n }\r\n for(j = 0; j < 1; j++)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_char_17_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE415_Double_Free__malloc_free_char_17_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE415_Double_Free__malloc_free_char_17_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_char_82_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_char_82_bad.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":914,"source_sha256":"a7536fc9405c943838b30530c8bdc1784e276fca005abeb2d21bad6c8f20ff17","line_count":32,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_char_82_bad.cpp\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-82_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE415_Double_Free__malloc_free_char_82.h\"\r\n\r\nnamespace CWE415_Double_Free__malloc_free_char_82\r\n{\r\n\r\nvoid CWE415_Double_Free__malloc_free_char_82_bad::action(char * data)\r\n{\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_char_83.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_char_83.h","cwe_id":"CWE415","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1469,"source_sha256":"156dc75e2650c9fac04136774d73595513d8a80309336145ac52aca16019c537","line_count":63,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_char_83.h\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-83.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__malloc_free_char_83\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE415_Double_Free__malloc_free_char_83_bad\r\n{\r\npublic:\r\n CWE415_Double_Free__malloc_free_char_83_bad(char * dataCopy);\r\n ~CWE415_Double_Free__malloc_free_char_83_bad();\r\n\r\nprivate:\r\n char * data;\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE415_Double_Free__malloc_free_char_83_goodG2B\r\n{\r\npublic:\r\n CWE415_Double_Free__malloc_free_char_83_goodG2B(char * dataCopy);\r\n ~CWE415_Double_Free__malloc_free_char_83_goodG2B();\r\n\r\nprivate:\r\n char * data;\r\n};\r\n\r\nclass CWE415_Double_Free__malloc_free_char_83_goodB2G\r\n{\r\npublic:\r\n CWE415_Double_Free__malloc_free_char_83_goodB2G(char * dataCopy);\r\n ~CWE415_Double_Free__malloc_free_char_83_goodB2G();\r\n\r\nprivate:\r\n char * data;\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_int64_t_18.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_int64_t_18.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":3018,"source_sha256":"60e9887dd66b9f80c5dc0a488708d267a82b699a45f2c9644a91b7b1f22a5162","line_count":113,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_int64_t_18.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-18.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 18 Control flow: goto statements\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE415_Double_Free__malloc_free_int64_t_18_bad()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goto source;\r\nsource:\r\n data = (int64_t *)malloc(100*sizeof(int64_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by reversing the blocks on the second goto statement */\r\nstatic void goodB2G()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goto source;\r\nsource:\r\n data = (int64_t *)malloc(100*sizeof(int64_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n goto sink;\r\nsink:\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by reversing the blocks on the first goto statement */\r\nstatic void goodG2B()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goto source;\r\nsource:\r\n data = (int64_t *)malloc(100*sizeof(int64_t));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_int64_t_18_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE415_Double_Free__malloc_free_int64_t_18_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE415_Double_Free__malloc_free_int64_t_18_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_int64_t_41.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_int64_t_41.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":3047,"source_sha256":"fbbe7d534f1f7c33c8ef2eda4efc4dd8a87f608e64f052006f61e815ea4c57df","line_count":116,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_int64_t_41.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-41.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 41 Data flow: data passed as an argument from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink(int64_t * data)\r\n{\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_int64_t_41_bad()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (int64_t *)malloc(100*sizeof(int64_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink(int64_t * data)\r\n{\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (int64_t *)malloc(100*sizeof(int64_t));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink(int64_t * data)\r\n{\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (int64_t *)malloc(100*sizeof(int64_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n goodB2GSink(data);\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_int64_t_41_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE415_Double_Free__malloc_free_int64_t_41_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE415_Double_Free__malloc_free_int64_t_41_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_int64_t_74a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_int64_t_74a.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":3253,"source_sha256":"e7a5483273132da45a4f2847515e1ab5e6155aedd9d59b3e2a0fee915514c2bd","line_count":128,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_int64_t_74a.cpp\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-74a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 74 Data flow: data passed in a map from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE415_Double_Free__malloc_free_int64_t_74\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(map dataMap);\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n map dataMap;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (int64_t *)malloc(100*sizeof(int64_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n /* Put data in a map */\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n badSink(dataMap);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(map dataMap);\r\n\r\nstatic void goodG2B()\r\n{\r\n int64_t * data;\r\n map dataMap;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (int64_t *)malloc(100*sizeof(int64_t));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n /* Put data in a map */\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n goodG2BSink(dataMap);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(map dataMap);\r\n\r\nstatic void goodB2G()\r\n{\r\n int64_t * data;\r\n map dataMap;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (int64_t *)malloc(100*sizeof(int64_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n goodB2GSink(dataMap);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__malloc_free_int64_t_74; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_int64_t_83a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_int64_t_83a.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":2320,"source_sha256":"41eb05f374ed4752077bdd4d196fe4f0cf38f371bc0b3b89c58e84b3ea843469","line_count":92,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_int64_t_83a.cpp\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-83a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE415_Double_Free__malloc_free_int64_t_83.h\"\r\n\r\nnamespace CWE415_Double_Free__malloc_free_int64_t_83\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n CWE415_Double_Free__malloc_free_int64_t_83_bad badObject(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n CWE415_Double_Free__malloc_free_int64_t_83_goodG2B goodG2BObject(data);\r\n}\r\n\r\n/* goodG2B uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n CWE415_Double_Free__malloc_free_int64_t_83_goodB2G goodB2GObject(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__malloc_free_int64_t_83; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_int64_t_84_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_int64_t_84_goodG2B.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":1325,"source_sha256":"c7681492018bd2f6979b94502b953a85c9d37d4dc44346d53934ff6225af33ef","line_count":38,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_int64_t_84_goodG2B.cpp\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-84_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE415_Double_Free__malloc_free_int64_t_84.h\"\r\n\r\nnamespace CWE415_Double_Free__malloc_free_int64_t_84\r\n{\r\nCWE415_Double_Free__malloc_free_int64_t_84_goodG2B::CWE415_Double_Free__malloc_free_int64_t_84_goodG2B(int64_t * dataCopy)\r\n{\r\n data = dataCopy;\r\n data = (int64_t *)malloc(100*sizeof(int64_t));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n}\r\n\r\nCWE415_Double_Free__malloc_free_int64_t_84_goodG2B::~CWE415_Double_Free__malloc_free_int64_t_84_goodG2B()\r\n{\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_int_01.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_int_01.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":2698,"source_sha256":"077dc5fbec209d5bac592fd254318d40600cff593ee0830678ac8047772af43f","line_count":101,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_int_01.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-01.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 01 Baseline\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE415_Double_Free__malloc_free_int_01_bad()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_int_01_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE415_Double_Free__malloc_free_int_01_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE415_Double_Free__malloc_free_int_01_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_int_09.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_int_09.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":4623,"source_sha256":"41e0d507aea5637d9e15793a7e27e3d13f5647f16444a6b8012643cae2b644b8","line_count":171,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_int_09.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-09.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE415_Double_Free__malloc_free_int_09_bad()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n }\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_int_09_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE415_Double_Free__malloc_free_int_09_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE415_Double_Free__malloc_free_int_09_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_int_16.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_int_16.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":3141,"source_sha256":"80e8219f1222dcfd1c5c8192f7c873d0343547d2d0e5ee5e3c22170c9c2767bf","line_count":125,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_int_16.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-16.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 16 Control flow: while(1)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE415_Double_Free__malloc_free_int_16_bad()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n while(1)\r\n {\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the sinks in the second while statement */\r\nstatic void goodB2G()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n while(1)\r\n {\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the sources in the first while statement */\r\nstatic void goodG2B()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n while(1)\r\n {\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_int_16_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE415_Double_Free__malloc_free_int_16_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE415_Double_Free__malloc_free_int_16_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_int_81_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_int_81_goodG2B.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":931,"source_sha256":"23e7bfd7d41b9f30936f7103bcf964a3a0cd3358d872c5cb3e7faa0eaebba9f9","line_count":32,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_int_81_goodG2B.cpp\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-81_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE415_Double_Free__malloc_free_int_81.h\"\r\n\r\nnamespace CWE415_Double_Free__malloc_free_int_81\r\n{\r\n\r\nvoid CWE415_Double_Free__malloc_free_int_81_goodG2B::action(int * data) const\r\n{\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_int_82_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_int_82_goodG2B.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":923,"source_sha256":"9d78a01a013a6398585f88572da9b492096d6b193ab5f94fd14b9bcef38fcb4f","line_count":32,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_int_82_goodG2B.cpp\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-82_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE415_Double_Free__malloc_free_int_82.h\"\r\n\r\nnamespace CWE415_Double_Free__malloc_free_int_82\r\n{\r\n\r\nvoid CWE415_Double_Free__malloc_free_int_82_goodG2B::action(int * data)\r\n{\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_int_83_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_int_83_goodG2B.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":1260,"source_sha256":"ab1d4f66eeb4599bf743aed7d1e1f58181923a9af2570f45b2e1f3aa8b31556e","line_count":38,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_int_83_goodG2B.cpp\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-83_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE415_Double_Free__malloc_free_int_83.h\"\r\n\r\nnamespace CWE415_Double_Free__malloc_free_int_83\r\n{\r\nCWE415_Double_Free__malloc_free_int_83_goodG2B::CWE415_Double_Free__malloc_free_int_83_goodG2B(int * dataCopy)\r\n{\r\n data = dataCopy;\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n}\r\n\r\nCWE415_Double_Free__malloc_free_int_83_goodG2B::~CWE415_Double_Free__malloc_free_int_83_goodG2B()\r\n{\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_long_12.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_long_12.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":4414,"source_sha256":"1f7085ddf65d8bb8d4378d24c16552b5f97f86d051c2ea73341222b568616253","line_count":159,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_long_12.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-12.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE415_Double_Free__malloc_free_long_12_bad()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n }\r\n else\r\n {\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n else\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the first \"if\" so that\r\n both branches use the BadSource and the second \"if\" so that both branches\r\n use the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n }\r\n else\r\n {\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n else\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the first \"if\" so that\r\n both branches use the GoodSource and the second \"if\" so that both branches\r\n use the BadSink */\r\nstatic void goodG2B()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n }\r\n else\r\n {\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n else\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_long_12_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE415_Double_Free__malloc_free_long_12_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE415_Double_Free__malloc_free_long_12_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_long_21.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_long_21.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":4424,"source_sha256":"9de461f5994ae00093c661e71129befb50ced50d22b8a168aa66ac11a11932e8","line_count":166,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_long_21.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-21.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 21 Control flow: Flow controlled by value of a static global variable. All functions contained in one file.\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* The static variable below is used to drive control flow in the sink function */\r\nstatic int badStatic = 0;\r\n\r\nstatic void badSink(long * data)\r\n{\r\n if(badStatic)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_long_21_bad()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n badStatic = 1; /* true */\r\n badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* The static variables below are used to drive control flow in the sink functions. */\r\nstatic int goodB2G1Static = 0;\r\nstatic int goodB2G2Static = 0;\r\nstatic int goodG2BStatic = 0;\r\n\r\n/* goodB2G1() - use badsource and goodsink by setting the static variable to false instead of true */\r\nstatic void goodB2G1Sink(long * data)\r\n{\r\n if(goodB2G1Static)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\nstatic void goodB2G1()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n goodB2G1Static = 0; /* false */\r\n goodB2G1Sink(data);\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */\r\nstatic void goodB2G2Sink(long * data)\r\n{\r\n if(goodB2G2Static)\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\nstatic void goodB2G2()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n goodB2G2Static = 1; /* true */\r\n goodB2G2Sink(data);\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink */\r\nstatic void goodG2BSink(long * data)\r\n{\r\n if(goodG2BStatic)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n goodG2BStatic = 1; /* true */\r\n goodG2BSink(data);\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_long_21_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE415_Double_Free__malloc_free_long_21_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE415_Double_Free__malloc_free_long_21_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_long_54c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_long_54c.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":1549,"source_sha256":"a25e3c2e59f8e66b2bbba699b64b09ed988defcead8a58e9fc0ae0b5ea2ac7bd","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_long_54c.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-54c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE415_Double_Free__malloc_free_long_54d_badSink(long * data);\r\n\r\nvoid CWE415_Double_Free__malloc_free_long_54c_badSink(long * data)\r\n{\r\n CWE415_Double_Free__malloc_free_long_54d_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE415_Double_Free__malloc_free_long_54d_goodG2BSink(long * data);\r\n\r\nvoid CWE415_Double_Free__malloc_free_long_54c_goodG2BSink(long * data)\r\n{\r\n CWE415_Double_Free__malloc_free_long_54d_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE415_Double_Free__malloc_free_long_54d_goodB2GSink(long * data);\r\n\r\nvoid CWE415_Double_Free__malloc_free_long_54c_goodB2GSink(long * data)\r\n{\r\n CWE415_Double_Free__malloc_free_long_54d_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_long_54e.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_long_54e.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":1383,"source_sha256":"0ed07c951669235da515ac67aad72bb38744bbe621d2a03509daf954966d567c","line_count":49,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_long_54e.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-54e.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE415_Double_Free__malloc_free_long_54e_badSink(long * data)\r\n{\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE415_Double_Free__malloc_free_long_54e_goodG2BSink(long * data)\r\n{\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE415_Double_Free__malloc_free_long_54e_goodB2GSink(long * data)\r\n{\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_long_63a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_long_63a.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":2972,"source_sha256":"25e9dbf00e9bc654e148574769dcb5891ab88b77550747a2038fe23ed163ea92","line_count":104,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_long_63a.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-63a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE415_Double_Free__malloc_free_long_63b_badSink(long * * dataPtr);\r\n\r\nvoid CWE415_Double_Free__malloc_free_long_63_bad()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n CWE415_Double_Free__malloc_free_long_63b_badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE415_Double_Free__malloc_free_long_63b_goodG2BSink(long * * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n CWE415_Double_Free__malloc_free_long_63b_goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE415_Double_Free__malloc_free_long_63b_goodB2GSink(long * * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n CWE415_Double_Free__malloc_free_long_63b_goodB2GSink(&data);\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_long_63_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE415_Double_Free__malloc_free_long_63_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE415_Double_Free__malloc_free_long_63_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_long_63b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_long_63b.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":1436,"source_sha256":"bee7ddbb38d4f86af71d3a9c2cd147c560fc4987e7b07d2f5136a76b0398355c","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_long_63b.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-63b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE415_Double_Free__malloc_free_long_63b_badSink(long * * dataPtr)\r\n{\r\n long * data = *dataPtr;\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE415_Double_Free__malloc_free_long_63b_goodG2BSink(long * * dataPtr)\r\n{\r\n long * data = *dataPtr;\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE415_Double_Free__malloc_free_long_63b_goodB2GSink(long * * dataPtr)\r\n{\r\n long * data = *dataPtr;\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_long_64a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_long_64a.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":2989,"source_sha256":"0d6d674d1cfa18424999a560905b68ff8122bbe3095071bcdcff3ab50f955235","line_count":104,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_long_64a.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-64a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE415_Double_Free__malloc_free_long_64b_badSink(void * dataVoidPtr);\r\n\r\nvoid CWE415_Double_Free__malloc_free_long_64_bad()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n CWE415_Double_Free__malloc_free_long_64b_badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE415_Double_Free__malloc_free_long_64b_goodG2BSink(void * dataVoidPtr);\r\n\r\nstatic void goodG2B()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n CWE415_Double_Free__malloc_free_long_64b_goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE415_Double_Free__malloc_free_long_64b_goodB2GSink(void * dataVoidPtr);\r\n\r\nstatic void goodB2G()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n CWE415_Double_Free__malloc_free_long_64b_goodB2GSink(&data);\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_long_64_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE415_Double_Free__malloc_free_long_64_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE415_Double_Free__malloc_free_long_64_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_long_73b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_long_73b.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":1513,"source_sha256":"1440d22b5cb9bb1b9950ca8313e7ad875be192a632f844a53d37173b2c3fd078","line_count":61,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_long_73b.cpp\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-73b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE415_Double_Free__malloc_free_long_73\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(list dataList)\r\n{\r\n /* copy data out of dataList */\r\n long * data = dataList.back();\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(list dataList)\r\n{\r\n long * data = dataList.back();\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(list dataList)\r\n{\r\n long * data = dataList.back();\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_long_82_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_long_82_goodB2G.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":984,"source_sha256":"98408b4b9a61b593d498fd8491079601819e872a40180259281b154d45d1a381","line_count":33,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_long_82_goodB2G.cpp\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-82_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE415_Double_Free__malloc_free_long_82.h\"\r\n\r\nnamespace CWE415_Double_Free__malloc_free_long_82\r\n{\r\n\r\nvoid CWE415_Double_Free__malloc_free_long_82_goodB2G::action(long * data)\r\n{\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_struct_04.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_struct_04.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":5059,"source_sha256":"77fa681c9725516d345aac6173dcda3a40675fc0e7b3847bb9a3ab3fe7e7638b","line_count":177,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_struct_04.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-04.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 04 Control flow: if(STATIC_CONST_TRUE) and if(STATIC_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The two variables below are declared \"const\", so a tool should\r\n be able to identify that reads of these will always return their\r\n initialized values. */\r\nstatic const int STATIC_CONST_TRUE = 1; /* true */\r\nstatic const int STATIC_CONST_FALSE = 0; /* false */\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE415_Double_Free__malloc_free_struct_04_bad()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second STATIC_CONST_TRUE to STATIC_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n }\r\n if(STATIC_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first STATIC_CONST_TRUE to STATIC_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_struct_04_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE415_Double_Free__malloc_free_struct_04_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE415_Double_Free__malloc_free_struct_04_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_struct_21.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_struct_21.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":4578,"source_sha256":"8b7deededac2ee54aea074c185aa83891a1444cf6d53db4214ad202123eb2d00","line_count":166,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_struct_21.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-21.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 21 Control flow: Flow controlled by value of a static global variable. All functions contained in one file.\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* The static variable below is used to drive control flow in the sink function */\r\nstatic int badStatic = 0;\r\n\r\nstatic void badSink(twoIntsStruct * data)\r\n{\r\n if(badStatic)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_struct_21_bad()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n badStatic = 1; /* true */\r\n badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* The static variables below are used to drive control flow in the sink functions. */\r\nstatic int goodB2G1Static = 0;\r\nstatic int goodB2G2Static = 0;\r\nstatic int goodG2BStatic = 0;\r\n\r\n/* goodB2G1() - use badsource and goodsink by setting the static variable to false instead of true */\r\nstatic void goodB2G1Sink(twoIntsStruct * data)\r\n{\r\n if(goodB2G1Static)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n goodB2G1Static = 0; /* false */\r\n goodB2G1Sink(data);\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */\r\nstatic void goodB2G2Sink(twoIntsStruct * data)\r\n{\r\n if(goodB2G2Static)\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n goodB2G2Static = 1; /* true */\r\n goodB2G2Sink(data);\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink */\r\nstatic void goodG2BSink(twoIntsStruct * data)\r\n{\r\n if(goodG2BStatic)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n goodG2BStatic = 1; /* true */\r\n goodG2BSink(data);\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_struct_21_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE415_Double_Free__malloc_free_struct_21_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE415_Double_Free__malloc_free_struct_21_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_struct_53d.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_struct_53d.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":1417,"source_sha256":"c00eb82feb52a2863b400492a2ba9305366b5db400d1d6c52574a58bae0da1c5","line_count":49,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_struct_53d.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-53d.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE415_Double_Free__malloc_free_struct_53d_badSink(twoIntsStruct * data)\r\n{\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE415_Double_Free__malloc_free_struct_53d_goodG2BSink(twoIntsStruct * data)\r\n{\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE415_Double_Free__malloc_free_struct_53d_goodB2GSink(twoIntsStruct * data)\r\n{\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_struct_81a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_struct_81a.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":3123,"source_sha256":"90f6a754436cda5923c4582c70bc8472de58fcc11c8e46512187a1d11b6701af","line_count":106,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_struct_81a.cpp\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-81a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE415_Double_Free__malloc_free_struct_81.h\"\r\n\r\nnamespace CWE415_Double_Free__malloc_free_struct_81\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n const CWE415_Double_Free__malloc_free_struct_81_base& baseObject = CWE415_Double_Free__malloc_free_struct_81_bad();\r\n baseObject.action(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n const CWE415_Double_Free__malloc_free_struct_81_base& baseObject = CWE415_Double_Free__malloc_free_struct_81_goodG2B();\r\n baseObject.action(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n const CWE415_Double_Free__malloc_free_struct_81_base& baseObject = CWE415_Double_Free__malloc_free_struct_81_goodB2G();\r\n baseObject.action(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__malloc_free_struct_81; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_02.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_02.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":4442,"source_sha256":"c9ffaa3d4289d15d2f4f51ef2ab01d232602accba4ee9ad584c2b971ff8d0f2d","line_count":171,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_wchar_t_02.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-02.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 02 Control flow: if(1) and if(0)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_02_bad()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 1 to 0 */\r\nstatic void goodB2G1()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n }\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n }\r\n if(1)\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 1 to 0 */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_02_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE415_Double_Free__malloc_free_wchar_t_02_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE415_Double_Free__malloc_free_wchar_t_02_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_03.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_03.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":4490,"source_sha256":"399f18e9ae3ea26ce3943edba5b9ad5d07144bee610ec6d78999bc51cae71473","line_count":171,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_wchar_t_03.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-03.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 03 Control flow: if(5==5) and if(5!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_03_bad()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5==5)\r\n {\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 5==5 to 5!=5 */\r\nstatic void goodB2G1()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5==5)\r\n {\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n }\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5==5)\r\n {\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n }\r\n if(5==5)\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 5==5 to 5!=5 */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5==5)\r\n {\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_03_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE415_Double_Free__malloc_free_wchar_t_03_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE415_Double_Free__malloc_free_wchar_t_03_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_09.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_09.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":4703,"source_sha256":"095c82325f82dea93ebfaf02e27de5f1a6e7b318a88c76575a0b792a344bc505","line_count":171,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_wchar_t_09.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-09.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_09_bad()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n }\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_09_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE415_Double_Free__malloc_free_wchar_t_09_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE415_Double_Free__malloc_free_wchar_t_09_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_15.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_15.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":6026,"source_sha256":"c24103552f418a8a72abb6d2f14ef186b099c983f2c813b598aba82ea75628be","line_count":221,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_wchar_t_15.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-15.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 15 Control flow: switch(6) and switch(7)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_15_bad()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second switch to switch(8) */\r\nstatic void goodB2G1()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(8)\r\n {\r\n case 7:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second switch */\r\nstatic void goodB2G2()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first switch to switch(5) */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(5)\r\n {\r\n case 6:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first switch */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_15_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE415_Double_Free__malloc_free_wchar_t_15_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE415_Double_Free__malloc_free_wchar_t_15_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_21.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_21.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":4487,"source_sha256":"10388d029abb00a0448cf3b77bcdc244c718fc8e7c6ae5aa967c66e95ae978da","line_count":166,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_wchar_t_21.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-21.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 21 Control flow: Flow controlled by value of a static global variable. All functions contained in one file.\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* The static variable below is used to drive control flow in the sink function */\r\nstatic int badStatic = 0;\r\n\r\nstatic void badSink(wchar_t * data)\r\n{\r\n if(badStatic)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_21_bad()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n badStatic = 1; /* true */\r\n badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* The static variables below are used to drive control flow in the sink functions. */\r\nstatic int goodB2G1Static = 0;\r\nstatic int goodB2G2Static = 0;\r\nstatic int goodG2BStatic = 0;\r\n\r\n/* goodB2G1() - use badsource and goodsink by setting the static variable to false instead of true */\r\nstatic void goodB2G1Sink(wchar_t * data)\r\n{\r\n if(goodB2G1Static)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\nstatic void goodB2G1()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n goodB2G1Static = 0; /* false */\r\n goodB2G1Sink(data);\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */\r\nstatic void goodB2G2Sink(wchar_t * data)\r\n{\r\n if(goodB2G2Static)\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\nstatic void goodB2G2()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n goodB2G2Static = 1; /* true */\r\n goodB2G2Sink(data);\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink */\r\nstatic void goodG2BSink(wchar_t * data)\r\n{\r\n if(goodG2BStatic)\r\n {\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n goodG2BStatic = 1; /* true */\r\n goodG2BSink(data);\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_21_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE415_Double_Free__malloc_free_wchar_t_21_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE415_Double_Free__malloc_free_wchar_t_21_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_33.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_33.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":3111,"source_sha256":"b425fa0a5d46601ddd24ca68ab401c26d3c87b7a3b2561800a008d1cddb7f322","line_count":119,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_wchar_t_33.cpp\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-33.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 33 Data flow: use of a C++ reference to data within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__malloc_free_wchar_t_33\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n wchar_t * &dataRef = data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n {\r\n wchar_t * data = dataRef;\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n wchar_t * &dataRef = data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n {\r\n wchar_t * data = dataRef;\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n wchar_t * &dataRef = data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n {\r\n wchar_t * data = dataRef;\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__malloc_free_wchar_t_33; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_41.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_41.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":3047,"source_sha256":"12bea26a44bc8deb6056fdc7abedbcc389aae41f13baa16e909e297a04df697c","line_count":116,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_wchar_t_41.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-41.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 41 Data flow: data passed as an argument from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink(wchar_t * data)\r\n{\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_41_bad()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink(wchar_t * data)\r\n{\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink(wchar_t * data)\r\n{\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n goodB2GSink(data);\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_41_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE415_Double_Free__malloc_free_wchar_t_41_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE415_Double_Free__malloc_free_wchar_t_41_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_52c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_52c.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":1376,"source_sha256":"d75ad42a0fb5068b32b0c57136e6c3537ccd138a5e89c7c5a9cd3e71f115b9e8","line_count":49,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_wchar_t_52c.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-52c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_52c_badSink(wchar_t * data)\r\n{\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_52c_goodG2BSink(wchar_t * data)\r\n{\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_52c_goodB2GSink(wchar_t * data)\r\n{\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_54b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_54b.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":1597,"source_sha256":"0b168a9d538c82ec576f8bdd50c0841c251da05d71ddb496d6974c9c570c9013","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_wchar_t_54b.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-54b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_54c_badSink(wchar_t * data);\r\n\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_54b_badSink(wchar_t * data)\r\n{\r\n CWE415_Double_Free__malloc_free_wchar_t_54c_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_54c_goodG2BSink(wchar_t * data);\r\n\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_54b_goodG2BSink(wchar_t * data)\r\n{\r\n CWE415_Double_Free__malloc_free_wchar_t_54c_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_54c_goodB2GSink(wchar_t * data);\r\n\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_54b_goodB2GSink(wchar_t * data)\r\n{\r\n CWE415_Double_Free__malloc_free_wchar_t_54c_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_61b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_61b.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":1682,"source_sha256":"49bb8474ca62bb8287b391678a287a1377c776faf612461e0e6383bde012fbeb","line_count":56,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_wchar_t_61b.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-61b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 61 Data flow: data returned from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nwchar_t * CWE415_Double_Free__malloc_free_wchar_t_61b_badSource(wchar_t * data)\r\n{\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n return data;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nwchar_t * CWE415_Double_Free__malloc_free_wchar_t_61b_goodG2BSource(wchar_t * data)\r\n{\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n return data;\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nwchar_t * CWE415_Double_Free__malloc_free_wchar_t_61b_goodB2GSource(wchar_t * data)\r\n{\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n return data;\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_64a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_64a.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":3049,"source_sha256":"9fe44a4fdd75cf49f670d90667084eeca913ceb89132f9754503622e6ee4d3b7","line_count":104,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_wchar_t_64a.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-64a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_64b_badSink(void * dataVoidPtr);\r\n\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_64_bad()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n CWE415_Double_Free__malloc_free_wchar_t_64b_badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_64b_goodG2BSink(void * dataVoidPtr);\r\n\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Do NOT free data in the source - the bad sink frees data */\r\n CWE415_Double_Free__malloc_free_wchar_t_64b_goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_64b_goodB2GSink(void * dataVoidPtr);\r\n\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Free data in the source - the bad sink frees data as well */\r\n free(data);\r\n CWE415_Double_Free__malloc_free_wchar_t_64b_goodB2GSink(&data);\r\n}\r\n\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_64_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE415_Double_Free__malloc_free_wchar_t_64_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE415_Double_Free__malloc_free_wchar_t_64_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_65b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_65b.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":1401,"source_sha256":"6e6790e763e756650af1bdb032211546935a132354fab6b94486f80c8799ae7c","line_count":49,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_wchar_t_65b.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-65b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_65b_badSink(wchar_t * data)\r\n{\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_65b_goodG2BSink(wchar_t * data)\r\n{\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_65b_goodB2GSink(wchar_t * data)\r\n{\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_66b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__malloc_free_wchar_t_66b.c","cwe_id":"CWE415","file_extension":".c","language_hint":"C","source_size_bytes":1523,"source_sha256":"cccc3807c7f889b031624482730ba8f715e456b5f71e2180a74b2f11e3e73ae4","line_count":53,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__malloc_free_wchar_t_66b.c\r\nLabel Definition File: CWE415_Double_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-66b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using malloc() and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc()\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using free()\r\n * Flow Variant: 66 Data flow: data passed in an array from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_66b_badSink(wchar_t * dataArray[])\r\n{\r\n /* copy data out of dataArray */\r\n wchar_t * data = dataArray[2];\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_66b_goodG2BSink(wchar_t * dataArray[])\r\n{\r\n wchar_t * data = dataArray[2];\r\n /* POTENTIAL FLAW: Possibly freeing memory twice */\r\n free(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE415_Double_Free__malloc_free_wchar_t_66b_goodB2GSink(wchar_t * dataArray[])\r\n{\r\n wchar_t * data = dataArray[2];\r\n /* do nothing */\r\n /* FIX: Don't attempt to free the memory */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_char_03.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_char_03.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":4337,"source_sha256":"38e966f03971a0a4a5ea5f12b6778d6c10393d731567ff4e6570d8859f24d12e","line_count":173,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_char_03.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-03.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 03 Control flow: if(5==5) and if(5!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_char_03\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5==5)\r\n {\r\n data = new char[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 5==5 to 5!=5 */\r\nstatic void goodB2G1()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5==5)\r\n {\r\n data = new char[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5==5)\r\n {\r\n data = new char[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(5==5)\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 5==5 to 5!=5 */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new char[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5==5)\r\n {\r\n data = new char[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__new_delete_array_char_03; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_char_62a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_char_62a.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":2553,"source_sha256":"89cd1858dd5da71d268859bd0c64f5bf0782f72b00eab97fb0d2b5b332a34d23","line_count":107,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_char_62a.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-62a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_char_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSource(char * &data);\r\n\r\nvoid bad()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n badSource(data);\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(char * &data);\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goodG2BSource(data);\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(char * &data);\r\n\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goodB2GSource(data);\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__new_delete_array_char_62; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_char_67b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_char_67b.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":1535,"source_sha256":"0b0387cee4d381de9c8b70477bfb6dd565a57056a7eb1f9c49a08fee81926755","line_count":62,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_char_67b.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-67b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_char_67\r\n{\r\n\r\ntypedef struct _structType\r\n{\r\n char * structFirst;\r\n} structType;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(structType myStruct)\r\n{\r\n char * data = myStruct.structFirst;\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(structType myStruct)\r\n{\r\n char * data = myStruct.structFirst;\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(structType myStruct)\r\n{\r\n char * data = myStruct.structFirst;\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_class_02.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_class_02.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":4372,"source_sha256":"5c6c90ae6acb4090abce65417a1d1406ac34cb9c9a97c38b2d378dbcff238401","line_count":173,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_class_02.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-02.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 02 Control flow: if(1) and if(0)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_class_02\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = new TwoIntsClass[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 1 to 0 */\r\nstatic void goodB2G1()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = new TwoIntsClass[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = new TwoIntsClass[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(1)\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 1 to 0 */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new TwoIntsClass[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = new TwoIntsClass[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__new_delete_array_class_02; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_class_03.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_class_03.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":4420,"source_sha256":"7b5012096549f2137bf19acc52f12802f751ab9ca415940677e87e2b4d97681f","line_count":173,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_class_03.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-03.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 03 Control flow: if(5==5) and if(5!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_class_03\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5==5)\r\n {\r\n data = new TwoIntsClass[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 5==5 to 5!=5 */\r\nstatic void goodB2G1()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5==5)\r\n {\r\n data = new TwoIntsClass[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5==5)\r\n {\r\n data = new TwoIntsClass[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(5==5)\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 5==5 to 5!=5 */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new TwoIntsClass[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5==5)\r\n {\r\n data = new TwoIntsClass[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__new_delete_array_class_03; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_class_04.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_class_04.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":4904,"source_sha256":"b18b64cfd6932046fc6803e68d684f614b59ab6b33e8df0d3308f29709215a32","line_count":179,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_class_04.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-04.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 04 Control flow: if(STATIC_CONST_TRUE) and if(STATIC_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The two variables below are declared \"const\", so a tool should\r\n be able to identify that reads of these will always return their\r\n initialized values. */\r\nstatic const int STATIC_CONST_TRUE = 1; /* true */\r\nstatic const int STATIC_CONST_FALSE = 0; /* false */\r\n\r\nnamespace CWE415_Double_Free__new_delete_array_class_04\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n data = new TwoIntsClass[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second STATIC_CONST_TRUE to STATIC_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n data = new TwoIntsClass[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(STATIC_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n data = new TwoIntsClass[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first STATIC_CONST_TRUE to STATIC_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new TwoIntsClass[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n data = new TwoIntsClass[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__new_delete_array_class_04; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_class_12.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_class_12.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":4361,"source_sha256":"a81d39ffdbc3888ad9ad2e1a5a66ab3879cd99202a78d18dd336d387c8c4073c","line_count":159,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_class_12.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-12.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_class_12\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n data = new TwoIntsClass[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n else\r\n {\r\n data = new TwoIntsClass[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n else\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the first \"if\" so that\r\n both branches use the BadSource and the second \"if\" so that both branches\r\n use the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n data = new TwoIntsClass[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n else\r\n {\r\n data = new TwoIntsClass[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n else\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the first \"if\" so that\r\n both branches use the GoodSource and the second \"if\" so that both branches\r\n use the BadSink */\r\nstatic void goodG2B()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n data = new TwoIntsClass[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n }\r\n else\r\n {\r\n data = new TwoIntsClass[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n else\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__new_delete_array_class_12; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_class_31.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_class_31.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":3102,"source_sha256":"4a83bd08d58affd4a9a929daa32d14e339bf532bc6d90118f225fcb26de9ec4e","line_count":116,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_class_31.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-31.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 31 Data flow using a copy of data within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_class_31\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new TwoIntsClass[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n {\r\n TwoIntsClass * dataCopy = data;\r\n TwoIntsClass * data = dataCopy;\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new TwoIntsClass[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n {\r\n TwoIntsClass * dataCopy = data;\r\n TwoIntsClass * data = dataCopy;\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new TwoIntsClass[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n {\r\n TwoIntsClass * dataCopy = data;\r\n TwoIntsClass * data = dataCopy;\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__new_delete_array_class_31; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_class_53c.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_class_53c.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":1338,"source_sha256":"4b0eea20afb779c9967cdfcf129cb0463f3e126e947f11aae74bd84f22d73ee3","line_count":57,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_class_53c.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-53c.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_class_53\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink_d(TwoIntsClass * data);\r\n\r\nvoid badSink_c(TwoIntsClass * data)\r\n{\r\n badSink_d(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink_d(TwoIntsClass * data);\r\n\r\nvoid goodG2BSink_c(TwoIntsClass * data)\r\n{\r\n goodG2BSink_d(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink_d(TwoIntsClass * data);\r\n\r\nvoid goodB2GSink_c(TwoIntsClass * data)\r\n{\r\n goodB2GSink_d(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_class_63b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_class_63b.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":1466,"source_sha256":"0fda7eb8d4f32fd407b0cc9190ec60fa17178e561b7de0ae1144a78c7b381604","line_count":57,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_class_63b.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-63b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_class_63\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(TwoIntsClass * * dataPtr)\r\n{\r\n TwoIntsClass * data = *dataPtr;\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(TwoIntsClass * * dataPtr)\r\n{\r\n TwoIntsClass * data = *dataPtr;\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(TwoIntsClass * * dataPtr)\r\n{\r\n TwoIntsClass * data = *dataPtr;\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_class_65a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_class_65a.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":3017,"source_sha256":"81b2a840e0f6e2407c065845234058ddee5ad65b66db2ec9e674cdc9a7309a9e","line_count":113,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_class_65a.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-65a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_class_65\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(TwoIntsClass * data);\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n /* define a function pointer */\r\n void (*funcPtr) (TwoIntsClass *) = badSink;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new TwoIntsClass[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n /* use the function pointer */\r\n funcPtr(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(TwoIntsClass * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n TwoIntsClass * data;\r\n void (*funcPtr) (TwoIntsClass *) = goodG2BSink;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new TwoIntsClass[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n funcPtr(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(TwoIntsClass * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n TwoIntsClass * data;\r\n void (*funcPtr) (TwoIntsClass *) = goodB2GSink;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new TwoIntsClass[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n funcPtr(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__new_delete_array_class_65; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_class_73a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_class_73a.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":3280,"source_sha256":"c87875fefee114cf53bc004d398ccf263306300874c7cec8b513c50299a1695d","line_count":125,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_class_73a.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-73a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE415_Double_Free__new_delete_array_class_73\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(list dataList);\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n list dataList;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new TwoIntsClass[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n /* Put data in a list */\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n badSink(dataList);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(list dataList);\r\n\r\nstatic void goodG2B()\r\n{\r\n TwoIntsClass * data;\r\n list dataList;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new TwoIntsClass[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n /* Put data in a list */\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n goodG2BSink(dataList);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(list dataList);\r\n\r\nstatic void goodB2G()\r\n{\r\n TwoIntsClass * data;\r\n list dataList;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new TwoIntsClass[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n dataList.push_back(data);\r\n goodB2GSink(dataList);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__new_delete_array_class_73; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_class_82.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_class_82.h","cwe_id":"CWE415","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1462,"source_sha256":"51ebae1c9da3580c75469a8b02546695948c6e7c86f32959ab2a0337e48bc3e8","line_count":58,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_class_82.h\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-82.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_class_82\r\n{\r\n\r\nclass CWE415_Double_Free__new_delete_array_class_82_base\r\n{\r\npublic:\r\n /* pure virtual function */\r\n virtual void action(TwoIntsClass * data) = 0;\r\n};\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE415_Double_Free__new_delete_array_class_82_bad : public CWE415_Double_Free__new_delete_array_class_82_base\r\n{\r\npublic:\r\n void action(TwoIntsClass * data);\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE415_Double_Free__new_delete_array_class_82_goodG2B : public CWE415_Double_Free__new_delete_array_class_82_base\r\n{\r\npublic:\r\n void action(TwoIntsClass * data);\r\n};\r\n\r\nclass CWE415_Double_Free__new_delete_array_class_82_goodB2G : public CWE415_Double_Free__new_delete_array_class_82_base\r\n{\r\npublic:\r\n void action(TwoIntsClass * data);\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_class_84_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_class_84_goodB2G.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":1409,"source_sha256":"9b2ac97e1e873157f7221b49f6bdf0e1d42d0cd6006decda232c3af63da57a2e","line_count":39,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_class_84_goodB2G.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-84_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE415_Double_Free__new_delete_array_class_84.h\"\r\n\r\nnamespace CWE415_Double_Free__new_delete_array_class_84\r\n{\r\nCWE415_Double_Free__new_delete_array_class_84_goodB2G::CWE415_Double_Free__new_delete_array_class_84_goodB2G(TwoIntsClass * dataCopy)\r\n{\r\n data = dataCopy;\r\n data = new TwoIntsClass[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n}\r\n\r\nCWE415_Double_Free__new_delete_array_class_84_goodB2G::~CWE415_Double_Free__new_delete_array_class_84_goodB2G()\r\n{\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int64_t_07.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int64_t_07.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":4743,"source_sha256":"b635453a2125c266ec529d021566cb2f1b68b47298021d2c415533cf9fd7d5db","line_count":178,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_int64_t_07.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-07.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 07 Control flow: if(staticFive==5) and if(staticFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The variable below is not declared \"const\", but is never assigned\r\n any other value so a tool should be able to identify that reads of\r\n this will always give its initialized value. */\r\nstatic int staticFive = 5;\r\n\r\nnamespace CWE415_Double_Free__new_delete_array_int64_t_07\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n data = new int64_t[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticFive==5 to staticFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n data = new int64_t[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n data = new int64_t[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(staticFive==5)\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticFive==5 to staticFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new int64_t[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n data = new int64_t[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__new_delete_array_int64_t_07; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int64_t_22a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int64_t_22a.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":3758,"source_sha256":"4d46832bbb149209514afc8de6200a0c95ebc816f4fd64a11e8e30b0c721730f","line_count":136,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_int64_t_22a.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-22a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 22 Control flow: Flow controlled by value of a global variable. Sink functions are in a separate file from sources.\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_int64_t_22\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* The global variable below is used to drive control flow in the sink function. Since it is in\r\na C++ namespace, it doesn't need a globally unique name. */\r\nint badGlobal = 0;\r\n\r\nvoid badSink(int64_t * data);\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int64_t[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n badGlobal = 1; /* true */\r\n badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* The global variables below are used to drive control flow in the sink functions. Since they are in\r\na C++ namespace, they don't need globally unique names. */\r\nint goodB2G1Global = 0;\r\nint goodB2G2Global = 0;\r\nint goodG2B1Global = 0;\r\n\r\n/* goodB2G1() - use badsource and goodsink by setting the static variable to false instead of true */\r\nvoid goodB2G1Sink(int64_t * data);\r\n\r\nstatic void goodB2G1()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int64_t[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n goodB2G1Global = 0; /* false */\r\n goodB2G1Sink(data);\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */\r\nvoid goodB2G2Sink(int64_t * data);\r\n\r\nstatic void goodB2G2()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int64_t[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n goodB2G2Global = 1; /* true */\r\n goodB2G2Sink(data);\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink */\r\nvoid goodG2B1Sink(int64_t * data);\r\n\r\nstatic void goodG2B1()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int64_t[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n goodG2B1Global = 1; /* true */\r\n goodG2B1Sink(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__new_delete_array_int64_t_22; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int64_t_31.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int64_t_31.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":3048,"source_sha256":"03956abda53c0f8a96a0aae84452f037bdd9a2ba9dfd7ecb9e10624abfe71c92","line_count":116,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_int64_t_31.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-31.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 31 Data flow using a copy of data within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_int64_t_31\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int64_t[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n {\r\n int64_t * dataCopy = data;\r\n int64_t * data = dataCopy;\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int64_t[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n {\r\n int64_t * dataCopy = data;\r\n int64_t * data = dataCopy;\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int64_t[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n {\r\n int64_t * dataCopy = data;\r\n int64_t * data = dataCopy;\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__new_delete_array_int64_t_31; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int64_t_52c.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int64_t_52c.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":1356,"source_sha256":"1d6c389ce69b68574f372159d6563ba82b9b5946898fef035a2d0e76d816379b","line_count":54,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_int64_t_52c.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-52c.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_int64_t_52\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink_c(int64_t * data)\r\n{\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink_c(int64_t * data)\r\n{\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink_c(int64_t * data)\r\n{\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int64_t_53c.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int64_t_53c.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":1312,"source_sha256":"0c9b6f35e50e8af5b6eab78bead35bd09b59de7d6db42f0ae19af3896dd086ab","line_count":57,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_int64_t_53c.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-53c.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_int64_t_53\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink_d(int64_t * data);\r\n\r\nvoid badSink_c(int64_t * data)\r\n{\r\n badSink_d(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink_d(int64_t * data);\r\n\r\nvoid goodG2BSink_c(int64_t * data)\r\n{\r\n goodG2BSink_d(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink_d(int64_t * data);\r\n\r\nvoid goodB2GSink_c(int64_t * data)\r\n{\r\n goodB2GSink_d(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int64_t_64a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int64_t_64a.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":2732,"source_sha256":"34b4b48f03a20100c8c016ee187e9a582a8d6fc0c59893dc98716ea1f90abc97","line_count":108,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_int64_t_64a.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-64a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_int64_t_64\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(void * dataVoidPtr);\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int64_t[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(void * dataVoidPtr);\r\n\r\nstatic void goodG2B()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int64_t[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(void * dataVoidPtr);\r\n\r\nstatic void goodB2G()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int64_t[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n goodB2GSink(&data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__new_delete_array_int64_t_64; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int64_t_64b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int64_t_64b.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":1928,"source_sha256":"380671b840b684c2846239d45643ea6ca381a7b262f5237f77861b8d33ca9586","line_count":66,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_int64_t_64b.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-64b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_int64_t_64\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n int64_t * * dataPtr = (int64_t * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n int64_t * data = (*dataPtr);\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n int64_t * * dataPtr = (int64_t * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n int64_t * data = (*dataPtr);\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n int64_t * * dataPtr = (int64_t * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n int64_t * data = (*dataPtr);\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int64_t_67b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int64_t_67b.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":1553,"source_sha256":"45e11220f1b7001c9de56685478b0cb372f963daf5230405aea98dd299f47165","line_count":62,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_int64_t_67b.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-67b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_int64_t_67\r\n{\r\n\r\ntypedef struct _structType\r\n{\r\n int64_t * structFirst;\r\n} structType;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(structType myStruct)\r\n{\r\n int64_t * data = myStruct.structFirst;\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(structType myStruct)\r\n{\r\n int64_t * data = myStruct.structFirst;\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(structType myStruct)\r\n{\r\n int64_t * data = myStruct.structFirst;\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int64_t_68a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int64_t_68a.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":2841,"source_sha256":"84846f9184deccf78d0fc56903eff209dc37630802b2f90281ddc7bd9633d24d","line_count":115,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_int64_t_68a.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-68a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 68 Data flow: data passed as a global variable from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_int64_t_68\r\n{\r\n\r\nint64_t * badData;\r\nint64_t * goodG2BData;\r\nint64_t * goodB2GData;\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink();\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int64_t[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n badData = data;\r\n badSink();\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\nvoid goodG2BSink();\r\nvoid goodB2GSink();\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int64_t[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n goodG2BData = data;\r\n goodG2BSink();\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int64_t[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n goodB2GData = data;\r\n goodB2GSink();\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__new_delete_array_int64_t_68; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int64_t_81_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int64_t_81_bad.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":956,"source_sha256":"f94d61e134d9a6b2c0bf46dcef35d2d457d3d242b912ccfd2960d2360656b189","line_count":32,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_int64_t_81_bad.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-81_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE415_Double_Free__new_delete_array_int64_t_81.h\"\r\n\r\nnamespace CWE415_Double_Free__new_delete_array_int64_t_81\r\n{\r\n\r\nvoid CWE415_Double_Free__new_delete_array_int64_t_81_bad::action(int64_t * data) const\r\n{\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n}\r\n\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int64_t_84_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int64_t_84_goodG2B.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":1327,"source_sha256":"67e75bd4ff9aa5baea2e0ea9f9674aa0bdffb0be7741670bbdabec809967fd2a","line_count":37,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_int64_t_84_goodG2B.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-84_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE415_Double_Free__new_delete_array_int64_t_84.h\"\r\n\r\nnamespace CWE415_Double_Free__new_delete_array_int64_t_84\r\n{\r\nCWE415_Double_Free__new_delete_array_int64_t_84_goodG2B::CWE415_Double_Free__new_delete_array_int64_t_84_goodG2B(int64_t * dataCopy)\r\n{\r\n data = dataCopy;\r\n data = new int64_t[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n}\r\n\r\nCWE415_Double_Free__new_delete_array_int64_t_84_goodG2B::~CWE415_Double_Free__new_delete_array_int64_t_84_goodG2B()\r\n{\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int_10.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int_10.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":4425,"source_sha256":"7b3689133df5cf549f56cff6f80cd8c3dcc6e18fa7ab549efb0cbe94c8fdf89b","line_count":173,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_int_10.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-10.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 10 Control flow: if(globalTrue) and if(globalFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_int_10\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n data = new int[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalTrue to globalFalse */\r\nstatic void goodB2G1()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n data = new int[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n data = new int[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(globalTrue)\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalTrue to globalFalse */\r\nstatic void goodG2B1()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new int[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n data = new int[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__new_delete_array_int_10; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int_11.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int_11.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":4565,"source_sha256":"d6118936b2203ac85d033f76616d51da5b7a23b6eb724dea2f3c19b41bb92fe3","line_count":172,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_int_11.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-11.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_int_11\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n data = new int[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n data = new int[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n data = new int[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new int[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n data = new int[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__new_delete_array_int_11; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int_21.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int_21.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":4360,"source_sha256":"46c9895a59f3536bc9ff80324d15ca1f7230138cadca79ca36d3a7b2c31a8715","line_count":169,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_int_21.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-21.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 21 Control flow: Flow controlled by value of a static global variable. All functions contained in one file.\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_int_21\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* The static variable below is used to drive control flow in the sink function */\r\nstatic int badStatic = 0;\r\n\r\nstatic void badSink(int * data)\r\n{\r\n if(badStatic)\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid bad()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n badStatic = 1; /* true */\r\n badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* The static variables below are used to drive control flow in the sink functions. */\r\nstatic int goodB2G1Static = 0;\r\nstatic int goodB2G2Static = 0;\r\nstatic int goodG2bStatic = 0;\r\n\r\n/* goodB2G1() - use badsource and goodsink by setting the static variable to false instead of true */\r\nstatic void goodB2G1Sink(int * data)\r\n{\r\n if(goodB2G1Static)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\nstatic void goodB2G1()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n goodB2G1Static = 0; /* false */\r\n goodB2G1Sink(data);\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */\r\nstatic void goodB2G2Sink(int * data)\r\n{\r\n if(goodB2G2Static)\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\nstatic void goodB2G2()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n goodB2G2Static = 1; /* true */\r\n goodB2G2Sink(data);\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink */\r\nstatic void goodG2BSink(int * data)\r\n{\r\n if(goodG2bStatic)\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n goodG2bStatic = 1; /* true */\r\n goodG2BSink(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__new_delete_array_int_21; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int_53a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int_53a.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":2724,"source_sha256":"2515343d3548f337fdfa5fbc3ae38dee4b26402ad2c9139371bf4d7141343dff","line_count":108,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_int_53a.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-53a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_int_53\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink_b(int * data);\r\n\r\nvoid bad()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n badSink_b(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink_b(int * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n goodG2BSink_b(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink_b(int * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n goodB2GSink_b(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__new_delete_array_int_53; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int_67a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int_67a.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":2956,"source_sha256":"a52e7804276aa895c7a7fc8e82bce8587a848fa84f6407ab332a42e3c82eb4b1","line_count":119,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_int_67a.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-67a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_int_67\r\n{\r\n\r\ntypedef struct _structType\r\n{\r\n int * structFirst;\r\n} structType;\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(structType myStruct);\r\n\r\nvoid bad()\r\n{\r\n int * data;\r\n structType myStruct;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n myStruct.structFirst = data;\r\n badSink(myStruct);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(structType myStruct);\r\n\r\nstatic void goodG2B()\r\n{\r\n int * data;\r\n structType myStruct;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n myStruct.structFirst = data;\r\n goodG2BSink(myStruct);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(structType myStruct);\r\n\r\nstatic void goodB2G()\r\n{\r\n int * data;\r\n structType myStruct;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n myStruct.structFirst = data;\r\n goodB2GSink(myStruct);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__new_delete_array_int_67; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int_67b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int_67b.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":1529,"source_sha256":"c2c67ecce4c0f1f7e8b7015394a241da354b96efeefaa38f9a0ddc5653df264a","line_count":62,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_int_67b.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-67b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_int_67\r\n{\r\n\r\ntypedef struct _structType\r\n{\r\n int * structFirst;\r\n} structType;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(structType myStruct)\r\n{\r\n int * data = myStruct.structFirst;\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(structType myStruct)\r\n{\r\n int * data = myStruct.structFirst;\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(structType myStruct)\r\n{\r\n int * data = myStruct.structFirst;\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int_83_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_int_83_goodB2G.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":1352,"source_sha256":"76164827ffc00199055e79f792ab5a6bea0b968b5cf44353d0c403ec3540cd7c","line_count":39,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_int_83_goodB2G.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-83_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 83 Data flow: data passed to class constructor and destructor by declaring the class object on the stack\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE415_Double_Free__new_delete_array_int_83.h\"\r\n\r\nnamespace CWE415_Double_Free__new_delete_array_int_83\r\n{\r\nCWE415_Double_Free__new_delete_array_int_83_goodB2G::CWE415_Double_Free__new_delete_array_int_83_goodB2G(int * dataCopy)\r\n{\r\n data = dataCopy;\r\n data = new int[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n}\r\n\r\nCWE415_Double_Free__new_delete_array_int_83_goodB2G::~CWE415_Double_Free__new_delete_array_int_83_goodB2G()\r\n{\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_long_11.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_long_11.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":4578,"source_sha256":"49c873b7746d9817352dd74c8c3b09d77e9986f8e4141986f156d08c977fed6e","line_count":172,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_long_11.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-11.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_long_11\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n data = new long[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n data = new long[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n data = new long[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new long[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n data = new long[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__new_delete_array_long_11; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_long_14.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_long_14.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":4477,"source_sha256":"c7e07693c53e979a2d6df9482206b6035083bf45d7efac099311618fe07e34bc","line_count":172,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_long_14.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-14.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 14 Control flow: if(globalFive==5) and if(globalFive!=5)\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_long_14\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalFive==5)\r\n {\r\n data = new long[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalFive==5 to globalFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalFive==5)\r\n {\r\n data = new long[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalFive==5)\r\n {\r\n data = new long[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n }\r\n if(globalFive==5)\r\n {\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalFive==5 to globalFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new long[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalFive==5)\r\n {\r\n data = new long[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__new_delete_array_long_14; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_long_41.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_long_41.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":2974,"source_sha256":"18b97006bac152b8210bf9ddce3f4d665385fef1189ca5493f7f4c0fff8017f0","line_count":120,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_long_41.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-41.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 41 Data flow: data passed as an argument from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_long_41\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink(long * data)\r\n{\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n}\r\n\r\nvoid bad()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new long[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink(long * data)\r\n{\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new long[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink(long * data)\r\n{\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new long[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n goodB2GSink(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__new_delete_array_long_41; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_long_44.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_long_44.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":3211,"source_sha256":"da7b2f837e34cc37be27baaa5b24f8b3b23b3c79d90150890e270db44943412c","line_count":125,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_long_44.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-44.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * 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\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_long_44\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink(long * data)\r\n{\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n}\r\n\r\nvoid bad()\r\n{\r\n long * data;\r\n /* define a function pointer */\r\n void (*funcPtr) (long *) = badSink;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new long[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n /* use the function pointer */\r\n funcPtr(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink(long * data)\r\n{\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n long * data;\r\n void (*funcPtr) (long *) = goodG2BSink;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new long[100];\r\n /* FIX: Do NOT delete the array data in the source - the bad sink deletes the array data */\r\n funcPtr(data);\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink(long * data)\r\n{\r\n /* do nothing */\r\n /* FIX: Don't attempt to delete the memory */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n long * data;\r\n void (*funcPtr) (long *) = goodB2GSink;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new long[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n funcPtr(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE415_Double_Free__new_delete_array_long_44; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_long_82.h","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_long_82.h","cwe_id":"CWE415","file_extension":".h","language_hint":"C/C++ header; language not resolved","source_size_bytes":1421,"source_sha256":"ce3f2904d2425a85e29852378f457f095cb49096fc4a8447bc459ad2092f7a8c","line_count":58,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_long_82.h\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-82.tmpl.h\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE415_Double_Free__new_delete_array_long_82\r\n{\r\n\r\nclass CWE415_Double_Free__new_delete_array_long_82_base\r\n{\r\npublic:\r\n /* pure virtual function */\r\n virtual void action(long * data) = 0;\r\n};\r\n\r\n#ifndef OMITBAD\r\n\r\nclass CWE415_Double_Free__new_delete_array_long_82_bad : public CWE415_Double_Free__new_delete_array_long_82_base\r\n{\r\npublic:\r\n void action(long * data);\r\n};\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\nclass CWE415_Double_Free__new_delete_array_long_82_goodG2B : public CWE415_Double_Free__new_delete_array_long_82_base\r\n{\r\npublic:\r\n void action(long * data);\r\n};\r\n\r\nclass CWE415_Double_Free__new_delete_array_long_82_goodB2G : public CWE415_Double_Free__new_delete_array_long_82_base\r\n{\r\npublic:\r\n void action(long * data);\r\n};\r\n\r\n#endif /* OMITGOOD */\r\n\r\n}\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_long_82_goodG2B.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_long_82_goodG2B.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":947,"source_sha256":"3236c2d3830405d5d290023e3d9a7ddf1b0cf989cd8fa29fbe654f6b6f9e9686","line_count":32,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_long_82_goodG2B.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-82_goodG2B.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE415_Double_Free__new_delete_array_long_82.h\"\r\n\r\nnamespace CWE415_Double_Free__new_delete_array_long_82\r\n{\r\n\r\nvoid CWE415_Double_Free__new_delete_array_long_82_goodG2B::action(long * data)\r\n{\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_long_84_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE415_Double_Free/s01/CWE415_Double_Free__new_delete_array_long_84_bad.cpp","cwe_id":"CWE415","file_extension":".cpp","language_hint":"C++","source_size_bytes":1307,"source_sha256":"959595c9d31ade55c4c1e82464f2c6e1372ba18a9e57de415009132b89594660","line_count":38,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE415_Double_Free__new_delete_array_long_84_bad.cpp\r\nLabel Definition File: CWE415_Double_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-84_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 415 Double Free\r\n * BadSource: Allocate data using new and Deallocae data using delete\r\n * GoodSource: Allocate data using new\r\n * Sinks:\r\n * GoodSink: do nothing\r\n * BadSink : Deallocate data using delete\r\n * Flow Variant: 84 Data flow: data passed to class constructor and destructor by declaring the class object on the heap and deleting it after use\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE415_Double_Free__new_delete_array_long_84.h\"\r\n\r\nnamespace CWE415_Double_Free__new_delete_array_long_84\r\n{\r\nCWE415_Double_Free__new_delete_array_long_84_bad::CWE415_Double_Free__new_delete_array_long_84_bad(long * dataCopy)\r\n{\r\n data = dataCopy;\r\n data = new long[100];\r\n /* POTENTIAL FLAW: delete the array data in the source - the bad sink deletes the array data as well */\r\n delete [] data;\r\n}\r\n\r\nCWE415_Double_Free__new_delete_array_long_84_bad::~CWE415_Double_Free__new_delete_array_long_84_bad()\r\n{\r\n /* POTENTIAL FLAW: Possibly deleting memory twice */\r\n delete [] data;\r\n}\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_char_02.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_char_02.c","cwe_id":"CWE416","file_extension":".c","language_hint":"C","source_size_bytes":5207,"source_sha256":"e25ffcbe3358f629b922ce5be4e7189ac2f6b71b39d7330013cbddd18addd34f","line_count":186,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_char_02.c\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-02.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 02 Control flow: if(1) and if(0)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_char_02_bad()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 1 to 0 */\r\nstatic void goodB2G1()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(1)\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 1 to 0 */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* FIX: Do not free data in the source */\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* FIX: Do not free data in the source */\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_char_02_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE416_Use_After_Free__malloc_free_char_02_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE416_Use_After_Free__malloc_free_char_02_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_char_10.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_char_10.c","cwe_id":"CWE416","file_extension":".c","language_hint":"C","source_size_bytes":5356,"source_sha256":"8482fc09fb185564ceeb1aeac530bdac260f8036d973fa010cadf14afd38887a","line_count":186,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_char_10.c\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-10.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 10 Control flow: if(globalTrue) and if(globalFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_char_10_bad()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalTrue to globalFalse */\r\nstatic void goodB2G1()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(globalTrue)\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalTrue to globalFalse */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* FIX: Do not free data in the source */\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* FIX: Do not free data in the source */\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_char_10_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE416_Use_After_Free__malloc_free_char_10_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE416_Use_After_Free__malloc_free_char_10_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_char_11.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_char_11.c","cwe_id":"CWE416","file_extension":".c","language_hint":"C","source_size_bytes":5500,"source_sha256":"e21447b9ce7028065bf852002c4b68fdfbf1242ee9f3bd3e7efe2a6799bb6da7","line_count":186,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_char_11.c\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-11.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_char_11_bad()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* FIX: Do not free data in the source */\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* FIX: Do not free data in the source */\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_char_11_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE416_Use_After_Free__malloc_free_char_11_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE416_Use_After_Free__malloc_free_char_11_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_char_15.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_char_15.c","cwe_id":"CWE416","file_extension":".c","language_hint":"C","source_size_bytes":6791,"source_sha256":"a605445c7f6375403b099a76ef72e032139c9c88937e6245a833f26e40b60f96","line_count":236,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_char_15.c\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-15.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 15 Control flow: switch(6) and switch(7)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_char_15_bad()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second switch to switch(8) */\r\nstatic void goodB2G1()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(8)\r\n {\r\n case 7:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second switch */\r\nstatic void goodB2G2()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first switch to switch(5) */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(5)\r\n {\r\n case 6:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* FIX: Do not free data in the source */\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first switch */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n data = (char *)malloc(100*sizeof(char));\r\n if (data == NULL) {exit(-1);}\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* FIX: Do not free data in the source */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_char_15_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE416_Use_After_Free__malloc_free_char_15_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE416_Use_After_Free__malloc_free_char_15_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_int64_t_09.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_int64_t_09.c","cwe_id":"CWE416","file_extension":".c","language_hint":"C","source_size_bytes":5966,"source_sha256":"45300e3be78207fb29d65ad7fef821a57fc0a40492e3de866b3b504bd67f9844","line_count":211,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_int64_t_09.c\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-09.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_int64_t_09_bad()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n data = (int64_t *)malloc(100*sizeof(int64_t));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLongLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n data = (int64_t *)malloc(100*sizeof(int64_t));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n data = (int64_t *)malloc(100*sizeof(int64_t));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = (int64_t *)malloc(100*sizeof(int64_t));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* FIX: Do not free data in the source */\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLongLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n data = (int64_t *)malloc(100*sizeof(int64_t));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* FIX: Do not free data in the source */\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLongLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_int64_t_09_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE416_Use_After_Free__malloc_free_int64_t_09_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE416_Use_After_Free__malloc_free_int64_t_09_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_int64_t_17.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_int64_t_17.c","cwe_id":"CWE416","file_extension":".c","language_hint":"C","source_size_bytes":3954,"source_sha256":"01a091c1d7bec778fcb956fb686575dd70ddd1b0619e8d201453a316478e02b4","line_count":146,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_int64_t_17.c\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-17.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 17 Control flow: for loops\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_int64_t_17_bad()\r\n{\r\n int i,j;\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n for(i = 0; i < 1; i++)\r\n {\r\n data = (int64_t *)malloc(100*sizeof(int64_t));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n for(j = 0; j < 1; j++)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLongLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink in the for statements */\r\nstatic void goodB2G()\r\n{\r\n int i,k;\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n for(i = 0; i < 1; i++)\r\n {\r\n data = (int64_t *)malloc(100*sizeof(int64_t));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n for(k = 0; k < 1; k++)\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink in the for statements */\r\nstatic void goodG2B()\r\n{\r\n int h,j;\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n for(h = 0; h < 1; h++)\r\n {\r\n data = (int64_t *)malloc(100*sizeof(int64_t));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* FIX: Do not free data in the source */\r\n }\r\n for(j = 0; j < 1; j++)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLongLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_int64_t_17_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE416_Use_After_Free__malloc_free_int64_t_17_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE416_Use_After_Free__malloc_free_int64_t_17_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_int64_t_62a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_int64_t_62a.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":2915,"source_sha256":"f27898b8537f409b7460215f751e4a5ffcbc198803008d2ff8a4fc49f455d1fd","line_count":110,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_int64_t_62a.cpp\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-62a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__malloc_free_int64_t_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSource(int64_t * &data);\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n badSource(data);\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLongLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(int64_t * &data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goodG2BSource(data);\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLongLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(int64_t * &data);\r\n\r\nstatic void goodB2G()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goodB2GSource(data);\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__malloc_free_int64_t_62; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_int64_t_63b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_int64_t_63b.c","cwe_id":"CWE416","file_extension":".c","language_hint":"C","source_size_bytes":1825,"source_sha256":"5d1d030f8580c99cad2cbdaa96489d88541b998dbf8e0fbd05cfe177d0ed0c3b","line_count":55,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_int64_t_63b.c\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-63b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_int64_t_63b_badSink(int64_t * * dataPtr)\r\n{\r\n int64_t * data = *dataPtr;\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLongLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE416_Use_After_Free__malloc_free_int64_t_63b_goodG2BSink(int64_t * * dataPtr)\r\n{\r\n int64_t * data = *dataPtr;\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLongLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE416_Use_After_Free__malloc_free_int64_t_63b_goodB2GSink(int64_t * * dataPtr)\r\n{\r\n int64_t * data = *dataPtr;\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_int_02.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_int_02.c","cwe_id":"CWE416","file_extension":".c","language_hint":"C","source_size_bytes":5600,"source_sha256":"f4288b0efe9ff3449d01030847d43f1cfd9a9b407bcc24e3e83fcefb1fe4a58c","line_count":211,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_int_02.c\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-02.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 02 Control flow: if(1) and if(0)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_int_02_bad()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printIntLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 1 to 0 */\r\nstatic void goodB2G1()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(1)\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 1 to 0 */\r\nstatic void goodG2B1()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* FIX: Do not free data in the source */\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printIntLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* FIX: Do not free data in the source */\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printIntLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_int_02_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE416_Use_After_Free__malloc_free_int_02_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE416_Use_After_Free__malloc_free_int_02_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_int_10.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_int_10.c","cwe_id":"CWE416","file_extension":".c","language_hint":"C","source_size_bytes":5749,"source_sha256":"219538b48770832bd23cb21eda42ef4e63f1bc449e65d43a62ba9bf39567289f","line_count":211,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_int_10.c\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-10.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 10 Control flow: if(globalTrue) and if(globalFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_int_10_bad()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printIntLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalTrue to globalFalse */\r\nstatic void goodB2G1()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(globalTrue)\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalTrue to globalFalse */\r\nstatic void goodG2B1()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* FIX: Do not free data in the source */\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printIntLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* FIX: Do not free data in the source */\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printIntLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_int_10_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE416_Use_After_Free__malloc_free_int_10_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE416_Use_After_Free__malloc_free_int_10_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_int_16.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_int_16.c","cwe_id":"CWE416","file_extension":".c","language_hint":"C","source_size_bytes":3912,"source_sha256":"c51ae5c4fee6c77a4a1d859f1ccaac59bba1a535ca79ce6a0a02b44664b53a9e","line_count":149,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_int_16.c\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-16.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 16 Control flow: while(1)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_int_16_bad()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n while(1)\r\n {\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printIntLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the sinks in the second while statement */\r\nstatic void goodB2G()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n while(1)\r\n {\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the sources in the first while statement */\r\nstatic void goodG2B()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n while(1)\r\n {\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* FIX: Do not free data in the source */\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printIntLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_int_16_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE416_Use_After_Free__malloc_free_int_16_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE416_Use_After_Free__malloc_free_int_16_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_int_43.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_int_43.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":3707,"source_sha256":"9c7a1ebacbee32be384e0c33fd0cf0ed40d0bc97e9b36993c29f9a743841a73b","line_count":147,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_int_43.cpp\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-43.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 43 Data flow: data flows using a C++ reference from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__malloc_free_int_43\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSource(int * &data)\r\n{\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n}\r\n\r\nvoid bad()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n badSource(data);\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printIntLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSource(int * &data)\r\n{\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* FIX: Do not free data in the source */\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goodG2BSource(data);\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printIntLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSource(int * &data)\r\n{\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goodB2GSource(data);\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__malloc_free_int_43; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_int_62b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_int_62b.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":1918,"source_sha256":"95a5c8984b397eee54cdebb79729e4b61152138ec13bef500e0f52a531c3fe99","line_count":79,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_int_62b.cpp\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-62b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__malloc_free_int_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSource(int * &data)\r\n{\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(int * &data)\r\n{\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* FIX: Do not free data in the source */\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(int * &data)\r\n{\r\n data = (int *)malloc(100*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_long_03.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_long_03.c","cwe_id":"CWE416","file_extension":".c","language_hint":"C","source_size_bytes":5676,"source_sha256":"2c090ed908f26219a1001b7fdcff53b6e180f9ba21580ae7d4ad4e500493f4a3","line_count":211,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_long_03.c\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-03.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 03 Control flow: if(5==5) and if(5!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_long_03_bad()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5==5)\r\n {\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 5==5 to 5!=5 */\r\nstatic void goodB2G1()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5==5)\r\n {\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5==5)\r\n {\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(5==5)\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 5==5 to 5!=5 */\r\nstatic void goodG2B1()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* FIX: Do not free data in the source */\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5==5)\r\n {\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* FIX: Do not free data in the source */\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_long_03_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE416_Use_After_Free__malloc_free_long_03_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE416_Use_After_Free__malloc_free_long_03_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_long_07.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_long_07.c","cwe_id":"CWE416","file_extension":".c","language_hint":"C","source_size_bytes":6043,"source_sha256":"e6f5a3cd708d5471c3f2d6905ca7b38383713e24dddec1f55787bd7d1c812882","line_count":216,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_long_07.c\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-07.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 07 Control flow: if(staticFive==5) and if(staticFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The variable below is not declared \"const\", but is never assigned\r\n any other value so a tool should be able to identify that reads of\r\n this will always give its initialized value. */\r\nstatic int staticFive = 5;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_long_07_bad()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticFive==5 to staticFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(staticFive==5)\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticFive==5 to staticFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* FIX: Do not free data in the source */\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* FIX: Do not free data in the source */\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_long_07_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE416_Use_After_Free__malloc_free_long_07_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE416_Use_After_Free__malloc_free_long_07_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_long_08.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_long_08.c","cwe_id":"CWE416","file_extension":".c","language_hint":"C","source_size_bytes":6199,"source_sha256":"37d991a60d360a818d050ecf028fede964cf44d41d9e0a7f3d646f5432f336d2","line_count":224,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_long_08.c\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-08.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 08 Control flow: if(staticReturnsTrue()) and if(staticReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The two function below always return the same value, so a tool\r\n should be able to identify that calls to the functions will always\r\n return a fixed value. */\r\nstatic int staticReturnsTrue()\r\n{\r\n return 1;\r\n}\r\n\r\nstatic int staticReturnsFalse()\r\n{\r\n return 0;\r\n}\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_long_08_bad()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* FIX: Do not free data in the source */\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* FIX: Do not free data in the source */\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_long_08_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE416_Use_After_Free__malloc_free_long_08_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE416_Use_After_Free__malloc_free_long_08_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_long_62b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_long_62b.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":1932,"source_sha256":"268d6bb05934839b9bde25ab3e10d044885874c7a28bef63595e6b9ca4a6e88f","line_count":79,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_long_62b.cpp\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-62b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__malloc_free_long_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSource(long * &data)\r\n{\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(long * &data)\r\n{\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* FIX: Do not free data in the source */\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(long * &data)\r\n{\r\n data = (long *)malloc(100*sizeof(long));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_struct_03.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_struct_03.c","cwe_id":"CWE416","file_extension":".c","language_hint":"C","source_size_bytes":6045,"source_sha256":"ff9bf00b93718b5c7ab50e45d7641eb10a57907cd81a9583f99193c26e8c00df","line_count":216,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_struct_03.c\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-03.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 03 Control flow: if(5==5) and if(5!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_struct_03_bad()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5==5)\r\n {\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printStructLine(&data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 5==5 to 5!=5 */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5==5)\r\n {\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5==5)\r\n {\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(5==5)\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 5==5 to 5!=5 */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* FIX: Do not free data in the source */\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printStructLine(&data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5==5)\r\n {\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* FIX: Do not free data in the source */\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printStructLine(&data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_struct_03_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE416_Use_After_Free__malloc_free_struct_03_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE416_Use_After_Free__malloc_free_struct_03_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_struct_06.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_struct_06.c","cwe_id":"CWE416","file_extension":".c","language_hint":"C","source_size_bytes":6495,"source_sha256":"e6ad5afca1b0d76218d3ca7e50c669e122be325faee479ac38fad921dbc6dfe6","line_count":221,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_struct_06.c\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-06.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 06 Control flow: if(STATIC_CONST_FIVE==5) and if(STATIC_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The variable below is declared \"const\", so a tool should be able\r\n to identify that reads of this will always give its initialized\r\n value. */\r\nstatic const int STATIC_CONST_FIVE = 5;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_struct_06_bad()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printStructLine(&data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* FIX: Do not free data in the source */\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printStructLine(&data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* FIX: Do not free data in the source */\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printStructLine(&data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_struct_06_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE416_Use_After_Free__malloc_free_struct_06_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE416_Use_After_Free__malloc_free_struct_06_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_struct_10.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_struct_10.c","cwe_id":"CWE416","file_extension":".c","language_hint":"C","source_size_bytes":6146,"source_sha256":"202e7a6c2ce3081471fa6768334e17a0ce38decad50eac6ccfe602bf800662f7","line_count":216,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_struct_10.c\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-10.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 10 Control flow: if(globalTrue) and if(globalFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_struct_10_bad()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printStructLine(&data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalTrue to globalFalse */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n }\r\n if(globalTrue)\r\n {\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalTrue to globalFalse */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* FIX: Do not free data in the source */\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printStructLine(&data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* FIX: Do not free data in the source */\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printStructLine(&data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n }\r\n}\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_struct_10_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE416_Use_After_Free__malloc_free_struct_10_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE416_Use_After_Free__malloc_free_struct_10_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_struct_43.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_struct_43.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":3964,"source_sha256":"0169a86a9e7e46e4b651412d9e10bb2b26cb2f43c3f5d1f99160d1171a4b0c64","line_count":150,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_struct_43.cpp\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-43.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 43 Data flow: data flows using a C++ reference from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__malloc_free_struct_43\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSource(twoIntsStruct * &data)\r\n{\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n}\r\n\r\nvoid bad()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n badSource(data);\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printStructLine(&data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSource(twoIntsStruct * &data)\r\n{\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* FIX: Do not free data in the source */\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goodG2BSource(data);\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printStructLine(&data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSource(twoIntsStruct * &data)\r\n{\r\n data = (twoIntsStruct *)malloc(100*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goodB2GSource(data);\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__malloc_free_struct_43; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_struct_62a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_struct_62a.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":2946,"source_sha256":"8438c12891c755093d13ac1df9c7a57bee0027d60960a299d13f911a11bd9ed2","line_count":110,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_struct_62a.cpp\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-62a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__malloc_free_struct_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSource(twoIntsStruct * &data);\r\n\r\nvoid bad()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n badSource(data);\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printStructLine(&data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(twoIntsStruct * &data);\r\n\r\nstatic void goodG2B()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goodG2BSource(data);\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printStructLine(&data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(twoIntsStruct * &data);\r\n\r\nstatic void goodB2G()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goodB2GSource(data);\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__malloc_free_struct_62; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_wchar_t_15.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_wchar_t_15.c","cwe_id":"CWE416","file_extension":".c","language_hint":"C","source_size_bytes":6869,"source_sha256":"0099152e444b6760a16ec3f1ffe5c0fe03c249396f91e7a5ae68a04c1bc1dba9","line_count":236,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_wchar_t_15.c\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-15.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 15 Control flow: switch(6) and switch(7)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_wchar_t_15_bad()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n wmemset(data, L'A', 100-1);\r\n data[100-1] = L'\\0';\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printWLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second switch to switch(8) */\r\nstatic void goodB2G1()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n wmemset(data, L'A', 100-1);\r\n data[100-1] = L'\\0';\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(8)\r\n {\r\n case 7:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second switch */\r\nstatic void goodB2G2()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n wmemset(data, L'A', 100-1);\r\n data[100-1] = L'\\0';\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first switch to switch(5) */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(5)\r\n {\r\n case 6:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n wmemset(data, L'A', 100-1);\r\n data[100-1] = L'\\0';\r\n /* FIX: Do not free data in the source */\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printWLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first switch */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n wmemset(data, L'A', 100-1);\r\n data[100-1] = L'\\0';\r\n /* FIX: Do not free data in the source */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printWLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE416_Use_After_Free__malloc_free_wchar_t_15_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE416_Use_After_Free__malloc_free_wchar_t_15_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE416_Use_After_Free__malloc_free_wchar_t_15_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_wchar_t_43.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_wchar_t_43.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":3589,"source_sha256":"2d901cc97c28f2b8e8ade77146869edd53143581bccbc2a8dcb04cfed603bc5f","line_count":132,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_wchar_t_43.cpp\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-43.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 43 Data flow: data flows using a C++ reference from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__malloc_free_wchar_t_43\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSource(wchar_t * &data)\r\n{\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n wmemset(data, L'A', 100-1);\r\n data[100-1] = L'\\0';\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n}\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n badSource(data);\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printWLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSource(wchar_t * &data)\r\n{\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n wmemset(data, L'A', 100-1);\r\n data[100-1] = L'\\0';\r\n /* FIX: Do not free data in the source */\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goodG2BSource(data);\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printWLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSource(wchar_t * &data)\r\n{\r\n data = (wchar_t *)malloc(100*sizeof(wchar_t));\r\n if (data == NULL) {exit(-1);}\n wmemset(data, L'A', 100-1);\r\n data[100-1] = L'\\0';\r\n /* POTENTIAL FLAW: Free data in the source - the bad sink attempts to use data */\r\n free(data);\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goodB2GSource(data);\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__malloc_free_wchar_t_43; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_wchar_t_62a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__malloc_free_wchar_t_62a.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":2895,"source_sha256":"fda38a9a9379c5f2be397316c97bd309c028bc622f509d75f07c99ed91f0651c","line_count":110,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__malloc_free_wchar_t_62a.cpp\r\nLabel Definition File: CWE416_Use_After_Free__malloc_free.label.xml\r\nTemplate File: sources-sinks-62a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using malloc(), initialize memory block, and Deallocate data using free()\r\n * GoodSource: Allocate data using malloc() and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__malloc_free_wchar_t_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSource(wchar_t * &data);\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n badSource(data);\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printWLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(wchar_t * &data);\r\n\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goodG2BSource(data);\r\n /* POTENTIAL FLAW: Use of data that may have been freed */\r\n printWLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(wchar_t * &data);\r\n\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goodB2GSource(data);\r\n /* FIX: Don't use data that may have been freed already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not freed */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__malloc_free_wchar_t_62; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_char_01.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_char_01.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":3116,"source_sha256":"d67ce5c9e4a6102d9c93be28c0007a4a1d43e2c6aa4ad42dd2a634e7a0e5b230","line_count":114,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_array_char_01.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-01.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete[]\r\n * Flow Variant: 01 Baseline\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_array_char_01\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new char[100];\r\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new char[100];\r\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* FIX: Do not delete data in the source */\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new char[100];\r\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_array_char_01; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_char_11.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_char_11.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5304,"source_sha256":"be46f2bac088bde6426b8ddd75deafb73f7f6a7942abb4c2c9ce71552d050ebb","line_count":187,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_array_char_11.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-11.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete[]\r\n * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_array_char_11\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n data = new char[100];\r\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n data = new char[100];\r\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n data = new char[100];\r\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new char[100];\r\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n data = new char[100];\r\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_array_char_11; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_char_12.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_char_12.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5131,"source_sha256":"7430d7950ef2b5c1ae529eaf3e41742805544a631ad5d1cd26042c9c446836ed","line_count":177,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_array_char_12.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-12.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete[]\r\n * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_array_char_12\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n data = new char[100];\r\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n else\r\n {\r\n data = new char[100];\r\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the first \"if\" so that\r\n both branches use the BadSource and the second \"if\" so that both branches\r\n use the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n data = new char[100];\r\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n else\r\n {\r\n data = new char[100];\r\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the first \"if\" so that\r\n both branches use the GoodSource and the second \"if\" so that both branches\r\n use the BadSink */\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n data = new char[100];\r\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* FIX: Do not delete data in the source */\r\n }\r\n else\r\n {\r\n data = new char[100];\r\n memset(data, 'A', 100-1);\r\n data[100-1] = '\\0';\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n else\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_array_char_12; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_class_02.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_class_02.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5752,"source_sha256":"e3dc421e2dc9fa0356f42fefb640fed2e98cfa2ce759ac67c91f5ff972306071","line_count":218,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_array_class_02.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-02.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete[]\r\n * Flow Variant: 02 Control flow: if(1) and if(0)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_array_class_02\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = new TwoIntsClass[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data[0].intOne);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 1 to 0 */\r\nstatic void goodB2G1()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = new TwoIntsClass[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = new TwoIntsClass[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(1)\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 1 to 0 */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new TwoIntsClass[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data[0].intOne);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = new TwoIntsClass[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data[0].intOne);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_array_class_02; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_class_05.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_class_05.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":6192,"source_sha256":"98b71b25670e9549eac5e586556f93063e81086b9a7ad14b674f855db6e81df4","line_count":224,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_array_class_05.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-05.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete[]\r\n * Flow Variant: 05 Control flow: if(staticTrue) and if(staticFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The two variables below are not defined as \"const\", but are never\r\n assigned any other value, so a tool should be able to identify that\r\n reads of these will always return their initialized values. */\r\nstatic int staticTrue = 1; /* true */\r\nstatic int staticFalse = 0; /* false */\r\n\r\nnamespace CWE416_Use_After_Free__new_delete_array_class_05\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticTrue)\r\n {\r\n data = new TwoIntsClass[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data[0].intOne);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticTrue to staticFalse */\r\nstatic void goodB2G1()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticTrue)\r\n {\r\n data = new TwoIntsClass[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(staticFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticTrue)\r\n {\r\n data = new TwoIntsClass[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(staticTrue)\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticTrue to staticFalse */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new TwoIntsClass[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data[0].intOne);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticTrue)\r\n {\r\n data = new TwoIntsClass[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data[0].intOne);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_array_class_05; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_class_10.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_class_10.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5901,"source_sha256":"105a198256a2bee4fb7b176f07f3b453f80ba69ce19bbc590055b2362e5fa392","line_count":218,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_array_class_10.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-10.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete[]\r\n * Flow Variant: 10 Control flow: if(globalTrue) and if(globalFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_array_class_10\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n data = new TwoIntsClass[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data[0].intOne);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalTrue to globalFalse */\r\nstatic void goodB2G1()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n data = new TwoIntsClass[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n data = new TwoIntsClass[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(globalTrue)\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalTrue to globalFalse */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new TwoIntsClass[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data[0].intOne);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n data = new TwoIntsClass[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data[0].intOne);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_array_class_10; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_class_11.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_class_11.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":6041,"source_sha256":"77e468408b64169696895e638f67a0f50463f8156bafe50c41367e1f11453003","line_count":217,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_array_class_11.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-11.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete[]\r\n * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_array_class_11\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n data = new TwoIntsClass[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data[0].intOne);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n data = new TwoIntsClass[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n data = new TwoIntsClass[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new TwoIntsClass[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data[0].intOne);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n data = new TwoIntsClass[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data[0].intOne);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_array_class_11; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_class_64a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_class_64a.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":3198,"source_sha256":"3e2010077fed6121e7ba59ffb30a8bcd4578229a4208472a0cad66df2646c9ef","line_count":132,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_array_class_64a.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-64a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete[]\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_array_class_64\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(void * dataVoidPtr);\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new TwoIntsClass[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(void * dataVoidPtr);\r\n\r\nstatic void goodG2B()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new TwoIntsClass[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(void * dataVoidPtr);\r\n\r\nstatic void goodB2G()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new TwoIntsClass[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n goodB2GSink(&data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_array_class_64; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_int64_t_07.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_int64_t_07.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5907,"source_sha256":"504d98bc6f35f93088656fa91acaf4fa4e2b6df8bfd644cb4c02ae542e2a5522","line_count":218,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_array_int64_t_07.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-07.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete[]\r\n * Flow Variant: 07 Control flow: if(staticFive==5) and if(staticFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The variable below is not declared \"const\", but is never assigned\r\n any other value so a tool should be able to identify that reads of\r\n this will always give its initialized value. */\r\nstatic int staticFive = 5;\r\n\r\nnamespace CWE416_Use_After_Free__new_delete_array_int64_t_07\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n data = new int64_t[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticFive==5 to staticFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n data = new int64_t[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n data = new int64_t[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(staticFive==5)\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticFive==5 to staticFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new int64_t[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n data = new int64_t[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_array_int64_t_07; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_int64_t_13.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_int64_t_13.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5792,"source_sha256":"1bbdf7050f3b32c5faf6e3e24c35018e6e144d016ed20f869b4ef5c7291fb523","line_count":212,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_array_int64_t_13.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-13.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete[]\r\n * Flow Variant: 13 Control flow: if(GLOBAL_CONST_FIVE==5) and if(GLOBAL_CONST_FIVE!=5)\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_array_int64_t_13\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n data = new int64_t[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_FIVE==5 to GLOBAL_CONST_FIVE!=5 */\r\nstatic void goodB2G1()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n data = new int64_t[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(GLOBAL_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n data = new int64_t[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_FIVE==5 to GLOBAL_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new int64_t[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n data = new int64_t[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_array_int64_t_13; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_int64_t_15.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_int64_t_15.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":7072,"source_sha256":"dc6e0ac20844a0a0a89fd3b2e86cfa581ff3729ca64520849354c70ebb6edd67","line_count":262,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_array_int64_t_15.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-15.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete[]\r\n * Flow Variant: 15 Control flow: switch(6) and switch(7)\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_array_int64_t_15\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n data = new int64_t[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second switch to switch(8) */\r\nstatic void goodB2G1()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n data = new int64_t[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(8)\r\n {\r\n case 7:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second switch */\r\nstatic void goodB2G2()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n data = new int64_t[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first switch to switch(5) */\r\nstatic void goodG2B1()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(5)\r\n {\r\n case 6:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n data = new int64_t[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first switch */\r\nstatic void goodG2B2()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n data = new int64_t[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_array_int64_t_15; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_int64_t_16.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_int64_t_16.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":3871,"source_sha256":"8ef24289a83374df3bd8e0c1e37b9b9d5cd79f203d17ba49c4d3556691568050","line_count":152,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_array_int64_t_16.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-16.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete[]\r\n * Flow Variant: 16 Control flow: while(1)\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_array_int64_t_16\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n while(1)\r\n {\r\n data = new int64_t[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the sinks in the second while statement */\r\nstatic void goodB2G()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n while(1)\r\n {\r\n data = new int64_t[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the sources in the first while statement */\r\nstatic void goodG2B()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n while(1)\r\n {\r\n data = new int64_t[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5LL;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n break;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_array_int64_t_16; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_int64_t_62a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_int64_t_62a.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":2952,"source_sha256":"5b2b18d89284668b1011e109b1a9c935dd86e45c8ee539396ef140be32c1f434","line_count":110,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_array_int64_t_62a.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-62a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete[]\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_array_int64_t_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSource(int64_t * &data);\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n badSource(data);\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(int64_t * &data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goodG2BSource(data);\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(int64_t * &data);\r\n\r\nstatic void goodB2G()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goodB2GSource(data);\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_array_int64_t_62; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_int_08.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_int_08.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5986,"source_sha256":"fd5a9ffdd9947caa57a1bb38e9c445f55a042a3827c3118f7516efc0eeb3b351","line_count":226,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_array_int_08.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-08.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete[]\r\n * Flow Variant: 08 Control flow: if(staticReturnsTrue()) and if(staticReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The two function below always return the same value, so a tool\r\n should be able to identify that calls to the functions will always\r\n return a fixed value. */\r\nstatic int staticReturnsTrue()\r\n{\r\n return 1;\r\n}\r\n\r\nstatic int staticReturnsFalse()\r\n{\r\n return 0;\r\n}\r\n\r\nnamespace CWE416_Use_After_Free__new_delete_array_int_08\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n data = new int[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n data = new int[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n data = new int[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new int[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n data = new int[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_array_int_08; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_int_43.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_int_43.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":3595,"source_sha256":"c089f441edfed0d327dcab5e2f86d56b0fe77799fe40f16fd1091f717195d60b","line_count":144,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_array_int_43.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-43.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete[]\r\n * Flow Variant: 43 Data flow: data flows using a C++ reference from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_array_int_43\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSource(int * &data)\r\n{\r\n data = new int[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n}\r\n\r\nvoid bad()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n badSource(data);\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSource(int * &data)\r\n{\r\n data = new int[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goodG2BSource(data);\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSource(int * &data)\r\n{\r\n data = new int[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goodB2GSource(data);\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_array_int_43; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_long_07.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_long_07.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5851,"source_sha256":"8a55aeb9af818257018cd8122a0784d8d8daa24847c87e0a364766bbc1338ddb","line_count":218,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_array_long_07.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-07.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete[]\r\n * Flow Variant: 07 Control flow: if(staticFive==5) and if(staticFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The variable below is not declared \"const\", but is never assigned\r\n any other value so a tool should be able to identify that reads of\r\n this will always give its initialized value. */\r\nstatic int staticFive = 5;\r\n\r\nnamespace CWE416_Use_After_Free__new_delete_array_long_07\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n data = new long[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticFive==5 to staticFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n data = new long[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n data = new long[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(staticFive==5)\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticFive==5 to staticFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new long[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n data = new long[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_array_long_07; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_long_12.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_long_12.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5632,"source_sha256":"2938416e909e3c2f5e70314b781e815000e29fd70aac8f242e3b84ef17e4bf36","line_count":207,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_array_long_12.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-12.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete[]\r\n * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_array_long_12\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n data = new long[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n else\r\n {\r\n data = new long[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the first \"if\" so that\r\n both branches use the BadSource and the second \"if\" so that both branches\r\n use the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n data = new long[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n else\r\n {\r\n data = new long[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the first \"if\" so that\r\n both branches use the GoodSource and the second \"if\" so that both branches\r\n use the BadSink */\r\nstatic void goodG2B()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n data = new long[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n }\r\n else\r\n {\r\n data = new long[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n else\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_array_long_12; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_long_43.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_long_43.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":3612,"source_sha256":"0d31aeb74c06e244eb7bd5c431ff87ebe954351ea3fb74da3b64f0aa94ba613a","line_count":144,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_array_long_43.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-43.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete[]\r\n * Flow Variant: 43 Data flow: data flows using a C++ reference from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_array_long_43\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSource(long * &data)\r\n{\r\n data = new long[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n}\r\n\r\nvoid bad()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n badSource(data);\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSource(long * &data)\r\n{\r\n data = new long[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goodG2BSource(data);\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSource(long * &data)\r\n{\r\n data = new long[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i] = 5L;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goodB2GSource(data);\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_array_long_43; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_struct_01.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_struct_01.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":3493,"source_sha256":"a74db99ac181ab17b3547743d6d6d5ba27cc9824099b748090bed3c71dad321b","line_count":132,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_array_struct_01.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-01.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete[]\r\n * Flow Variant: 01 Baseline\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_array_struct_01\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new twoIntsStruct[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printStructLine(&data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new twoIntsStruct[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printStructLine(&data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new twoIntsStruct[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_array_struct_01; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_struct_02.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_struct_02.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5756,"source_sha256":"98d0703fee9afdf7fde00a98ea5fd13e92e27bafe6bd90b813572f7335e72c16","line_count":218,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_array_struct_02.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-02.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete[]\r\n * Flow Variant: 02 Control flow: if(1) and if(0)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_array_struct_02\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = new twoIntsStruct[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printStructLine(&data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 1 to 0 */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = new twoIntsStruct[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = new twoIntsStruct[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n }\r\n if(1)\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 1 to 0 */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new twoIntsStruct[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printStructLine(&data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = new twoIntsStruct[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printStructLine(&data[0]);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_array_struct_02; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_struct_62b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_struct_62b.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":1982,"source_sha256":"e34afe608dc9825bfd5334a071b337754acef61222cee3828495600e85f1cb66","line_count":79,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_array_struct_62b.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-62b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete[]\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_array_struct_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSource(twoIntsStruct * &data)\r\n{\r\n data = new twoIntsStruct[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(twoIntsStruct * &data)\r\n{\r\n data = new twoIntsStruct[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* FIX: Do not delete data in the source */\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(twoIntsStruct * &data)\r\n{\r\n data = new twoIntsStruct[100];\r\n {\r\n size_t i;\r\n for(i = 0; i < 100; i++)\r\n {\r\n data[i].intOne = 1;\r\n data[i].intTwo = 2;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_wchar_t_18.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_wchar_t_18.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":3414,"source_sha256":"e653360013c7d32558e39cc94dda625c3689b8c914323129e7a2302b2cddd18f","line_count":125,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_array_wchar_t_18.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-18.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete[]\r\n * Flow Variant: 18 Control flow: goto statements\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_array_wchar_t_18\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goto source;\r\nsource:\r\n data = new wchar_t[100];\r\n wmemset(data, L'A', 100-1);\r\n data[100-1] = L'\\0';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printWLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by reversing the blocks on the second goto statement */\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goto source;\r\nsource:\r\n data = new wchar_t[100];\r\n wmemset(data, L'A', 100-1);\r\n data[100-1] = L'\\0';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete [] data;\r\n goto sink;\r\nsink:\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by reversing the blocks on the first goto statement */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goto source;\r\nsource:\r\n data = new wchar_t[100];\r\n wmemset(data, L'A', 100-1);\r\n data[100-1] = L'\\0';\r\n /* FIX: Do not delete data in the source */\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printWLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_array_wchar_t_18; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_wchar_t_62a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_array_wchar_t_62a.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":2932,"source_sha256":"cfe46db98967c9f5d97f096f33d0425b320a237329ba20f47674f0cab8cfb3c4","line_count":110,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_array_wchar_t_62a.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete_array.label.xml\r\nTemplate File: sources-sinks-62a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete[]\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_array_wchar_t_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSource(wchar_t * &data);\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n badSource(data);\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printWLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(wchar_t * &data);\r\n\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goodG2BSource(data);\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printWLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(wchar_t * &data);\r\n\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goodB2GSource(data);\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_array_wchar_t_62; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_char_08.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_char_08.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5340,"source_sha256":"998323e3bbee84146e5360db6c7a639bce6747c2afac80eb0dd58c2d781c334c","line_count":196,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_char_08.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-08.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 08 Control flow: if(staticReturnsTrue()) and if(staticReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The two function below always return the same value, so a tool\r\n should be able to identify that calls to the functions will always\r\n return a fixed value. */\r\nstatic int staticReturnsTrue()\r\n{\r\n return 1;\r\n}\r\n\r\nstatic int staticReturnsFalse()\r\n{\r\n return 0;\r\n}\r\n\r\nnamespace CWE416_Use_After_Free__new_delete_char_08\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n data = new char;\r\n *data = 'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printHexCharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n data = new char;\r\n *data = 'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n data = new char;\r\n *data = 'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new char;\r\n *data = 'A';\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printHexCharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n data = new char;\r\n *data = 'A';\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printHexCharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_char_08; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_char_10.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_char_10.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":4918,"source_sha256":"07ecab6999460769f7b48b8a71d0fd80603ea886f829fbc12ba831b1af3090af","line_count":183,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_char_10.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-10.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 10 Control flow: if(globalTrue) and if(globalFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_char_10\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n data = new char;\r\n *data = 'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printHexCharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalTrue to globalFalse */\r\nstatic void goodB2G1()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n data = new char;\r\n *data = 'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n data = new char;\r\n *data = 'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(globalTrue)\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalTrue to globalFalse */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new char;\r\n *data = 'A';\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printHexCharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalTrue)\r\n {\r\n data = new char;\r\n *data = 'A';\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printHexCharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_char_10; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_char_14.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_char_14.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":4957,"source_sha256":"0f0b4c920507010c749dcc9ba236bcd3075aeaac3dd6e0c8b8029c1956e0c774","line_count":182,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_char_14.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-14.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 14 Control flow: if(globalFive==5) and if(globalFive!=5)\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_char_14\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalFive==5)\r\n {\r\n data = new char;\r\n *data = 'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printHexCharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalFive==5 to globalFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalFive==5)\r\n {\r\n data = new char;\r\n *data = 'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalFive==5)\r\n {\r\n data = new char;\r\n *data = 'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(globalFive==5)\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalFive==5 to globalFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new char;\r\n *data = 'A';\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printHexCharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalFive==5)\r\n {\r\n data = new char;\r\n *data = 'A';\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printHexCharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_char_14; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_char_15.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_char_15.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":6349,"source_sha256":"0b6b76abfb8cd88cc838104477848feeb8d22c07b237c233d87884e229750856","line_count":232,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_char_15.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-15.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 15 Control flow: switch(6) and switch(7)\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_char_15\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n data = new char;\r\n *data = 'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printHexCharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second switch to switch(8) */\r\nstatic void goodB2G1()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n data = new char;\r\n *data = 'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(8)\r\n {\r\n case 7:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second switch */\r\nstatic void goodB2G2()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n data = new char;\r\n *data = 'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first switch to switch(5) */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(5)\r\n {\r\n case 6:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n data = new char;\r\n *data = 'A';\r\n /* FIX: Do not delete data in the source */\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printHexCharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first switch */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n data = new char;\r\n *data = 'A';\r\n /* FIX: Do not delete data in the source */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printHexCharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_char_15; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_char_18.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_char_18.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":3231,"source_sha256":"b240b1fddb392c4caafada368f01379cc6102458bf8dbb7302f2c95bb87b4151","line_count":122,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_char_18.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-18.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 18 Control flow: goto statements\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_char_18\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goto source;\r\nsource:\r\n data = new char;\r\n *data = 'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printHexCharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by reversing the blocks on the second goto statement */\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goto source;\r\nsource:\r\n data = new char;\r\n *data = 'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n goto sink;\r\nsink:\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by reversing the blocks on the first goto statement */\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goto source;\r\nsource:\r\n data = new char;\r\n *data = 'A';\r\n /* FIX: Do not delete data in the source */\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printHexCharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_char_18; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_char_62a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_char_62a.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":2893,"source_sha256":"76aace457269ff6edeba83dce2b64e9ee8aa653f17534affeed43201bd237500","line_count":110,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_char_62a.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-62a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_char_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSource(char * &data);\r\n\r\nvoid bad()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n badSource(data);\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printHexCharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(char * &data);\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goodG2BSource(data);\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printHexCharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(char * &data);\r\n\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goodB2GSource(data);\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_char_62; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_class_06.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_class_06.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5519,"source_sha256":"148b60bc7b2b5123536eec365e6e8a29ca00b1fd78a072425d155b0be4a17ef4","line_count":193,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_class_06.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-06.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 06 Control flow: if(STATIC_CONST_FIVE==5) and if(STATIC_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The variable below is declared \"const\", so a tool should be able\r\n to identify that reads of this will always give its initialized\r\n value. */\r\nstatic const int STATIC_CONST_FIVE = 5;\r\n\r\nnamespace CWE416_Use_After_Free__new_delete_class_06\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n data = new TwoIntsClass;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data->intOne);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodB2G1()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n data = new TwoIntsClass;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n data = new TwoIntsClass;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new TwoIntsClass;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data->intOne);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n data = new TwoIntsClass;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data->intOne);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_class_06; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_class_14.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_class_14.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5209,"source_sha256":"ee625521f65f53449eccf72962e8505e449810ef1dcc35a8d27ca48367c6b911","line_count":187,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_class_14.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-14.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 14 Control flow: if(globalFive==5) and if(globalFive!=5)\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_class_14\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalFive==5)\r\n {\r\n data = new TwoIntsClass;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data->intOne);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalFive==5 to globalFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalFive==5)\r\n {\r\n data = new TwoIntsClass;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalFive==5)\r\n {\r\n data = new TwoIntsClass;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(globalFive==5)\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalFive==5 to globalFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new TwoIntsClass;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data->intOne);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalFive==5)\r\n {\r\n data = new TwoIntsClass;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data->intOne);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_class_14; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_class_63b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_class_63b.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":1815,"source_sha256":"c548cacf51dafe44c4c1bc301ab39246b7b6a6d643a1422df66c40889c35080f","line_count":60,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_class_63b.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-63b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_class_63\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(TwoIntsClass * * dataPtr)\r\n{\r\n TwoIntsClass * data = *dataPtr;\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data->intOne);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(TwoIntsClass * * dataPtr)\r\n{\r\n TwoIntsClass * data = *dataPtr;\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(data->intOne);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(TwoIntsClass * * dataPtr)\r\n{\r\n TwoIntsClass * data = *dataPtr;\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_int64_t_06.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_int64_t_06.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5309,"source_sha256":"ae18787352a10a349cb1571130816dc70a82d31fd2467598d19a36730abed4b4","line_count":188,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_int64_t_06.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-06.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 06 Control flow: if(STATIC_CONST_FIVE==5) and if(STATIC_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The variable below is declared \"const\", so a tool should be able\r\n to identify that reads of this will always give its initialized\r\n value. */\r\nstatic const int STATIC_CONST_FIVE = 5;\r\n\r\nnamespace CWE416_Use_After_Free__new_delete_int64_t_06\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n data = new int64_t;\r\n *data = 5LL;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLongLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodB2G1()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n data = new int64_t;\r\n *data = 5LL;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n data = new int64_t;\r\n *data = 5LL;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new int64_t;\r\n *data = 5LL;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLongLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n data = new int64_t;\r\n *data = 5LL;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLongLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_int64_t_06; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_int64_t_16.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_int64_t_16.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":3451,"source_sha256":"580c4efe05cd24bb4e8f8d306800b3fb37554edd6d52e1a30e0c38246cb35681","line_count":134,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_int64_t_16.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-16.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 16 Control flow: while(1)\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_int64_t_16\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n while(1)\r\n {\r\n data = new int64_t;\r\n *data = 5LL;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLongLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the sinks in the second while statement */\r\nstatic void goodB2G()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n while(1)\r\n {\r\n data = new int64_t;\r\n *data = 5LL;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the sources in the first while statement */\r\nstatic void goodG2B()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n while(1)\r\n {\r\n data = new int64_t;\r\n *data = 5LL;\r\n /* FIX: Do not delete data in the source */\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLongLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n break;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_int64_t_16; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_int64_t_63a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_int64_t_63a.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":2708,"source_sha256":"c507fe8706830b5dff6c1fa6f9a13e1f929c0e6dd8a3ea0de095c5b2ee2970ed","line_count":111,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_int64_t_63a.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-63a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_int64_t_63\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(int64_t * * dataPtr);\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int64_t;\r\n *data = 5LL;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(int64_t * * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int64_t;\r\n *data = 5LL;\r\n /* FIX: Do not delete data in the source */\r\n goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(int64_t * * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n int64_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new int64_t;\r\n *data = 5LL;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n goodB2GSink(&data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_int64_t_63; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_int_11.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_int_11.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5023,"source_sha256":"314797c737220674cd124e3099a3564c85947eab9bf1aa6111890c8d2e149848","line_count":182,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_int_11.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-11.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_int_11\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n data = new int;\r\n *data = 5;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n data = new int;\r\n *data = 5;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n data = new int;\r\n *data = 5;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new int;\r\n *data = 5;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrue())\r\n {\r\n data = new int;\r\n *data = 5;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_int_11; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_int_17.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_int_17.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":3369,"source_sha256":"566e3fc36eb518eb811b9d5260e787eb6dcbe016967131bf009207065ffb0869","line_count":131,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_int_17.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-17.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 17 Control flow: for loops\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_int_17\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int i,j;\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n for(i = 0; i < 1; i++)\r\n {\r\n data = new int;\r\n *data = 5;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n for(j = 0; j < 1; j++)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink in the for statements */\r\nstatic void goodB2G()\r\n{\r\n int i,k;\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n for(i = 0; i < 1; i++)\r\n {\r\n data = new int;\r\n *data = 5;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n for(k = 0; k < 1; k++)\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink in the for statements */\r\nstatic void goodG2B()\r\n{\r\n int h,j;\r\n int * data;\r\n /* Initialize data */\r\n data = NULL;\r\n for(h = 0; h < 1; h++)\r\n {\r\n data = new int;\r\n *data = 5;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n for(j = 0; j < 1; j++)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printIntLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_int_17; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_long_02.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_long_02.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":4755,"source_sha256":"330793b73dd8904ab775b0681e534f12466d3eb0535e7e7ab86c3aa3e7622a0a","line_count":183,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_long_02.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-02.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 02 Control flow: if(1) and if(0)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_long_02\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 1 to 0 */\r\nstatic void goodB2G1()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(1)\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 1 to 0 */\r\nstatic void goodG2B1()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_long_02; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_long_06.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_long_06.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5253,"source_sha256":"21c5c430ebc6eb7938ea6d44e7044154c1e7c277dc1b73bda4b06b00cabe4b8d","line_count":188,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_long_06.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-06.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 06 Control flow: if(STATIC_CONST_FIVE==5) and if(STATIC_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The variable below is declared \"const\", so a tool should be able\r\n to identify that reads of this will always give its initialized\r\n value. */\r\nstatic const int STATIC_CONST_FIVE = 5;\r\n\r\nnamespace CWE416_Use_After_Free__new_delete_long_06\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodB2G1()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_long_06; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_long_07.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_long_07.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5170,"source_sha256":"2ef6ba5257b76b8f9d93f60c9d276895d553dbfe70992bd0857631b4b89a6eb3","line_count":188,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_long_07.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-07.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 07 Control flow: if(staticFive==5) and if(staticFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The variable below is not declared \"const\", but is never assigned\r\n any other value so a tool should be able to identify that reads of\r\n this will always give its initialized value. */\r\nstatic int staticFive = 5;\r\n\r\nnamespace CWE416_Use_After_Free__new_delete_long_07\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticFive==5 to staticFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(staticFive==5)\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticFive==5 to staticFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_long_07; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_long_08.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_long_08.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5326,"source_sha256":"29946a9030a934e0c07d694d884c1b732c5a351d37f6ace9cdb3063353a1e9cb","line_count":196,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_long_08.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-08.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 08 Control flow: if(staticReturnsTrue()) and if(staticReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The two function below always return the same value, so a tool\r\n should be able to identify that calls to the functions will always\r\n return a fixed value. */\r\nstatic int staticReturnsTrue()\r\n{\r\n return 1;\r\n}\r\n\r\nstatic int staticReturnsFalse()\r\n{\r\n return 0;\r\n}\r\n\r\nnamespace CWE416_Use_After_Free__new_delete_long_08\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_long_08; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_long_12.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_long_12.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":4823,"source_sha256":"78b5b0d6bed6f9fd5bd97338e69e74e4d9dceb1b407814c14ff6182081a11522","line_count":171,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_long_12.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-12.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_long_12\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n else\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the first \"if\" so that\r\n both branches use the BadSource and the second \"if\" so that both branches\r\n use the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n else\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the first \"if\" so that\r\n both branches use the GoodSource and the second \"if\" so that both branches\r\n use the BadSink */\r\nstatic void goodG2B()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n else\r\n {\r\n data = new long;\r\n *data = 5L;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n else\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_long_12; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_long_18.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_long_18.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":3222,"source_sha256":"9fbe6d20816ff472fdd7bd8b48f4b8bd56d139d8dd2b83fd342f3abcb578d349","line_count":122,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_long_18.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-18.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 18 Control flow: goto statements\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_long_18\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goto source;\r\nsource:\r\n data = new long;\r\n *data = 5L;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by reversing the blocks on the second goto statement */\r\nstatic void goodB2G()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goto source;\r\nsource:\r\n data = new long;\r\n *data = 5L;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n goto sink;\r\nsink:\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by reversing the blocks on the first goto statement */\r\nstatic void goodG2B()\r\n{\r\n long * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goto source;\r\nsource:\r\n data = new long;\r\n *data = 5L;\r\n /* FIX: Do not delete data in the source */\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printLongLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_long_18; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_struct_02.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_struct_02.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5019,"source_sha256":"8ea1cd9f6f123b57de48c986a3fef43aed5112de77946c4f211067bc13962b22","line_count":188,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_struct_02.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-02.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 02 Control flow: if(1) and if(0)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_struct_02\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printStructLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 1 to 0 */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(1)\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 1 to 0 */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printStructLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(1)\r\n {\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printStructLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_struct_02; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_struct_03.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_struct_03.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5067,"source_sha256":"064294457d6ccee2d52aa03dc24dce9a9a718e94bb38d60bd4c4eb802a394558","line_count":188,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_struct_03.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-03.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 03 Control flow: if(5==5) and if(5!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_struct_03\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5==5)\r\n {\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printStructLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 5==5 to 5!=5 */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5==5)\r\n {\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5==5)\r\n {\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(5==5)\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 5==5 to 5!=5 */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printStructLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(5==5)\r\n {\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printStructLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_struct_03; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_struct_05.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_struct_05.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5459,"source_sha256":"c884b149473ae664cb67bb8daee0da9d89ba4828646f4f31616523ce2e794009","line_count":194,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_struct_05.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-05.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 05 Control flow: if(staticTrue) and if(staticFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The two variables below are not defined as \"const\", but are never\r\n assigned any other value, so a tool should be able to identify that\r\n reads of these will always return their initialized values. */\r\nstatic int staticTrue = 1; /* true */\r\nstatic int staticFalse = 0; /* false */\r\n\r\nnamespace CWE416_Use_After_Free__new_delete_struct_05\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticTrue)\r\n {\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printStructLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticTrue to staticFalse */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticTrue)\r\n {\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(staticFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticTrue)\r\n {\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(staticTrue)\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticTrue to staticFalse */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printStructLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticTrue)\r\n {\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printStructLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_struct_05; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_struct_08.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_struct_08.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5590,"source_sha256":"9bc066ae3e8b527298af69c28c39a556b4fec03b51fc850f1a4adc66920d391c","line_count":201,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_struct_08.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-08.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 08 Control flow: if(staticReturnsTrue()) and if(staticReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The two function below always return the same value, so a tool\r\n should be able to identify that calls to the functions will always\r\n return a fixed value. */\r\nstatic int staticReturnsTrue()\r\n{\r\n return 1;\r\n}\r\n\r\nstatic int staticReturnsFalse()\r\n{\r\n return 0;\r\n}\r\n\r\nnamespace CWE416_Use_After_Free__new_delete_struct_08\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printStructLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printStructLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printStructLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_struct_08; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_struct_15.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_struct_15.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":6599,"source_sha256":"66cce0e1a08905d27b50535d4ec874a1c18d09d5d87c32f0f10d0c1c2a33e4e6","line_count":237,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_struct_15.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-15.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 15 Control flow: switch(6) and switch(7)\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_struct_15\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printStructLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second switch to switch(8) */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(8)\r\n {\r\n case 7:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second switch */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first switch to switch(5) */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(5)\r\n {\r\n case 6:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* FIX: Do not delete data in the source */\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printStructLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first switch */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct * data;\r\n /* Initialize data */\r\n data = NULL;\r\n switch(6)\r\n {\r\n case 6:\r\n data = new twoIntsStruct;\r\n data->intOne = 1;\r\n data->intTwo = 2;\r\n /* FIX: Do not delete data in the source */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printStructLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_struct_15; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_struct_63b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_struct_63b.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":1813,"source_sha256":"f39dc5cd4133ebbcbcf443c3395f335e37b2b4f8837adee6acdbf1cab972b780","line_count":60,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_struct_63b.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-63b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_struct_63\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(twoIntsStruct * * dataPtr)\r\n{\r\n twoIntsStruct * data = *dataPtr;\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printStructLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(twoIntsStruct * * dataPtr)\r\n{\r\n twoIntsStruct * data = *dataPtr;\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printStructLine(data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(twoIntsStruct * * dataPtr)\r\n{\r\n twoIntsStruct * data = *dataPtr;\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_wchar_t_04.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_wchar_t_04.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5339,"source_sha256":"fd917d80c6df4c0b8f245ce05d494d962a81430428d264060d9e50c5de3b5149","line_count":189,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_wchar_t_04.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-04.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 04 Control flow: if(STATIC_CONST_TRUE) and if(STATIC_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The two variables below are declared \"const\", so a tool should\r\n be able to identify that reads of these will always return their\r\n initialized values. */\r\nstatic const int STATIC_CONST_TRUE = 1; /* true */\r\nstatic const int STATIC_CONST_FALSE = 0; /* false */\r\n\r\nnamespace CWE416_Use_After_Free__new_delete_wchar_t_04\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n data = new wchar_t;\r\n *data = L'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printWcharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second STATIC_CONST_TRUE to STATIC_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n data = new wchar_t;\r\n *data = L'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(STATIC_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n data = new wchar_t;\r\n *data = L'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first STATIC_CONST_TRUE to STATIC_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new wchar_t;\r\n *data = L'A';\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printWcharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n data = new wchar_t;\r\n *data = L'A';\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printWcharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_wchar_t_04; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_wchar_t_07.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_wchar_t_07.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5222,"source_sha256":"665ad06bbd0bcc564ef178c87be1a1f55300baef46b6c1706b8b3cba580ce1a1","line_count":188,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_wchar_t_07.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-07.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 07 Control flow: if(staticFive==5) and if(staticFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The variable below is not declared \"const\", but is never assigned\r\n any other value so a tool should be able to identify that reads of\r\n this will always give its initialized value. */\r\nstatic int staticFive = 5;\r\n\r\nnamespace CWE416_Use_After_Free__new_delete_wchar_t_07\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n data = new wchar_t;\r\n *data = L'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printWcharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticFive==5 to staticFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n data = new wchar_t;\r\n *data = L'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n data = new wchar_t;\r\n *data = L'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(staticFive==5)\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticFive==5 to staticFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new wchar_t;\r\n *data = L'A';\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printWcharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticFive==5)\r\n {\r\n data = new wchar_t;\r\n *data = L'A';\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printWcharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_wchar_t_07; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_wchar_t_08.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_wchar_t_08.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":5378,"source_sha256":"c27f6eb1e2abeff8426f2d2101b73127cae0e02163154153e561c199fdef7adb","line_count":196,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_wchar_t_08.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-08.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 08 Control flow: if(staticReturnsTrue()) and if(staticReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The two function below always return the same value, so a tool\r\n should be able to identify that calls to the functions will always\r\n return a fixed value. */\r\nstatic int staticReturnsTrue()\r\n{\r\n return 1;\r\n}\r\n\r\nstatic int staticReturnsFalse()\r\n{\r\n return 0;\r\n}\r\n\r\nnamespace CWE416_Use_After_Free__new_delete_wchar_t_08\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n data = new wchar_t;\r\n *data = L'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printWcharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n data = new wchar_t;\r\n *data = L'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n data = new wchar_t;\r\n *data = L'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n data = new wchar_t;\r\n *data = L'A';\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printWcharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n if(staticReturnsTrue())\r\n {\r\n data = new wchar_t;\r\n *data = L'A';\r\n /* FIX: Do not delete data in the source */\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printWcharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_wchar_t_08; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_wchar_t_18.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_wchar_t_18.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":3257,"source_sha256":"0dac5bfae373ba568afaf45ed19e2dd81b7e221adc3f46c03a613a5fcce51ff4","line_count":122,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_wchar_t_18.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-18.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 18 Control flow: goto statements\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_wchar_t_18\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goto source;\r\nsource:\r\n data = new wchar_t;\r\n *data = L'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printWcharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by reversing the blocks on the second goto statement */\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goto source;\r\nsource:\r\n data = new wchar_t;\r\n *data = L'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n goto sink;\r\nsink:\r\n /* FIX: Don't use data that may have been deleted already */\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n /* do nothing */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by reversing the blocks on the first goto statement */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n goto source;\r\nsource:\r\n data = new wchar_t;\r\n *data = L'A';\r\n /* FIX: Do not delete data in the source */\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Use of data that may have been deleted */\r\n printWcharLine(*data);\r\n /* POTENTIAL INCIDENTAL - Possible memory leak here if data was not deleted */\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_wchar_t_18; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_wchar_t_63a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__new_delete_wchar_t_63a.cpp","cwe_id":"CWE416","file_extension":".cpp","language_hint":"C++","source_size_bytes":2711,"source_sha256":"c7bd3a44e3e9162bbca7061f3849a0901248dc071dc0511c9791027f71662c8e","line_count":111,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__new_delete_wchar_t_63a.cpp\r\nLabel Definition File: CWE416_Use_After_Free__new_delete.label.xml\r\nTemplate File: sources-sinks-63a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * BadSource: Allocate data using new, initialize memory block, and Deallocate data using delete\r\n * GoodSource: Allocate data using new and initialize memory block\r\n * Sinks:\r\n * GoodSink: Do nothing\r\n * BadSink : Use data after delete\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE416_Use_After_Free__new_delete_wchar_t_63\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(wchar_t * * dataPtr);\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new wchar_t;\r\n *data = L'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(wchar_t * * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new wchar_t;\r\n *data = L'A';\r\n /* FIX: Do not delete data in the source */\r\n goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(wchar_t * * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n /* Initialize data */\r\n data = NULL;\r\n data = new wchar_t;\r\n *data = L'A';\r\n /* POTENTIAL FLAW: Delete data in the source - the bad sink attempts to use data */\r\n delete data;\r\n goodB2GSink(&data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE416_Use_After_Free__new_delete_wchar_t_63; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__return_freed_ptr_04.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE416_Use_After_Free/CWE416_Use_After_Free__return_freed_ptr_04.c","cwe_id":"CWE416","file_extension":".c","language_hint":"C","source_size_bytes":4785,"source_sha256":"ac7f2413579f1c3705d7c03382cd65ea013ffc864886f51366767d3b13fdb9ef","line_count":166,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE416_Use_After_Free__return_freed_ptr_04.c\r\nLabel Definition File: CWE416_Use_After_Free__return_freed_ptr.label.xml\r\nTemplate File: point-flaw-04.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 416 Use After Free\r\n * Sinks:\r\n * GoodSink: Use a block of memory returned from a function\r\n * BadSink : Use a block of memory returned from a function after it has been freed\r\n * Flow Variant: 04 Control flow: if(STATIC_CONST_TRUE) and if(STATIC_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nstatic char * helperBad(char * aString)\r\n{\r\n size_t i = 0;\r\n size_t j;\r\n char * reversedString = NULL;\r\n if (aString != NULL)\r\n {\r\n i = strlen(aString);\r\n reversedString = (char *) malloc(i+1);\r\n if (reversedString == NULL) {exit(-1);}\n for (j = 0; j < i; j++)\r\n {\r\n reversedString[j] = aString[i-j-1];\r\n }\r\n reversedString[i] = '\\0';\r\n /* FLAW: Freeing a memory block and then returning a pointer to the freed memory */\r\n free(reversedString);\r\n return reversedString;\r\n }\r\n else\r\n {\r\n return NULL;\r\n }\r\n}\r\n\r\nstatic char * helperGood(char * aString)\r\n{\r\n size_t i = 0;\r\n size_t j;\r\n char * reversedString = NULL;\r\n if (aString != NULL)\r\n {\r\n i = strlen(aString);\r\n reversedString = (char *) malloc(i+1);\r\n if (reversedString == NULL) {exit(-1);}\n for (j = 0; j < i; j++)\r\n {\r\n reversedString[j] = aString[i-j-1];\r\n }\r\n reversedString[i] = '\\0';\r\n /* FIX: Do not free the memory before returning */\r\n return reversedString;\r\n }\r\n else\r\n {\r\n return NULL;\r\n }\r\n}\r\n\r\n/* The two variables below are declared \"const\", so a tool should\r\n be able to identify that reads of these will always return their\r\n initialized values. */\r\nstatic const int STATIC_CONST_TRUE = 1; /* true */\r\nstatic const int STATIC_CONST_FALSE = 0; /* false */\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE416_Use_After_Free__return_freed_ptr_04_bad()\r\n{\r\n if(STATIC_CONST_TRUE)\r\n {\r\n {\r\n /* Call the bad helper function */\r\n char * reversedString = helperBad(\"BadSink\");\r\n printLine(reversedString);\r\n /* free(reversedString);\r\n * This call to free() was removed because we want the tool to detect the use after free,\r\n * but we don't want that function to be free(). Essentially we want to avoid a double free\r\n */\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good1() uses if(STATIC_CONST_FALSE) instead of if(STATIC_CONST_TRUE) */\r\nstatic void good1()\r\n{\r\n if(STATIC_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n {\r\n /* Call the good helper function */\r\n char * reversedString = helperGood(\"GoodSink\");\r\n printLine(reversedString);\r\n /* free(reversedString);\r\n * This call to free() was removed because we want the tool to detect the use after free,\r\n * but we don't want that function to be free(). Essentially we want to avoid a double free\r\n */\r\n }\r\n }\r\n}\r\n\r\n/* good2() reverses the bodies in the if statement */\r\nstatic void good2()\r\n{\r\n if(STATIC_CONST_TRUE)\r\n {\r\n {\r\n /* Call the good helper function */\r\n char * reversedString = helperGood(\"GoodSink\");\r\n printLine(reversedString);\r\n /* free(reversedString);\r\n * This call to free() was removed because we want the tool to detect the use after free,\r\n * but we don't want that function to be free(). Essentially we want to avoid a double free\r\n */\r\n }\r\n }\r\n}\r\n\r\nvoid CWE416_Use_After_Free__return_freed_ptr_04_good()\r\n{\r\n good1();\r\n good2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE416_Use_After_Free__return_freed_ptr_04_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE416_Use_After_Free__return_freed_ptr_04_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__char_pointer_03.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__char_pointer_03.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":3760,"source_sha256":"071bd772caf927bb7cd4f2e950a7766f19190776f83783a4c985732b6dd8b1b9","line_count":153,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__char_pointer_03.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c.label.xml\r\nTemplate File: sources-sinks-03.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 03 Control flow: if(5==5) and if(5!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__char_pointer_03_bad()\r\n{\r\n char * data;\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 5==5 to 5!=5 */\r\nstatic void goodB2G1()\r\n{\r\n char * data;\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n data = \"string\";\r\n printLine(data);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n char * data;\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(5==5)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n data = \"string\";\r\n printLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 5==5 to 5!=5 */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Initialize data */\r\n data = \"string\";\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n if(5==5)\r\n {\r\n /* FIX: Initialize data */\r\n data = \"string\";\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printLine(data);\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__char_pointer_03_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__char_pointer_03_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__char_pointer_03_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_07.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_07.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":4107,"source_sha256":"a1198a5e6e100346b0f879b90a6dc13008b996efdd7fc1258c417aa36f6aba75","line_count":158,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__double_07.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c.label.xml\r\nTemplate File: sources-sinks-07.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 07 Control flow: if(staticFive==5) and if(staticFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The variable below is not declared \"const\", but is never assigned\r\n any other value so a tool should be able to identify that reads of\r\n this will always give its initialized value. */\r\nstatic int staticFive = 5;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_07_bad()\r\n{\r\n double data;\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printDoubleLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticFive==5 to staticFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n double data;\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n data = 5.0;\r\n printDoubleLine(data);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n double data;\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(staticFive==5)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n data = 5.0;\r\n printDoubleLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticFive==5 to staticFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n double data;\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Initialize data */\r\n data = 5.0;\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printDoubleLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n double data;\r\n if(staticFive==5)\r\n {\r\n /* FIX: Initialize data */\r\n data = 5.0;\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printDoubleLine(data);\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_07_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_07_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_07_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_08.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_08.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":4263,"source_sha256":"b00483f04b30925216619c9f64e504c5beb38840eb6f0c54b5e46d7634f3a4f5","line_count":166,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__double_08.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c.label.xml\r\nTemplate File: sources-sinks-08.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 08 Control flow: if(staticReturnsTrue()) and if(staticReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The two function below always return the same value, so a tool\r\n should be able to identify that calls to the functions will always\r\n return a fixed value. */\r\nstatic int staticReturnsTrue()\r\n{\r\n return 1;\r\n}\r\n\r\nstatic int staticReturnsFalse()\r\n{\r\n return 0;\r\n}\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_08_bad()\r\n{\r\n double data;\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printDoubleLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n double data;\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n data = 5.0;\r\n printDoubleLine(data);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n double data;\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n data = 5.0;\r\n printDoubleLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n double data;\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Initialize data */\r\n data = 5.0;\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printDoubleLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n double data;\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Initialize data */\r\n data = 5.0;\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printDoubleLine(data);\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_08_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_08_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_08_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_09.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_09.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":3953,"source_sha256":"493153faadffab2a7f5247033fbac84386f3f79622d7b67a85d7d451ca5ef697","line_count":153,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__double_09.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c.label.xml\r\nTemplate File: sources-sinks-09.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_09_bad()\r\n{\r\n double data;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printDoubleLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n double data;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n data = 5.0;\r\n printDoubleLine(data);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n double data;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n data = 5.0;\r\n printDoubleLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n double data;\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Initialize data */\r\n data = 5.0;\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printDoubleLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n double data;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Initialize data */\r\n data = 5.0;\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printDoubleLine(data);\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_09_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_09_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_09_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_16.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_16.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":2762,"source_sha256":"b5f86380d498d95fed66c67f2cae40a9c727ca57ce45ee9c71db340e31013907","line_count":114,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__double_16.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c.label.xml\r\nTemplate File: sources-sinks-16.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 16 Control flow: while(1)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_16_bad()\r\n{\r\n double data;\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printDoubleLine(data);\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the sinks in the second while statement */\r\nstatic void goodB2G()\r\n{\r\n double data;\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n data = 5.0;\r\n printDoubleLine(data);\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the sources in the first while statement */\r\nstatic void goodG2B()\r\n{\r\n double data;\r\n while(1)\r\n {\r\n /* FIX: Initialize data */\r\n data = 5.0;\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printDoubleLine(data);\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_16_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_16_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_16_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_43.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_43.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":2664,"source_sha256":"786d912124ea05c00f891a72d0e316a3f064f6adb8ea2620c4ddfc8e8c2511fa","line_count":112,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__double_43.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c.label.xml\r\nTemplate File: sources-sinks-43.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 43 Data flow: data flows using a C++ reference from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__double_43\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSource(double &data)\r\n{\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\nvoid bad()\r\n{\r\n double data;\r\n badSource(data);\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printDoubleLine(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSource(double &data)\r\n{\r\n /* FIX: Initialize data */\r\n data = 5.0;\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n double data;\r\n goodG2BSource(data);\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printDoubleLine(data);\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSource(double &data)\r\n{\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n double data;\r\n goodB2GSource(data);\r\n /* FIX: Ensure data is initialized before use */\r\n data = 5.0;\r\n printDoubleLine(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__double_43; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_63b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_63b.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":1442,"source_sha256":"4e30ca6dacd141508424403e9583244a95c664b751d13cd2432fa24da229e5a5","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__double_63b.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c.label.xml\r\nTemplate File: sources-sinks-63b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_63b_badSink(double * dataPtr)\r\n{\r\n double data = *dataPtr;\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printDoubleLine(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_63b_goodG2BSink(double * dataPtr)\r\n{\r\n double data = *dataPtr;\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printDoubleLine(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_63b_goodB2GSink(double * dataPtr)\r\n{\r\n double data = *dataPtr;\r\n /* FIX: Ensure data is initialized before use */\r\n data = 5.0;\r\n printDoubleLine(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_alloca_no_init_17.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_alloca_no_init_17.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":3577,"source_sha256":"ddfa20de02f8ef2ac01fcfdd069672350853e03d75ef0a7ccbcab92ed911e2f0","line_count":142,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__double_array_alloca_no_init_17.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-17.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 17 Control flow: for loops\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_alloca_no_init_17_bad()\r\n{\r\n int i,j;\r\n double * data;\r\n data = (double *)ALLOCA(10*sizeof(double));\r\n for(i = 0; i < 1; i++)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n for(j = 0; j < 1; j++)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink in the for statements */\r\nstatic void goodB2G()\r\n{\r\n int i,k;\r\n double * data;\r\n data = (double *)ALLOCA(10*sizeof(double));\r\n for(i = 0; i < 1; i++)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n for(k = 0; k < 1; k++)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink in the for statements */\r\nstatic void goodG2B()\r\n{\r\n int h,j;\r\n double * data;\r\n data = (double *)ALLOCA(10*sizeof(double));\r\n for(h = 0; h < 1; h++)\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n }\r\n for(j = 0; j < 1; j++)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_alloca_no_init_17_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_array_alloca_no_init_17_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_array_alloca_no_init_17_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_alloca_no_init_62a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_alloca_no_init_62a.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":2975,"source_sha256":"f805812db13768d1ab13a278cf3f5c0e88c33e3bec5714aca65325bf99f2538a","line_count":126,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__double_array_alloca_no_init_62a.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-62a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__double_array_alloca_no_init_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSource(double * &data);\r\n\r\nvoid bad()\r\n{\r\n double * data;\r\n data = (double *)ALLOCA(10*sizeof(double));\r\n badSource(data);\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(double * &data);\r\n\r\nstatic void goodG2B()\r\n{\r\n double * data;\r\n data = (double *)ALLOCA(10*sizeof(double));\r\n goodG2BSource(data);\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(double * &data);\r\n\r\nstatic void goodB2G()\r\n{\r\n double * data;\r\n data = (double *)ALLOCA(10*sizeof(double));\r\n goodB2GSource(data);\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__double_array_alloca_no_init_62; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_alloca_partial_init_15.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_alloca_partial_init_15.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":6648,"source_sha256":"105b521f8c4ffecf9321b7425012a28fd40fc0411a5db972aab1224ba024fc3a","line_count":278,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__double_array_alloca_partial_init_15.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-15.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 15 Control flow: switch(6) and switch(7)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_alloca_partial_init_15_bad()\r\n{\r\n double * data;\r\n data = (double *)ALLOCA(10*sizeof(double));\r\n switch(6)\r\n {\r\n case 6:\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second switch to switch(8) */\r\nstatic void goodB2G1()\r\n{\r\n double * data;\r\n data = (double *)ALLOCA(10*sizeof(double));\r\n switch(6)\r\n {\r\n case 6:\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(8)\r\n {\r\n case 7:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n break;\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second switch */\r\nstatic void goodB2G2()\r\n{\r\n double * data;\r\n data = (double *)ALLOCA(10*sizeof(double));\r\n switch(6)\r\n {\r\n case 6:\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first switch to switch(5) */\r\nstatic void goodG2B1()\r\n{\r\n double * data;\r\n data = (double *)ALLOCA(10*sizeof(double));\r\n switch(5)\r\n {\r\n case 6:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first switch */\r\nstatic void goodG2B2()\r\n{\r\n double * data;\r\n data = (double *)ALLOCA(10*sizeof(double));\r\n switch(6)\r\n {\r\n case 6:\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_alloca_partial_init_15_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_array_alloca_partial_init_15_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_array_alloca_partial_init_15_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_alloca_partial_init_18.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_alloca_partial_init_18.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":3506,"source_sha256":"af71cd9d95e42bd4925995369823d6f1388ec8e1c4127344980a91df96c5b95c","line_count":145,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__double_array_alloca_partial_init_18.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-18.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 18 Control flow: goto statements\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_alloca_partial_init_18_bad()\r\n{\r\n double * data;\r\n data = (double *)ALLOCA(10*sizeof(double));\r\n goto source;\r\nsource:\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by reversing the blocks on the second goto statement */\r\nstatic void goodB2G()\r\n{\r\n double * data;\r\n data = (double *)ALLOCA(10*sizeof(double));\r\n goto source;\r\nsource:\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n goto sink;\r\nsink:\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by reversing the blocks on the first goto statement */\r\nstatic void goodG2B()\r\n{\r\n double * data;\r\n data = (double *)ALLOCA(10*sizeof(double));\r\n goto source;\r\nsource:\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_alloca_partial_init_18_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_array_alloca_partial_init_18_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_array_alloca_partial_init_18_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_declare_no_init_12.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_declare_no_init_12.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":5422,"source_sha256":"78b9a6612ae69150ec71481ddb90d4a03cdc6a4dd70a177d3639f37320b29701","line_count":220,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__double_array_declare_no_init_12.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-12.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_declare_no_init_12_bad()\r\n{\r\n double * data;\r\n double dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the first \"if\" so that\r\n both branches use the BadSource and the second \"if\" so that both branches\r\n use the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n double * data;\r\n double dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n else\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the first \"if\" so that\r\n both branches use the GoodSource and the second \"if\" so that both branches\r\n use the BadSink */\r\nstatic void goodG2B()\r\n{\r\n double * data;\r\n double dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n }\r\n else\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_declare_no_init_12_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_array_declare_no_init_12_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_array_declare_no_init_12_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_declare_no_init_62a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_declare_no_init_62a.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":3017,"source_sha256":"aae232be841c3384fd36067724e98dd25ec1058329466b09d7c5c2c39e91d775","line_count":129,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__double_array_declare_no_init_62a.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-62a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__double_array_declare_no_init_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSource(double * &data);\r\n\r\nvoid bad()\r\n{\r\n double * data;\r\n double dataUninitArray[10];\r\n data = dataUninitArray;\r\n badSource(data);\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(double * &data);\r\n\r\nstatic void goodG2B()\r\n{\r\n double * data;\r\n double dataUninitArray[10];\r\n data = dataUninitArray;\r\n goodG2BSource(data);\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(double * &data);\r\n\r\nstatic void goodB2G()\r\n{\r\n double * data;\r\n double dataUninitArray[10];\r\n data = dataUninitArray;\r\n goodB2GSource(data);\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__double_array_declare_no_init_62; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_declare_partial_init_01.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_declare_partial_init_01.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":3286,"source_sha256":"6d1e1a77f410096f1d659c7fe7671bf5ca78a93f4915a26fb3002dea0eb0f350","line_count":136,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__double_array_declare_partial_init_01.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-01.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 01 Baseline\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_declare_partial_init_01_bad()\r\n{\r\n double * data;\r\n double dataUninitArray[10];\r\n data = dataUninitArray;\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n double * data;\r\n double dataUninitArray[10];\r\n data = dataUninitArray;\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n double * data;\r\n double dataUninitArray[10];\r\n data = dataUninitArray;\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_declare_partial_init_01_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_array_declare_partial_init_01_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_array_declare_partial_init_01_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_declare_partial_init_09.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_declare_partial_init_09.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":5771,"source_sha256":"86efb8ed35b5b0b272a9415c32a1948958788bbf4cd463c230788ac61f41f911","line_count":233,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__double_array_declare_partial_init_09.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-09.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_declare_partial_init_09_bad()\r\n{\r\n double * data;\r\n double dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n double * data;\r\n double dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n }\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n double * data;\r\n double dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n double * data;\r\n double dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n double * data;\r\n double dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_declare_partial_init_09_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_array_declare_partial_init_09_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_array_declare_partial_init_09_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_declare_partial_init_16.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_declare_partial_init_16.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":3877,"source_sha256":"6b273ea5c25d1d3d294eb93a9c21cf0a79fa871da8acf2392e2928abaa5bcd64","line_count":160,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__double_array_declare_partial_init_16.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-16.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 16 Control flow: while(1)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_declare_partial_init_16_bad()\r\n{\r\n double * data;\r\n double dataUninitArray[10];\r\n data = dataUninitArray;\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the sinks in the second while statement */\r\nstatic void goodB2G()\r\n{\r\n double * data;\r\n double dataUninitArray[10];\r\n data = dataUninitArray;\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the sources in the first while statement */\r\nstatic void goodG2B()\r\n{\r\n double * data;\r\n double dataUninitArray[10];\r\n data = dataUninitArray;\r\n while(1)\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_declare_partial_init_16_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_array_declare_partial_init_16_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_array_declare_partial_init_16_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_malloc_no_init_63a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_malloc_no_init_63a.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":3235,"source_sha256":"2df2e24684c5047bf49c1207ff7b1902e068657c53ddc7fd30707c8fd3677e97","line_count":103,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__double_array_malloc_no_init_63a.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-63a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_malloc_no_init_63b_badSink(double * * dataPtr);\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_malloc_no_init_63_bad()\r\n{\r\n double * data;\r\n data = (double *)malloc(10*sizeof(double));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n CWE457_Use_of_Uninitialized_Variable__double_array_malloc_no_init_63b_badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_malloc_no_init_63b_goodG2BSink(double * * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n double * data;\r\n data = (double *)malloc(10*sizeof(double));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n CWE457_Use_of_Uninitialized_Variable__double_array_malloc_no_init_63b_goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_malloc_no_init_63b_goodB2GSink(double * * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n double * data;\r\n data = (double *)malloc(10*sizeof(double));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n CWE457_Use_of_Uninitialized_Variable__double_array_malloc_no_init_63b_goodB2GSink(&data);\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_malloc_no_init_63_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_array_malloc_no_init_63_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_array_malloc_no_init_63_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_01.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_01.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":3344,"source_sha256":"5470337721a37ab101352edd7c1bbb163a82eeab42960265d755e5e16009c837","line_count":136,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_01.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-01.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 01 Baseline\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_01_bad()\r\n{\r\n double * data;\r\n data = (double *)malloc(10*sizeof(double));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n double * data;\r\n data = (double *)malloc(10*sizeof(double));\r\n if (data == NULL) {exit(-1);}\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n double * data;\r\n data = (double *)malloc(10*sizeof(double));\r\n if (data == NULL) {exit(-1);}\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_01_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_01_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_01_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_03.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_03.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":5658,"source_sha256":"a625a728141d046b70af260ac5aef3695243d67d4598b40779d8998ff190ff4d","line_count":233,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_03.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-03.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 03 Control flow: if(5==5) and if(5!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_03_bad()\r\n{\r\n double * data;\r\n data = (double *)malloc(10*sizeof(double));\r\n if (data == NULL) {exit(-1);}\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 5==5 to 5!=5 */\r\nstatic void goodB2G1()\r\n{\r\n double * data;\r\n data = (double *)malloc(10*sizeof(double));\r\n if (data == NULL) {exit(-1);}\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n }\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n double * data;\r\n data = (double *)malloc(10*sizeof(double));\r\n if (data == NULL) {exit(-1);}\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n }\r\n if(5==5)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 5==5 to 5!=5 */\r\nstatic void goodG2B1()\r\n{\r\n double * data;\r\n data = (double *)malloc(10*sizeof(double));\r\n if (data == NULL) {exit(-1);}\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n double * data;\r\n data = (double *)malloc(10*sizeof(double));\r\n if (data == NULL) {exit(-1);}\n if(5==5)\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_03_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_03_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_03_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_13.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_13.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":5914,"source_sha256":"5dda69726b884d522f85cf094a996ff4785a6ab8ea72915efe2de96df9ebe9d3","line_count":233,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_13.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-13.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 13 Control flow: if(GLOBAL_CONST_FIVE==5) and if(GLOBAL_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_13_bad()\r\n{\r\n double * data;\r\n data = (double *)malloc(10*sizeof(double));\r\n if (data == NULL) {exit(-1);}\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_FIVE==5 to GLOBAL_CONST_FIVE!=5 */\r\nstatic void goodB2G1()\r\n{\r\n double * data;\r\n data = (double *)malloc(10*sizeof(double));\r\n if (data == NULL) {exit(-1);}\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n }\r\n if(GLOBAL_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n double * data;\r\n data = (double *)malloc(10*sizeof(double));\r\n if (data == NULL) {exit(-1);}\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_FIVE==5 to GLOBAL_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n double * data;\r\n data = (double *)malloc(10*sizeof(double));\r\n if (data == NULL) {exit(-1);}\n if(GLOBAL_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n double * data;\r\n data = (double *)malloc(10*sizeof(double));\r\n if (data == NULL) {exit(-1);}\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_13_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_13_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_13_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_18.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_18.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":3608,"source_sha256":"16b83312c69b26e8ba915da86dd20df1ce68e49b209cd869b6e4eb1fd1bb15a4","line_count":148,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_18.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-18.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 18 Control flow: goto statements\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_18_bad()\r\n{\r\n double * data;\r\n data = (double *)malloc(10*sizeof(double));\r\n if (data == NULL) {exit(-1);}\n goto source;\r\nsource:\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by reversing the blocks on the second goto statement */\r\nstatic void goodB2G()\r\n{\r\n double * data;\r\n data = (double *)malloc(10*sizeof(double));\r\n if (data == NULL) {exit(-1);}\n goto source;\r\nsource:\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n goto sink;\r\nsink:\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by reversing the blocks on the first goto statement */\r\nstatic void goodG2B()\r\n{\r\n double * data;\r\n data = (double *)malloc(10*sizeof(double));\r\n if (data == NULL) {exit(-1);}\n goto source;\r\nsource:\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_18_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_18_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_array_malloc_partial_init_18_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_pointer_02.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__double_pointer_02.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":4371,"source_sha256":"de1c60fe3dd295bfda1d7b444115a2e9f6b9ea6a54ff2e58aa204b53d7524eaf","line_count":165,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__double_pointer_02.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c.label.xml\r\nTemplate File: sources-sinks-02.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 02 Control flow: if(1) and if(0)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_pointer_02_bad()\r\n{\r\n double * data;\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printDoubleLine(*data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 1 to 0 */\r\nstatic void goodB2G1()\r\n{\r\n double * data;\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n /* initialize both the pointer and the data pointed to */\r\n data = (double *)malloc(sizeof(double));\r\n if (data == NULL) {exit(-1);}\n *data = 5.0;\r\n printDoubleLine(*data);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n double * data;\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(1)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n /* initialize both the pointer and the data pointed to */\r\n data = (double *)malloc(sizeof(double));\r\n if (data == NULL) {exit(-1);}\n *data = 5.0;\r\n printDoubleLine(*data);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 1 to 0 */\r\nstatic void goodG2B1()\r\n{\r\n double * data;\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Initialize data */\r\n /* initialize both the pointer and the data pointed to */\r\n data = (double *)malloc(sizeof(double));\r\n if (data == NULL) {exit(-1);}\n *data = 5.0;\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printDoubleLine(*data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n double * data;\r\n if(1)\r\n {\r\n /* FIX: Initialize data */\r\n /* initialize both the pointer and the data pointed to */\r\n data = (double *)malloc(sizeof(double));\r\n if (data == NULL) {exit(-1);}\n *data = 5.0;\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printDoubleLine(*data);\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__double_pointer_02_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_pointer_02_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__double_pointer_02_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__int64_t_16.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__int64_t_16.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":2776,"source_sha256":"87a06987f4780f85b917dff71b606823e4bb339b5b09c584987057683d5c1c6e","line_count":114,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__int64_t_16.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c.label.xml\r\nTemplate File: sources-sinks-16.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 16 Control flow: while(1)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__int64_t_16_bad()\r\n{\r\n int64_t data;\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printLongLongLine(data);\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the sinks in the second while statement */\r\nstatic void goodB2G()\r\n{\r\n int64_t data;\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n data = 5LL;\r\n printLongLongLine(data);\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the sources in the first while statement */\r\nstatic void goodG2B()\r\n{\r\n int64_t data;\r\n while(1)\r\n {\r\n /* FIX: Initialize data */\r\n data = 5LL;\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printLongLongLine(data);\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__int64_t_16_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__int64_t_16_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__int64_t_16_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__int64_t_63a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__int64_t_63a.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":2667,"source_sha256":"8a38c12627986a60ffe2b685c6cee5056ac844de9b5e42d63f3414b7f3b65767","line_count":93,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__int64_t_63a.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c.label.xml\r\nTemplate File: sources-sinks-63a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE457_Use_of_Uninitialized_Variable__int64_t_63b_badSink(int64_t * dataPtr);\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__int64_t_63_bad()\r\n{\r\n int64_t data;\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n CWE457_Use_of_Uninitialized_Variable__int64_t_63b_badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE457_Use_of_Uninitialized_Variable__int64_t_63b_goodG2BSink(int64_t * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int64_t data;\r\n /* FIX: Initialize data */\r\n data = 5LL;\r\n CWE457_Use_of_Uninitialized_Variable__int64_t_63b_goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE457_Use_of_Uninitialized_Variable__int64_t_63b_goodB2GSink(int64_t * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n int64_t data;\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n CWE457_Use_of_Uninitialized_Variable__int64_t_63b_goodB2GSink(&data);\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__int64_t_63_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__int64_t_63_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__int64_t_63_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__int_11.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__int_11.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":3932,"source_sha256":"8c1894c4e1d37b1b1b943f616ff7551d16a9402e3bc111762d8176c7b39821fb","line_count":153,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__int_11.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c.label.xml\r\nTemplate File: sources-sinks-11.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_11_bad()\r\n{\r\n int data;\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printIntLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n int data;\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n data = 5;\r\n printIntLine(data);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int data;\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n data = 5;\r\n printIntLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n int data;\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Initialize data */\r\n data = 5;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printIntLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int data;\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Initialize data */\r\n data = 5;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printIntLine(data);\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_11_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__int_11_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__int_11_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__int_64a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__int_64a.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":2623,"source_sha256":"885e55331280358b60452eb04e56ddaa06642031712c034cae6d9166065df94f","line_count":93,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__int_64a.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c.label.xml\r\nTemplate File: sources-sinks-64a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_64b_badSink(void * dataVoidPtr);\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_64_bad()\r\n{\r\n int data;\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n CWE457_Use_of_Uninitialized_Variable__int_64b_badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_64b_goodG2BSink(void * dataVoidPtr);\r\n\r\nstatic void goodG2B()\r\n{\r\n int data;\r\n /* FIX: Initialize data */\r\n data = 5;\r\n CWE457_Use_of_Uninitialized_Variable__int_64b_goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_64b_goodB2GSink(void * dataVoidPtr);\r\n\r\nstatic void goodB2G()\r\n{\r\n int data;\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n CWE457_Use_of_Uninitialized_Variable__int_64b_goodB2GSink(&data);\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_64_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__int_64_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__int_64_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__int_array_alloca_partial_init_15.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__int_array_alloca_partial_init_15.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":6517,"source_sha256":"d35df15fcb335519896bde0daea3e2b3fa3a79f73a5bf7f1e6a6986017d7652c","line_count":278,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__int_array_alloca_partial_init_15.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-15.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 15 Control flow: switch(6) and switch(7)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_array_alloca_partial_init_15_bad()\r\n{\r\n int * data;\r\n data = (int *)ALLOCA(10*sizeof(int));\r\n switch(6)\r\n {\r\n case 6:\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second switch to switch(8) */\r\nstatic void goodB2G1()\r\n{\r\n int * data;\r\n data = (int *)ALLOCA(10*sizeof(int));\r\n switch(6)\r\n {\r\n case 6:\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(8)\r\n {\r\n case 7:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n break;\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second switch */\r\nstatic void goodB2G2()\r\n{\r\n int * data;\r\n data = (int *)ALLOCA(10*sizeof(int));\r\n switch(6)\r\n {\r\n case 6:\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first switch to switch(5) */\r\nstatic void goodG2B1()\r\n{\r\n int * data;\r\n data = (int *)ALLOCA(10*sizeof(int));\r\n switch(5)\r\n {\r\n case 6:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first switch */\r\nstatic void goodG2B2()\r\n{\r\n int * data;\r\n data = (int *)ALLOCA(10*sizeof(int));\r\n switch(6)\r\n {\r\n case 6:\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_array_alloca_partial_init_15_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__int_array_alloca_partial_init_15_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__int_array_alloca_partial_init_15_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__int_array_declare_partial_init_02.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__int_array_declare_partial_init_02.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":5394,"source_sha256":"3ce4ab3da6d6ac2af43a069594f29436ae62a4d495c30b6bc6f808012861ab8f","line_count":233,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__int_array_declare_partial_init_02.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-02.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 02 Control flow: if(1) and if(0)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_array_declare_partial_init_02_bad()\r\n{\r\n int * data;\r\n int dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 1 to 0 */\r\nstatic void goodB2G1()\r\n{\r\n int * data;\r\n int dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int * data;\r\n int dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(1)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 1 to 0 */\r\nstatic void goodG2B1()\r\n{\r\n int * data;\r\n int dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int * data;\r\n int dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(1)\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_array_declare_partial_init_02_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__int_array_declare_partial_init_02_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__int_array_declare_partial_init_02_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__int_array_declare_partial_init_64a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__int_array_declare_partial_init_64a.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":3327,"source_sha256":"8a00e9aefee0754879c2e77de9bdd2402fa05418d250d2093ec1962318ac0d7f","line_count":115,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__int_array_declare_partial_init_64a.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-64a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_array_declare_partial_init_64b_badSink(void * dataVoidPtr);\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_array_declare_partial_init_64_bad()\r\n{\r\n int * data;\r\n int dataUninitArray[10];\r\n data = dataUninitArray;\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n CWE457_Use_of_Uninitialized_Variable__int_array_declare_partial_init_64b_badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_array_declare_partial_init_64b_goodG2BSink(void * dataVoidPtr);\r\n\r\nstatic void goodG2B()\r\n{\r\n int * data;\r\n int dataUninitArray[10];\r\n data = dataUninitArray;\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n CWE457_Use_of_Uninitialized_Variable__int_array_declare_partial_init_64b_goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_array_declare_partial_init_64b_goodB2GSink(void * dataVoidPtr);\r\n\r\nstatic void goodB2G()\r\n{\r\n int * data;\r\n int dataUninitArray[10];\r\n data = dataUninitArray;\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n CWE457_Use_of_Uninitialized_Variable__int_array_declare_partial_init_64b_goodB2GSink(&data);\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_array_declare_partial_init_64_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__int_array_declare_partial_init_64_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__int_array_declare_partial_init_64_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__int_array_malloc_no_init_15.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__int_array_malloc_no_init_15.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":6493,"source_sha256":"0652adc0abd97f826ec9282f5b40aa15339c3a20ca0c6f5396bb146679dc248a","line_count":265,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__int_array_malloc_no_init_15.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-15.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 15 Control flow: switch(6) and switch(7)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_array_malloc_no_init_15_bad()\r\n{\r\n int * data;\r\n data = (int *)malloc(10*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n switch(6)\r\n {\r\n case 6:\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second switch to switch(8) */\r\nstatic void goodB2G1()\r\n{\r\n int * data;\r\n data = (int *)malloc(10*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n switch(6)\r\n {\r\n case 6:\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(8)\r\n {\r\n case 7:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n break;\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second switch */\r\nstatic void goodB2G2()\r\n{\r\n int * data;\r\n data = (int *)malloc(10*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n switch(6)\r\n {\r\n case 6:\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first switch to switch(5) */\r\nstatic void goodG2B1()\r\n{\r\n int * data;\r\n data = (int *)malloc(10*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n switch(5)\r\n {\r\n case 6:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first switch */\r\nstatic void goodG2B2()\r\n{\r\n int * data;\r\n data = (int *)malloc(10*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n switch(6)\r\n {\r\n case 6:\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_array_malloc_no_init_15_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__int_array_malloc_no_init_15_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__int_array_malloc_no_init_15_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__int_array_malloc_no_init_62a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__int_array_malloc_no_init_62a.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":3015,"source_sha256":"964df3fcbc0944536a6daf2003c9e50c7a3b5cd8b15d9709d271a3ab10fadc7e","line_count":129,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__int_array_malloc_no_init_62a.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-62a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__int_array_malloc_no_init_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSource(int * &data);\r\n\r\nvoid bad()\r\n{\r\n int * data;\r\n data = (int *)malloc(10*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n badSource(data);\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(int * &data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int * data;\r\n data = (int *)malloc(10*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n goodG2BSource(data);\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(int * &data);\r\n\r\nstatic void goodB2G()\r\n{\r\n int * data;\r\n data = (int *)malloc(10*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n goodB2GSource(data);\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__int_array_malloc_no_init_62; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__int_array_malloc_partial_init_03.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__int_array_malloc_partial_init_03.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":5527,"source_sha256":"637f4e221042f4f4d92999c8120d4c305cb267b729e90b9d2cb527abaa14ee12","line_count":233,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__int_array_malloc_partial_init_03.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-03.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 03 Control flow: if(5==5) and if(5!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_array_malloc_partial_init_03_bad()\r\n{\r\n int * data;\r\n data = (int *)malloc(10*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 5==5 to 5!=5 */\r\nstatic void goodB2G1()\r\n{\r\n int * data;\r\n data = (int *)malloc(10*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int * data;\r\n data = (int *)malloc(10*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(5==5)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 5==5 to 5!=5 */\r\nstatic void goodG2B1()\r\n{\r\n int * data;\r\n data = (int *)malloc(10*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int * data;\r\n data = (int *)malloc(10*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n if(5==5)\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_array_malloc_partial_init_03_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__int_array_malloc_partial_init_03_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__int_array_malloc_partial_init_03_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__int_array_malloc_partial_init_08.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__int_array_malloc_partial_init_08.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":6050,"source_sha256":"cce86aec7482e88baf85f1c707e6cd2981ae9eb91f5c3a282afa701ad9aa9a76","line_count":246,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__int_array_malloc_partial_init_08.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-08.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 08 Control flow: if(staticReturnsTrue()) and if(staticReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The two function below always return the same value, so a tool\r\n should be able to identify that calls to the functions will always\r\n return a fixed value. */\r\nstatic int staticReturnsTrue()\r\n{\r\n return 1;\r\n}\r\n\r\nstatic int staticReturnsFalse()\r\n{\r\n return 0;\r\n}\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_array_malloc_partial_init_08_bad()\r\n{\r\n int * data;\r\n data = (int *)malloc(10*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n int * data;\r\n data = (int *)malloc(10*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int * data;\r\n data = (int *)malloc(10*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n int * data;\r\n data = (int *)malloc(10*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int * data;\r\n data = (int *)malloc(10*sizeof(int));\r\n if (data == NULL) {exit(-1);}\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_array_malloc_partial_init_08_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__int_array_malloc_partial_init_08_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__int_array_malloc_partial_init_08_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__int_pointer_63a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__int_pointer_63a.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":2835,"source_sha256":"19fc7b0111f385687b1021e5d85e5e220b750c4845e74dffacb0dc374486b928","line_count":96,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__int_pointer_63a.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c.label.xml\r\nTemplate File: sources-sinks-63a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_pointer_63b_badSink(int * * dataPtr);\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_pointer_63_bad()\r\n{\r\n int * data;\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n CWE457_Use_of_Uninitialized_Variable__int_pointer_63b_badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_pointer_63b_goodG2BSink(int * * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int * data;\r\n /* FIX: Initialize data */\r\n /* initialize both the pointer and the data pointed to */\r\n data = (int *)malloc(sizeof(int));\r\n if (data == NULL) {exit(-1);}\n *data = 5;\r\n CWE457_Use_of_Uninitialized_Variable__int_pointer_63b_goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_pointer_63b_goodB2GSink(int * * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n int * data;\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n CWE457_Use_of_Uninitialized_Variable__int_pointer_63b_goodB2GSink(&data);\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__int_pointer_63_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__int_pointer_63_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__int_pointer_63_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__long_14.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__long_14.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":3850,"source_sha256":"144ab4dfb56a82dbe628eff069082abd363fa0112daf326bd97c92cb7b63cdaa","line_count":153,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__long_14.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c.label.xml\r\nTemplate File: sources-sinks-14.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 14 Control flow: if(globalFive==5) and if(globalFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__long_14_bad()\r\n{\r\n long data;\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printLongLine(data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalFive==5 to globalFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n long data;\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n data = 5L;\r\n printLongLine(data);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n long data;\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(globalFive==5)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n data = 5L;\r\n printLongLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalFive==5 to globalFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n long data;\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Initialize data */\r\n data = 5L;\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printLongLine(data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n long data;\r\n if(globalFive==5)\r\n {\r\n /* FIX: Initialize data */\r\n data = 5L;\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printLongLine(data);\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__long_14_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__long_14_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__long_14_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__long_62a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__long_62a.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":2370,"source_sha256":"38d5aebfb54af30ee17580cc952f9722d8c3be610b370f20d1791e62b39c2982","line_count":101,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__long_62a.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c.label.xml\r\nTemplate File: sources-sinks-62a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__long_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSource(long &data);\r\n\r\nvoid bad()\r\n{\r\n long data;\r\n badSource(data);\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printLongLine(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(long &data);\r\n\r\nstatic void goodG2B()\r\n{\r\n long data;\r\n goodG2BSource(data);\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printLongLine(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(long &data);\r\n\r\nstatic void goodB2G()\r\n{\r\n long data;\r\n goodB2GSource(data);\r\n /* FIX: Ensure data is initialized before use */\r\n data = 5L;\r\n printLongLine(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__long_62; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_double_array_no_init_08.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_double_array_no_init_08.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":5864,"source_sha256":"6b047f6324fc99ac18bcfcf91463422d486ce4d5b820e8d6823423df8dba0983","line_count":240,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__new_double_array_no_init_08.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__new.label.xml\r\nTemplate File: sources-sinks-08.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 08 Control flow: if(staticReturnsTrue()) and if(staticReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The two function below always return the same value, so a tool\r\n should be able to identify that calls to the functions will always\r\n return a fixed value. */\r\nstatic int staticReturnsTrue()\r\n{\r\n return 1;\r\n}\r\n\r\nstatic int staticReturnsFalse()\r\n{\r\n return 0;\r\n}\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__new_double_array_no_init_08\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n double * data;\r\n data = new double[10];\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n double * data;\r\n data = new double[10];\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n double * data;\r\n data = new double[10];\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n double * data;\r\n data = new double[10];\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n double * data;\r\n data = new double[10];\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__new_double_array_no_init_08; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_double_array_no_init_64a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_double_array_no_init_64a.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":2623,"source_sha256":"e91e2e1feb22c1835c72b2f4fd1bea4955fa87c60229e9f50f4d290c95cf4781","line_count":107,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__new_double_array_no_init_64a.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__new.label.xml\r\nTemplate File: sources-sinks-64a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__new_double_array_no_init_64\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(void * dataVoidPtr);\r\n\r\nvoid bad()\r\n{\r\n double * data;\r\n data = new double[10];\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(void * dataVoidPtr);\r\n\r\nstatic void goodG2B()\r\n{\r\n double * data;\r\n data = new double[10];\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(void * dataVoidPtr);\r\n\r\nstatic void goodB2G()\r\n{\r\n double * data;\r\n data = new double[10];\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n goodB2GSink(&data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__new_double_array_no_init_64; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_double_array_partial_init_18.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_double_array_partial_init_18.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":3553,"source_sha256":"0cf76fc17d237887cb8314134061f658326c2134df099ce1b5680150f010cb09","line_count":157,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__new_double_array_partial_init_18.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__new.label.xml\r\nTemplate File: sources-sinks-18.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 18 Control flow: goto statements\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__new_double_array_partial_init_18\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n double * data;\r\n data = new double[10];\r\n goto source;\r\nsource:\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by reversing the blocks on the second goto statement */\r\nstatic void goodB2G()\r\n{\r\n double * data;\r\n data = new double[10];\r\n goto source;\r\nsource:\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n goto sink;\r\nsink:\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by reversing the blocks on the first goto statement */\r\nstatic void goodG2B()\r\n{\r\n double * data;\r\n data = new double[10];\r\n goto source;\r\nsource:\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = (double)i;\r\n }\r\n }\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printDoubleLine(data[i]);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__new_double_array_partial_init_18; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_int_array_no_init_01.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_int_array_no_init_01.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":3073,"source_sha256":"b43a3ec5114cb9c60cfdf3112eae274d3cd70502206fc30d4935b9c5f5fdd881","line_count":134,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__new_int_array_no_init_01.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__new.label.xml\r\nTemplate File: sources-sinks-01.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 01 Baseline\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__new_int_array_no_init_01\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int * data;\r\n data = new int[10];\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int * data;\r\n data = new int[10];\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int * data;\r\n data = new int[10];\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__new_int_array_no_init_01; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_int_array_no_init_16.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_int_array_no_init_16.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":3636,"source_sha256":"b18bc6082d8246ffb2c517f8dbe4ee0d263c08ab3db62deea62a0dc4cf059f13","line_count":157,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__new_int_array_no_init_16.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__new.label.xml\r\nTemplate File: sources-sinks-16.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 16 Control flow: while(1)\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__new_int_array_no_init_16\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int * data;\r\n data = new int[10];\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the sinks in the second while statement */\r\nstatic void goodB2G()\r\n{\r\n int * data;\r\n data = new int[10];\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the sources in the first while statement */\r\nstatic void goodG2B()\r\n{\r\n int * data;\r\n data = new int[10];\r\n while(1)\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n break;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__new_int_array_no_init_16; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_int_array_partial_init_04.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_int_array_partial_init_04.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":6019,"source_sha256":"00850a1caafbeca6176d7ae697b42b1484ec72fee40c913346090e79f88b4c47","line_count":251,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__new_int_array_partial_init_04.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__new.label.xml\r\nTemplate File: sources-sinks-04.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 04 Control flow: if(STATIC_CONST_TRUE) and if(STATIC_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The two variables below are declared \"const\", so a tool should\r\n be able to identify that reads of these will always return their\r\n initialized values. */\r\nstatic const int STATIC_CONST_TRUE = 1; /* true */\r\nstatic const int STATIC_CONST_FALSE = 0; /* false */\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__new_int_array_partial_init_04\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int * data;\r\n data = new int[10];\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second STATIC_CONST_TRUE to STATIC_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n int * data;\r\n data = new int[10];\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(STATIC_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int * data;\r\n data = new int[10];\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first STATIC_CONST_TRUE to STATIC_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n int * data;\r\n data = new int[10];\r\n if(STATIC_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int * data;\r\n data = new int[10];\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__new_int_array_partial_init_04; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_int_array_partial_init_07.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_int_array_partial_init_07.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":5902,"source_sha256":"3f4e43fecf0a4718eb5a1c096996a171029bdc84d1fb8a0af15939c353ee7a48","line_count":250,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__new_int_array_partial_init_07.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__new.label.xml\r\nTemplate File: sources-sinks-07.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 07 Control flow: if(staticFive==5) and if(staticFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The variable below is not declared \"const\", but is never assigned\r\n any other value so a tool should be able to identify that reads of\r\n this will always give its initialized value. */\r\nstatic int staticFive = 5;\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__new_int_array_partial_init_07\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int * data;\r\n data = new int[10];\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticFive==5 to staticFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n int * data;\r\n data = new int[10];\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int * data;\r\n data = new int[10];\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(staticFive==5)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticFive==5 to staticFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n int * data;\r\n data = new int[10];\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int * data;\r\n data = new int[10];\r\n if(staticFive==5)\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i] = i;\r\n }\r\n }\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i]);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__new_int_array_partial_init_07; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_struct_array_no_init_05.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_struct_array_no_init_05.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":6202,"source_sha256":"48b34f0351032af550392d0541c69ca285f12fc699c68eec400c878f9ddff510","line_count":242,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__new_struct_array_no_init_05.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__new.label.xml\r\nTemplate File: sources-sinks-05.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 05 Control flow: if(staticTrue) and if(staticFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The two variables below are not defined as \"const\", but are never\r\n assigned any other value, so a tool should be able to identify that\r\n reads of these will always return their initialized values. */\r\nstatic int staticTrue = 1; /* true */\r\nstatic int staticFalse = 0; /* false */\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__new_struct_array_no_init_05\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticTrue to staticFalse */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(staticFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(staticTrue)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticTrue to staticFalse */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n if(staticFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n if(staticTrue)\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__new_struct_array_no_init_05; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_struct_array_no_init_16.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_struct_array_no_init_16.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":3955,"source_sha256":"da28ec644f6c706d1fdc493f52c5c2a182537dd4bb8b389d36b28fa338c80b18","line_count":162,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__new_struct_array_no_init_16.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__new.label.xml\r\nTemplate File: sources-sinks-16.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 16 Control flow: while(1)\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__new_struct_array_no_init_16\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the sinks in the second while statement */\r\nstatic void goodB2G()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the sources in the first while statement */\r\nstatic void goodG2B()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n while(1)\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n break;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__new_struct_array_no_init_16; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_struct_array_no_init_18.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_struct_array_no_init_18.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":3632,"source_sha256":"c27ab14007a35b2956f05184b6434789465422412509bc45d28cb6bf29090823","line_count":150,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__new_struct_array_no_init_18.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__new.label.xml\r\nTemplate File: sources-sinks-18.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 18 Control flow: goto statements\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__new_struct_array_no_init_18\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n goto source;\r\nsource:\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by reversing the blocks on the second goto statement */\r\nstatic void goodB2G()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n goto source;\r\nsource:\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n goto sink;\r\nsink:\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by reversing the blocks on the first goto statement */\r\nstatic void goodG2B()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n goto source;\r\nsource:\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__new_struct_array_no_init_18; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_struct_array_no_init_64b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_struct_array_no_init_64b.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":2676,"source_sha256":"79da8489fb01b3a3e1d94b168ec51198d40c91e590cf1210e658a6799beccb15","line_count":98,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__new_struct_array_no_init_64b.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__new.label.xml\r\nTemplate File: sources-sinks-64b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__new_struct_array_no_init_64\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n twoIntsStruct * * dataPtr = (twoIntsStruct * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n twoIntsStruct * data = (*dataPtr);\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n twoIntsStruct * * dataPtr = (twoIntsStruct * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n twoIntsStruct * data = (*dataPtr);\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n twoIntsStruct * * dataPtr = (twoIntsStruct * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n twoIntsStruct * data = (*dataPtr);\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_struct_array_partial_init_03.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_struct_array_partial_init_03.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":6222,"source_sha256":"e8c833770f5656d7de9aaaf93efd4294bcd7ae8106b86e8902ff3819158884c7","line_count":257,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__new_struct_array_partial_init_03.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__new.label.xml\r\nTemplate File: sources-sinks-03.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 03 Control flow: if(5==5) and if(5!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__new_struct_array_partial_init_03\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 5==5 to 5!=5 */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(5==5)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 5==5 to 5!=5 */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n if(5==5)\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__new_struct_array_partial_init_03; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_struct_array_partial_init_13.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_struct_array_partial_init_13.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":6474,"source_sha256":"f7cf0f04b484c17e1f35480dd50cb498409656529f598a8aeddbb6909f5b30ac","line_count":256,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__new_struct_array_partial_init_13.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__new.label.xml\r\nTemplate File: sources-sinks-13.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 13 Control flow: if(GLOBAL_CONST_FIVE==5) and if(GLOBAL_CONST_FIVE!=5)\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__new_struct_array_partial_init_13\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_FIVE==5 to GLOBAL_CONST_FIVE!=5 */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(GLOBAL_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_FIVE==5 to GLOBAL_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n if(GLOBAL_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__new_struct_array_partial_init_13; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_struct_array_partial_init_15.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__new_struct_array_partial_init_15.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":7290,"source_sha256":"e69f4beac5010ce33ddf107bcd52390d9758abc0a1d90104fd6963de75293f4c","line_count":306,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__new_struct_array_partial_init_15.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__new.label.xml\r\nTemplate File: sources-sinks-15.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 15 Control flow: switch(6) and switch(7)\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__new_struct_array_partial_init_15\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n switch(6)\r\n {\r\n case 6:\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second switch to switch(8) */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n switch(6)\r\n {\r\n case 6:\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(8)\r\n {\r\n case 7:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n break;\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second switch */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n switch(6)\r\n {\r\n case 6:\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first switch to switch(5) */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n switch(5)\r\n {\r\n case 6:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first switch */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct * data;\r\n data = new twoIntsStruct[10];\r\n switch(6)\r\n {\r\n case 6:\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n /* deallocate the memory */\r\n delete [] data;\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__new_struct_array_partial_init_15; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_11.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_11.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":4344,"source_sha256":"b44add2d7bb030209fb885b95f1e3f793c28d303e6e436d992be8b3c20d915a7","line_count":162,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__struct_11.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c.label.xml\r\nTemplate File: sources-sinks-11.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_11_bad()\r\n{\r\n twoIntsStruct data;\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct data;\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n data.intOne = 1;\r\n data.intTwo = 2;\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct data;\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n data.intOne = 1;\r\n data.intTwo = 2;\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct data;\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Initialize data */\r\n data.intOne = 1;\r\n data.intTwo = 2;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct data;\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Initialize data */\r\n data.intOne = 1;\r\n data.intTwo = 2;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_11_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__struct_11_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__struct_11_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_15.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_15.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":5635,"source_sha256":"b0274c8cc500865469220da6855f7fb5949c645be58722c6cf44eedac5faca7b","line_count":212,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__struct_15.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c.label.xml\r\nTemplate File: sources-sinks-15.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 15 Control flow: switch(6) and switch(7)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_15_bad()\r\n{\r\n twoIntsStruct data;\r\n switch(6)\r\n {\r\n case 6:\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second switch to switch(8) */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct data;\r\n switch(6)\r\n {\r\n case 6:\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(8)\r\n {\r\n case 7:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Ensure data is initialized before use */\r\n data.intOne = 1;\r\n data.intTwo = 2;\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n break;\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second switch */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct data;\r\n switch(6)\r\n {\r\n case 6:\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* FIX: Ensure data is initialized before use */\r\n data.intOne = 1;\r\n data.intTwo = 2;\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first switch to switch(5) */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct data;\r\n switch(5)\r\n {\r\n case 6:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Initialize data */\r\n data.intOne = 1;\r\n data.intTwo = 2;\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first switch */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct data;\r\n switch(6)\r\n {\r\n case 6:\r\n /* FIX: Initialize data */\r\n data.intOne = 1;\r\n data.intTwo = 2;\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_15_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__struct_15_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__struct_15_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_64a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_64a.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":2715,"source_sha256":"0841d92d174460c8b1c93d1127b6cf4da95d7e289a29d6bf35fc61b98810c911","line_count":94,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__struct_64a.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c.label.xml\r\nTemplate File: sources-sinks-64a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_64b_badSink(void * dataVoidPtr);\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_64_bad()\r\n{\r\n twoIntsStruct data;\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n CWE457_Use_of_Uninitialized_Variable__struct_64b_badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_64b_goodG2BSink(void * dataVoidPtr);\r\n\r\nstatic void goodG2B()\r\n{\r\n twoIntsStruct data;\r\n /* FIX: Initialize data */\r\n data.intOne = 1;\r\n data.intTwo = 2;\r\n CWE457_Use_of_Uninitialized_Variable__struct_64b_goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_64b_goodB2GSink(void * dataVoidPtr);\r\n\r\nstatic void goodB2G()\r\n{\r\n twoIntsStruct data;\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n CWE457_Use_of_Uninitialized_Variable__struct_64b_goodB2GSink(&data);\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_64_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__struct_64_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__struct_64_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_64b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_64b.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":2119,"source_sha256":"73e3150b4eb2018271cbf5ec3a0164b840e9058b796f21448ac932d4b8146298","line_count":65,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__struct_64b.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c.label.xml\r\nTemplate File: sources-sinks-64b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_64b_badSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n twoIntsStruct * dataPtr = (twoIntsStruct *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n twoIntsStruct data = (*dataPtr);\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_64b_goodG2BSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n twoIntsStruct * dataPtr = (twoIntsStruct *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n twoIntsStruct data = (*dataPtr);\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_64b_goodB2GSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n twoIntsStruct * dataPtr = (twoIntsStruct *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n twoIntsStruct data = (*dataPtr);\r\n /* FIX: Ensure data is initialized before use */\r\n data.intOne = 1;\r\n data.intTwo = 2;\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_array_alloca_no_init_04.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_array_alloca_no_init_04.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":6162,"source_sha256":"daf0f7ea56cb3287b1b7f98c6f4d44a966975d34f3126652f8f1aff858383732","line_count":225,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__struct_array_alloca_no_init_04.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-04.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 04 Control flow: if(STATIC_CONST_TRUE) and if(STATIC_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The two variables below are declared \"const\", so a tool should\r\n be able to identify that reads of these will always return their\r\n initialized values. */\r\nstatic const int STATIC_CONST_TRUE = 1; /* true */\r\nstatic const int STATIC_CONST_FALSE = 0; /* false */\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_array_alloca_no_init_04_bad()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)ALLOCA(10*sizeof(twoIntsStruct));\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second STATIC_CONST_TRUE to STATIC_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)ALLOCA(10*sizeof(twoIntsStruct));\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(STATIC_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)ALLOCA(10*sizeof(twoIntsStruct));\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first STATIC_CONST_TRUE to STATIC_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)ALLOCA(10*sizeof(twoIntsStruct));\r\n if(STATIC_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)ALLOCA(10*sizeof(twoIntsStruct));\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_array_alloca_no_init_04_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__struct_array_alloca_no_init_04_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__struct_array_alloca_no_init_04_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_array_alloca_no_init_10.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_array_alloca_no_init_10.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":5779,"source_sha256":"099a2374d36d7d6e09d77dd0ab1fb121b72d48c8dbe4a19449c9d8aa8d986557","line_count":219,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__struct_array_alloca_no_init_10.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-10.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 10 Control flow: if(globalTrue) and if(globalFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_array_alloca_no_init_10_bad()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)ALLOCA(10*sizeof(twoIntsStruct));\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalTrue to globalFalse */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)ALLOCA(10*sizeof(twoIntsStruct));\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)ALLOCA(10*sizeof(twoIntsStruct));\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(globalTrue)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalTrue to globalFalse */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)ALLOCA(10*sizeof(twoIntsStruct));\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)ALLOCA(10*sizeof(twoIntsStruct));\r\n if(globalTrue)\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_array_alloca_no_init_10_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__struct_array_alloca_no_init_10_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__struct_array_alloca_no_init_10_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_array_alloca_partial_init_63b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_array_alloca_partial_init_63b.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":2156,"source_sha256":"45aeae087cb2a06d685f7acbdf4d2333cd64c43fd7b3d62ee08b10402ad65bc0","line_count":78,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__struct_array_alloca_partial_init_63b.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-63b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_array_alloca_partial_init_63b_badSink(twoIntsStruct * * dataPtr)\r\n{\r\n twoIntsStruct * data = *dataPtr;\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_array_alloca_partial_init_63b_goodG2BSink(twoIntsStruct * * dataPtr)\r\n{\r\n twoIntsStruct * data = *dataPtr;\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_array_alloca_partial_init_63b_goodB2GSink(twoIntsStruct * * dataPtr)\r\n{\r\n twoIntsStruct * data = *dataPtr;\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_array_declare_no_init_02.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_array_declare_no_init_02.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":5665,"source_sha256":"b5b21e92c5c198a28af16d34603b06dc323635c46429f232737813d8dc42d8ea","line_count":224,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__struct_array_declare_no_init_02.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-02.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 02 Control flow: if(1) and if(0)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_array_declare_no_init_02_bad()\r\n{\r\n twoIntsStruct * data;\r\n twoIntsStruct dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 1 to 0 */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n twoIntsStruct dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n twoIntsStruct dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(1)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 1 to 0 */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct * data;\r\n twoIntsStruct dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct * data;\r\n twoIntsStruct dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(1)\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_array_declare_no_init_02_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__struct_array_declare_no_init_02_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__struct_array_declare_no_init_02_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_array_declare_partial_init_09.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_array_declare_partial_init_09.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":6348,"source_sha256":"450e059cf0ceb87ca4b06cab50331bcae60d9f04bd7c5babea6fcec15a47f3c4","line_count":245,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__struct_array_declare_partial_init_09.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-09.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_array_declare_partial_init_09_bad()\r\n{\r\n twoIntsStruct * data;\r\n twoIntsStruct dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n twoIntsStruct dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n twoIntsStruct dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct * data;\r\n twoIntsStruct dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct * data;\r\n twoIntsStruct dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_array_declare_partial_init_09_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__struct_array_declare_partial_init_09_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__struct_array_declare_partial_init_09_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_array_declare_partial_init_18.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_array_declare_partial_init_18.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":3861,"source_sha256":"be1e253a03e3f90ab5a86a909f8b74f4eaaf42b4648e2313508fdba23b0987cc","line_count":155,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__struct_array_declare_partial_init_18.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-18.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 18 Control flow: goto statements\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_array_declare_partial_init_18_bad()\r\n{\r\n twoIntsStruct * data;\r\n twoIntsStruct dataUninitArray[10];\r\n data = dataUninitArray;\r\n goto source;\r\nsource:\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by reversing the blocks on the second goto statement */\r\nstatic void goodB2G()\r\n{\r\n twoIntsStruct * data;\r\n twoIntsStruct dataUninitArray[10];\r\n data = dataUninitArray;\r\n goto source;\r\nsource:\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n goto sink;\r\nsink:\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by reversing the blocks on the first goto statement */\r\nstatic void goodG2B()\r\n{\r\n twoIntsStruct * data;\r\n twoIntsStruct dataUninitArray[10];\r\n data = dataUninitArray;\r\n goto source;\r\nsource:\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_array_declare_partial_init_18_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__struct_array_declare_partial_init_18_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__struct_array_declare_partial_init_18_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_no_init_62b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_no_init_62b.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":1495,"source_sha256":"015eb74f7da1f5b8bb645946fd842c17770874e049c76c40307667a30ddeb9ff","line_count":58,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_no_init_62b.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-62b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_no_init_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSource(twoIntsStruct * &data)\r\n{\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(twoIntsStruct * &data)\r\n{\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(twoIntsStruct * &data)\r\n{\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_no_init_63b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_no_init_63b.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":2111,"source_sha256":"889c7e606212cd378150b7e8580f2362978f8e7e9ff1cc7a44a8e603af86fb8f","line_count":78,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_no_init_63b.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-63b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_no_init_63b_badSink(twoIntsStruct * * dataPtr)\r\n{\r\n twoIntsStruct * data = *dataPtr;\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_no_init_63b_goodG2BSink(twoIntsStruct * * dataPtr)\r\n{\r\n twoIntsStruct * data = *dataPtr;\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_no_init_63b_goodB2GSink(twoIntsStruct * * dataPtr)\r\n{\r\n twoIntsStruct * data = *dataPtr;\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_04.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_04.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":6754,"source_sha256":"2c70614ef9d847835891bdff8904cbea0ecd62428ca924375dd117d32ee913d4","line_count":251,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_04.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-04.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 04 Control flow: if(STATIC_CONST_TRUE) and if(STATIC_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The two variables below are declared \"const\", so a tool should\r\n be able to identify that reads of these will always return their\r\n initialized values. */\r\nstatic const int STATIC_CONST_TRUE = 1; /* true */\r\nstatic const int STATIC_CONST_FALSE = 0; /* false */\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_04_bad()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)malloc(10*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second STATIC_CONST_TRUE to STATIC_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)malloc(10*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(STATIC_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)malloc(10*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first STATIC_CONST_TRUE to STATIC_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)malloc(10*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n if(STATIC_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)malloc(10*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n if(STATIC_CONST_TRUE)\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_04_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_04_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_04_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_05.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_05.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":6662,"source_sha256":"fa8ef720c65aa6393c2aba03aa2421cdb48e0c325772d7e0a11cb3dc6469ab24","line_count":251,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_05.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-05.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 05 Control flow: if(staticTrue) and if(staticFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The two variables below are not defined as \"const\", but are never\r\n assigned any other value, so a tool should be able to identify that\r\n reads of these will always return their initialized values. */\r\nstatic int staticTrue = 1; /* true */\r\nstatic int staticFalse = 0; /* false */\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_05_bad()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)malloc(10*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticTrue to staticFalse */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)malloc(10*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(staticFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)malloc(10*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(staticTrue)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticTrue to staticFalse */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)malloc(10*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n if(staticFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)malloc(10*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n if(staticTrue)\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_05_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_05_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_05_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_11.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_11.c","cwe_id":"CWE457","file_extension":".c","language_hint":"C","source_size_bytes":6515,"source_sha256":"4389218752d6a27a7cdde89b0daeffbd9ba4f06e57b900845c50e30ac58698d9","line_count":245,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_11.c\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-11.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_11_bad()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)malloc(10*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)malloc(10*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)malloc(10*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n {\r\n int i;\r\n for(i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)malloc(10*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)malloc(10*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Completely initialize data */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_11_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_11_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_11_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_62a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_62a.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":3374,"source_sha256":"ba4ed982e12e50d7a8e9b82c493f7fd39c471a1b4ffaacb3b90d64a6f3e17c95","line_count":133,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_62a.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable.c_array.label.xml\r\nTemplate File: sources-sinks-62a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 62 Data flow: data flows using a C++ reference from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_62\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSource(twoIntsStruct * &data);\r\n\r\nvoid bad()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)malloc(10*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n badSource(data);\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSource(twoIntsStruct * &data);\r\n\r\nstatic void goodG2B()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)malloc(10*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n goodG2BSource(data);\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSource(twoIntsStruct * &data);\r\n\r\nstatic void goodB2G()\r\n{\r\n twoIntsStruct * data;\r\n data = (twoIntsStruct *)malloc(10*sizeof(twoIntsStruct));\r\n if (data == NULL) {exit(-1);}\n goodB2GSource(data);\r\n /* FIX: Ensure data is initialized before use */\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n {\r\n int i;\r\n for(i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__struct_array_malloc_partial_init_62; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_05.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_05.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":4499,"source_sha256":"c78be5e877b75c664f8d789c8727d9ffe9e610632ec2d28fbea9ad1f00750ecd","line_count":173,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__twointsclass_05.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__twointsclass.label.xml\r\nTemplate File: sources-sinks-05.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 05 Control flow: if(staticTrue) and if(staticFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The two variables below are not defined as \"const\", but are never\r\n assigned any other value, so a tool should be able to identify that\r\n reads of these will always return their initialized values. */\r\nstatic int staticTrue = 1; /* true */\r\nstatic int staticFalse = 0; /* false */\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__twointsclass_05\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass data;\r\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticTrue to staticFalse */\r\nstatic void goodB2G1()\r\n{\r\n TwoIntsClass data;\r\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(staticFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n data.intOne = 1;\r\n data.intTwo = 2;\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n TwoIntsClass data;\r\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(staticTrue)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n data.intOne = 1;\r\n data.intTwo = 2;\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticTrue to staticFalse */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass data;\r\n if(staticFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Initialize data */\r\n data.intOne = 1;\r\n data.intTwo = 2;\r\n }\r\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass data;\r\n if(staticTrue)\r\n {\r\n /* FIX: Initialize data */\r\n data.intOne = 1;\r\n data.intTwo = 2;\r\n }\r\n if(staticTrue)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__twointsclass_05; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_06.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_06.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":4557,"source_sha256":"c669156e4d0b3e7068d18aa2efd8814df6fd8892a4b4a6af81235f4d6a08ad10","line_count":172,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__twointsclass_06.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__twointsclass.label.xml\r\nTemplate File: sources-sinks-06.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 06 Control flow: if(STATIC_CONST_FIVE==5) and if(STATIC_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The variable below is declared \"const\", so a tool should be able\r\n to identify that reads of this will always give its initialized\r\n value. */\r\nstatic const int STATIC_CONST_FIVE = 5;\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__twointsclass_06\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass data;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodB2G1()\r\n{\r\n TwoIntsClass data;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n data.intOne = 1;\r\n data.intTwo = 2;\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n TwoIntsClass data;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n data.intOne = 1;\r\n data.intTwo = 2;\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass data;\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Initialize data */\r\n data.intOne = 1;\r\n data.intTwo = 2;\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass data;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: Initialize data */\r\n data.intOne = 1;\r\n data.intTwo = 2;\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__twointsclass_06; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_07.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_07.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":4474,"source_sha256":"f00cac2d8ce78bba93a6bb406d03ee5f07c27f25a5c2e3ceeab8affa47a24a0d","line_count":172,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__twointsclass_07.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__twointsclass.label.xml\r\nTemplate File: sources-sinks-07.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 07 Control flow: if(staticFive==5) and if(staticFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The variable below is not declared \"const\", but is never assigned\r\n any other value so a tool should be able to identify that reads of\r\n this will always give its initialized value. */\r\nstatic int staticFive = 5;\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__twointsclass_07\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass data;\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticFive==5 to staticFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n TwoIntsClass data;\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n data.intOne = 1;\r\n data.intTwo = 2;\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n TwoIntsClass data;\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(staticFive==5)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n data.intOne = 1;\r\n data.intTwo = 2;\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticFive==5 to staticFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass data;\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Initialize data */\r\n data.intOne = 1;\r\n data.intTwo = 2;\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass data;\r\n if(staticFive==5)\r\n {\r\n /* FIX: Initialize data */\r\n data.intOne = 1;\r\n data.intTwo = 2;\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n printIntLine(data.intOne);\r\n printIntLine(data.intTwo);\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__twointsclass_07; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_array_alloca_no_init_08.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_array_alloca_no_init_08.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":5758,"source_sha256":"74851f9ba2d2693d13e5458810844408eeaae7b9c870af9f6d381678ad7f8ed9","line_count":212,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__twointsclass_array_alloca_no_init_08.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__twointsclass_array.label.xml\r\nTemplate File: sources-sinks-08.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 08 Control flow: if(staticReturnsTrue()) and if(staticReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The two function below always return the same value, so a tool\r\n should be able to identify that calls to the functions will always\r\n return a fixed value. */\r\nstatic int staticReturnsTrue()\r\n{\r\n return 1;\r\n}\r\n\r\nstatic int staticReturnsFalse()\r\n{\r\n return 0;\r\n}\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__twointsclass_array_alloca_no_init_08\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n data = reinterpret_cast(ALLOCA(10*sizeof(TwoIntsClass)));\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n TwoIntsClass * data;\r\n data = reinterpret_cast(ALLOCA(10*sizeof(TwoIntsClass)));\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n TwoIntsClass * data;\r\n data = reinterpret_cast(ALLOCA(10*sizeof(TwoIntsClass)));\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticReturnsTrue() to staticReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass * data;\r\n data = reinterpret_cast(ALLOCA(10*sizeof(TwoIntsClass)));\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass * data;\r\n data = reinterpret_cast(ALLOCA(10*sizeof(TwoIntsClass)));\r\n if(staticReturnsTrue())\r\n {\r\n /* FIX: Completely initialize data */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n if(staticReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__twointsclass_array_alloca_no_init_08; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_array_alloca_no_init_12.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_array_alloca_no_init_12.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":5312,"source_sha256":"d24be3a0411c7a14a05c46092870c860ec9d5f7e8d37305d3c39c2c2415ce83b","line_count":199,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__twointsclass_array_alloca_no_init_12.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__twointsclass_array.label.xml\r\nTemplate File: sources-sinks-12.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__twointsclass_array_alloca_no_init_12\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n data = reinterpret_cast(ALLOCA(10*sizeof(TwoIntsClass)));\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the first \"if\" so that\r\n both branches use the BadSource and the second \"if\" so that both branches\r\n use the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n TwoIntsClass * data;\r\n data = reinterpret_cast(ALLOCA(10*sizeof(TwoIntsClass)));\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n else\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the first \"if\" so that\r\n both branches use the GoodSource and the second \"if\" so that both branches\r\n use the BadSink */\r\nstatic void goodG2B()\r\n{\r\n TwoIntsClass * data;\r\n data = reinterpret_cast(ALLOCA(10*sizeof(TwoIntsClass)));\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Completely initialize data */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n else\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__twointsclass_array_alloca_no_init_12; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_array_alloca_no_init_14.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_array_alloca_no_init_14.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":5375,"source_sha256":"5596660c187078679191ff8e6b28763ac937488eb75ec241bc53c42ee146fce1","line_count":198,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__twointsclass_array_alloca_no_init_14.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__twointsclass_array.label.xml\r\nTemplate File: sources-sinks-14.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 14 Control flow: if(globalFive==5) and if(globalFive!=5)\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__twointsclass_array_alloca_no_init_14\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n data = reinterpret_cast(ALLOCA(10*sizeof(TwoIntsClass)));\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalFive==5 to globalFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n TwoIntsClass * data;\r\n data = reinterpret_cast(ALLOCA(10*sizeof(TwoIntsClass)));\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n TwoIntsClass * data;\r\n data = reinterpret_cast(ALLOCA(10*sizeof(TwoIntsClass)));\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(globalFive==5)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalFive==5 to globalFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass * data;\r\n data = reinterpret_cast(ALLOCA(10*sizeof(TwoIntsClass)));\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass * data;\r\n data = reinterpret_cast(ALLOCA(10*sizeof(TwoIntsClass)));\r\n if(globalFive==5)\r\n {\r\n /* FIX: Completely initialize data */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__twointsclass_array_alloca_no_init_14; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_array_alloca_no_init_64b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_array_alloca_no_init_64b.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":2354,"source_sha256":"4835b8f708094f9cb3f3550efd4fd10d10c14fb0690f4e81ab3869422a1ad386","line_count":80,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__twointsclass_array_alloca_no_init_64b.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__twointsclass_array.label.xml\r\nTemplate File: sources-sinks-64b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__twointsclass_array_alloca_no_init_64\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n TwoIntsClass * * dataPtr = (TwoIntsClass * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n TwoIntsClass * data = (*dataPtr);\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n TwoIntsClass * * dataPtr = (TwoIntsClass * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n TwoIntsClass * data = (*dataPtr);\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n TwoIntsClass * * dataPtr = (TwoIntsClass * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n TwoIntsClass * data = (*dataPtr);\r\n /* FIX: Ensure data is initialized before use */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_no_init_01.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_no_init_01.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":3121,"source_sha256":"9a641587199619c81bd2ebe8468f5f0e6bcd9f2cdac6b766354f7e86ac47f376","line_count":121,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_no_init_01.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__twointsclass_array.label.xml\r\nTemplate File: sources-sinks-01.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 01 Baseline\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_no_init_01\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n /* FIX: Completely initialize data */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n /* FIX: Ensure data is initialized before use */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_no_init_01; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_no_init_06.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_no_init_06.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":5633,"source_sha256":"64bf834138465069387f5b53875eddfde17e5ab2984178dc62d77767da78461b","line_count":209,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_no_init_06.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__twointsclass_array.label.xml\r\nTemplate File: sources-sinks-06.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 06 Control flow: if(STATIC_CONST_FIVE==5) and if(STATIC_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The variable below is declared \"const\", so a tool should be able\r\n to identify that reads of this will always give its initialized\r\n value. */\r\nstatic const int STATIC_CONST_FIVE = 5;\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_no_init_06\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodB2G1()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: Completely initialize data */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_no_init_06; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_no_init_14.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_no_init_14.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":5323,"source_sha256":"2aee90ab05407ca6541fdf4beec158500c83f9e9a4dfadb4e0987bd15be401cc","line_count":203,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_no_init_14.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__twointsclass_array.label.xml\r\nTemplate File: sources-sinks-14.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 14 Control flow: if(globalFive==5) and if(globalFive!=5)\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_no_init_14\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalFive==5 to globalFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Don't initialize data */\r\n ; /* empty statement needed for some flow variants */\r\n }\r\n if(globalFive==5)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalFive==5 to globalFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(globalFive==5)\r\n {\r\n /* FIX: Completely initialize data */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_no_init_14; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_no_init_64b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_no_init_64b.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":2356,"source_sha256":"31bf899fbc0037438f719d9de640a7dae34c2301628953a463798ffc70869fd8","line_count":80,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_no_init_64b.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__twointsclass_array.label.xml\r\nTemplate File: sources-sinks-64b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: no_init Don't initialize data\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_no_init_64\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n TwoIntsClass * * dataPtr = (TwoIntsClass * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n TwoIntsClass * data = (*dataPtr);\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n TwoIntsClass * * dataPtr = (TwoIntsClass * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n TwoIntsClass * data = (*dataPtr);\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n TwoIntsClass * * dataPtr = (TwoIntsClass * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n TwoIntsClass * data = (*dataPtr);\r\n /* FIX: Ensure data is initialized before use */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_partial_init_06.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_partial_init_06.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":5871,"source_sha256":"2a1b4a6491e134bc6d50f781230fcadc7a05271abc7376dd482ad80809911e95","line_count":221,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_partial_init_06.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__twointsclass_array.label.xml\r\nTemplate File: sources-sinks-06.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 06 Control flow: if(STATIC_CONST_FIVE==5) and if(STATIC_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The variable below is declared \"const\", so a tool should be able\r\n to identify that reads of this will always give its initialized\r\n value. */\r\nstatic const int STATIC_CONST_FIVE = 5;\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_partial_init_06\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n for(int i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodB2G1()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n for(int i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n for(int i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first STATIC_CONST_FIVE==5 to STATIC_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(STATIC_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* FIX: Completely initialize data */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n if(STATIC_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_partial_init_06; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_partial_init_07.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_partial_init_07.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":5788,"source_sha256":"546dcfe823b212fb4307af6bf8514c44d0c9b0a5cda623837e75fd840799aa94","line_count":221,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_partial_init_07.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__twointsclass_array.label.xml\r\nTemplate File: sources-sinks-07.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 07 Control flow: if(staticFive==5) and if(staticFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The variable below is not declared \"const\", but is never assigned\r\n any other value so a tool should be able to identify that reads of\r\n this will always give its initialized value. */\r\nstatic int staticFive = 5;\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_partial_init_07\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n for(int i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second staticFive==5 to staticFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n for(int i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n for(int i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n if(staticFive==5)\r\n {\r\n /* FIX: Ensure data is initialized before use */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first staticFive==5 to staticFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Completely initialize data */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n if(staticFive==5)\r\n {\r\n /* FIX: Completely initialize data */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n }\r\n if(staticFive==5)\r\n {\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_partial_init_07; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_partial_init_18.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE457_Use_of_Uninitialized_Variable/s01/CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_partial_init_18.cpp","cwe_id":"CWE457","file_extension":".cpp","language_hint":"C++","source_size_bytes":3521,"source_sha256":"8aa2bc1428385ff111f7e1f36aa6c8913ce847e4e7a742af60d9b01d874f8c17","line_count":140,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_partial_init_18.cpp\r\nLabel Definition File: CWE457_Use_of_Uninitialized_Variable__twointsclass_array.label.xml\r\nTemplate File: sources-sinks-18.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 457 Use of Uninitialized Variable\r\n * BadSource: partial_init Initialize part, but not all of the array\r\n * GoodSource: Initialize data\r\n * Sinks: use\r\n * GoodSink: Initialize then use data\r\n * BadSink : Use data\r\n * Flow Variant: 18 Control flow: goto statements\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_partial_init_18\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n goto source;\r\nsource:\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n for(int i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by reversing the blocks on the second goto statement */\r\nstatic void goodB2G()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n goto source;\r\nsource:\r\n /* POTENTIAL FLAW: Partially initialize data */\r\n for(int i=0; i<(10/2); i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n goto sink;\r\nsink:\r\n /* FIX: Ensure data is initialized before use */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by reversing the blocks on the first goto statement */\r\nstatic void goodG2B()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass dataUninitArray[10];\r\n data = dataUninitArray;\r\n goto source;\r\nsource:\r\n /* FIX: Completely initialize data */\r\n for(int i=0; i<10; i++)\r\n {\r\n data[i].intOne = i;\r\n data[i].intTwo = i;\r\n }\r\n goto sink;\r\nsink:\r\n /* POTENTIAL FLAW: Use data without initializing it */\r\n for(int i=0; i<10; i++)\r\n {\r\n printIntLine(data[i].intOne);\r\n printIntLine(data[i].intTwo);\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE457_Use_of_Uninitialized_Variable__twointsclass_array_declare_partial_init_18; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__binary_if_04.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__binary_if_04.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":3526,"source_sha256":"e44ad9145545ce5a88d1717d554ebad929289150e5b6433744293ff1b7159ab5","line_count":117,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__binary_if_04.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.pointflaw.label.xml\r\nTemplate File: point-flaw-04.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * Sinks: binary_if\r\n * GoodSink: Do not check for NULL after the pointer has been dereferenced\r\n * BadSink : Check for NULL after a pointer has already been dereferenced\r\n * Flow Variant: 04 Control flow: if(STATIC_CONST_TRUE) and if(STATIC_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The two variables below are declared \"const\", so a tool should\r\n be able to identify that reads of these will always return their\r\n initialized values. */\r\nstatic const int STATIC_CONST_TRUE = 1; /* true */\r\nstatic const int STATIC_CONST_FALSE = 0; /* false */\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__binary_if_04_bad()\r\n{\r\n if(STATIC_CONST_TRUE)\r\n {\r\n {\r\n twoIntsStruct *twoIntsStructPointer = NULL;\r\n /* FLAW: Using a single & in the if statement will cause both sides of the expression to be evaluated\r\n * thus causing a NPD */\r\n if ((twoIntsStructPointer != NULL) & (twoIntsStructPointer->intOne == 5))\r\n {\r\n printLine(\"intOne == 5\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good1() uses if(STATIC_CONST_FALSE) instead of if(STATIC_CONST_TRUE) */\r\nstatic void good1()\r\n{\r\n if(STATIC_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n {\r\n twoIntsStruct *twoIntsStructPointer = NULL;\r\n /* FIX: Use && in the if statement so that if the left side of the expression fails then\r\n * the right side will not be evaluated */\r\n if ((twoIntsStructPointer != NULL) && (twoIntsStructPointer->intOne == 5))\r\n {\r\n printLine(\"intOne == 5\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* good2() reverses the bodies in the if statement */\r\nstatic void good2()\r\n{\r\n if(STATIC_CONST_TRUE)\r\n {\r\n {\r\n twoIntsStruct *twoIntsStructPointer = NULL;\r\n /* FIX: Use && in the if statement so that if the left side of the expression fails then\r\n * the right side will not be evaluated */\r\n if ((twoIntsStructPointer != NULL) && (twoIntsStructPointer->intOne == 5))\r\n {\r\n printLine(\"intOne == 5\");\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__binary_if_04_good()\r\n{\r\n good1();\r\n good2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__binary_if_04_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__binary_if_04_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__char_02.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__char_02.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":4146,"source_sha256":"2090d86ade6252131becf9c3a0e994df75b58ca60025b6f48c5269fa2cecbf6a","line_count":170,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__char_02.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-02.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 02 Control flow: if(1) and if(0)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__char_02_bad()\r\n{\r\n char * data;\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 1 to 0 */\r\nstatic void goodB2G1()\r\n{\r\n char * data;\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n char * data;\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(1)\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 1 to 0 */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n if(0)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Initialize data */\r\n data = \"Good\";\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n if(1)\r\n {\r\n /* FIX: Initialize data */\r\n data = \"Good\";\r\n }\r\n if(1)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__char_02_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__char_02_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__char_02_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__char_04.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__char_04.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":4678,"source_sha256":"074279f06b62369a620eceaba4a6722368def7ba34ef338363f9646785950f8a","line_count":176,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__char_04.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-04.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 04 Control flow: if(STATIC_CONST_TRUE) and if(STATIC_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The two variables below are declared \"const\", so a tool should\r\n be able to identify that reads of these will always return their\r\n initialized values. */\r\nstatic const int STATIC_CONST_TRUE = 1; /* true */\r\nstatic const int STATIC_CONST_FALSE = 0; /* false */\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__char_04_bad()\r\n{\r\n char * data;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second STATIC_CONST_TRUE to STATIC_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n char * data;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(STATIC_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n char * data;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first STATIC_CONST_TRUE to STATIC_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n if(STATIC_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Initialize data */\r\n data = \"Good\";\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* FIX: Initialize data */\r\n data = \"Good\";\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__char_04_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__char_04_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__char_04_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__char_11.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__char_11.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":4439,"source_sha256":"4d242d7e339f6363eb877f192487d7ddea585925e480add0ece14391aab6df21","line_count":170,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__char_11.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-11.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__char_11_bad()\r\n{\r\n char * data;\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n char * data;\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n char * data;\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Initialize data */\r\n data = \"Good\";\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Initialize data */\r\n data = \"Good\";\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__char_11_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__char_11_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__char_11_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__char_14.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__char_14.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":4338,"source_sha256":"01ed68dfa29bf0f0258cca424217553d003dee45573c4255d4fe5bc605a184c4","line_count":170,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__char_14.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-14.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 14 Control flow: if(globalFive==5) and if(globalFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__char_14_bad()\r\n{\r\n char * data;\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalFive==5 to globalFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n char * data;\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n char * data;\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(globalFive==5)\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalFive==5 to globalFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n char * data;\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Initialize data */\r\n data = \"Good\";\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n char * data;\r\n if(globalFive==5)\r\n {\r\n /* FIX: Initialize data */\r\n data = \"Good\";\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__char_14_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__char_14_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__char_14_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__char_53c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__char_53c.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":1531,"source_sha256":"6bc194f1e84513e745a61e3ff39c36beb0580b002d7cb357f4d9f3bd3ccc2c14","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__char_53c.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-53c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 53 Data flow: data passed as an argument from one function through two others to a fourth; all four functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE476_NULL_Pointer_Dereference__char_53d_badSink(char * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__char_53c_badSink(char * data)\r\n{\r\n CWE476_NULL_Pointer_Dereference__char_53d_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE476_NULL_Pointer_Dereference__char_53d_goodG2BSink(char * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__char_53c_goodG2BSink(char * data)\r\n{\r\n CWE476_NULL_Pointer_Dereference__char_53d_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE476_NULL_Pointer_Dereference__char_53d_goodB2GSink(char * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__char_53c_goodB2GSink(char * data)\r\n{\r\n CWE476_NULL_Pointer_Dereference__char_53d_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__char_54a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__char_54a.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":2517,"source_sha256":"13c391c4da21c6be508ab9dfc1504fcd39fe74bdc398f76b917953e0602e458c","line_count":93,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__char_54a.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-54a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE476_NULL_Pointer_Dereference__char_54b_badSink(char * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__char_54_bad()\r\n{\r\n char * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n CWE476_NULL_Pointer_Dereference__char_54b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE476_NULL_Pointer_Dereference__char_54b_goodG2BSink(char * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n char * data;\r\n /* FIX: Initialize data */\r\n data = \"Good\";\r\n CWE476_NULL_Pointer_Dereference__char_54b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE476_NULL_Pointer_Dereference__char_54b_goodB2GSink(char * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n char * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n CWE476_NULL_Pointer_Dereference__char_54b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__char_54_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__char_54_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__char_54_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__char_67b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__char_67b.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":2050,"source_sha256":"00364c354d24cb0f4ef99363350b3a4f2a71391f91953618050e2995e566200d","line_count":66,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__char_67b.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-67b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\ntypedef struct _CWE476_NULL_Pointer_Dereference__char_67_structType\r\n{\r\n char * structFirst;\r\n} CWE476_NULL_Pointer_Dereference__char_67_structType;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__char_67b_badSink(CWE476_NULL_Pointer_Dereference__char_67_structType myStruct)\r\n{\r\n char * data = myStruct.structFirst;\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE476_NULL_Pointer_Dereference__char_67b_goodG2BSink(CWE476_NULL_Pointer_Dereference__char_67_structType myStruct)\r\n{\r\n char * data = myStruct.structFirst;\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE476_NULL_Pointer_Dereference__char_67b_goodB2GSink(CWE476_NULL_Pointer_Dereference__char_67_structType myStruct)\r\n{\r\n char * data = myStruct.structFirst;\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__char_73b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__char_73b.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":1801,"source_sha256":"b3bf016f18283a717caa412815104eb3e45b60befea43d190c65642aa002f528","line_count":70,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__char_73b.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-73b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 73 Data flow: data passed in a list from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__char_73\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(list dataList)\r\n{\r\n /* copy data out of dataList */\r\n char * data = dataList.back();\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(list dataList)\r\n{\r\n char * data = dataList.back();\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(list dataList)\r\n{\r\n char * data = dataList.back();\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__char_81_bad.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__char_81_bad.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":988,"source_sha256":"e15eb40f2327ffb78616101ce42284d88bc4fc774cee6c33d11b2615cc020970","line_count":33,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__char_81_bad.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-81_bad.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITBAD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE476_NULL_Pointer_Dereference__char_81.h\"\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__char_81\r\n{\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__char_81_bad::action(char * data) const\r\n{\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printLine() checks for NULL, so we cannot use it here */\r\n printHexCharLine(data[0]);\r\n}\r\n\r\n}\r\n#endif /* OMITBAD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_12.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_12.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":4882,"source_sha256":"f20b209c2c0109072d70a407bfbf55106b445ef6eac948841bea0c528ddcee21","line_count":190,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__class_12.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference__class.label.xml\r\nTemplate File: sources-sinks-12.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check data for NULL before attempting to print data->a\r\n * BadSink : Print data->a\r\n * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__class_12\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n else\r\n {\r\n {\r\n TwoIntsClass * tmpData = new TwoIntsClass;\r\n tmpData->intOne = 0;\r\n tmpData->intTwo = 0;\r\n /* FIX: Initialize data */\r\n data = tmpData;\r\n }\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n }\r\n else\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(data->intOne);\r\n delete data;\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the first \"if\" so that\r\n both branches use the BadSource and the second \"if\" so that both branches\r\n use the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n TwoIntsClass * data;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n else\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(data->intOne);\r\n delete data;\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n else\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(data->intOne);\r\n delete data;\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the first \"if\" so that\r\n both branches use the GoodSource and the second \"if\" so that both branches\r\n use the BadSink */\r\nstatic void goodG2B()\r\n{\r\n TwoIntsClass * data;\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n {\r\n TwoIntsClass * tmpData = new TwoIntsClass;\r\n tmpData->intOne = 0;\r\n tmpData->intTwo = 0;\r\n /* FIX: Initialize data */\r\n data = tmpData;\r\n }\r\n }\r\n else\r\n {\r\n {\r\n TwoIntsClass * tmpData = new TwoIntsClass;\r\n tmpData->intOne = 0;\r\n tmpData->intTwo = 0;\r\n /* FIX: Initialize data */\r\n data = tmpData;\r\n }\r\n }\r\n if(globalReturnsTrueOrFalse())\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n }\r\n else\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE476_NULL_Pointer_Dereference__class_12; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_13.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_13.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":4913,"source_sha256":"10067c027b0c52cada1e6f5a1c2ee34056eeccdbda31fd8a76f5080c009f5643","line_count":187,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__class_13.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference__class.label.xml\r\nTemplate File: sources-sinks-13.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check data for NULL before attempting to print data->a\r\n * BadSink : Print data->a\r\n * Flow Variant: 13 Control flow: if(GLOBAL_CONST_FIVE==5) and if(GLOBAL_CONST_FIVE!=5)\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__class_13\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_FIVE==5 to GLOBAL_CONST_FIVE!=5 */\r\nstatic void goodB2G1()\r\n{\r\n TwoIntsClass * data;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(GLOBAL_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(data->intOne);\r\n delete data;\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n TwoIntsClass * data;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(data->intOne);\r\n delete data;\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_FIVE==5 to GLOBAL_CONST_FIVE!=5 */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass * data;\r\n if(GLOBAL_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n {\r\n TwoIntsClass * tmpData = new TwoIntsClass;\r\n tmpData->intOne = 0;\r\n tmpData->intTwo = 0;\r\n /* FIX: Initialize data */\r\n data = tmpData;\r\n }\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass * data;\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n {\r\n TwoIntsClass * tmpData = new TwoIntsClass;\r\n tmpData->intOne = 0;\r\n tmpData->intTwo = 0;\r\n /* FIX: Initialize data */\r\n data = tmpData;\r\n }\r\n }\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE476_NULL_Pointer_Dereference__class_13; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_14.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_14.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":4801,"source_sha256":"8cabce02bbf3cfb080e36bfbc64be204225052d2590415017c0f95ea603fd797","line_count":187,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__class_14.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference__class.label.xml\r\nTemplate File: sources-sinks-14.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check data for NULL before attempting to print data->a\r\n * BadSink : Print data->a\r\n * Flow Variant: 14 Control flow: if(globalFive==5) and if(globalFive!=5)\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__class_14\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalFive==5 to globalFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n TwoIntsClass * data;\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(data->intOne);\r\n delete data;\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n TwoIntsClass * data;\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(globalFive==5)\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(data->intOne);\r\n delete data;\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalFive==5 to globalFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n TwoIntsClass * data;\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n {\r\n TwoIntsClass * tmpData = new TwoIntsClass;\r\n tmpData->intOne = 0;\r\n tmpData->intTwo = 0;\r\n /* FIX: Initialize data */\r\n data = tmpData;\r\n }\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n TwoIntsClass * data;\r\n if(globalFive==5)\r\n {\r\n {\r\n TwoIntsClass * tmpData = new TwoIntsClass;\r\n tmpData->intOne = 0;\r\n tmpData->intTwo = 0;\r\n /* FIX: Initialize data */\r\n data = tmpData;\r\n }\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE476_NULL_Pointer_Dereference__class_14; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_32.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_32.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":3613,"source_sha256":"d82e6dab4d96dda2956406a57e336bf85911fcf08093eb0b4581f79eb81b097a","line_count":137,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__class_32.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference__class.label.xml\r\nTemplate File: sources-sinks-32.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check data for NULL before attempting to print data->a\r\n * BadSink : Print data->a\r\n * Flow Variant: 32 Data flow using two pointers to the same value within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__class_32\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass * *dataPtr1 = &data;\r\n TwoIntsClass * *dataPtr2 = &data;\r\n {\r\n TwoIntsClass * data = *dataPtr1;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n *dataPtr1 = data;\r\n }\r\n {\r\n TwoIntsClass * data = *dataPtr2;\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass * *dataPtr1 = &data;\r\n TwoIntsClass * *dataPtr2 = &data;\r\n {\r\n TwoIntsClass * data = *dataPtr1;\r\n {\r\n TwoIntsClass * tmpData = new TwoIntsClass;\r\n tmpData->intOne = 0;\r\n tmpData->intTwo = 0;\r\n /* FIX: Initialize data */\r\n data = tmpData;\r\n }\r\n *dataPtr1 = data;\r\n }\r\n {\r\n TwoIntsClass * data = *dataPtr2;\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass * *dataPtr1 = &data;\r\n TwoIntsClass * *dataPtr2 = &data;\r\n {\r\n TwoIntsClass * data = *dataPtr1;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n *dataPtr1 = data;\r\n }\r\n {\r\n TwoIntsClass * data = *dataPtr2;\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(data->intOne);\r\n delete data;\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE476_NULL_Pointer_Dereference__class_32; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_33.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_33.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":3186,"source_sha256":"93d8f84939a3c0483cf7569ecd49ec58a1cdae2172f8f2123b77fd2aff5e82c2","line_count":122,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__class_33.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference__class.label.xml\r\nTemplate File: sources-sinks-33.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check data for NULL before attempting to print data->a\r\n * BadSink : Print data->a\r\n * Flow Variant: 33 Data flow: use of a C++ reference to data within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__class_33\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass * &dataRef = data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n {\r\n TwoIntsClass * data = dataRef;\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass * &dataRef = data;\r\n {\r\n TwoIntsClass * tmpData = new TwoIntsClass;\r\n tmpData->intOne = 0;\r\n tmpData->intTwo = 0;\r\n /* FIX: Initialize data */\r\n data = tmpData;\r\n }\r\n {\r\n TwoIntsClass * data = dataRef;\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n TwoIntsClass * data;\r\n TwoIntsClass * &dataRef = data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n {\r\n TwoIntsClass * data = dataRef;\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(data->intOne);\r\n delete data;\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE476_NULL_Pointer_Dereference__class_33; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_41.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_41.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":3093,"source_sha256":"897d10dd344b28255038527486c6ae81c1b91e6235fc3adefdf3835ec5b6537c","line_count":126,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__class_41.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference__class.label.xml\r\nTemplate File: sources-sinks-41.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check data for NULL before attempting to print data->a\r\n * BadSink : Print data->a\r\n * Flow Variant: 41 Data flow: data passed as an argument from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__class_41\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink(TwoIntsClass * data)\r\n{\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n}\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink(TwoIntsClass * data)\r\n{\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n TwoIntsClass * data;\r\n {\r\n TwoIntsClass * tmpData = new TwoIntsClass;\r\n tmpData->intOne = 0;\r\n tmpData->intTwo = 0;\r\n /* FIX: Initialize data */\r\n data = tmpData;\r\n }\r\n goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink(TwoIntsClass * data)\r\n{\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(data->intOne);\r\n delete data;\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n TwoIntsClass * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n goodB2GSink(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE476_NULL_Pointer_Dereference__class_41; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_45.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_45.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":3330,"source_sha256":"69279aa3cca40121cb399b0c2123da032cf9d14de99873fb2435b9ea42f896c1","line_count":136,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__class_45.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference__class.label.xml\r\nTemplate File: sources-sinks-45.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check data for NULL before attempting to print data->a\r\n * BadSink : Print data->a\r\n * Flow Variant: 45 Data flow: data passed as a static global variable from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__class_45\r\n{\r\n\r\nstatic TwoIntsClass * badData;\r\nstatic TwoIntsClass * goodG2BData;\r\nstatic TwoIntsClass * goodB2GData;\r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink()\r\n{\r\n TwoIntsClass * data = badData;\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n}\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n badData = data;\r\n badSink();\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink()\r\n{\r\n TwoIntsClass * data = goodG2BData;\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n TwoIntsClass * data;\r\n {\r\n TwoIntsClass * tmpData = new TwoIntsClass;\r\n tmpData->intOne = 0;\r\n tmpData->intTwo = 0;\r\n /* FIX: Initialize data */\r\n data = tmpData;\r\n }\r\n goodG2BData = data;\r\n goodG2BSink();\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink()\r\n{\r\n TwoIntsClass * data = goodB2GData;\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(data->intOne);\r\n delete data;\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n TwoIntsClass * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n goodB2GData = data;\r\n goodB2GSink();\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE476_NULL_Pointer_Dereference__class_45; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_51b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_51b.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":1678,"source_sha256":"7f8575dcf7a50c9d649a18df6b3f900e28e7f143a4b07cd6ae8b75430027f963","line_count":63,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__class_51b.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference__class.label.xml\r\nTemplate File: sources-sinks-51b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check data for NULL before attempting to print data->a\r\n * BadSink : Print data->a\r\n * Flow Variant: 51 Data flow: data passed as an argument from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__class_51\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(TwoIntsClass * data)\r\n{\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(TwoIntsClass * data)\r\n{\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(TwoIntsClass * data)\r\n{\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(data->intOne);\r\n delete data;\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_52a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_52a.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":2461,"source_sha256":"5137da2abcebdf50d60888d4ec16c6962bd5f8ed6de974a0a3b5f75da1f5426c","line_count":103,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__class_52a.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference__class.label.xml\r\nTemplate File: sources-sinks-52a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check data for NULL before attempting to print data->a\r\n * BadSink : Print data->a\r\n * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__class_52\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink_b(TwoIntsClass * data);\r\n\r\nvoid bad()\r\n{\r\n TwoIntsClass * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n badSink_b(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink_b(TwoIntsClass * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n TwoIntsClass * data;\r\n {\r\n TwoIntsClass * tmpData = new TwoIntsClass;\r\n tmpData->intOne = 0;\r\n tmpData->intTwo = 0;\r\n /* FIX: Initialize data */\r\n data = tmpData;\r\n }\r\n goodG2BSink_b(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink_b(TwoIntsClass * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n TwoIntsClass * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n goodB2GSink_b(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE476_NULL_Pointer_Dereference__class_52; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_63b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_63b.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":1800,"source_sha256":"be31fdf94d86ff0c56e19e3ff939d8924d5ed8375b8a6f0adba2ffb2fa14f0b0","line_count":66,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__class_63b.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference__class.label.xml\r\nTemplate File: sources-sinks-63b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check data for NULL before attempting to print data->a\r\n * BadSink : Print data->a\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__class_63\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(TwoIntsClass * * dataPtr)\r\n{\r\n TwoIntsClass * data = *dataPtr;\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(TwoIntsClass * * dataPtr)\r\n{\r\n TwoIntsClass * data = *dataPtr;\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(TwoIntsClass * * dataPtr)\r\n{\r\n TwoIntsClass * data = *dataPtr;\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(data->intOne);\r\n delete data;\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_64b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_64b.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":2303,"source_sha256":"89b1399088350375a08a1e5ae06dd32bf203554565fa6e0bd61809fe3d1a15d2","line_count":75,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__class_64b.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference__class.label.xml\r\nTemplate File: sources-sinks-64b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check data for NULL before attempting to print data->a\r\n * BadSink : Print data->a\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__class_64\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n TwoIntsClass * * dataPtr = (TwoIntsClass * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n TwoIntsClass * data = (*dataPtr);\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n TwoIntsClass * * dataPtr = (TwoIntsClass * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n TwoIntsClass * data = (*dataPtr);\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n TwoIntsClass * * dataPtr = (TwoIntsClass * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n TwoIntsClass * data = (*dataPtr);\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(data->intOne);\r\n delete data;\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_65b.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_65b.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":1720,"source_sha256":"5909c7b6770d6049fef3b25b201008b90df94ef6b26616a6c4eabe5dbfdf3559","line_count":63,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__class_65b.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference__class.label.xml\r\nTemplate File: sources-sinks-65b.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check data for NULL before attempting to print data->a\r\n * BadSink : Print data->a\r\n * Flow Variant: 65 Data/control flow: data passed as an argument from one function to a function in a different source file called via a function pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__class_65\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid badSink(TwoIntsClass * data)\r\n{\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(TwoIntsClass * data)\r\n{\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n /* avoid memory leak - deleting a NULL pointer is a no-op in C++ so no need to check for NULL */\r\n delete data;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(TwoIntsClass * data)\r\n{\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(data->intOne);\r\n delete data;\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_81_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__class_81_goodB2G.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":1088,"source_sha256":"de23dbb7c357316494a06de575c90d0c30e52d3b47d2c83d0b6006989eb731aa","line_count":40,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__class_81_goodB2G.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference__class.label.xml\r\nTemplate File: sources-sinks-81_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check data for NULL before attempting to print data->a\r\n * BadSink : Print data->a\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE476_NULL_Pointer_Dereference__class_81.h\"\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__class_81\r\n{\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__class_81_goodB2G::action(TwoIntsClass * data) const\r\n{\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(data->intOne);\r\n delete data;\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__deref_after_check_07.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__deref_after_check_07.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":3052,"source_sha256":"b24175aa0d3d88037dc5c791444977e56056dc8b9cec745a375026ba25a7df28","line_count":113,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__deref_after_check_07.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.pointflaw.label.xml\r\nTemplate File: point-flaw-07.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * Sinks: deref_after_check\r\n * GoodSink: Do not dereference a pointer if it is NULL\r\n * BadSink : Dereference a pointer after checking to see if it is NULL\r\n * Flow Variant: 07 Control flow: if(staticFive==5) and if(staticFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The variable below is not declared \"const\", but is never assigned\r\n any other value so a tool should be able to identify that reads of\r\n this will always give its initialized value. */\r\nstatic int staticFive = 5;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__deref_after_check_07_bad()\r\n{\r\n if(staticFive==5)\r\n {\r\n {\r\n /* FLAW: Check for NULL, but still dereference the pointer */\r\n int *intPointer = NULL;\r\n if (intPointer == NULL)\r\n {\r\n printIntLine(*intPointer);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good1() uses if(staticFive!=5) instead of if(staticFive==5) */\r\nstatic void good1()\r\n{\r\n if(staticFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n {\r\n /* FIX: Check for NULL and do not dereference the pointer if it is NULL */\r\n int *intPointer = NULL;\r\n if (intPointer == NULL)\r\n {\r\n printLine(\"pointer is NULL\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* good2() reverses the bodies in the if statement */\r\nstatic void good2()\r\n{\r\n if(staticFive==5)\r\n {\r\n {\r\n /* FIX: Check for NULL and do not dereference the pointer if it is NULL */\r\n int *intPointer = NULL;\r\n if (intPointer == NULL)\r\n {\r\n printLine(\"pointer is NULL\");\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__deref_after_check_07_good()\r\n{\r\n good1();\r\n good2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__deref_after_check_07_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__deref_after_check_07_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__deref_after_check_08.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__deref_after_check_08.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":3152,"source_sha256":"c117271fdd0d0e5cc5efddd4a7cb673748632d1a3f601c44c84eb16310abdf76","line_count":121,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__deref_after_check_08.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.pointflaw.label.xml\r\nTemplate File: point-flaw-08.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * Sinks: deref_after_check\r\n * GoodSink: Do not dereference a pointer if it is NULL\r\n * BadSink : Dereference a pointer after checking to see if it is NULL\r\n * Flow Variant: 08 Control flow: if(staticReturnsTrue()) and if(staticReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The two function below always return the same value, so a tool\r\n should be able to identify that calls to the functions will always\r\n return a fixed value. */\r\nstatic int staticReturnsTrue()\r\n{\r\n return 1;\r\n}\r\n\r\nstatic int staticReturnsFalse()\r\n{\r\n return 0;\r\n}\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__deref_after_check_08_bad()\r\n{\r\n if(staticReturnsTrue())\r\n {\r\n {\r\n /* FLAW: Check for NULL, but still dereference the pointer */\r\n int *intPointer = NULL;\r\n if (intPointer == NULL)\r\n {\r\n printIntLine(*intPointer);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good1() uses if(staticReturnsFalse()) instead of if(staticReturnsTrue()) */\r\nstatic void good1()\r\n{\r\n if(staticReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n {\r\n /* FIX: Check for NULL and do not dereference the pointer if it is NULL */\r\n int *intPointer = NULL;\r\n if (intPointer == NULL)\r\n {\r\n printLine(\"pointer is NULL\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* good2() reverses the bodies in the if statement */\r\nstatic void good2()\r\n{\r\n if(staticReturnsTrue())\r\n {\r\n {\r\n /* FIX: Check for NULL and do not dereference the pointer if it is NULL */\r\n int *intPointer = NULL;\r\n if (intPointer == NULL)\r\n {\r\n printLine(\"pointer is NULL\");\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__deref_after_check_08_good()\r\n{\r\n good1();\r\n good2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__deref_after_check_08_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__deref_after_check_08_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__deref_after_check_13.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__deref_after_check_13.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":2878,"source_sha256":"7f721191aaf6e218054908c3fe690885a4267dfee53a2b1b8fab4c9b5c527a33","line_count":108,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__deref_after_check_13.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.pointflaw.label.xml\r\nTemplate File: point-flaw-13.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * Sinks: deref_after_check\r\n * GoodSink: Do not dereference a pointer if it is NULL\r\n * BadSink : Dereference a pointer after checking to see if it is NULL\r\n * Flow Variant: 13 Control flow: if(GLOBAL_CONST_FIVE==5) and if(GLOBAL_CONST_FIVE!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__deref_after_check_13_bad()\r\n{\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n {\r\n /* FLAW: Check for NULL, but still dereference the pointer */\r\n int *intPointer = NULL;\r\n if (intPointer == NULL)\r\n {\r\n printIntLine(*intPointer);\r\n }\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good1() uses if(GLOBAL_CONST_FIVE!=5) instead of if(GLOBAL_CONST_FIVE==5) */\r\nstatic void good1()\r\n{\r\n if(GLOBAL_CONST_FIVE!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n {\r\n /* FIX: Check for NULL and do not dereference the pointer if it is NULL */\r\n int *intPointer = NULL;\r\n if (intPointer == NULL)\r\n {\r\n printLine(\"pointer is NULL\");\r\n }\r\n }\r\n }\r\n}\r\n\r\n/* good2() reverses the bodies in the if statement */\r\nstatic void good2()\r\n{\r\n if(GLOBAL_CONST_FIVE==5)\r\n {\r\n {\r\n /* FIX: Check for NULL and do not dereference the pointer if it is NULL */\r\n int *intPointer = NULL;\r\n if (intPointer == NULL)\r\n {\r\n printLine(\"pointer is NULL\");\r\n }\r\n }\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__deref_after_check_13_good()\r\n{\r\n good1();\r\n good2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__deref_after_check_13_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__deref_after_check_13_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__deref_after_check_15.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__deref_after_check_15.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":3034,"source_sha256":"eb7c7bef98792aede695d666dc2a348567a3944c67768a72359e5a459d065126","line_count":121,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__deref_after_check_15.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.pointflaw.label.xml\r\nTemplate File: point-flaw-15.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * Sinks: deref_after_check\r\n * GoodSink: Do not dereference a pointer if it is NULL\r\n * BadSink : Dereference a pointer after checking to see if it is NULL\r\n * Flow Variant: 15 Control flow: switch(6)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__deref_after_check_15_bad()\r\n{\r\n switch(6)\r\n {\r\n case 6:\r\n {\r\n /* FLAW: Check for NULL, but still dereference the pointer */\r\n int *intPointer = NULL;\r\n if (intPointer == NULL)\r\n {\r\n printIntLine(*intPointer);\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good1() changes the switch to switch(5) */\r\nstatic void good1()\r\n{\r\n switch(5)\r\n {\r\n case 6:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n {\r\n /* FIX: Check for NULL and do not dereference the pointer if it is NULL */\r\n int *intPointer = NULL;\r\n if (intPointer == NULL)\r\n {\r\n printLine(\"pointer is NULL\");\r\n }\r\n }\r\n break;\r\n }\r\n}\r\n\r\n/* good2() reverses the blocks in the switch */\r\nstatic void good2()\r\n{\r\n switch(6)\r\n {\r\n case 6:\r\n {\r\n /* FIX: Check for NULL and do not dereference the pointer if it is NULL */\r\n int *intPointer = NULL;\r\n if (intPointer == NULL)\r\n {\r\n printLine(\"pointer is NULL\");\r\n }\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__deref_after_check_15_good()\r\n{\r\n good1();\r\n good2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__deref_after_check_15_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__deref_after_check_15_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int64_t_09.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int64_t_09.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":4187,"source_sha256":"3822ac8f9af0d04e73f93abdd0df099c098db9352fbbde4168b6ee09c004d1a7","line_count":171,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__int64_t_09.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-09.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_09_bad()\r\n{\r\n int64_t * data;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printLongLongLine(*data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n int64_t * data;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printLongLongLine(*data);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int64_t * data;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printLongLongLine(*data);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n int64_t * data;\n int64_t tmpData = 5LL;\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printLongLongLine(*data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int64_t * data;\n int64_t tmpData = 5LL;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printLongLongLine(*data);\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_09_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__int64_t_09_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__int64_t_09_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int64_t_14.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int64_t_14.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":4118,"source_sha256":"cd69165ff09e52e4be9310ddd786416019bcbb6167aa8e95629c0540600b5125","line_count":171,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__int64_t_14.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-14.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 14 Control flow: if(globalFive==5) and if(globalFive!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_14_bad()\r\n{\r\n int64_t * data;\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printLongLongLine(*data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalFive==5 to globalFive!=5 */\r\nstatic void goodB2G1()\r\n{\r\n int64_t * data;\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printLongLongLine(*data);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int64_t * data;\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(globalFive==5)\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printLongLongLine(*data);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalFive==5 to globalFive!=5 */\r\nstatic void goodG2B1()\r\n{\r\n int64_t * data;\n int64_t tmpData = 5LL;\r\n if(globalFive!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printLongLongLine(*data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int64_t * data;\n int64_t tmpData = 5LL;\r\n if(globalFive==5)\r\n {\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n }\r\n if(globalFive==5)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printLongLongLine(*data);\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_14_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__int64_t_14_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__int64_t_14_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int64_t_31.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int64_t_31.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":2754,"source_sha256":"de46daa6b6286e8180f3a13b7fc4b5229502837629db6d56b4614dad093e3cc8","line_count":110,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__int64_t_31.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-31.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 31 Data flow using a copy of data within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_31_bad()\r\n{\r\n int64_t * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n {\r\n int64_t * dataCopy = data;\r\n int64_t * data = dataCopy;\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printLongLongLine(*data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int64_t * data;\n int64_t tmpData = 5LL;\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n {\r\n int64_t * dataCopy = data;\r\n int64_t * data = dataCopy;\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printLongLongLine(*data);\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int64_t * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n {\r\n int64_t * dataCopy = data;\r\n int64_t * data = dataCopy;\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printLongLongLine(*data);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_31_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__int64_t_31_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__int64_t_31_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int64_t_52b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int64_t_52b.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":1552,"source_sha256":"c8a71df028e8118d57c82356a1ccdf16a997e63d54c834a8f7ac5259a1f5b6b9","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__int64_t_52b.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-52b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_52c_badSink(int64_t * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_52b_badSink(int64_t * data)\r\n{\r\n CWE476_NULL_Pointer_Dereference__int64_t_52c_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_52c_goodG2BSink(int64_t * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_52b_goodG2BSink(int64_t * data)\r\n{\r\n CWE476_NULL_Pointer_Dereference__int64_t_52c_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_52c_goodB2GSink(int64_t * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_52b_goodB2GSink(int64_t * data)\r\n{\r\n CWE476_NULL_Pointer_Dereference__int64_t_52c_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int64_t_54b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int64_t_54b.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":1580,"source_sha256":"9dff4c7175d113d960de4e49280f1673dbc27cb5749cfae483a8cddeaf29b940","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__int64_t_54b.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-54b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_54c_badSink(int64_t * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_54b_badSink(int64_t * data)\r\n{\r\n CWE476_NULL_Pointer_Dereference__int64_t_54c_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_54c_goodG2BSink(int64_t * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_54b_goodG2BSink(int64_t * data)\r\n{\r\n CWE476_NULL_Pointer_Dereference__int64_t_54c_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_54c_goodB2GSink(int64_t * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_54b_goodB2GSink(int64_t * data)\r\n{\r\n CWE476_NULL_Pointer_Dereference__int64_t_54c_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int64_t_54d.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int64_t_54d.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":1580,"source_sha256":"a651bb434a31c081307d96c63320b7b3acdc79f69b9454944286c267329466ed","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__int64_t_54d.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-54d.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_54e_badSink(int64_t * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_54d_badSink(int64_t * data)\r\n{\r\n CWE476_NULL_Pointer_Dereference__int64_t_54e_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_54e_goodG2BSink(int64_t * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_54d_goodG2BSink(int64_t * data)\r\n{\r\n CWE476_NULL_Pointer_Dereference__int64_t_54e_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_54e_goodB2GSink(int64_t * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_54d_goodB2GSink(int64_t * data)\r\n{\r\n CWE476_NULL_Pointer_Dereference__int64_t_54e_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int64_t_63a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int64_t_63a.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":2577,"source_sha256":"30971200c1415be0a733d7ff018f73d348eb8b5ffc23e763a49e57b85795235c","line_count":96,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__int64_t_63a.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-63a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 63 Data flow: pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_63b_badSink(int64_t * * dataPtr);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_63_bad()\r\n{\r\n int64_t * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n CWE476_NULL_Pointer_Dereference__int64_t_63b_badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_63b_goodG2BSink(int64_t * * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int64_t * data;\n int64_t tmpData = 5LL;\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n CWE476_NULL_Pointer_Dereference__int64_t_63b_goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_63b_goodB2GSink(int64_t * * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n int64_t * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n CWE476_NULL_Pointer_Dereference__int64_t_63b_goodB2GSink(&data);\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_63_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__int64_t_63_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__int64_t_63_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int64_t_64b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int64_t_64b.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":2042,"source_sha256":"6969cf805f9c18ecc35be7349d01ce81ff229a451be00372640a82328d0e642f","line_count":67,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__int64_t_64b.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-64b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_64b_badSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n int64_t * * dataPtr = (int64_t * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n int64_t * data = (*dataPtr);\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printLongLongLine(*data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_64b_goodG2BSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n int64_t * * dataPtr = (int64_t * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n int64_t * data = (*dataPtr);\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printLongLongLine(*data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_64b_goodB2GSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n int64_t * * dataPtr = (int64_t * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n int64_t * data = (*dataPtr);\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printLongLongLine(*data);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int64_t_66a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int64_t_66a.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":2796,"source_sha256":"040374db067e64889796c2c37413624f4edce0491ce1f54f5b63daa85f35a513","line_count":103,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__int64_t_66a.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-66a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 66 Data flow: data passed in an array from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_66b_badSink(int64_t * dataArray[]);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_66_bad()\r\n{\r\n int64_t * data;\r\n int64_t * dataArray[5];\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n /* put data in array */\r\n dataArray[2] = data;\r\n CWE476_NULL_Pointer_Dereference__int64_t_66b_badSink(dataArray);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_66b_goodG2BSink(int64_t * dataArray[]);\r\n\r\nstatic void goodG2B()\r\n{\r\n int64_t * data;\n int64_t tmpData = 5LL;\r\n int64_t * dataArray[5];\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n dataArray[2] = data;\r\n CWE476_NULL_Pointer_Dereference__int64_t_66b_goodG2BSink(dataArray);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_66b_goodB2GSink(int64_t * dataArray[]);\r\n\r\nstatic void goodB2G()\r\n{\r\n int64_t * data;\r\n int64_t * dataArray[5];\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n dataArray[2] = data;\r\n CWE476_NULL_Pointer_Dereference__int64_t_66b_goodB2GSink(dataArray);\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_66_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__int64_t_66_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__int64_t_66_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int64_t_81_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int64_t_81_goodB2G.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":1049,"source_sha256":"ce493830d524d2a9a95b417f066567635f5cb7cbfb5105e7b114ca453ae58f25","line_count":39,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__int64_t_81_goodB2G.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-81_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE476_NULL_Pointer_Dereference__int64_t_81.h\"\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__int64_t_81\r\n{\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int64_t_81_goodB2G::action(int64_t * data) const\r\n{\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printLongLongLine(*data);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int64_t_81a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int64_t_81a.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":2614,"source_sha256":"4ac9c2b8ed76c0e76603b78ca5075ff2bf4e36b546f7a692d2ea8ef060603aa7","line_count":98,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__int64_t_81a.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-81a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE476_NULL_Pointer_Dereference__int64_t_81.h\"\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__int64_t_81\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n const CWE476_NULL_Pointer_Dereference__int64_t_81_base& baseObject = CWE476_NULL_Pointer_Dereference__int64_t_81_bad();\r\n baseObject.action(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int64_t * data;\n int64_t tmpData = 5LL;\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n const CWE476_NULL_Pointer_Dereference__int64_t_81_base& baseObject = CWE476_NULL_Pointer_Dereference__int64_t_81_goodG2B();\r\n baseObject.action(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int64_t * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n const CWE476_NULL_Pointer_Dereference__int64_t_81_base& baseObject = CWE476_NULL_Pointer_Dereference__int64_t_81_goodB2G();\r\n baseObject.action(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE476_NULL_Pointer_Dereference__int64_t_81; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int64_t_82a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int64_t_82a.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":2675,"source_sha256":"d30c3d77d24412d6f8c2ada40787f6e653282590dd626f84341a32aef3c07d53","line_count":101,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__int64_t_82a.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-82a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE476_NULL_Pointer_Dereference__int64_t_82.h\"\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__int64_t_82\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int64_t * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n CWE476_NULL_Pointer_Dereference__int64_t_82_base* baseObject = new CWE476_NULL_Pointer_Dereference__int64_t_82_bad;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int64_t * data;\n int64_t tmpData = 5LL;\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n CWE476_NULL_Pointer_Dereference__int64_t_82_base* baseObject = new CWE476_NULL_Pointer_Dereference__int64_t_82_goodG2B;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int64_t * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n CWE476_NULL_Pointer_Dereference__int64_t_82_base* baseObject = new CWE476_NULL_Pointer_Dereference__int64_t_82_goodB2G;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE476_NULL_Pointer_Dereference__int64_t_82; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int_10.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int_10.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":3998,"source_sha256":"1607e3ac452806caee23a17d87af80a0d3087a990b44a1f60fb6fe357e1e6c4d","line_count":171,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__int_10.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-10.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 10 Control flow: if(globalTrue) and if(globalFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int_10_bad()\r\n{\r\n int * data;\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(*data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalTrue to globalFalse */\r\nstatic void goodB2G1()\r\n{\r\n int * data;\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(*data);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int * data;\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(globalTrue)\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(*data);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalTrue to globalFalse */\r\nstatic void goodG2B1()\r\n{\r\n int * data;\n int tmpData = 5;\r\n if(globalFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(*data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int * data;\n int tmpData = 5;\r\n if(globalTrue)\r\n {\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n }\r\n if(globalTrue)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(*data);\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int_10_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__int_10_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__int_10_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int_11.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int_11.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":4142,"source_sha256":"2d21d7d6ba4e7438e44244d01bec4e6ba784b52b20af0ee7b41b73072d8d6038","line_count":171,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__int_11.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-11.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int_11_bad()\r\n{\r\n int * data;\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(*data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodB2G1()\r\n{\r\n int * data;\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(*data);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n int * data;\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(*data);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first globalReturnsTrue() to globalReturnsFalse() */\r\nstatic void goodG2B1()\r\n{\r\n int * data;\n int tmpData = 5;\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(*data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n int * data;\n int tmpData = 5;\r\n if(globalReturnsTrue())\r\n {\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n }\r\n if(globalReturnsTrue())\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(*data);\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int_11_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__int_11_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__int_11_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int_41.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int_41.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":2623,"source_sha256":"9d603c543b191b00adb038698f00418d40da3182d14ede1aae124b1f3b8916c9","line_count":114,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__int_41.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-41.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 41 Data flow: data passed as an argument from one function to another in the same source file\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink(int * data)\r\n{\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(*data);\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int_41_bad()\r\n{\r\n int * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink(int * data)\r\n{\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(*data);\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n int * data;\n int tmpData = 5;\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink(int * data)\r\n{\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(*data);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n int * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n goodB2GSink(data);\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int_41_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__int_41_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__int_41_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int_51a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int_51a.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":2495,"source_sha256":"df24f9252a6b7a64d4e4b20282beda511c09583ccfc6cf1176beb9f7bfcaed32","line_count":96,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__int_51a.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-51a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 51 Data flow: data passed as an argument from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE476_NULL_Pointer_Dereference__int_51b_badSink(int * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int_51_bad()\r\n{\r\n int * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n CWE476_NULL_Pointer_Dereference__int_51b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE476_NULL_Pointer_Dereference__int_51b_goodG2BSink(int * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int * data;\n int tmpData = 5;\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n CWE476_NULL_Pointer_Dereference__int_51b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE476_NULL_Pointer_Dereference__int_51b_goodB2GSink(int * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n int * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n CWE476_NULL_Pointer_Dereference__int_51b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int_51_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__int_51_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__int_51_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int_54a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int_54a.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":2540,"source_sha256":"0e0a600a5a0cf4f86866899f9bb14c7c925f992168b75aecad2740ee4e49772b","line_count":96,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__int_54a.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-54a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE476_NULL_Pointer_Dereference__int_54b_badSink(int * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int_54_bad()\r\n{\r\n int * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n CWE476_NULL_Pointer_Dereference__int_54b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE476_NULL_Pointer_Dereference__int_54b_goodG2BSink(int * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n int * data;\n int tmpData = 5;\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n CWE476_NULL_Pointer_Dereference__int_54b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE476_NULL_Pointer_Dereference__int_54b_goodB2GSink(int * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n int * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n CWE476_NULL_Pointer_Dereference__int_54b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int_54_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__int_54_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__int_54_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int_64a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int_64a.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":2523,"source_sha256":"aeb5ba7d7dd1a8c0a7400b13177d63f1c33b3124a501575f80720e570c31e230","line_count":96,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__int_64a.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-64a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE476_NULL_Pointer_Dereference__int_64b_badSink(void * dataVoidPtr);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int_64_bad()\r\n{\r\n int * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n CWE476_NULL_Pointer_Dereference__int_64b_badSink(&data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE476_NULL_Pointer_Dereference__int_64b_goodG2BSink(void * dataVoidPtr);\r\n\r\nstatic void goodG2B()\r\n{\r\n int * data;\n int tmpData = 5;\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n CWE476_NULL_Pointer_Dereference__int_64b_goodG2BSink(&data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE476_NULL_Pointer_Dereference__int_64b_goodB2GSink(void * dataVoidPtr);\r\n\r\nstatic void goodB2G()\r\n{\r\n int * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n CWE476_NULL_Pointer_Dereference__int_64b_goodB2GSink(&data);\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int_64_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__int_64_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__int_64_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int_68b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int_68b.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":1788,"source_sha256":"e7d5560e9aad3d5435679fc9f787211b07089442c4b6773dfc894357ce3c2c33","line_count":62,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__int_68b.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-68b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 68 Data flow: data passed as a global variable from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nextern int * CWE476_NULL_Pointer_Dereference__int_68_badData;\r\nextern int * CWE476_NULL_Pointer_Dereference__int_68_goodG2BData;\r\nextern int * CWE476_NULL_Pointer_Dereference__int_68_goodB2GData;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int_68b_badSink()\r\n{\r\n int * data = CWE476_NULL_Pointer_Dereference__int_68_badData;\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(*data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE476_NULL_Pointer_Dereference__int_68b_goodG2BSink()\r\n{\r\n int * data = CWE476_NULL_Pointer_Dereference__int_68_goodG2BData;\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(*data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE476_NULL_Pointer_Dereference__int_68b_goodB2GSink()\r\n{\r\n int * data = CWE476_NULL_Pointer_Dereference__int_68_goodB2GData;\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(*data);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int_74a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int_74a.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":2727,"source_sha256":"a055811513288ea34b60a47572a5d980b5a4e1ca65bf8ecba7c40da9074e3710","line_count":120,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__int_74a.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-74a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 74 Data flow: data passed in a map from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__int_74\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(map dataMap);\r\n\r\nvoid bad()\r\n{\r\n int * data;\r\n map dataMap;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n /* Put data in a map */\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n badSink(dataMap);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(map dataMap);\r\n\r\nstatic void goodG2B()\r\n{\r\n int * data;\n int tmpData = 5;\r\n map dataMap;\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n /* Put data in a map */\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n goodG2BSink(dataMap);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(map dataMap);\r\n\r\nstatic void goodB2G()\r\n{\r\n int * data;\r\n map dataMap;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n goodB2GSink(dataMap);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE476_NULL_Pointer_Dereference__int_74; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int_82_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int_82_goodB2G.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":1016,"source_sha256":"9285e94881ded22bcc27e52fa2e0ec323b5f057d57309621281f9750918c5d28","line_count":39,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__int_82_goodB2G.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-82_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE476_NULL_Pointer_Dereference__int_82.h\"\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__int_82\r\n{\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__int_82_goodB2G::action(int * data)\r\n{\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(*data);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int_82a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__int_82a.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":2617,"source_sha256":"2abf7f6638f9a6916d6df3a64fcdbd43bdddb9f00e9daa1906fcae0a89ba868a","line_count":101,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__int_82a.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-82a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE476_NULL_Pointer_Dereference__int_82.h\"\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__int_82\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n int * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n CWE476_NULL_Pointer_Dereference__int_82_base* baseObject = new CWE476_NULL_Pointer_Dereference__int_82_bad;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n int * data;\n int tmpData = 5;\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n CWE476_NULL_Pointer_Dereference__int_82_base* baseObject = new CWE476_NULL_Pointer_Dereference__int_82_goodG2B;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n int * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n CWE476_NULL_Pointer_Dereference__int_82_base* baseObject = new CWE476_NULL_Pointer_Dereference__int_82_goodB2G;\r\n baseObject->action(data);\r\n delete baseObject;\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE476_NULL_Pointer_Dereference__int_82; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__long_01.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__long_01.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":2355,"source_sha256":"09957ae0da768312892f5b8cb489491fbce04a739519b8f24ff8d44b58871ae4","line_count":99,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__long_01.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-01.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 01 Baseline\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__long_01_bad()\r\n{\r\n long * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printLongLine(*data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n long * data;\n long tmpData = 5L;\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printLongLine(*data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n long * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printLongLine(*data);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__long_01_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__long_01_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__long_01_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__long_03.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__long_03.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":3916,"source_sha256":"47386094f4c94774d54e3f5a8132962df6f83f8dd9366ce8ba9f4fed7f4002a5","line_count":171,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__long_03.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-03.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 03 Control flow: if(5==5) and if(5!=5)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__long_03_bad()\r\n{\r\n long * data;\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printLongLine(*data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second 5==5 to 5!=5 */\r\nstatic void goodB2G1()\r\n{\r\n long * data;\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printLongLine(*data);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n long * data;\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(5==5)\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printLongLine(*data);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first 5==5 to 5!=5 */\r\nstatic void goodG2B1()\r\n{\r\n long * data;\n long tmpData = 5L;\r\n if(5!=5)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printLongLine(*data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n long * data;\n long tmpData = 5L;\r\n if(5==5)\r\n {\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n }\r\n if(5==5)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printLongLine(*data);\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__long_03_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__long_03_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__long_03_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__long_09.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__long_09.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":4129,"source_sha256":"991a472d82ec2d464b065ff2747cf6d58f0b4674e9f28e97f8530b167160dbba","line_count":171,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__long_09.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-09.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__long_09_bad()\r\n{\r\n long * data;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printLongLine(*data);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n long * data;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printLongLine(*data);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n long * data;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printLongLine(*data);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n long * data;\n long tmpData = 5L;\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printLongLine(*data);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n long * data;\n long tmpData = 5L;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printLongLine(*data);\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__long_09_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__long_09_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__long_09_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__long_44.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__long_44.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":2878,"source_sha256":"71d294bbea77f19cd58e7236c6cc886ce1477738c8f7cefdce3f02fb234e5e2b","line_count":118,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__long_44.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-44.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * 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\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nstatic void badSink(long * data)\r\n{\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printLongLine(*data);\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__long_44_bad()\r\n{\r\n long * data;\r\n /* define a function pointer */\r\n void (*funcPtr) (long *) = badSink;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n /* use the function pointer */\r\n funcPtr(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2BSink(long * data)\r\n{\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printLongLine(*data);\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n long * data;\n long tmpData = 5L;\r\n void (*funcPtr) (long *) = goodG2BSink;\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n funcPtr(data);\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2GSink(long * data)\r\n{\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printLongLine(*data);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n}\r\n\r\nstatic void goodB2G()\r\n{\r\n long * data;\r\n void (*funcPtr) (long *) = goodB2GSink;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n funcPtr(data);\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__long_44_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__long_44_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__long_44_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__long_54c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__long_54c.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":1532,"source_sha256":"79cc553e239b6e9c301c2b143559d6aadfca8af3353ae3aec527a2ba161d6010","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__long_54c.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-54c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE476_NULL_Pointer_Dereference__long_54d_badSink(long * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__long_54c_badSink(long * data)\r\n{\r\n CWE476_NULL_Pointer_Dereference__long_54d_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE476_NULL_Pointer_Dereference__long_54d_goodG2BSink(long * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__long_54c_goodG2BSink(long * data)\r\n{\r\n CWE476_NULL_Pointer_Dereference__long_54d_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE476_NULL_Pointer_Dereference__long_54d_goodB2GSink(long * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__long_54c_goodB2GSink(long * data)\r\n{\r\n CWE476_NULL_Pointer_Dereference__long_54d_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__long_68b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__long_68b.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":1807,"source_sha256":"d3ddb0878454bcd31bb5f47033db54ad0c7be10625d1450726a2f034421fe558","line_count":62,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__long_68b.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-68b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 68 Data flow: data passed as a global variable from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nextern long * CWE476_NULL_Pointer_Dereference__long_68_badData;\r\nextern long * CWE476_NULL_Pointer_Dereference__long_68_goodG2BData;\r\nextern long * CWE476_NULL_Pointer_Dereference__long_68_goodB2GData;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__long_68b_badSink()\r\n{\r\n long * data = CWE476_NULL_Pointer_Dereference__long_68_badData;\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printLongLine(*data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE476_NULL_Pointer_Dereference__long_68b_goodG2BSink()\r\n{\r\n long * data = CWE476_NULL_Pointer_Dereference__long_68_goodG2BData;\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printLongLine(*data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE476_NULL_Pointer_Dereference__long_68b_goodB2GSink()\r\n{\r\n long * data = CWE476_NULL_Pointer_Dereference__long_68_goodB2GData;\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printLongLine(*data);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__long_72a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__long_72a.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":3011,"source_sha256":"8b77807e1bdac1f45beba984674978b10b6d6b7252cccc45bdd6728e73d1ac56","line_count":120,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__long_72a.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-72a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 72 Data flow: data passed in a vector from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__long_72\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(vector dataVector);\r\n\r\nvoid bad()\r\n{\r\n long * data;\r\n vector dataVector;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n /* Put data in a vector */\r\n dataVector.insert(dataVector.end(), 1, data);\r\n dataVector.insert(dataVector.end(), 1, data);\r\n dataVector.insert(dataVector.end(), 1, data);\r\n badSink(dataVector);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(vector dataVector);\r\n\r\nstatic void goodG2B()\r\n{\r\n long * data;\n long tmpData = 5L;\r\n vector dataVector;\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n /* Put data in a vector */\r\n dataVector.insert(dataVector.end(), 1, data);\r\n dataVector.insert(dataVector.end(), 1, data);\r\n dataVector.insert(dataVector.end(), 1, data);\r\n goodG2BSink(dataVector);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(vector dataVector);\r\n\r\nstatic void goodB2G()\r\n{\r\n long * data;\r\n vector dataVector;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n dataVector.insert(dataVector.end(), 1, data);\r\n dataVector.insert(dataVector.end(), 1, data);\r\n dataVector.insert(dataVector.end(), 1, data);\r\n goodB2GSink(dataVector);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE476_NULL_Pointer_Dereference__long_72; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__long_81_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__long_81_goodB2G.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":1030,"source_sha256":"66a64b3fab1bebf5d9b9e1425b7702ef6632dd38e9ebeeeac3a6df5b27c5b7dd","line_count":39,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__long_81_goodB2G.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-81_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE476_NULL_Pointer_Dereference__long_81.h\"\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__long_81\r\n{\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__long_81_goodB2G::action(long * data) const\r\n{\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printLongLine(*data);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__long_81a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__long_81a.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":2571,"source_sha256":"a566b2e8871d43dd121d7fc439f6775527426cc27a8b11017509a05a98d35467","line_count":98,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__long_81a.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-81a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 81 Data flow: data passed in a parameter to an virtual method called via a reference\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE476_NULL_Pointer_Dereference__long_81.h\"\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__long_81\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n long * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n const CWE476_NULL_Pointer_Dereference__long_81_base& baseObject = CWE476_NULL_Pointer_Dereference__long_81_bad();\r\n baseObject.action(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n long * data;\n long tmpData = 5L;\r\n /* FIX: Initialize data */\r\n {\n data = &tmpData;\r\n }\r\n const CWE476_NULL_Pointer_Dereference__long_81_base& baseObject = CWE476_NULL_Pointer_Dereference__long_81_goodG2B();\r\n baseObject.action(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n long * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n const CWE476_NULL_Pointer_Dereference__long_81_base& baseObject = CWE476_NULL_Pointer_Dereference__long_81_goodB2G();\r\n baseObject.action(data);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE476_NULL_Pointer_Dereference__long_81; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__null_check_after_deref_05.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__null_check_after_deref_05.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":3531,"source_sha256":"cca99ea279b4451cd854e361278737ab6e07ec010ff3ac65b460ede794047196","line_count":120,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__null_check_after_deref_05.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.pointflaw.label.xml\r\nTemplate File: point-flaw-05.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * Sinks: null_check_after_deref\r\n * GoodSink: Do not check for NULL after the pointer has been dereferenced\r\n * BadSink : Check for NULL after a pointer has already been dereferenced\r\n * Flow Variant: 05 Control flow: if(staticTrue) and if(staticFalse)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n/* The two variables below are not defined as \"const\", but are never\r\n assigned any other value, so a tool should be able to identify that\r\n reads of these will always return their initialized values. */\r\nstatic int staticTrue = 1; /* true */\r\nstatic int staticFalse = 0; /* false */\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__null_check_after_deref_05_bad()\r\n{\r\n if(staticTrue)\r\n {\r\n {\r\n int *intPointer = NULL;\r\n intPointer = (int *)malloc(sizeof(int));\r\n *intPointer = 5;\r\n printIntLine(*intPointer);\r\n /* FLAW: Check for NULL after dereferencing the pointer. This NULL check is unnecessary. */\r\n if (intPointer != NULL)\r\n {\r\n *intPointer = 10;\r\n }\r\n printIntLine(*intPointer);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good1() uses if(staticFalse) instead of if(staticTrue) */\r\nstatic void good1()\r\n{\r\n if(staticFalse)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n {\r\n int *intPointer = NULL;\r\n intPointer = (int *)malloc(sizeof(int));\r\n *intPointer = 5;\r\n printIntLine(*intPointer);\r\n /* FIX: Don't check for NULL since we wouldn't reach this line if the pointer was NULL */\r\n *intPointer = 10;\r\n printIntLine(*intPointer);\r\n }\r\n }\r\n}\r\n\r\n/* good2() reverses the bodies in the if statement */\r\nstatic void good2()\r\n{\r\n if(staticTrue)\r\n {\r\n {\r\n int *intPointer = NULL;\r\n intPointer = (int *)malloc(sizeof(int));\r\n *intPointer = 5;\r\n printIntLine(*intPointer);\r\n /* FIX: Don't check for NULL since we wouldn't reach this line if the pointer was NULL */\r\n *intPointer = 10;\r\n printIntLine(*intPointer);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__null_check_after_deref_05_good()\r\n{\r\n good1();\r\n good2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__null_check_after_deref_05_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__null_check_after_deref_05_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__null_check_after_deref_09.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__null_check_after_deref_09.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":3289,"source_sha256":"c01b1f88ae22b463c4ce031f86d624118a0b4865cb7ef5678db21c7edfda8db0","line_count":114,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__null_check_after_deref_09.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.pointflaw.label.xml\r\nTemplate File: point-flaw-09.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * Sinks: null_check_after_deref\r\n * GoodSink: Do not check for NULL after the pointer has been dereferenced\r\n * BadSink : Check for NULL after a pointer has already been dereferenced\r\n * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__null_check_after_deref_09_bad()\r\n{\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n {\r\n int *intPointer = NULL;\r\n intPointer = (int *)malloc(sizeof(int));\r\n *intPointer = 5;\r\n printIntLine(*intPointer);\r\n /* FLAW: Check for NULL after dereferencing the pointer. This NULL check is unnecessary. */\r\n if (intPointer != NULL)\r\n {\r\n *intPointer = 10;\r\n }\r\n printIntLine(*intPointer);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good1() uses if(GLOBAL_CONST_FALSE) instead of if(GLOBAL_CONST_TRUE) */\r\nstatic void good1()\r\n{\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n {\r\n int *intPointer = NULL;\r\n intPointer = (int *)malloc(sizeof(int));\r\n *intPointer = 5;\r\n printIntLine(*intPointer);\r\n /* FIX: Don't check for NULL since we wouldn't reach this line if the pointer was NULL */\r\n *intPointer = 10;\r\n printIntLine(*intPointer);\r\n }\r\n }\r\n}\r\n\r\n/* good2() reverses the bodies in the if statement */\r\nstatic void good2()\r\n{\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n {\r\n int *intPointer = NULL;\r\n intPointer = (int *)malloc(sizeof(int));\r\n *intPointer = 5;\r\n printIntLine(*intPointer);\r\n /* FIX: Don't check for NULL since we wouldn't reach this line if the pointer was NULL */\r\n *intPointer = 10;\r\n printIntLine(*intPointer);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__null_check_after_deref_09_good()\r\n{\r\n good1();\r\n good2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__null_check_after_deref_09_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__null_check_after_deref_09_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__null_check_after_deref_11.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__null_check_after_deref_11.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":3303,"source_sha256":"4881ec9fe9832eac28914967006a649e94c39eab7858583b017a626fce9bc627","line_count":114,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__null_check_after_deref_11.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.pointflaw.label.xml\r\nTemplate File: point-flaw-11.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * Sinks: null_check_after_deref\r\n * GoodSink: Do not check for NULL after the pointer has been dereferenced\r\n * BadSink : Check for NULL after a pointer has already been dereferenced\r\n * Flow Variant: 11 Control flow: if(globalReturnsTrue()) and if(globalReturnsFalse())\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__null_check_after_deref_11_bad()\r\n{\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n int *intPointer = NULL;\r\n intPointer = (int *)malloc(sizeof(int));\r\n *intPointer = 5;\r\n printIntLine(*intPointer);\r\n /* FLAW: Check for NULL after dereferencing the pointer. This NULL check is unnecessary. */\r\n if (intPointer != NULL)\r\n {\r\n *intPointer = 10;\r\n }\r\n printIntLine(*intPointer);\r\n }\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good1() uses if(globalReturnsFalse()) instead of if(globalReturnsTrue()) */\r\nstatic void good1()\r\n{\r\n if(globalReturnsFalse())\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n {\r\n int *intPointer = NULL;\r\n intPointer = (int *)malloc(sizeof(int));\r\n *intPointer = 5;\r\n printIntLine(*intPointer);\r\n /* FIX: Don't check for NULL since we wouldn't reach this line if the pointer was NULL */\r\n *intPointer = 10;\r\n printIntLine(*intPointer);\r\n }\r\n }\r\n}\r\n\r\n/* good2() reverses the bodies in the if statement */\r\nstatic void good2()\r\n{\r\n if(globalReturnsTrue())\r\n {\r\n {\r\n int *intPointer = NULL;\r\n intPointer = (int *)malloc(sizeof(int));\r\n *intPointer = 5;\r\n printIntLine(*intPointer);\r\n /* FIX: Don't check for NULL since we wouldn't reach this line if the pointer was NULL */\r\n *intPointer = 10;\r\n printIntLine(*intPointer);\r\n }\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__null_check_after_deref_11_good()\r\n{\r\n good1();\r\n good2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__null_check_after_deref_11_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__null_check_after_deref_11_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__struct_21.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__struct_21.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":4179,"source_sha256":"f20ac8d06b3d84a56aa200ee25877ea6120b8684ed4e6301e0e7302cb7e027be","line_count":168,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__struct_21.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-21.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 21 Control flow: Flow controlled by value of a static global variable. All functions contained in one file.\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* The static variable below is used to drive control flow in the sink function */\r\nstatic int badStatic = 0;\r\n\r\nstatic void badSink(twoIntsStruct * data)\r\n{\r\n if(badStatic)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__struct_21_bad()\r\n{\r\n twoIntsStruct * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n badStatic = 1; /* true */\r\n badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* The static variables below are used to drive control flow in the sink functions. */\r\nstatic int goodB2G1Static = 0;\r\nstatic int goodB2G2Static = 0;\r\nstatic int goodG2BStatic = 0;\r\n\r\n/* goodB2G1() - use badsource and goodsink by setting the static variable to false instead of true */\r\nstatic void goodB2G1Sink(twoIntsStruct * data)\r\n{\r\n if(goodB2G1Static)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(data->intOne);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n goodB2G1Static = 0; /* false */\r\n goodB2G1Sink(data);\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */\r\nstatic void goodB2G2Sink(twoIntsStruct * data)\r\n{\r\n if(goodB2G2Static)\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(data->intOne);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n goodB2G2Static = 1; /* true */\r\n goodB2G2Sink(data);\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink */\r\nstatic void goodG2BSink(twoIntsStruct * data)\r\n{\r\n if(goodG2BStatic)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n }\r\n}\r\n\r\nstatic void goodG2B()\r\n{\r\n twoIntsStruct * data;\n twoIntsStruct tmpData;\r\n /* FIX: Initialize data */\r\n {\n tmpData.intOne = 0;\r\n tmpData.intTwo = 0;\r\n data = &tmpData;\r\n }\r\n goodG2BStatic = 1; /* true */\r\n goodG2BSink(data);\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__struct_21_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__struct_21_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__struct_21_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__struct_22a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__struct_22a.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":3831,"source_sha256":"759905ea260567c7853b4193904180721276e66cf4c958fa772b308578c2ad71","line_count":121,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__struct_22a.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-22a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 22 Control flow: Flow controlled by value of a global variable. Sink functions are in a separate file from sources.\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* The global variable below is used to drive control flow in the sink function */\r\nint CWE476_NULL_Pointer_Dereference__struct_22_badGlobal = 0;\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__struct_22_badSink(twoIntsStruct * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__struct_22_bad()\r\n{\r\n twoIntsStruct * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n CWE476_NULL_Pointer_Dereference__struct_22_badGlobal = 1; /* true */\r\n CWE476_NULL_Pointer_Dereference__struct_22_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* The global variables below are used to drive control flow in the sink functions. */\r\nint CWE476_NULL_Pointer_Dereference__struct_22_goodB2G1Global = 0;\r\nint CWE476_NULL_Pointer_Dereference__struct_22_goodB2G2Global = 0;\r\nint CWE476_NULL_Pointer_Dereference__struct_22_goodG2BGlobal = 0;\r\n\r\n/* goodB2G1() - use badsource and goodsink by setting the static variable to false instead of true */\r\nvoid CWE476_NULL_Pointer_Dereference__struct_22_goodB2G1Sink(twoIntsStruct * data);\r\n\r\nstatic void goodB2G1()\r\n{\r\n twoIntsStruct * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n CWE476_NULL_Pointer_Dereference__struct_22_goodB2G1Global = 0; /* false */\r\n CWE476_NULL_Pointer_Dereference__struct_22_goodB2G1Sink(data);\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the if in the sink function */\r\nvoid CWE476_NULL_Pointer_Dereference__struct_22_goodB2G2Sink(twoIntsStruct * data);\r\n\r\nstatic void goodB2G2()\r\n{\r\n twoIntsStruct * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n CWE476_NULL_Pointer_Dereference__struct_22_goodB2G2Global = 1; /* true */\r\n CWE476_NULL_Pointer_Dereference__struct_22_goodB2G2Sink(data);\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink */\r\nvoid CWE476_NULL_Pointer_Dereference__struct_22_goodG2BSink(twoIntsStruct * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n twoIntsStruct * data;\n twoIntsStruct tmpData;\r\n /* FIX: Initialize data */\r\n {\n tmpData.intOne = 0;\r\n tmpData.intTwo = 0;\r\n data = &tmpData;\r\n }\r\n CWE476_NULL_Pointer_Dereference__struct_22_goodG2BGlobal = 1; /* true */\r\n CWE476_NULL_Pointer_Dereference__struct_22_goodG2BSink(data);\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__struct_22_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__struct_22_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__struct_22_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__struct_33.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__struct_33.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":2889,"source_sha256":"91f9e2a0868e5a08e07603c624cb47ad93b7941dd9f9ee2b3f91800f585fd27a","line_count":119,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__struct_33.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-33.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 33 Data flow: use of a C++ reference to data within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__struct_33\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n twoIntsStruct * data;\r\n twoIntsStruct * &dataRef = data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n {\r\n twoIntsStruct * data = dataRef;\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n twoIntsStruct * data;\n twoIntsStruct tmpData;\r\n twoIntsStruct * &dataRef = data;\r\n /* FIX: Initialize data */\r\n {\n tmpData.intOne = 0;\r\n tmpData.intTwo = 0;\r\n data = &tmpData;\r\n }\r\n {\r\n twoIntsStruct * data = dataRef;\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n twoIntsStruct * data;\r\n twoIntsStruct * &dataRef = data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n {\r\n twoIntsStruct * data = dataRef;\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(data->intOne);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE476_NULL_Pointer_Dereference__struct_33; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__struct_52c.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__struct_52c.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":1484,"source_sha256":"fc21103201a40e2a33b8576ed23bb539dbfbc7e6043c2197910fcb6bc9d58e4b","line_count":55,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__struct_52c.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-52c.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 52 Data flow: data passed as an argument from one function to another to another in three different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__struct_52c_badSink(twoIntsStruct * data)\r\n{\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE476_NULL_Pointer_Dereference__struct_52c_goodG2BSink(twoIntsStruct * data)\r\n{\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n printIntLine(data->intOne);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE476_NULL_Pointer_Dereference__struct_52c_goodB2GSink(twoIntsStruct * data)\r\n{\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(data->intOne);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__struct_54b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__struct_54b.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":1606,"source_sha256":"b15c441b28efc31f64a164898c686773d7b913922e0fc98e43bbc91f6bd2f49d","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__struct_54b.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-54b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE476_NULL_Pointer_Dereference__struct_54c_badSink(twoIntsStruct * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__struct_54b_badSink(twoIntsStruct * data)\r\n{\r\n CWE476_NULL_Pointer_Dereference__struct_54c_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE476_NULL_Pointer_Dereference__struct_54c_goodG2BSink(twoIntsStruct * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__struct_54b_goodG2BSink(twoIntsStruct * data)\r\n{\r\n CWE476_NULL_Pointer_Dereference__struct_54c_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE476_NULL_Pointer_Dereference__struct_54c_goodB2GSink(twoIntsStruct * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__struct_54b_goodB2GSink(twoIntsStruct * data)\r\n{\r\n CWE476_NULL_Pointer_Dereference__struct_54c_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__struct_54d.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__struct_54d.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":1606,"source_sha256":"147a951cf269a713bb565485304ed5a9a73ecc53688511377cb4270222b24d3b","line_count":52,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__struct_54d.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-54d.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE476_NULL_Pointer_Dereference__struct_54e_badSink(twoIntsStruct * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__struct_54d_badSink(twoIntsStruct * data)\r\n{\r\n CWE476_NULL_Pointer_Dereference__struct_54e_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE476_NULL_Pointer_Dereference__struct_54e_goodG2BSink(twoIntsStruct * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__struct_54d_goodG2BSink(twoIntsStruct * data)\r\n{\r\n CWE476_NULL_Pointer_Dereference__struct_54e_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE476_NULL_Pointer_Dereference__struct_54e_goodB2GSink(twoIntsStruct * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__struct_54d_goodB2GSink(twoIntsStruct * data)\r\n{\r\n CWE476_NULL_Pointer_Dereference__struct_54e_goodB2GSink(data);\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__struct_67a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__struct_67a.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":3264,"source_sha256":"38df6da19798feabed661b9a63edbce07f1ac3d9c42a59b86993c6b8823d7935","line_count":109,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__struct_67a.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-67a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 67 Data flow: data passed in a struct from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\ntypedef struct _CWE476_NULL_Pointer_Dereference__struct_67_structType\r\n{\r\n twoIntsStruct * structFirst;\r\n} CWE476_NULL_Pointer_Dereference__struct_67_structType;\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE476_NULL_Pointer_Dereference__struct_67b_badSink(CWE476_NULL_Pointer_Dereference__struct_67_structType myStruct);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__struct_67_bad()\r\n{\r\n twoIntsStruct * data;\r\n CWE476_NULL_Pointer_Dereference__struct_67_structType myStruct;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n myStruct.structFirst = data;\r\n CWE476_NULL_Pointer_Dereference__struct_67b_badSink(myStruct);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE476_NULL_Pointer_Dereference__struct_67b_goodG2BSink(CWE476_NULL_Pointer_Dereference__struct_67_structType myStruct);\r\n\r\nstatic void goodG2B()\r\n{\r\n twoIntsStruct * data;\n twoIntsStruct tmpData;\r\n CWE476_NULL_Pointer_Dereference__struct_67_structType myStruct;\r\n /* FIX: Initialize data */\r\n {\n tmpData.intOne = 0;\r\n tmpData.intTwo = 0;\r\n data = &tmpData;\r\n }\r\n myStruct.structFirst = data;\r\n CWE476_NULL_Pointer_Dereference__struct_67b_goodG2BSink(myStruct);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE476_NULL_Pointer_Dereference__struct_67b_goodB2GSink(CWE476_NULL_Pointer_Dereference__struct_67_structType myStruct);\r\n\r\nstatic void goodB2G()\r\n{\r\n twoIntsStruct * data;\r\n CWE476_NULL_Pointer_Dereference__struct_67_structType myStruct;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n myStruct.structFirst = data;\r\n CWE476_NULL_Pointer_Dereference__struct_67b_goodB2GSink(myStruct);\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__struct_67_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__struct_67_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__struct_67_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__struct_68a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__struct_68a.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":3035,"source_sha256":"0d3bfa55cc966899eb3a4cec9f0e75c29f906c17699a2301ee44696d3bfb84b3","line_count":105,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__struct_68a.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-68a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 68 Data flow: data passed as a global variable from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\ntwoIntsStruct * CWE476_NULL_Pointer_Dereference__struct_68_badData;\r\ntwoIntsStruct * CWE476_NULL_Pointer_Dereference__struct_68_goodG2BData;\r\ntwoIntsStruct * CWE476_NULL_Pointer_Dereference__struct_68_goodB2GData;\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE476_NULL_Pointer_Dereference__struct_68b_badSink();\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__struct_68_bad()\r\n{\r\n twoIntsStruct * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n CWE476_NULL_Pointer_Dereference__struct_68_badData = data;\r\n CWE476_NULL_Pointer_Dereference__struct_68b_badSink();\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* good function declarations */\r\nvoid CWE476_NULL_Pointer_Dereference__struct_68b_goodG2BSink();\r\nvoid CWE476_NULL_Pointer_Dereference__struct_68b_goodB2GSink();\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n twoIntsStruct * data;\n twoIntsStruct tmpData;\r\n /* FIX: Initialize data */\r\n {\n tmpData.intOne = 0;\r\n tmpData.intTwo = 0;\r\n data = &tmpData;\r\n }\r\n CWE476_NULL_Pointer_Dereference__struct_68_goodG2BData = data;\r\n CWE476_NULL_Pointer_Dereference__struct_68b_goodG2BSink();\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n twoIntsStruct * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n CWE476_NULL_Pointer_Dereference__struct_68_goodB2GData = data;\r\n CWE476_NULL_Pointer_Dereference__struct_68b_goodB2GSink();\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__struct_68_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__struct_68_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__struct_68_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__struct_82_goodB2G.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__struct_82_goodB2G.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":1045,"source_sha256":"c4b6406dfc0fc8a2f2f480568f5c41b79d26455836cf9ed049b4c6c13cfdfe78","line_count":39,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__struct_82_goodB2G.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-82_goodB2G.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 82 Data flow: data passed in a parameter to an virtual method called via a pointer\r\n *\r\n * */\r\n#ifndef OMITGOOD\r\n\r\n#include \"std_testcase.h\"\r\n#include \"CWE476_NULL_Pointer_Dereference__struct_82.h\"\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__struct_82\r\n{\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__struct_82_goodB2G::action(twoIntsStruct * data)\r\n{\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n printIntLine(data->intOne);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n}\r\n\r\n}\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__wchar_t_01.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__wchar_t_01.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":2549,"source_sha256":"b5b36d839514c936c048590a2c41f16f8ec6e83a3ac4535934281d60a3c2e6b2","line_count":99,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__wchar_t_01.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-01.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 01 Baseline\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__wchar_t_01_bad()\r\n{\r\n wchar_t * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n /* FIX: Initialize data */\r\n data = L\"Good\";\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__wchar_t_01_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__wchar_t_01_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__wchar_t_01_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__wchar_t_04.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__wchar_t_04.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":4705,"source_sha256":"477b447d0bfbdd381ef8c55cb62469f5cfbf374cb861386c14a946ec4bb460b2","line_count":176,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__wchar_t_04.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-04.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 04 Control flow: if(STATIC_CONST_TRUE) and if(STATIC_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n/* The two variables below are declared \"const\", so a tool should\r\n be able to identify that reads of these will always return their\r\n initialized values. */\r\nstatic const int STATIC_CONST_TRUE = 1; /* true */\r\nstatic const int STATIC_CONST_FALSE = 0; /* false */\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__wchar_t_04_bad()\r\n{\r\n wchar_t * data;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second STATIC_CONST_TRUE to STATIC_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n wchar_t * data;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(STATIC_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n wchar_t * data;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first STATIC_CONST_TRUE to STATIC_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n if(STATIC_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Initialize data */\r\n data = L\"Good\";\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* FIX: Initialize data */\r\n data = L\"Good\";\r\n }\r\n if(STATIC_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__wchar_t_04_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__wchar_t_04_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__wchar_t_04_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__wchar_t_09.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__wchar_t_09.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":4434,"source_sha256":"5b248432dcda61db9c3896fba5754e020104226940bb95f330aaedaa2faf64ac","line_count":170,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__wchar_t_09.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-09.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 09 Control flow: if(GLOBAL_CONST_TRUE) and if(GLOBAL_CONST_FALSE)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__wchar_t_09_bad()\r\n{\r\n wchar_t * data;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodB2G1()\r\n{\r\n wchar_t * data;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second if */\r\nstatic void goodB2G2()\r\n{\r\n wchar_t * data;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first GLOBAL_CONST_TRUE to GLOBAL_CONST_FALSE */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n if(GLOBAL_CONST_FALSE)\r\n {\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n }\r\n else\r\n {\r\n /* FIX: Initialize data */\r\n data = L\"Good\";\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first if */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* FIX: Initialize data */\r\n data = L\"Good\";\r\n }\r\n if(GLOBAL_CONST_TRUE)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__wchar_t_09_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__wchar_t_09_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__wchar_t_09_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__wchar_t_15.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__wchar_t_15.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":5757,"source_sha256":"1d9b96dfb51f298ab8a7bcbb2ec1bd474c2061e9df21a2e0a8502b6ee87a5fa3","line_count":220,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__wchar_t_15.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-15.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 15 Control flow: switch(6) and switch(7)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__wchar_t_15_bad()\r\n{\r\n wchar_t * data;\r\n switch(6)\r\n {\r\n case 6:\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G1() - use badsource and goodsink by changing the second switch to switch(8) */\r\nstatic void goodB2G1()\r\n{\r\n wchar_t * data;\r\n switch(6)\r\n {\r\n case 6:\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(8)\r\n {\r\n case 7:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n break;\r\n }\r\n}\r\n\r\n/* goodB2G2() - use badsource and goodsink by reversing the blocks in the second switch */\r\nstatic void goodB2G2()\r\n{\r\n wchar_t * data;\r\n switch(6)\r\n {\r\n case 6:\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B1() - use goodsource and badsink by changing the first switch to switch(5) */\r\nstatic void goodG2B1()\r\n{\r\n wchar_t * data;\r\n switch(5)\r\n {\r\n case 6:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n default:\r\n /* FIX: Initialize data */\r\n data = L\"Good\";\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B2() - use goodsource and badsink by reversing the blocks in the first switch */\r\nstatic void goodG2B2()\r\n{\r\n wchar_t * data;\r\n switch(6)\r\n {\r\n case 6:\r\n /* FIX: Initialize data */\r\n data = L\"Good\";\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n switch(7)\r\n {\r\n case 7:\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n break;\r\n default:\r\n /* INCIDENTAL: CWE 561 Dead Code, the code below will never run */\r\n printLine(\"Benign, fixed string\");\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__wchar_t_15_good()\r\n{\r\n goodB2G1();\r\n goodB2G2();\r\n goodG2B1();\r\n goodG2B2();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__wchar_t_15_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__wchar_t_15_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__wchar_t_16.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__wchar_t_16.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":3008,"source_sha256":"28f909f2ec3d2155b5ebcb6d600fbab3cf0af779828833f794c2a2c250a39b64","line_count":123,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__wchar_t_16.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-16.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 16 Control flow: while(1)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__wchar_t_16_bad()\r\n{\r\n wchar_t * data;\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n break;\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodB2G() - use badsource and goodsink by changing the sinks in the second while statement */\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n break;\r\n }\r\n}\r\n\r\n/* goodG2B() - use goodsource and badsink by changing the sources in the first while statement */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n while(1)\r\n {\r\n /* FIX: Initialize data */\r\n data = L\"Good\";\r\n break;\r\n }\r\n while(1)\r\n {\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n break;\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__wchar_t_16_good()\r\n{\r\n goodB2G();\r\n goodG2B();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__wchar_t_16_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__wchar_t_16_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__wchar_t_33.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__wchar_t_33.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":2940,"source_sha256":"51fa7450bc932741f673d3cd6c60d8d6d7652f1bb1f9b1f0386f75e6a008f2ef","line_count":117,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__wchar_t_33.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-33.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 33 Data flow: use of a C++ reference to data within the same function\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__wchar_t_33\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n wchar_t * &dataRef = data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n {\r\n wchar_t * data = dataRef;\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n wchar_t * &dataRef = data;\r\n /* FIX: Initialize data */\r\n data = L\"Good\";\r\n {\r\n wchar_t * data = dataRef;\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n wchar_t * &dataRef = data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n {\r\n wchar_t * data = dataRef;\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE476_NULL_Pointer_Dereference__wchar_t_33; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__wchar_t_34.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__wchar_t_34.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":3325,"source_sha256":"881678c8b08692eab1c27cfd358051e08dd6ca46821115047e8c741dc3c5cb52","line_count":119,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__wchar_t_34.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-34.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 34 Data flow: use of a union containing two methods of accessing the same data (within the same function)\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\ntypedef union\r\n{\r\n wchar_t * unionFirst;\r\n wchar_t * unionSecond;\r\n} CWE476_NULL_Pointer_Dereference__wchar_t_34_unionType;\r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__wchar_t_34_bad()\r\n{\r\n wchar_t * data;\r\n CWE476_NULL_Pointer_Dereference__wchar_t_34_unionType myUnion;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n myUnion.unionFirst = data;\r\n {\r\n wchar_t * data = myUnion.unionSecond;\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n }\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B() uses the GoodSource with the BadSink */\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n CWE476_NULL_Pointer_Dereference__wchar_t_34_unionType myUnion;\r\n /* FIX: Initialize data */\r\n data = L\"Good\";\r\n myUnion.unionFirst = data;\r\n {\r\n wchar_t * data = myUnion.unionSecond;\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n }\r\n}\r\n\r\n/* goodB2G() uses the BadSource with the GoodSink */\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n CWE476_NULL_Pointer_Dereference__wchar_t_34_unionType myUnion;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n myUnion.unionFirst = data;\r\n {\r\n wchar_t * data = myUnion.unionSecond;\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n }\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__wchar_t_34_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__wchar_t_34_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__wchar_t_34_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__wchar_t_54a.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__wchar_t_54a.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":2569,"source_sha256":"90a483a60809d632fada7ac5d3f198f2f2ffd09f4e1cdff3e3cff73c002f20d6","line_count":93,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__wchar_t_54a.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-54a.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 54 Data flow: data passed as an argument from one function through three others to a fifth; all five functions are in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid CWE476_NULL_Pointer_Dereference__wchar_t_54b_badSink(wchar_t * data);\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__wchar_t_54_bad()\r\n{\r\n wchar_t * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n CWE476_NULL_Pointer_Dereference__wchar_t_54b_badSink(data);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE476_NULL_Pointer_Dereference__wchar_t_54b_goodG2BSink(wchar_t * data);\r\n\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n /* FIX: Initialize data */\r\n data = L\"Good\";\r\n CWE476_NULL_Pointer_Dereference__wchar_t_54b_goodG2BSink(data);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE476_NULL_Pointer_Dereference__wchar_t_54b_goodB2GSink(wchar_t * data);\r\n\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n CWE476_NULL_Pointer_Dereference__wchar_t_54b_goodB2GSink(data);\r\n}\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__wchar_t_54_good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n CWE476_NULL_Pointer_Dereference__wchar_t_54_good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n CWE476_NULL_Pointer_Dereference__wchar_t_54_bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__wchar_t_64b.c","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__wchar_t_64b.c","cwe_id":"CWE476","file_extension":".c","language_hint":"C","source_size_bytes":2241,"source_sha256":"31e748171b6ed0ecae26be70028504ba1021fb2e6a0a274343d73fb281b3abd3","line_count":70,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__wchar_t_64b.c\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-64b.tmpl.c\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 64 Data flow: void pointer to data passed from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n\r\n#include \r\n\r\n#ifndef OMITBAD\r\n\r\nvoid CWE476_NULL_Pointer_Dereference__wchar_t_64b_badSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n wchar_t * * dataPtr = (wchar_t * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n wchar_t * data = (*dataPtr);\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid CWE476_NULL_Pointer_Dereference__wchar_t_64b_goodG2BSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n wchar_t * * dataPtr = (wchar_t * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n wchar_t * data = (*dataPtr);\r\n /* POTENTIAL FLAW: Attempt to use data, which may be NULL */\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid CWE476_NULL_Pointer_Dereference__wchar_t_64b_goodB2GSink(void * dataVoidPtr)\r\n{\r\n /* cast void pointer to a pointer of the appropriate type */\r\n wchar_t * * dataPtr = (wchar_t * *)dataVoidPtr;\r\n /* dereference dataPtr into data */\r\n wchar_t * data = (*dataPtr);\r\n /* FIX: Check for NULL before attempting to print data */\r\n if (data != NULL)\r\n {\r\n /* printWLine() checks for NULL, so we cannot use it here */\r\n printWcharLine(data[0]);\r\n }\r\n else\r\n {\r\n printLine(\"data is NULL\");\r\n }\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n"} {"sample_id":"juliet-1.3:C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__wchar_t_74a.cpp","source_dataset":"NIST SARD Juliet C/C++ 1.3","source_url":"https://samate.nist.gov/SARD/test-suites/112","archive_member":"C/testcases/CWE476_NULL_Pointer_Dereference/CWE476_NULL_Pointer_Dereference__wchar_t_74a.cpp","cwe_id":"CWE476","file_extension":".cpp","language_hint":"C++","source_size_bytes":2736,"source_sha256":"034f3ad8205ef92be4d69bbe5e52ac006b1cb4b4ef4177a91063cdefc54256bc","line_count":117,"label_status":"not_inferred_review_required","split":"unassigned","code":"/* TEMPLATE GENERATED TESTCASE FILE\r\nFilename: CWE476_NULL_Pointer_Dereference__wchar_t_74a.cpp\r\nLabel Definition File: CWE476_NULL_Pointer_Dereference.label.xml\r\nTemplate File: sources-sinks-74a.tmpl.cpp\r\n*/\r\n/*\r\n * @description\r\n * CWE: 476 NULL Pointer Dereference\r\n * BadSource: Set data to NULL\r\n * GoodSource: Initialize data\r\n * Sinks:\r\n * GoodSink: Check for NULL before attempting to print data\r\n * BadSink : Print data\r\n * Flow Variant: 74 Data flow: data passed in a map from one function to another in different source files\r\n *\r\n * */\r\n\r\n#include \"std_testcase.h\"\r\n#include \r\n\r\n#include \r\n\r\nusing namespace std;\r\n\r\nnamespace CWE476_NULL_Pointer_Dereference__wchar_t_74\r\n{\r\n\r\n#ifndef OMITBAD\r\n\r\n/* bad function declaration */\r\nvoid badSink(map dataMap);\r\n\r\nvoid bad()\r\n{\r\n wchar_t * data;\r\n map dataMap;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n /* Put data in a map */\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n badSink(dataMap);\r\n}\r\n\r\n#endif /* OMITBAD */\r\n\r\n#ifndef OMITGOOD\r\n\r\n/* goodG2B uses the GoodSource with the BadSink */\r\nvoid goodG2BSink(map dataMap);\r\n\r\nstatic void goodG2B()\r\n{\r\n wchar_t * data;\r\n map dataMap;\r\n /* FIX: Initialize data */\r\n data = L\"Good\";\r\n /* Put data in a map */\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n goodG2BSink(dataMap);\r\n}\r\n\r\n/* goodB2G uses the BadSource with the GoodSink */\r\nvoid goodB2GSink(map dataMap);\r\n\r\nstatic void goodB2G()\r\n{\r\n wchar_t * data;\r\n map dataMap;\r\n /* POTENTIAL FLAW: Set data to NULL */\r\n data = NULL;\r\n dataMap[0] = data;\r\n dataMap[1] = data;\r\n dataMap[2] = data;\r\n goodB2GSink(dataMap);\r\n}\r\n\r\nvoid good()\r\n{\r\n goodG2B();\r\n goodB2G();\r\n}\r\n\r\n#endif /* OMITGOOD */\r\n\r\n} /* close namespace */\r\n\r\n/* Below is the main(). It is only used when building this testcase on\r\n its own for testing or for building a binary to use in testing binary\r\n analysis tools. It is not used when compiling all the testcases as one\r\n application, which is how source code analysis tools are tested. */\r\n\r\n#ifdef INCLUDEMAIN\r\n\r\nusing namespace CWE476_NULL_Pointer_Dereference__wchar_t_74; /* so that we can use good and bad easily */\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n /* seed randomness */\r\n srand( (unsigned)time(NULL) );\r\n#ifndef OMITGOOD\r\n printLine(\"Calling good()...\");\r\n good();\r\n printLine(\"Finished good()\");\r\n#endif /* OMITGOOD */\r\n#ifndef OMITBAD\r\n printLine(\"Calling bad()...\");\r\n bad();\r\n printLine(\"Finished bad()\");\r\n#endif /* OMITBAD */\r\n return 0;\r\n}\r\n\r\n#endif\r\n"}