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; }