Fix: memory and fd leaks in error counter
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 15 Dec 2020 14:30:53 +0000 (09:30 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 24 Feb 2021 15:14:42 +0000 (10:14 -0500)
Use a regular pattern for all commands:

If the command callback takes ownership of a pointer or file descriptor,
it sets them to NULL or -1. Therefore, the caller can always try to free
the pointer, or close it if it is greater or equal to 0.

This eliminates memory and fd leaks on error.

Change-Id: I4916920095526c166636c6a432b95fdd6fd1ca4b
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
liblttng-ust/lttng-ust-comm.c

index 682992ca3d46fe719a8b22cfb761fd86ea47fa70..8d90e2c367dafae1a88b8f077208a8fb0596e82d 100644 (file)
@@ -1203,6 +1203,7 @@ int handle_message(struct sock_info *sock_info,
                                        &args, sock_info);
                else
                        ret = -ENOSYS;
+               free(args.counter.counter_data);
                break;
        }
        case LTTNG_UST_COUNTER_GLOBAL:
@@ -1220,6 +1221,16 @@ int handle_message(struct sock_info *sock_info,
                                        &args, sock_info);
                else
                        ret = -ENOSYS;
+               if (args.counter_shm.shm_fd >= 0) {
+                       int close_ret;
+
+                       lttng_ust_lock_fd_tracker();
+                       close_ret = close(args.counter_shm.shm_fd);
+                       lttng_ust_unlock_fd_tracker();
+                       args.counter_shm.shm_fd = -1;
+                       if (close_ret)
+                               PERROR("close");
+               }
                break;
        }
        case LTTNG_UST_COUNTER_CPU:
@@ -1237,6 +1248,16 @@ int handle_message(struct sock_info *sock_info,
                                        &args, sock_info);
                else
                        ret = -ENOSYS;
+               if (args.counter_shm.shm_fd >= 0) {
+                       int close_ret;
+
+                       lttng_ust_lock_fd_tracker();
+                       close_ret = close(args.counter_shm.shm_fd);
+                       lttng_ust_unlock_fd_tracker();
+                       args.counter_shm.shm_fd = -1;
+                       if (close_ret)
+                               PERROR("close");
+               }
                break;
        }
        case LTTNG_UST_EVENT_NOTIFIER_CREATE:
This page took 0.026071 seconds and 4 git commands to generate.