example/cuse_client.c: fix fd leakage problem
authorZhiqiang Liu <liuzhiqiang26@huawei.com>
Thu, 5 Nov 2020 10:16:58 +0000 (18:16 +0800)
committerNikolaus Rath <Nikolaus@rath.org>
Fri, 6 Nov 2020 19:26:03 +0000 (19:26 +0000)
In cuse_client.c, fd should be closed before return.
Otherwise, it will cause fd leakage problem.

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

index fe928d6027acc8b1d443b085220e6836826e6e8b..465302c5ef76e89637f924552a565fb402ec5015 100644 (file)
@@ -121,16 +121,17 @@ int main(int argc, char **argv)
                if (!argc) {
                        if (ioctl(fd, FIOC_GET_SIZE, &size)) {
                                perror("ioctl");
-                               return 1;
+                               goto error;
                        }
                        printf("%zu\n", size);
                } else {
                        size = param[0];
                        if (ioctl(fd, FIOC_SET_SIZE, &size)) {
                                perror("ioctl");
-                               return 1;
+                               goto error;
                        }
                }
+               close(fd);
                return 0;
 
        case 'r':
@@ -138,13 +139,18 @@ int main(int argc, char **argv)
                rc = do_rw(fd, cmd == 'r', param[0], param[1],
                           &prev_size, &new_size);
                if (rc < 0)
-                       return 1;
+                       goto error;
                fprintf(stderr, "transferred %d bytes (%zu -> %zu)\n",
                        rc, prev_size, new_size);
+               close(fd);
                return 0;
        }
 
- usage:
+usage:
        fprintf(stderr, "%s", usage);
        return 1;
+
+error:
+       close(fd);
+       return 1;
 }