Resolve a resource leak
authordyamy-lee <dyamy.lee@samsung.com>
Mon, 26 Apr 2021 05:03:27 +0000 (14:03 +0900)
committerduna.oh <duna.oh@samsung.com>
Fri, 27 Jan 2023 05:46:34 +0000 (14:46 +0900)
fd going out of scope leaks the handle. So, it add close(fd) when it unfortunately return.

Change-Id: I00caca9166b30575503efbb41522bc2d5be8c2c4

haltests/test_hal_libinput_info.cpp

index 70d45fe..ce0fa1c 100644 (file)
@@ -2,6 +2,7 @@
 #include "test_hal_libinput_eventgen.h"
 #include "test_hal_libinput_mtdev_check.h"
 #include <limits.h>
+#include <errno.h>
 #include <libevdev/libevdev.h>
 #include <libevdev/libevdev-uinput.h>
 
@@ -28,6 +29,7 @@ int handle_device_notify(struct libinput_event *ev)
        struct udev_device *udev_device = libinput_device_get_udev_device(dev);
        enum libinput_event_type type;
        const char *devnode;
+       errno = 0;
 
        switch(libinput_event_get_type(ev)) {
        case LIBINPUT_EVENT_DEVICE_ADDED:
@@ -56,6 +58,10 @@ int handle_device_notify(struct libinput_event *ev)
                                devices[devices_cnt].cap = LIBINPUT_DEVICE_CAP_KEYBOARD;
                                devices[devices_cnt++].path = devnode;
                                int fd = open(devnode, O_RDWR);
+                               if (fd < 0) {
+                                       LOGE("ERROR: %m\n");
+                                       return -1;
+                               }
                                unsigned long evbits[NLONGS(EV_CNT)] = { 0 };
                                ioctl(fd, EVIOCGBIT(0, sizeof(evbits)), evbits);
                                if (!evdev_bit_is_set(evbits, EV_KEY))
@@ -66,6 +72,7 @@ int handle_device_notify(struct libinput_event *ev)
                                        if(evdev_bit_is_set(keybits, i)){
                                                break;
                                        }
+                               close(fd);
                        }
                }
        }
@@ -423,6 +430,7 @@ int create_mouse_event(struct libinput *li, int idx)
 {
        int fd = -1;
        int n = 0;
+       errno = 0;
 
        struct record_event rc_e;
        struct record_libinput_event_pointer p[] = {
@@ -435,7 +443,7 @@ int create_mouse_event(struct libinput *li, int idx)
                LOG("create_mouse_event\n");
                fd = open(devices[idx].path, O_RDWR);
                if (fd < 0) {
-                       LOGE("ERROR: could not open device\n");
+                       LOGE("ERROR: could not open device. %m\n");
                        return -1;
                }
 
@@ -464,6 +472,7 @@ int create_keyboard_event(struct libinput *li, int idx)
 {
        int fd = -1;
        int n = 0;
+       errno = 0;
 
        struct record_event rc_e;
        struct record_libinput_event_keyboard k[] = {
@@ -477,7 +486,7 @@ int create_keyboard_event(struct libinput *li, int idx)
                LOG("create_keyboard_event\n");
                fd = open(devices[idx].path, O_RDWR);
                if (fd < 0) {
-                       LOGE("ERROR: could not open device\n");
+                       LOGE("ERROR: could not open device. %m\n");
                        return -1;
                }
 
@@ -505,22 +514,24 @@ int create_keyboard_event(struct libinput *li, int idx)
 int multi_touch_support(struct libinput *li, int idx)
 {
        int fd = -1;
+       errno = 0;
 
        if(devices[idx].cap == LIBINPUT_DEVICE_CAP_TOUCH){
                LOG("multi_touch_support\n");
                fd = open(devices[idx].path, O_RDONLY | O_NONBLOCK);
                if (fd < 0) {
-                       LOGE("ERROR: could not open device\n");
+                       LOGE("ERROR: could not open device. %m\n");
                        return -1;
                }
                if (ioctl(fd, EVIOCGRAB, 1)) {
-                       LOGE("ERROR: could not grab the device\n");
+                       close(fd);
+                       LOGE("ERROR: could not grab the device. %m\n");
                        return -1;
                }
                if (check_device_mtprops(fd) < 0) {
                        ioctl(fd, EVIOCGRAB, 0);
                        close(fd);
-                       LOGE("ERROR: NO mt must props\n");
+                       LOGE("ERROR: NO mt must props. %m\n");
                        return -1;
                }
                ioctl(fd, EVIOCGRAB, 0);
@@ -535,6 +546,7 @@ int create_touch_event(struct libinput *li, int idx)
 {
        int fd = -1;
        int n = 0;
+       errno = 0;
 
        struct record_event rc_e;
        struct record_libinput_event_touch t[] = {
@@ -547,7 +559,7 @@ int create_touch_event(struct libinput *li, int idx)
                LOG("create_touch_event\n");
                fd = open(devices[idx].path, O_RDWR);
                if (fd < 0) {
-                       LOGE("ERROR: could not open device\n");
+                       LOGE("ERROR: could not open device. %m\n");
                        return -1;
                }