[PATCH 3/5] erofs-utils: avoid using a static srcpath

Gao Xiang hsiangkao at linux.alibaba.com
Sun Mar 5 06:58:10 AEDT 2023


To avoid unnecessary memory overhead.

Signed-off-by: Gao Xiang <hsiangkao at linux.alibaba.com>
---
 include/erofs/internal.h |  2 +-
 lib/inode.c              | 10 ++++++----
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/include/erofs/internal.h b/include/erofs/internal.h
index 08a3877..d4ae3b8 100644
--- a/include/erofs/internal.h
+++ b/include/erofs/internal.h
@@ -180,7 +180,7 @@ struct erofs_inode {
 		};
 	} u;
 
-	char i_srcpath[PATH_MAX + 1];
+	char *i_srcpath;
 
 	unsigned char datalayout;
 	unsigned char inode_isize;
diff --git a/lib/inode.c b/lib/inode.c
index 8364451..bcb0986 100644
--- a/lib/inode.c
+++ b/lib/inode.c
@@ -119,6 +119,8 @@ unsigned int erofs_iput(struct erofs_inode *inode)
 	if (inode->eof_tailraw)
 		free(inode->eof_tailraw);
 	list_del(&inode->i_hash);
+	if (inode->i_srcpath)
+		free(inode->i_srcpath);
 	free(inode);
 	return 0;
 }
@@ -905,8 +907,9 @@ static int erofs_fill_inode(struct erofs_inode *inode, struct stat *st,
 		return -EINVAL;
 	}
 
-	strncpy(inode->i_srcpath, path, sizeof(inode->i_srcpath) - 1);
-	inode->i_srcpath[sizeof(inode->i_srcpath) - 1] = '\0';
+	inode->i_srcpath = strdup(path);
+	if (!inode->i_srcpath)
+		return -ENOMEM;
 
 	inode->dev = st->st_dev;
 	inode->i_ino[1] = st->st_ino;
@@ -977,10 +980,9 @@ static struct erofs_inode *erofs_iget_from_path(const char *path, bool is_src)
 
 	ret = erofs_fill_inode(inode, &st, path);
 	if (ret) {
-		free(inode);
+		erofs_iput(inode);
 		return ERR_PTR(ret);
 	}
-
 	return inode;
 }
 
-- 
2.24.4



More information about the Linux-erofs mailing list