ioctl_client.c: fix potential fd leakage problem
authorZhiqiang Liu <liuzhiqiang26@huawei.com>
Thu, 5 Nov 2020 10:00:32 +0000 (18:00 +0800)
committerNikolaus Rath <Nikolaus@rath.org>
Fri, 6 Nov 2020 19:26:03 +0000 (19:26 +0000)
In ioctl_client.c, fd is not closed before return, thus
it will cause fd leakage problem.

Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
Signed-off-by: Haotian Li <lihaotian9@huawei.com>
example/ioctl_client.c

index d93f549225e3c7ca224da28261519ae0fa435082..dcb18ccdcf9c2f322d52eccdc1230b0dd3dd5c60 100644 (file)
@@ -41,6 +41,7 @@ int main(int argc, char **argv)
 {
        size_t size;
        int fd;
+       int ret = 0;
 
        if (argc < 2) {
                fprintf(stderr, "%s", usage);
@@ -56,15 +57,19 @@ int main(int argc, char **argv)
        if (argc == 2) {
                if (ioctl(fd, FIOC_GET_SIZE, &size)) {
                        perror("ioctl");
-                       return 1;
+                       ret = 1;
+                       goto out;
                }
                printf("%zu\n", size);
        } else {
                size = strtoul(argv[2], NULL, 0);
                if (ioctl(fd, FIOC_SET_SIZE, &size)) {
                        perror("ioctl");
-                       return 1;
+                       ret = 1;
+                       goto out;
                }
        }
-       return 0;
+out:
+       close(fd);
+       return ret;
 }