Message ID | 20250222114709.50011-1-ritvikfoss@gmail.com |
---|---|
State | Superseded |
Headers | show |
Series | selftests/mount: Close 'fd' when write fails | expand |
Yes, the kernel will handle the 'fd' cleanup automatically, but the existing implementation already closes it before exiting. However, in case where write fails, its unhandled. This patch addresses that gap :) Nevertheless it's subjective indeed. Thanks for reviewing! Regards Ritvik
> Yes, the kernel will handle the 'fd' cleanup automatically, but > the existing implementation already closes it before exiting. ^^^^^^^ Whoops! I meant 'returning' there. Wording issue on my part :P We're referring to the same thing! Thanks for detailed response :) Regards Ritvik
diff --git a/tools/testing/selftests/mount/unprivileged-remount-test.c b/tools/testing/selftests/mount/unprivileged-remount-test.c index d2917054fe3a..3dd9df58725b 100644 --- a/tools/testing/selftests/mount/unprivileged-remount-test.c +++ b/tools/testing/selftests/mount/unprivileged-remount-test.c @@ -54,6 +54,13 @@ static void die(char *fmt, ...) exit(EXIT_FAILURE); } +static void close_or_die(char *filename, int fd) { + if (close(fd) != 0) { + die("close of %s failed: %s\n", + filename, strerror(errno)); + } +} + static void vmaybe_write_file(bool enoent_ok, char *filename, char *fmt, va_list ap) { char buf[4096]; @@ -79,6 +86,7 @@ static void vmaybe_write_file(bool enoent_ok, char *filename, char *fmt, va_list } written = write(fd, buf, buf_len); if (written != buf_len) { + close_or_die(filename, fd); if (written >= 0) { die("short write to %s\n", filename); } else { @@ -86,10 +94,7 @@ static void vmaybe_write_file(bool enoent_ok, char *filename, char *fmt, va_list filename, strerror(errno)); } } - if (close(fd) != 0) { - die("close of %s failed: %s\n", - filename, strerror(errno)); - } + close_or_die(filename, fd); } static void maybe_write_file(char *filename, char *fmt, ...)