Skip to contentSkip to conversation

Misc fixes in erofs-utils

PATCH In review Sandeep Dhavale via Linux-erofs 2 participants 9 messages Sep 13, 2023 – Sep 14, 2023 Library code

lore
Share & tools
lore
#0 lore raw

cover Misc fixes in erofs-utils

Hi,
Below series contains few small misc fixes found after running through clang static checker.

Sandeep Dhavale (7):
erofs-utils: fsck: Fix potential memory leak in error path
erofs-utils: lib: Remove redundant line to get padding
erofs-utils: lib: Fix memory leak if __erofs_battach() fails
erofs-utils: lib: Check for error from z_erofs_pack_file_from_fd()
erofs-utils: lib: Fix the memory leak in error path
erofs-utils: lib: Remove redundant assignment
erofs-utils: lib: tar: Initialize the variable to avoid using garbage
value

 fsck/main.c      | 4 +++-
 lib/blobchunk.c  | 1 -
 lib/cache.c      | 4 +++-
 lib/compress.c   | 2 ++
 lib/decompress.c | 4 +++-
 lib/namei.c      | 1 -
 lib/tar.c        | 2 +-
 7 files changed, 12 insertions(+), 6 deletions(-)
Signature · 1 line
2.42.0.283.g2d96d420d3-goog
#1 lore raw

PATCH 1/7 erofs-utils: fsck: Fix potential memory leak in error path

Free the memory allocated for 'entry' if strdup() fails.

---
 fsck/main.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fsck/main.c +3 -1
index 7f78513..3f86da4 100644
@@ -585,8 +585,10 @@ static int erofsfsck_hardlink_insert(erofs_nid_t nid, const char *path)
 
 	entry->nid = nid;
 	entry->path = strdup(path);
-	if (!entry->path)
+	if (!entry->path) {
+		free(entry);
 		return -ENOMEM;
+	}
 
 	list_add_tail(&entry->list,
 		      &erofsfsck_link_hashtable[nid % NR_HARDLINK_HASHTABLE]);
Signature · 1 line
2.42.0.283.g2d96d420d3-goog
#2 lore raw

PATCH 2/7 erofs-utils: lib: Remove redundant line to get padding

In reply to Sandeep Dhavale via Linux-erofs

padding is only used in the next if() block. Remove the redundant call.

---
 lib/blobchunk.c | 1 -
 1 file changed, 1 deletion(-)
