diff mbox series

libxfs: fix krealloc to allow freeing data

Message ID 20240109055118.GC722975@frogsfrogsfrogs (mailing list archive)
State New
Headers show
Series libxfs: fix krealloc to allow freeing data | expand

Commit Message

Darrick J. Wong Jan. 9, 2024, 5:51 a.m. UTC
From: Darrick J. Wong <djwong@kernel.org>

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>
---
 libxfs/kmem.c |   10 ++++++++++
 1 file changed, 10 insertions(+)

Comments

Christoph Hellwig Jan. 9, 2024, 5:53 a.m. UTC | #1
Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>
Carlos Maiolino Jan. 10, 2024, 2:32 p.m. UTC | #2
On Mon, Jan 08, 2024 at 09:51:18PM -0800, Darrick J. Wong wrote:
> From: Darrick J. Wong <djwong@kernel.org>
> 
> 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: Carlos Maiolino <cmaiolino@redhat.com>

> ---
>  libxfs/kmem.c |   10 ++++++++++
>  1 file changed, 10 insertions(+)
> 
> diff --git a/libxfs/kmem.c b/libxfs/kmem.c
> index 42d813088d6a..c264be018bdc 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"),
diff mbox series

Patch

diff --git a/libxfs/kmem.c b/libxfs/kmem.c
index 42d813088d6a..c264be018bdc 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"),