aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDarrick J. Wong <djwong@kernel.org>2024-01-11 18:07:03 -0800
committerDarrick J. Wong <djwong@kernel.org>2024-01-11 18:07:03 -0800
commit55021e7533bc55100f8ae0125aec513885cc5987 (patch)
treeb9751ef4e5580d0c6f8615db82b55b0a7b388265
parentc2371fdd0ffeecb407969ad3e4e1d55f26e26407 (diff)
downloadxfsprogs-dev-55021e7533bc55100f8ae0125aec513885cc5987.tar.gz
libxfs: fix krealloc to allow freeing data
A recent refactoring to xfs_idata_realloc in the kernel made it depend on krealloc returning NULL if the new size is zero. The xfsprogs wrapper instead aborts, so we need to make it follow the kernel behavior. Signed-off-by: Darrick J. Wong <djwong@kernel.org> Reviewed-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Carlos Maiolino <cmaiolino@redhat.com>
-rw-r--r--libxfs/kmem.c10
1 files changed, 10 insertions, 0 deletions
diff --git a/libxfs/kmem.c b/libxfs/kmem.c
index 42d813088d..c264be018b 100644
--- a/libxfs/kmem.c
+++ b/libxfs/kmem.c
@@ -98,6 +98,16 @@ kmem_zalloc(size_t size, int flags)
void *
krealloc(void *ptr, size_t new_size, int flags)
{
+ /*
+ * If @new_size is zero, Linux krealloc will free the memory and return
+ * NULL, so force that behavior here. The return value of realloc with
+ * a zero size is implementation dependent, so we cannot use that.
+ */
+ if (!new_size) {
+ free(ptr);
+ return NULL;
+ }
+
ptr = realloc(ptr, new_size);
if (ptr == NULL) {
fprintf(stderr, _("%s: realloc failed (%d bytes): %s\n"),