diff mbox series

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

Message ID 20200414020231.50670-1-wqu@suse.com (mailing list archive)
State New, archived
Headers show
Series btrfs-progs: Do proper error handling in add_cache_extent() | expand

Commit Message

Qu Wenruo April 14, 2020, 2:02 a.m. UTC
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.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 check/main.c   | 12 +++++++++++-
 extent-cache.c |  6 ++----
 2 files changed, 13 insertions(+), 5 deletions(-)

Comments

David Sterba April 14, 2020, 3:07 p.m. UTC | #1
On Tue, Apr 14, 2020 at 10:02:31AM +0800, Qu Wenruo wrote:
> 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.
> 
> Signed-off-by: Qu Wenruo <wqu@suse.com>

Added to devel, thanks.
diff mbox series

Patch

diff --git a/check/main.c b/check/main.c
index a41117fe79f9..c51dad8f2c89 100644
--- a/check/main.c
+++ b/check/main.c
@@ -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;
 }
 
diff --git a/extent-cache.c b/extent-cache.c
index 4065522aafcf..927597b31978 100644
--- a/extent-cache.c
+++ b/extent-cache.c
@@ -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)