Skip to contentSkip to conversation

btrfs-progs: Do proper error handling in add_cache_extent()

PATCH In review Qu Wenruo 2 participants 2 messages Apr 14, 2020 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

PATCH 1/1 btrfs-progs: Do proper error handling in add_cache_extent()

If we have memory allocation failure in add_cache_extent(), it will simply exit with one error message.

That's definitely not proper, especially when all but one call sites have handled the error.

This patch will return -ENOMEM for add_cache_extent(), and fix the only call site which doesn't handle error from it.

---
 check/main.c   | 12 +++++++++++-
 extent-cache.c |  6 ++----
 2 files changed, 13 insertions(+), 5 deletions(-)
check/main.c +11 -1
index a41117fe79f9..c51dad8f2c89 100644
@@ -4924,7 +4924,17 @@ static int add_pending(struct cache_tree *pending,
 	ret = add_cache_extent(seen, bytenr, size);
 	if (ret)
 		return ret;
-	add_cache_extent(pending, bytenr, size);
+	ret = add_cache_extent(pending, bytenr, size);
+	if (ret) {
+		struct cache_extent *entry;
+
+		entry = lookup_cache_extent(seen, bytenr, size);
+		if (entry && entry->start == bytenr && entry->size == size) {
+			remove_cache_extent(seen, entry);
+			free(entry);
+		}
+		return ret;
+	}
 	return 0;
 }
 
extent-cache.c +2 -4
index 4065522aafcf..927597b31978 100644
@@ -111,10 +111,8 @@ int add_cache_extent(struct cache_tree *tree, u64 start, u64 size)
 	struct cache_extent *pe = alloc_cache_extent(start, size);
 	int ret;
 
-	if (!pe) {
-		fprintf(stderr, "memory allocation failed\n");
-		exit(1);
-	}
+	if (!pe)
+		return -ENOMEM;
 
 	ret = insert_cache_extent(tree, pe);
 	if (ret)
Signature · 1 line
2.26.0
#1 lore raw
Quoted 10 lines · Qu Wenruo

If we have memory allocation failure in add_cache_extent(), it will simply exit with one error message.

That's definitely not proper, especially when all but one call sites have handled the error.

This patch will return -ENOMEM for add_cache_extent(), and fix the only call site which doesn't handle error from it.

Added to devel, thanks.