From 2774d1eec1e02c0da190a0dbcb2b60e855ccef36 Mon Sep 17 00:00:00 2001 From: Joachim Gabler Date: Sun, 15 Sep 2024 18:07:10 +0200 Subject: [PATCH] added test scenarios to test_valgrind.cc // BelongsTo: CS-581 --- test/misc/CMakeLists.txt | 1 + test/misc/test_valgrind.cc | 26 +++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/test/misc/CMakeLists.txt b/test/misc/CMakeLists.txt index c57953c7e..b438bbbfa 100644 --- a/test/misc/CMakeLists.txt +++ b/test/misc/CMakeLists.txt @@ -21,6 +21,7 @@ # test/misc add_executable(test_valgrind test_valgrind.cc) +add_compile_options(test_valgrind PRIVATE "-no-pedantic -Wno-error") if (INSTALL_SGE_TEST) install(TARGETS test_valgrind DESTINATION testbin/${SGE_ARCH}) diff --git a/test/misc/test_valgrind.cc b/test/misc/test_valgrind.cc index 89c37a212..813573419 100644 --- a/test/misc/test_valgrind.cc +++ b/test/misc/test_valgrind.cc @@ -19,14 +19,38 @@ /*___INFO__MARK_END_NEW__*/ #include +#include -void leak_scope(void) { +#pragma GCC diagnostic ignored "-Wuse-after-free" +void free_errors() { + std::cout << "accessing freed memory and freeing it twice" << std::endl; + const char *str = "hello"; + const char *dup = strdup(str); + std::cout << dup << std::endl; + free((void *)dup); + std::cout << dup << std::endl; + free((void *)dup); +} + +void memory_access_error() { + std::cout << "invalid access before and after allocated memory" << std::endl; + int array[10]{}; + // @todo valgrind doesn't catch these errors + array[10] = 42; + array[-1] = 3; + std::cout << array[0] << std::endl; +} + +void leak_scope() { std::cout << "allocating memory which will leak" << std::endl; int *leak = new int[10]; leak[0] = 42; + std::cout << leak[0] << std::endl; } int main() { leak_scope(); + memory_access_error(); + free_errors(); return 0; }