Skip to contentSkip to conversation

erofs-utils: lib: fix memory leak in xattr handling

PATCH In review Sandeep Dhavale 3 participants 4 messages Jul 11, 2025 – Jul 15, 2025 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs-utils: lib: fix memory leak in xattr handling

In android the LeakSanitizer reported memory leaks originating from functions like erofs_get_selabel_xattr.

The root cause is that the 'kvbuf' buffer, which is allocated to store xattr data, was not being freed when its owning 'xattr_item' struct was deallocated. The functions put_xattritem() and erofs_cleanxattrs() were freeing the xattr_item struct but neglected to free the kvbuf pointer within it.

This patch fixes the leak by adding the necessary free() calls for kvbuf in both functions.

---
 lib/xattr.c | 2 ++
 1 file changed, 2 insertions(+)
lib/xattr.c +2 -0
index 091c88c..6711dcc 100644
@@ -194,6 +194,7 @@ static unsigned int put_xattritem(struct xattr_item *item)
 	if (item->count > 1)
 		return --item->count;
 	hash_del(&item->node);
+	free((void *)item->kvbuf);
 	free(item);
 	return 0;
 }
@@ -775,6 +776,7 @@ static void erofs_cleanxattrs(bool sharedxattrs)
 			continue;
 
 		hash_del(&item->node);
+		free((void *)item->kvbuf);
 		free(item);
 	}
 
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Sandeep Dhavale

In android the LeakSanitizer reported memory leaks originating from functions like erofs_get_selabel_xattr.

The root cause is that the 'kvbuf' buffer, which is allocated to store xattr data, was not being freed when its owning 'xattr_item' struct was deallocated. The functions put_xattritem() and erofs_cleanxattrs() were freeing the xattr_item struct but neglected to free the kvbuf pointer within it.

Quoted 4 lines

This patch fixes the leak by adding the necessary free() calls for kvbuf in both functions.

Thanks,
Gao Xiang

Reviewed-by on v1 1/1
#2 lore raw
In reply to Sandeep Dhavale
Quoted 9 lines · Sandeep Dhavale

In android the LeakSanitizer reported memory leaks originating from functions like erofs_get_selabel_xattr.

The root cause is that the 'kvbuf' buffer, which is allocated to store xattr data, was not being freed when its owning 'xattr_item' struct was deallocated. The functions put_xattritem() and erofs_cleanxattrs() were freeing the xattr_item struct but neglected to free the kvbuf pointer within it.

Quoted 4 lines

This patch fixes the leak by adding the necessary free() calls for kvbuf in both functions.

Thanks,

#3 lore raw

Hi Chao,

Quoted 10 lines · Chao Yu
Quoted 9 lines · Sandeep Dhavale

In android the LeakSanitizer reported memory leaks originating from functions like erofs_get_selabel_xattr.

The root cause is that the 'kvbuf' buffer, which is allocated to store xattr data, was not being freed when its owning 'xattr_item' struct was deallocated. The functions put_xattritem() and erofs_cleanxattrs() were freeing the xattr_item struct but neglected to free the kvbuf pointer within it.

Quoted 6 lines
Quoted 4 lines

This patch fixes the leak by adding the necessary free() calls for kvbuf in both functions.

I released erofs-utils 1.8.10 hours ago to resolve some fragment mkfs regression so it won't be in appended.

But thanks for your review!

Thanks,
Gao Xiang

Quoted 2 lines

Thanks,