summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Guifu <bluce.liguifu@huawei.com>2019-11-14 01:03:35 +0800
committerGao Xiang <hsiangkao@aol.com>2019-11-14 21:11:09 +0800
commit203fb2925f99f613583bf7b06a7c9397280e4da8 (patch)
treed097066809d3f6b43d3649470e4238fc1d6781bb
parente023d47593ff8dcd6e8b256f2d06e9177199ee12 (diff)
erofs-utils: complete missing memory handling
Memory allocation failure should be handled properly in principle. Link: https://lore.kernel.org/r/20191114091914.6789-1-hsiangkao@aol.com Signed-off-by: Li Guifu <bluce.liguifu@huawei.com> [ Gao Xiang: due to Huawei outgoing email limitation, I have to help Guifu send out his patches at work. ] Signed-off-by: Li Guifu <blucerlee@gmail.com> Signed-off-by: Gao Xiang <hsiangkao@aol.com>
-rw-r--r--lib/inode.c20
1 files changed, 18 insertions, 2 deletions
diff --git a/lib/inode.c b/lib/inode.c
index 86c465e..0e19b11 100644
--- a/lib/inode.c
+++ b/lib/inode.c
@@ -264,6 +264,8 @@ int erofs_write_dir_file(struct erofs_inode *dir)
if (used) {
/* fill tail-end dir block */
dir->idata = malloc(used);
+ if (!dir->idata)
+ return -ENOMEM;
DBG_BUGON(used != dir->idata_size);
fill_dirblock(dir->idata, dir->idata_size, q, head, d);
}
@@ -286,6 +288,8 @@ int erofs_write_file_from_buffer(struct erofs_inode *inode, char *buf)
inode->idata_size = inode->i_size % EROFS_BLKSIZ;
if (inode->idata_size) {
inode->idata = malloc(inode->idata_size);
+ if (!inode->idata)
+ return -ENOMEM;
memcpy(inode->idata, buf + blknr_to_addr(nblocks),
inode->idata_size);
}
@@ -347,9 +351,15 @@ int erofs_write_file(struct erofs_inode *inode)
inode->idata_size = inode->i_size % EROFS_BLKSIZ;
if (inode->idata_size) {
inode->idata = malloc(inode->idata_size);
+ if (!inode->idata) {
+ close(fd);
+ return -ENOMEM;
+ }
ret = read(fd, inode->idata, inode->idata_size);
if (ret < inode->idata_size) {
+ free(inode->idata);
+ inode->idata = NULL;
close(fd);
return -EIO;
}
@@ -825,12 +835,18 @@ struct erofs_inode *erofs_mkfs_build_tree(struct erofs_inode *dir)
if (S_ISLNK(dir->i_mode)) {
char *const symlink = malloc(dir->i_size);
+ if (!symlink)
+ return ERR_PTR(-ENOMEM);
ret = readlink(dir->i_srcpath, symlink, dir->i_size);
- if (ret < 0)
+ if (ret < 0) {
+ free(symlink);
return ERR_PTR(-errno);
+ }
- erofs_write_file_from_buffer(dir, symlink);
+ ret = erofs_write_file_from_buffer(dir, symlink);
free(symlink);
+ if (ret)
+ return ERR_PTR(ret);
} else {
erofs_write_file(dir);
}