diff mbox series

[v2,3/4] reftable: handle realloc error in parse_names()

Message ID c2cd277b-ba9d-41cb-b9bd-c519b445d179@web.de (mailing list archive)
State New
Headers show
Series reftable: fix realloc error handling | expand

Commit Message

René Scharfe Dec. 28, 2024, 9:48 a.m. UTC
Check the final reallocation for adding the terminating NULL and handle
it just like those in the loop.  Simply use REFTABLE_ALLOC_GROW instead
of keeping the REFTABLE_REALLOC_ARRAY call and adding code to preserve
the original pointer value around it.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 reftable/basics.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

--
2.47.1

Comments

Patrick Steinhardt Dec. 30, 2024, 7:25 a.m. UTC | #1
On Sat, Dec 28, 2024 at 10:48:50AM +0100, René Scharfe wrote:
> Check the final reallocation for adding the terminating NULL and handle
> it just like those in the loop.  Simply use REFTABLE_ALLOC_GROW instead
> of keeping the REFTABLE_REALLOC_ARRAY call and adding code to preserve
> the original pointer value around it.
> 
> Signed-off-by: René Scharfe <l.s.r@web.de>
> ---
>  reftable/basics.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/reftable/basics.c b/reftable/basics.c
> index cd6b39dbe9..fe2b83ff83 100644
> --- a/reftable/basics.c
> +++ b/reftable/basics.c
> @@ -241,7 +241,8 @@ char **parse_names(char *buf, int size)
>  		p = next + 1;
>  	}
> 
> -	REFTABLE_REALLOC_ARRAY(names, names_len + 1);
> +	if (REFTABLE_ALLOC_GROW(names, names_len + 1, names_cap))
> +		goto err;
>  	names[names_len] = NULL;

Okay. We may be overallocating in some cases now, but it's most likely
that we can completely skip this allocation here because `names_cap` is
big enough already.

Patrick
diff mbox series

Patch

diff --git a/reftable/basics.c b/reftable/basics.c
index cd6b39dbe9..fe2b83ff83 100644
--- a/reftable/basics.c
+++ b/reftable/basics.c
@@ -241,7 +241,8 @@  char **parse_names(char *buf, int size)
 		p = next + 1;
 	}

-	REFTABLE_REALLOC_ARRAY(names, names_len + 1);
+	if (REFTABLE_ALLOC_GROW(names, names_len + 1, names_cap))
+		goto err;
 	names[names_len] = NULL;

 	return names;