Skip to contentSkip to conversation

cachefiles: fix __cachefiles_prepare_write() error handling

PATCH In review Arnd Bergmann 2 participants 2 messages Dec 4, 2021 – Dec 7, 2021 Cachefiles: fs-cache backend for caching on mounted filesystems

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cachefiles: fix __cachefiles_prepare_write() error handling

From: Arnd Bergmann <[email protected]>

clang points out that __cachefiles_prepare_write() returns an uninitialized error code in one of the code paths:

fs/cachefiles/io.c:489:6: error: variable 'ret' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]

        if (pos == 0)
            ^~~~~~~~
fs/cachefiles/io.c:492:6: note: uninitialized use occurs here
        if (ret < 0) {
            ^~~
fs/cachefiles/io.c:489:2: note: remove the 'if' if its condition is always true
        if (pos == 0)
        ^~~~~~~~~~~~~
fs/cachefiles/io.c:440:9: note: initialize the variable 'ret' to silence this warning
        int ret;
               ^

Rework to return zero for success here and skip the rest of the function.

Fixes 0443b01eccbb ("cachefiles: Implement the I/O routines")
---
 fs/cachefiles/io.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
fs/cachefiles/io.c +5 -3
index 74ef4d1fc562..0fab313a604d 100644
@@ -486,9 +486,11 @@ static int __cachefiles_prepare_write(struct netfs_cache_resources *cres,
 	/* Partially allocated, but insufficient space: cull. */
 	fscache_count_no_write_space();
 	pos = cachefiles_inject_remove_error();
-	if (pos == 0)
-		ret = vfs_fallocate(file, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
-				    *_start, *_len);
+	if (pos != 0)
+		return 0;
+
+	ret = vfs_fallocate(file, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
+			    *_start, *_len);
 	if (ret < 0) {
 		trace_cachefiles_io_error(object, file_inode(file), ret,
 					  cachefiles_trace_fallocate_error);
Signature · 1 line
2.29.2
maintainer
#1 lore raw

fs/cachefiles/io.c static int __cachefiles_prepare_write(struct netfs_cache_resources *cres, · line 494

Quoted 10 lines · Arnd Bergmann
fs/cachefiles/io.c +5 -3
 	pos = cachefiles_inject_remove_error();
-	if (pos == 0)
-		ret = vfs_fallocate(file, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
-				    *_start, *_len);
+	if (pos != 0)
+		return 0;
+
+	ret = vfs_fallocate(file, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
+			    *_start, *_len);
 	if (ret < 0) {

The right fix is to use "ret" rather than "pos". I've fixed this already on my fscache-rewrite branch. Hopefully I'll be able to push that over to my next branch shortly.

David