Skip to contentSkip to conversation

erofs-utils: s3: fix memory leak in s3erofs_create_object_iterator

PATCH Superseded Nithurshen 2 participants 3 messages Apr 4 – Apr 5

lore
Share & tools
lore

Nithurshen

author
#0 lore raw

PATCH 1/1 erofs-utils: s3: fix memory leak in s3erofs_create_object_iterator

In s3erofs_create_object_iterator(), if the parsed prefix length exceeds S3EROFS_PATH_MAX, the function aborts and returns an -EINVAL error pointer. However, the 'iter' structure was already allocated via calloc() and left unfreed, causing a memory leak.

This commit adds the missing free(iter) call in the error path to prevent leaking memory when excessively long S3 bucket paths are provided.

---
 lib/remotes/s3.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/remotes/s3.c +3 -1
index 768232a..94b4ffb 100644
@@ -911,8 +911,10 @@ s3erofs_create_object_iterator(struct erofs_s3 *s3, const char *path,
 		iter->bucket = NULL;
 		iter->prefix = strdup(path + 1);
 	} else {
-		if (++prefix - path > S3EROFS_PATH_MAX)
+		if (++prefix - path > S3EROFS_PATH_MAX){
+			free(iter);
 			return ERR_PTR(-EINVAL);
+		}
 		iter->bucket = strndup(path, prefix - path);
 		iter->prefix = strdup(prefix);
 	}
Signature · 1 line
2.52.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 19 lines · Nithurshen

In s3erofs_create_object_iterator(), if the parsed prefix length exceeds S3EROFS_PATH_MAX, the function aborts and returns an -EINVAL error pointer. However, the 'iter' structure was already allocated via calloc() and left unfreed, causing a memory leak.

This commit adds the missing free(iter) call in the error path to prevent leaking memory when excessively long S3 bucket paths are provided.

---
  lib/remotes/s3.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
lib/remotes/s3.c +0 -0
index 768232a..94b4ffb 100644

lib/remotes/s3.c s3erofs_create_object_iterator(struct erofs_s3 *s3, const char *path, · line 914

Quoted 5 lines
lib/remotes/s3.c +1 -1
@@ -911,8 +911,10 @@ s3erofs_create_object_iterator(struct erofs_s3 *s3, const char *path,
  		iter->bucket = NULL;
  		iter->prefix = strdup(path + 1);
  	} else {
-		if (++prefix - path > S3EROFS_PATH_MAX)
+		if (++prefix - path > S3EROFS_PATH_MAX){

missing a space before the brace, otherwise LGTM.

Quoted 6 lines
lib/remotes/s3.c +2 -0
+			free(iter);
  			return ERR_PTR(-EINVAL);
+		}
  		iter->bucket = strndup(path, prefix - path);
  		iter->prefix = strdup(prefix);
  	}
author
#2 lore raw

PATCH v2 1/1 erofs-utils: s3: fix memory leak in s3erofs_create_object_iterator

In reply to Nithurshen

In s3erofs_create_object_iterator(), if the parsed prefix length exceeds S3EROFS_PATH_MAX, the function aborts and returns an -EINVAL error pointer. However, the 'iter' structure was already allocated via calloc() and left unfreed, causing a memory leak.

This commit adds the missing free(iter) call in the error path to prevent leaking memory when excessively long S3 bucket paths are provided.

---
 lib/remotes/s3.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/remotes/s3.c +3 -1
index 768232a..abfa5dc 100644
@@ -911,8 +911,10 @@ s3erofs_create_object_iterator(struct erofs_s3 *s3, const char *path,
 		iter->bucket = NULL;
 		iter->prefix = strdup(path + 1);
 	} else {
-		if (++prefix - path > S3EROFS_PATH_MAX)
+		if (++prefix - path > S3EROFS_PATH_MAX) {
+			free(iter);
 			return ERR_PTR(-EINVAL);
+		}
 		iter->bucket = strndup(path, prefix - path);
 		iter->prefix = strdup(prefix);
 	}
Signature · 1 line
2.52.0