erofs-utils: complete missing memory handling
PATCH 1/2 erofs-utils: complete missing memory handling
From: Li Guifu <[email protected]>
memory allocation failure should be handled
properly in principle.
[ Gao Xiang: due to Huawei outgoing email limitation,
I have to help Guifu send out his patches at work. ]
--- lib/inode.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)
lib/inode.c +15 -2
@@ -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,12 @@ 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) +return -ENOMEM ; ret =read (fd, inode->idata, inode->idata_size);if (ret < inode->idata_size) { +free (inode->idata);close (fd);return -EIO ; }
@@ -825,12 +832,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); }
Signature · 1 line
2.17.1
PATCH 2/2 erofs-utils: set up all compiler/linker variables independently
Otherwise, the following checking will be effected and it can cause unexpected behavior on configuring.
Founded by the upcoming XZ algorithm patches.
--- configure.ac | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-)
configure.ac +18 -17
@@ -131,7 +131,7 @@ if test "x$enable_lz4" = "xyes"; then test -z "${with_lz4_libdir}" || LZ4_LIBS="-L$with_lz4_libdir $LZ4_LIBS" saved_CPPFLAGS=${CPPFLAGS} - CPPFLAGS="${LZ4_CFLAGS} ${CFLAGS}" + CPPFLAGS="${LZ4_CFLAGS} ${CPPFLAGS}" AC_CHECK_HEADERS([lz4.h],[have_lz4h="yes"], [])
@@ -150,28 +150,29 @@ if test "x$enable_lz4" = "xyes"; then ]) ], [AC_MSG_ERROR([Cannot find proper lz4 version (>= 1.8.0)])]) LDFLAGS=${saved_LDFLAGS} - - if test "x${have_lz4}" = "xyes"; then - AC_DEFINE([LZ4_ENABLED], [1], [Define to 1 if lz4 is enabled.]) - - if test "x${have_lz4hc}" = "xyes"; then - AC_DEFINE([LZ4HC_ENABLED], [1], [Define to 1 if lz4hc is enabled.]) - fi - - if test "x${lz4_force_static}" = "xyes"; then - LDFLAGS="-all-static ${LDFLAGS}" - else - test -z "${with_lz4_libdir}" || LZ4_LIBS="-R ${with_lz4_libdir} $LZ4_LIBS" - fi - LIBS="$LZ4_LIBS $LIBS" - fi fi - CFLAGS=${saved_CPPFLAGS} + CPPFLAGS=${saved_CPPFLAGS} fi +# Set up needed symbols, conditionals and compiler/linker flags AM_CONDITIONAL([ENABLE_LZ4], [test "x${have_lz4}" = "xyes"]) AM_CONDITIONAL([ENABLE_LZ4HC], [test "x${have_lz4hc}" = "xyes"]) +if test "x${have_lz4}" = "xyes"; then + AC_DEFINE([LZ4_ENABLED], [1], [Define to 1 if lz4 is enabled.]) + + if test "x${have_lz4hc}" = "xyes"; then + AC_DEFINE([LZ4HC_ENABLED], [1], [Define to 1 if lz4hc is enabled.]) + fi + + if test "x${lz4_force_static}" = "xyes"; then + LDFLAGS="-all-static ${LDFLAGS}" + else + test -z "${with_lz4_libdir}" || LZ4_LIBS="-R ${with_lz4_libdir} $LZ4_LIBS" + fi + LIBS="$LZ4_LIBS $LIBS" +fi + AC_CONFIG_FILES([Makefile man/Makefile lib/Makefile
Signature · 1 line
2.17.1
Re: [PATCH 2/2] erofs-utils: set up all compiler/linker variables independently
Gao Xiang
Can you resend this patch ? It is conflict with uuid. uuid patch :
https://lore.kernel.org/linux-erofs/[email protected]/T/#u
Quoted 7 lines · Gao Xiang
Otherwise, the following checking will be effected and it can cause unexpected behavior on configuring.
Founded by the upcoming XZ algorithm patches.
Signed-off-by Gao Xiang <[email protected]>---
Re: [PATCH 2/2] erofs-utils: set up all compiler/linker variables independently
Hi Guifu,
Quoted 4 lines · Li Guifu
Gao Xiang
Can you resend this patch ? It is conflict with uuid. uuid patch :
https://lore.kernel.org/linux-erofs/[email protected]/T/#u
You're right. I'll sort out all erofs-utils pending patches, rebase this patch and resend soon.
Thanks,
Gao Xiang
Quoted 9 lines · Gao Xiang
Quoted 7 lines
Otherwise, the following checking will be effected and it can cause unexpected behavior on configuring.
Founded by the upcoming XZ algorithm patches.
Signed-off-by Gao Xiang <[email protected]>---
PATCH v2 1/1 erofs-utils: set up all compiler/linker variables independently
Otherwise, the following checking will be effected and it can cause unexpected behavior on configuring.
Founded by the upcoming XZ algorithm patches.
---
changes since v1:
- rebase the patch suggested by Guifu.
configure.ac | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-)
configure.ac +20 -19
@@ -168,7 +168,7 @@ if test "x$enable_lz4" = "xyes"; then test -z "${with_lz4_libdir}" || LZ4_LIBS="-L$with_lz4_libdir $LZ4_LIBS" saved_CPPFLAGS=${CPPFLAGS} - CPPFLAGS="${LZ4_CFLAGS} ${CFLAGS}" + CPPFLAGS="${LZ4_CFLAGS} ${CPPFLAGS}" AC_CHECK_HEADERS([lz4.h],[have_lz4h="yes"], [])
@@ -187,31 +187,32 @@ if test "x$enable_lz4" = "xyes"; then ]) ], [AC_MSG_ERROR([Cannot find proper lz4 version (>= 1.8.0)])]) LDFLAGS=${saved_LDFLAGS} - - if test "x${have_lz4}" = "xyes"; then - AC_DEFINE([LZ4_ENABLED], [1], [Define to 1 if lz4 is enabled.]) - - if test "x${have_lz4hc}" = "xyes"; then - AC_DEFINE([LZ4HC_ENABLED], [1], [Define to 1 if lz4hc is enabled.]) - fi - - if test "x${lz4_force_static}" = "xyes"; then - LDFLAGS="-all-static ${LDFLAGS}" - else - test -z "${with_lz4_libdir}" || LZ4_LIBS="-R ${with_lz4_libdir} $LZ4_LIBS" - fi - LIBS="$LZ4_LIBS $LIBS" - fi fi - CFLAGS=${saved_CPPFLAGS} + CPPFLAGS=${saved_CPPFLAGS} fi +# Set up needed symbols, conditionals and compiler/linker flags +AM_CONDITIONAL([ENABLE_LZ4], [test "x${have_lz4}" = "xyes"]) +AM_CONDITIONAL([ENABLE_LZ4HC], [test "x${have_lz4hc}" = "xyes"]) + if test "x$have_uuid" = "xyes"; then AC_DEFINE([HAVE_LIBUUID], 1, [Define to 1 if libuuid is found]) fi -AM_CONDITIONAL([ENABLE_LZ4], [test "x${have_lz4}" = "xyes"]) -AM_CONDITIONAL([ENABLE_LZ4HC], [test "x${have_lz4hc}" = "xyes"]) +if test "x${have_lz4}" = "xyes"; then + AC_DEFINE([LZ4_ENABLED], [1], [Define to 1 if lz4 is enabled.]) + + if test "x${have_lz4hc}" = "xyes"; then + AC_DEFINE([LZ4HC_ENABLED], [1], [Define to 1 if lz4hc is enabled.]) + fi + + if test "x${lz4_force_static}" = "xyes"; then + LDFLAGS="-all-static ${LDFLAGS}" + else + test -z "${with_lz4_libdir}" || LZ4_LIBS="-R ${with_lz4_libdir} $LZ4_LIBS" + fi + LIBS="$LZ4_LIBS $LIBS" +fi AC_CONFIG_FILES([Makefile man/Makefile
Signature · 1 line
2.17.1
Re: [PATCH v2] erofs-utils: set up all compiler/linker variables independently
Quoted 7 lines · Gao Xiang
Otherwise, the following checking will be effected and it can cause unexpected behavior on configuring.
Founded by the upcoming XZ algorithm patches.
Signed-off-by Gao Xiang <[email protected]>---
It looks good
Thanks,
PATCH v2 1/1 erofs-utils: complete missing memory handling
In reply to Gao XiangFrom: Li Guifu <[email protected]>
memory allocation failure should be handled
properly in principle.
[ Gao Xiang: due to Huawei outgoing email limitation,
I have to help Guifu send out his patches at work. ]
--- lib/inode.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-)
lib/inode.c +18 -3
@@ -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,14 @@ 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) { + errno =ENOMEM ; + goto fail; + } 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 +834,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); }
Signature · 1 line
2.17.1
Hi Guifu,
Quoted 11 lines · Li Guifu
From: Li Guifu <[email protected]>
memory allocation failure should be handled
properly in principle.Signed-off-by Li Guifu <[email protected]>[ Gao Xiang: due to Huawei outgoing email limitation,
I have to help Guifu send out his patches at work. ]Signed-off-by Gao Xiang <[email protected]>Signed-off-by Li Guifu <[email protected]>---
As a common practice, It's perferred to leave some useful comments at this about what you modified compared wtih the last version.
Quoted 28 lines
lib/inode.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-)lib/inode.c +4
-0@@ -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,14 @@ int erofs_write_file(struct erofs_inode *inode) inode->idata_size = inode->i_size %EROFS_BLKSIZ ;if (inode->idata_size) {
lib/inode.c int erofs_write_file(struct erofs_inode *inode) · line 361
Quoted 10 lines
lib/inode.c +6
-1inode->idata =malloc (inode->idata_size); - +if (!inode->idata) { + errno =ENOMEM ; + goto fail; + } ret =read (fd, inode->idata, inode->idata_size);if (ret < inode->idata_size) { +free (inode->idata); + inode->idata =NULL ;
Anyway, it seems the diffstat is this line.
I think it' better than v1 so let's use this version.
Thanks,
Gao Xiang
Quoted 45 lines · Gao Xiang via Linux-erofs
Hi Guifu,
Quoted 11 lines · Li Guifu
From: Li Guifu <[email protected]>
memory allocation failure should be handled
properly in principle.Signed-off-by Li Guifu <[email protected]>[ Gao Xiang: due to Huawei outgoing email limitation,
I have to help Guifu send out his patches at work. ]Signed-off-by Gao Xiang <[email protected]>Signed-off-by Li Guifu <[email protected]>---
As a common practice, It's perferred to leave some useful comments at this about what you modified compared wtih the last version.
Quoted 26 lines
lib/inode.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-)lib/inode.c +4
-0@@ -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,14 @@ int erofs_write_file(struct erofs_inode *inode)
Quoted 8 lines
lib/inode.c int erofs_write_file(struct erofs_inode *inode) · line 357
Quoted 8 lines
lib/inode.c +4
-1inode->idata_size = inode->i_size %EROFS_BLKSIZ ;if (inode->idata_size) { inode->idata =malloc (inode->idata_size); - +if (!inode->idata) { + errno =ENOMEM ; + goto fail; + }
When I revisited this patch, I noticed it's some weird to operate `errno' here.
The same sequence "close(fd); return -ENOMEM;" is indeed some unclean, but I think you could make a separate cleanup patch then.
I will resend PATCH v3 since you aren't able to post public mail at work and apply it to experimental branch.
Thanks,
Gao Xiang
Quoted 11 lines
lib/inode.c int erofs_write_file(struct erofs_inode *inode) · line 361
Quoted 4 lines
lib/inode.c +2
-0ret =read (fd, inode->idata, inode->idata_size);if (ret < inode->idata_size) { +free (inode->idata); + inode->idata =NULL ;Anyway, it seems the diffstat is this line.
I think it' better than v1 so let's use this version.Thanks,
Gao Xiang
PATCH v3 1/1 erofs-utils: complete missing memory handling
In reply to Li GuifuFrom: Li Guifu <[email protected]>
Memory allocation failure should be handled
properly in principle.
[ Gao Xiang: due to Huawei outgoing email limitation,
I have to help Guifu send out his patches at work. ]
---
changes since v2:
- avoid using system errno mechanism to report errors:
https://lore.kernel.org/r/20191114015110.GA155186@architecture4
lib/inode.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)
lib/inode.c +18 -2
@@ -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); }
Signature · 1 line
2.17.1