[PATCH] erofs-utils: lib: fix memory leak in xattr handling

Sandeep Dhavale dhavale at google.com
Sat Jul 12 09:35:48 AEST 2025


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.

Signed-off-by: Sandeep Dhavale <dhavale at google.com>
---
 lib/xattr.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/lib/xattr.c b/lib/xattr.c
index 091c88c..6711dcc 100644
--- a/lib/xattr.c
+++ b/lib/xattr.c
@@ -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);
 	}
 
-- 
2.50.0.727.gbf7dc18ff4-goog



More information about the Linux-erofs mailing list