lib/blobchunk.c +0 -1
index 86b29c1..fcc71b8 100644
@@ -91,7 +91,6 @@ static struct erofs_blobchunk *erofs_blob_getchunk(struct erofs_sb_info *sbi,
 
 	erofs_dbg("Writing chunk (%u bytes) to %u", chunksize, chunk->blkaddr);
 	ret = fwrite(buf, chunksize, 1, blobfile);
-	padding = erofs_blkoff(sbi, chunksize);
 	if (ret == 1) {
 		padding = erofs_blkoff(sbi, chunksize);
 		if (padding) {
Signature · 1 line
2.42.0.283.g2d96d420d3-goog
#3 lore raw

PATCH 3/7 erofs-utils: lib: Fix memory leak if __erofs_battach() fails

In reply to Sandeep Dhavale via Linux-erofs

Need to free allocated buffer_head bh if __erofs_battach() fails.

---
 lib/cache.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/cache.c +3 -1
index 5205d57..a9948f0 100644
@@ -282,8 +282,10 @@ struct erofs_buffer_head *erofs_balloc(int type, erofs_off_t size,
 
 	ret = __erofs_battach(bb, bh, size, alignsize,
 			      required_ext + inline_ext, false);
-	if (ret < 0)
+	if (ret < 0) {
+		free(bh);
 		return ERR_PTR(ret);
+	}
 	return bh;
 }
 
Signature · 1 line
2.42.0.283.g2d96d420d3-goog
#4 lore raw

PATCH 4/7 erofs-utils: lib: Check for error from z_erofs_pack_file_from_fd()

In reply to Sandeep Dhavale via Linux-erofs

If z_erofs_pack_file_from_fd() fails, take the error path to free up the allocated resources.

---
 lib/compress.c | 2 ++
 1 file changed, 2 insertions(+)
lib/compress.c +2 -0
index 8c79418..81f277a 100644
@@ -927,6 +927,8 @@ int erofs_write_compressed_file(struct erofs_inode *inode, int fd)
 	if (cfg.c_all_fragments && !erofs_is_packed_inode(inode) &&
 	    !inode->fragment_size) {
 		ret = z_erofs_pack_file_from_fd(inode, fd, ctx.tof_chksum);
+		if (ret)
+			goto err_free_idata;
 	} else {
 		while (ctx.remaining) {
 			const u64 rx = min_t(u64, ctx.remaining,
Signature · 1 line
2.42.0.283.g2d96d420d3-goog
#5 lore raw

PATCH 5/7 erofs-utils: lib: Fix the memory leak in error path

In reply to Sandeep Dhavale via Linux-erofs

If call to inflateInit2() fails, release the memory allocated for buff before returning.

---
 lib/decompress.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/decompress.c +3 -1
index 01f0141..fe8a40c 100644
@@ -126,8 +126,10 @@ static int z_erofs_decompress_deflate(struct z_erofs_decompress_req *rq)
 	strm.avail_in = 0;
 	strm.next_in = Z_NULL;
 	ret = inflateInit2(&strm, -15);
-	if (ret != Z_OK)
+	if (ret != Z_OK) {
+		free(buff);
 		return zerr(ret);
+	}
 
 	strm.next_in = src + inputmargin;
 	strm.avail_in = rq->inputsize - inputmargin;
Signature · 1 line
2.42.0.283.g2d96d420d3-goog
#6 lore raw

PATCH 6/7 erofs-utils: lib: Remove redundant assignment

In reply to Sandeep Dhavale via Linux-erofs

The intended assignment is already part of the next for loop initialization.

---
 lib/namei.c | 1 -
 1 file changed, 1 deletion(-)
lib/namei.c +0 -1
index 45dbcd3..294d7a3 100644
@@ -262,7 +262,6 @@ static int link_path_walk(const char *name, struct nameidata *nd)
 		if (ret)
 			return ret;
 
-		name = p;
 		/* Skip until no more slashes. */
 		for (name = p; *name == '/'; ++name)
 			;
Signature · 1 line
2.42.0.283.g2d96d420d3-goog
#7 lore raw

PATCH 7/7 erofs-utils: lib: tar: Initialize the variable to avoid using garbage value

In reply to Sandeep Dhavale via Linux-erofs

The value in variable 'e' is checked without initializing and can wrongly signify end of tar 2 empty blocks.

---
 lib/tar.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/tar.c +1 -1
index b58bfd5..c4c89ec 100644
@@ -549,7 +549,7 @@ int tarerofs_parse_tar(struct erofs_inode *root, struct erofs_tarfile *tar)
 	char path[PATH_MAX];
 	struct erofs_pax_header eh = tar->global;
 	struct erofs_sb_info *sbi = root->sbi;
-	bool e, whout, opq;
+	bool whout, opq, e = false;
 	struct stat st;
 	erofs_off_t tar_offset, data_offset;
 
Signature · 1 line
2.42.0.283.g2d96d420d3-goog
#8 lore raw
In reply to Sandeep Dhavale via Linux-erofs
Quoted 3 lines · Sandeep Dhavale

Hi,
Below series contains few small misc fixes found after running through clang static checker.

All these patches look good to me, will apply.

Thanks,
Gao Xiang

Quoted 20 lines

Sandeep Dhavale (7):
erofs-utils: fsck: Fix potential memory leak in error path
erofs-utils: lib: Remove redundant line to get padding
erofs-utils: lib: Fix memory leak if __erofs_battach() fails
erofs-utils: lib: Check for error from z_erofs_pack_file_from_fd()
erofs-utils: lib: Fix the memory leak in error path
erofs-utils: lib: Remove redundant assignment
erofs-utils: lib: tar: Initialize the variable to avoid using garbage
value

  fsck/main.c      | 4 +++-
  lib/blobchunk.c  | 1 -
  lib/cache.c      | 4 +++-
  lib/compress.c   | 2 ++
  lib/decompress.c | 4 +++-
  lib/namei.c      | 1 -
  lib/tar.c        | 2 +-
  7 files changed, 12 insertions(+), 6 deletions(-)