]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
test/on_exit: use static variables for on_exit hooks 56596/head
authorKefu Chai <tchaikov@gmail.com>
Sun, 31 Mar 2024 01:14:21 +0000 (09:14 +0800)
committerKefu Chai <tchaikov@gmail.com>
Sun, 31 Mar 2024 08:44:34 +0000 (16:44 +0800)
before this change, we allocate memory chunks using malloc(), but
we never free them. and LeakSanitizer points this out

```
Direct leak of 4 byte(s) in 1 object(s) allocated from:
    #0 0x5588bfe532de in malloc (/home/jenkins-build/build/workspace/ceph-pull-requests/build/bin/unittest_on_exit+0xa52de) (BuildId: 7c7a92bf5719592938c5307214bcd9b080bd847f)
    #1 0x5588bfe911d7 in func_scope() /home/jenkins-build/build/workspace/ceph-pull-requests/src/test/on_exit.cc:33:22
    #2 0x5588bfe90804 in main /home/jenkins-build/build/workspace/ceph-pull-requests/src/test/on_exit.cc:64:3
    #3 0x7f23081c1d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16

Direct leak of 4 byte(s) in 1 object(s) allocated from:
    #0 0x5588bfe532de in malloc (/home/jenkins-build/build/workspace/ceph-pull-requests/build/bin/unittest_on_exit+0xa52de) (BuildId: 7c7a92bf5719592938c5307214bcd9b080bd847f)
    #1 0x5588bfe91160 in func_scope() /home/jenkins-build/build/workspace/ceph-pull-requests/src/test/on_exit.cc:29:22
    #2 0x5588bfe90804 in main /home/jenkins-build/build/workspace/ceph-pull-requests/src/test/on_exit.cc:64:3
    #3 0x7f23081c1d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
```

in this change, instead of allocating the variables using `malloc()`,
we keep them in static variables, so that they can be accessed by
`OnExitManager` even if it is a static variable.
with this change, the memory leak reports for this source file go away.

Signed-off-by: Kefu Chai <tchaikov@gmail.com>
src/test/on_exit.cc

index aec1f78b787c16228f4a5e7c6e11360b89770f52..27ba3e49c4144b5cd9e05357c0b9137cfdbc8f64 100644 (file)
@@ -26,13 +26,11 @@ static void func_scope(void)
 {
   OnExitManager mgr;
 
-  int *inc_1 = (int*)malloc(sizeof(*inc_1));
-  *inc_1 = 5;
-  mgr.add_callback(add, inc_1);
+  static int inc_1 = 5;
+  mgr.add_callback(add, &inc_1);
 
-  int *inc_2 = (int*)malloc(sizeof(*inc_2));
-  *inc_2 = 3;
-  mgr.add_callback(add, inc_2);
+  static int inc_2 = 3;
+  mgr.add_callback(add, &inc_2);
 }
 
 // shared between processes
@@ -84,9 +82,8 @@ int main(int argc, char **argv)
     // exits by returning from main. The parent checks the value after the
     // child exits via the memory map.
     ceph_assert(*shared_val == 0);
-    int *new_val = (int*)malloc(sizeof(*new_val));
-    *new_val = MAIN_SCOPE_VAL;
-    main_scope_mgr.add_callback(main_scope_cb, new_val);
+    static int new_val = MAIN_SCOPE_VAL;
+    main_scope_mgr.add_callback(main_scope_cb, &new_val);
     return 0;
   }
 
@@ -104,9 +101,8 @@ int main(int argc, char **argv)
     // child adds a callback to the static scope callback manager and then
     // exits via exit().
     ceph_assert(*shared_val == 0);
-    int *new_val = (int*)malloc(sizeof(*new_val));
-    *new_val = EXIT_FUNC_VAL;
-    exit_func_mgr.add_callback(exit_func_cb, new_val);
+    static int new_val = EXIT_FUNC_VAL;
+    exit_func_mgr.add_callback(exit_func_cb, &new_val);
     call_exit();
     ceph_abort();
   }