[PATCH] ntfs: fix error handling in ntfs_readdir()

Hongling Zeng <[email protected]>
Newsgroups dev.linux.lists.ntfs,org.kernel.vger.linux-kernel,org.kernel.vger.stable
Message-ID <[email protected]>
ntfs_readdir() converts all non-zero errors to success before returning.
This hides real errors such as -ENOMEM or -EIO from userspace and makes
them look like end-of-directory.

Only positive return values from ntfs_filldir(), which indicate that the
user buffer is full, should be converted to 0. Negative errors should be
propagated.

Additionally, when the user buffer is full (err > 0), the iterator should
not be marked as completed since directory traversal has not ended.
This allows subsequent readdir() calls to continue from the saved position.

Fixes: 956ce2083c93 ("[readdir] convert ntfs")
Cc: [email protected]
Signed-off-by: Hongling Zeng <[email protected]>
---
 fs/ntfs/dir.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/fs/ntfs/dir.c b/fs/ntfs/dir.c
index 2d594cbb4ebe..ba17d9805326 100644
--- a/fs/ntfs/dir.c
+++ b/fs/ntfs/dir.c
@@ -982,8 +982,10 @@ static int ntfs_readdir(struct file *file, struct dir_context *actor)
 
 	if (!err)
 		private->end_in_iterate = true;
-	else
+	else if (err > 0)
 		err = 0;
+	else
+		goto out;
 
 	private->curr_pos = actor->pos = ie_pos;
 out:
@@ -1000,12 +1002,13 @@ static int ntfs_readdir(struct file *file, struct dir_context *actor)
 		kfree(cnir);
 	}
 
-	if (err) {
+	if (err > 0) {
 		if (private) {
 			private->curr_pos = actor->pos;
-			private->end_in_iterate = true;
 		}
 		err = 0;
+	} else if (err < 0 && private) {
+		private->curr_pos = actor->pos;
 	}
 	ntfs_index_ctx_put(ictx);
 	kfree(name);
-- 
2.25.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.