diff mbox

[3/3] Btrfs-progs: subvol_uuid_search: Return error code on memory allocation failure

Message ID 20161210134744.10825-3-kosigiprasanth@gmail.com (mailing list archive)
State Accepted
Headers show

Commit Message

Prasanth KSR Dec. 10, 2016, 1:47 p.m. UTC
From: Prasanth K S R <prasanth.ksr@dell.com>

On failure of memory allocation for a 'struct subvol_info', we would end
up dereferencing a NULL pointer. This commit fixes the issue by returning an
error encoded pointer.

Signed-off-by: Prasanth K S R <prasanth.ksr@dell.com>
---
 send-utils.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
diff mbox

Patch

diff --git a/send-utils.c b/send-utils.c
index 252ca6d..95445b5 100644
--- a/send-utils.c
+++ b/send-utils.c
@@ -474,6 +474,10 @@  struct subvol_info *subvol_uuid_search(struct subvol_uuid_search *s,
 		goto out;
 
 	info = calloc(1, sizeof(*info));
+	if (!info) {
+		ret = -ENOMEM;
+		goto out;
+	}
 	info->root_id = root_id;
 	memcpy(info->uuid, root_item.uuid, BTRFS_UUID_SIZE);
 	memcpy(info->received_uuid, root_item.received_uuid, BTRFS_UUID_SIZE);
@@ -495,9 +499,11 @@  struct subvol_info *subvol_uuid_search(struct subvol_uuid_search *s,
 	}
 
 out:
-	if (ret && info) {
-		free(info->path);
-		free(info);
+	if (ret) {
+		if (info) {
+			free(info->path);
+			free(info);
+		}
 		return ERR_PTR(ret);
 	}