diff mbox series

[v2,1/2] fs: avoid fdput() after failed fdget() in ksys_sync_file_range()

Message ID 71cc3966f60f884924f9dff8875ed478e858dca1.1589311577.git.skhan@linuxfoundation.org (mailing list archive)
State New, archived
Headers show
Series fs: avoid fdput() after failed fdget() | expand

Commit Message

Shuah Khan May 12, 2020, 7:43 p.m. UTC
Fix ksys_sync_file_range() to avoid fdput() after a failed fdget().
fdput() doesn't do fput() on this file since FDPUT_FPUT isn't set
in fd.flags.

Change it anyway since failed fdget() doesn't require a fdput(). Refine
the code path a bit for it to read more clearly.
Reference: commit 22f96b3808c1 ("fs: add sync_file_range() helper")

Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
---
 fs/sync.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

Comments

Al Viro May 13, 2020, 5:46 a.m. UTC | #1
On Tue, May 12, 2020 at 01:43:04PM -0600, Shuah Khan wrote:

> @@ -364,15 +364,15 @@ int sync_file_range(struct file *file, loff_t offset, loff_t nbytes,
>  int ksys_sync_file_range(int fd, loff_t offset, loff_t nbytes,
>  			 unsigned int flags)
>  {
> -	int ret;
> -	struct fd f;
> +	int ret = -EBADF;
> +	struct fd f = fdget(fd);
>  
> -	ret = -EBADF;
> -	f = fdget(fd);
> -	if (f.file)
> -		ret = sync_file_range(f.file, offset, nbytes, flags);
> +	if (!f.file)
> +		goto out;
>  
> +	ret = sync_file_range(f.file, offset, nbytes, flags);
>  	fdput(f);
> +out:
>  	return ret;

IDGI...  What's the point of that goto out, when it leads straight to return?
diff mbox series

Patch

diff --git a/fs/sync.c b/fs/sync.c
index 4d1ff010bc5a..300ca73ec87c 100644
--- a/fs/sync.c
+++ b/fs/sync.c
@@ -364,15 +364,15 @@  int sync_file_range(struct file *file, loff_t offset, loff_t nbytes,
 int ksys_sync_file_range(int fd, loff_t offset, loff_t nbytes,
 			 unsigned int flags)
 {
-	int ret;
-	struct fd f;
+	int ret = -EBADF;
+	struct fd f = fdget(fd);
 
-	ret = -EBADF;
-	f = fdget(fd);
-	if (f.file)
-		ret = sync_file_range(f.file, offset, nbytes, flags);
+	if (!f.file)
+		goto out;
 
+	ret = sync_file_range(f.file, offset, nbytes, flags);
 	fdput(f);
+out:
 	return ret;
 }