From 0b873d1d7829dedbce47c95ef0247f95e2195643 Mon Sep 17 00:00:00 2001 From: joyfaker Date: Fri, 12 Dec 2025 15:56:35 +0800 Subject: [PATCH] Add files via upload Signed-off-by: joyfaker --- tools/macro_builder/cpp_security_test.cpp | 54 +++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 tools/macro_builder/cpp_security_test.cpp diff --git a/tools/macro_builder/cpp_security_test.cpp b/tools/macro_builder/cpp_security_test.cpp new file mode 100644 index 0000000000..0b4f8d3773 --- /dev/null +++ b/tools/macro_builder/cpp_security_test.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include + +#define BUFFER_SIZE 10 + +void bufferOverflowVuln(const char* input) { + char buffer[BUFFER_SIZE]; + strcpy(buffer, input); + std::cout << "Vulnerable Buffer: " << buffer << std::endl; +} + +void memoryLeakVuln() { + int* data = new int[100]; + if (data == nullptr) return; + + data[0] = 1; + std::cout << "Memory allocated and leaked." << std::endl; +} + +int integerOverflowVuln(int a, int b) { + return a + b; +} + +void formatStringVuln(const char* logMessage) { + printf(logMessage); + printf("\n"); +} + +void bufferOverflowSafe(const char* input) { + char buffer[BUFFER_SIZE]; + strncpy(buffer, input, BUFFER_SIZE - 1); + buffer[BUFFER_SIZE - 1] = '\0'; + std::cout << "Safe Buffer: " << buffer << std::endl; +} + +int main_cwe_test(int argc, char** argv) { + if (argc < 2) { + std::cerr << "Usage: " << argv[0] << " " << std::endl; + return 1; + } + + const char* user_input = argv[1]; + + bufferOverflowVuln(user_input); + memoryLeakVuln(); + int result = integerOverflowVuln(2147483647, 1); + formatStringVuln(user_input); + + bufferOverflowSafe(user_input); + + return 0; +} \ No newline at end of file