Skip to content

Commit

Permalink
added test scenarios to test_valgrind.cc
Browse files Browse the repository at this point in the history
// BelongsTo: CS-581
  • Loading branch information
jgabler-hpc committed Sep 15, 2024
1 parent f0a9e80 commit 2774d1e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions test/misc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
26 changes: 25 additions & 1 deletion test/misc/test_valgrind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,38 @@
/*___INFO__MARK_END_NEW__*/

#include <iostream>
#include <cstring>

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

0 comments on commit 2774d1e

Please sign in to comment.