public inbox for linux-xfs@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] libxfs: fix krealloc to allow freeing data
@ 2024-01-09  5:51 ` Darrick J. Wong
  2024-01-09  5:53   ` Christoph Hellwig
  2024-01-10 14:32   ` Carlos Maiolino
  0 siblings, 2 replies; 3+ messages in thread
From: Darrick J. Wong @ 2024-01-09  5:51 UTC (permalink / raw)
  To: Carlos Maiolino, Christoph Hellwig; +Cc: xfs

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(+)

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"),

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] libxfs: fix krealloc to allow freeing data
  2024-01-09  5:51 ` [PATCH] libxfs: fix krealloc to allow freeing data Darrick J. Wong
@ 2024-01-09  5:53   ` Christoph Hellwig
  2024-01-10 14:32   ` Carlos Maiolino
  1 sibling, 0 replies; 3+ messages in thread
From: Christoph Hellwig @ 2024-01-09  5:53 UTC (permalink / raw)
  To: Darrick J. Wong; +Cc: Carlos Maiolino, Christoph Hellwig, xfs

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] libxfs: fix krealloc to allow freeing data
  2024-01-09  5:51 ` [PATCH] libxfs: fix krealloc to allow freeing data Darrick J. Wong
  2024-01-09  5:53   ` Christoph Hellwig
@ 2024-01-10 14:32   ` Carlos Maiolino
  1 sibling, 0 replies; 3+ messages in thread
From: Carlos Maiolino @ 2024-01-10 14:32 UTC (permalink / raw)
  To: Darrick J. Wong; +Cc: Christoph Hellwig, xfs

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"),

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-01-10 14:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <2onNX45hvnUFtiC16p8O9n99X8jUyzmVCTFDAh86Ad8TlJFw-TI9pxu8Q7mQX7jCGMxA4XV6Q4zisMbabJN5YQ==@protonmail.internalid>
2024-01-09  5:51 ` [PATCH] libxfs: fix krealloc to allow freeing data Darrick J. Wong
2024-01-09  5:53   ` Christoph Hellwig
2024-01-10 14:32   ` Carlos Maiolino

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox