diff mbox series

[4/4] t-reftable-merged: check realloc errors

Message ID 6084c017-9557-478b-b485-a1c1a21842e7@web.de (mailing list archive)
State Superseded
Headers show
Series reftable: fix realloc error handling | expand

Commit Message

René Scharfe Dec. 25, 2024, 6:38 p.m. UTC
Report reallocation errors in unit tests, like everywhere else.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 t/unit-tests/t-reftable-merged.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

--
2.47.1

Comments

Junio C Hamano Dec. 27, 2024, 5:46 a.m. UTC | #1
René Scharfe <l.s.r@web.de> writes:

> Report reallocation errors in unit tests, like everywhere else.

OK.  That's good for consistency if anything else.

We have a test framework for doing unit test at such low level, yet
we cannot really write tests that validates that the right thing
happens when a particular realloc() call returns NULL, which feels
somewhat disappointing, but that is not a fault of this series.

Thanks.


>
> Signed-off-by: René Scharfe <l.s.r@web.de>
> ---
>  t/unit-tests/t-reftable-merged.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/t/unit-tests/t-reftable-merged.c b/t/unit-tests/t-reftable-merged.c
> index a12bd0e1a3..60836f80d6 100644
> --- a/t/unit-tests/t-reftable-merged.c
> +++ b/t/unit-tests/t-reftable-merged.c
> @@ -178,7 +178,7 @@ static void t_merged_refs(void)
>  		if (err > 0)
>  			break;
>
> -		REFTABLE_ALLOC_GROW(out, len + 1, cap);
> +		check(!REFTABLE_ALLOC_GROW(out, len + 1, cap));
>  		out[len++] = ref;
>  	}
>  	reftable_iterator_destroy(&it);
> @@ -459,7 +459,7 @@ static void t_merged_logs(void)
>  		if (err > 0)
>  			break;
>
> -		REFTABLE_ALLOC_GROW(out, len + 1, cap);
> +		check(!REFTABLE_ALLOC_GROW(out, len + 1, cap));
>  		out[len++] = log;
>  	}
>  	reftable_iterator_destroy(&it);
> --
> 2.47.1
Patrick Steinhardt Dec. 27, 2024, 10:34 a.m. UTC | #2
On Thu, Dec 26, 2024 at 09:46:51PM -0800, Junio C Hamano wrote:
> René Scharfe <l.s.r@web.de> writes:
> 
> > Report reallocation errors in unit tests, like everywhere else.
> 
> OK.  That's good for consistency if anything else.
> 
> We have a test framework for doing unit test at such low level, yet
> we cannot really write tests that validates that the right thing
> happens when a particular realloc() call returns NULL, which feels
> somewhat disappointing, but that is not a fault of this series.

In the context of the reftable library we can because we've got
pluggable allocators. We could in theory swap them out against variants
that fail, but it's not easy to make them fail in one specific code
path. For the case at hand though it would work alright.

Patrick
René Scharfe Dec. 27, 2024, 8:16 p.m. UTC | #3
Am 27.12.24 um 11:34 schrieb Patrick Steinhardt:
> On Thu, Dec 26, 2024 at 09:46:51PM -0800, Junio C Hamano wrote:
>> René Scharfe <l.s.r@web.de> writes:
>>
>>> Report reallocation errors in unit tests, like everywhere else.
>>
>> OK.  That's good for consistency if anything else.
>>
>> We have a test framework for doing unit test at such low level, yet
>> we cannot really write tests that validates that the right thing
>> happens when a particular realloc() call returns NULL, which feels
>> somewhat disappointing, but that is not a fault of this series.
>
> In the context of the reftable library we can because we've got
> pluggable allocators. We could in theory swap them out against variants
> that fail, but it's not easy to make them fail in one specific code
> path. For the case at hand though it would work alright.

It should be easy and safe to provide allocator stubs that just fail
and swap them in and out with reftable_set_alloc() before and after
an operation that performs a single allocation.  Will add basic
tests in the next round.

Injecting an allocation error in the middle of parse_names() would
require a version that starts failing after a certain number of
allocations (or allocated bytes).  Possible, but not in scope for
this series.

René
diff mbox series

Patch

diff --git a/t/unit-tests/t-reftable-merged.c b/t/unit-tests/t-reftable-merged.c
index a12bd0e1a3..60836f80d6 100644
--- a/t/unit-tests/t-reftable-merged.c
+++ b/t/unit-tests/t-reftable-merged.c
@@ -178,7 +178,7 @@  static void t_merged_refs(void)
 		if (err > 0)
 			break;

-		REFTABLE_ALLOC_GROW(out, len + 1, cap);
+		check(!REFTABLE_ALLOC_GROW(out, len + 1, cap));
 		out[len++] = ref;
 	}
 	reftable_iterator_destroy(&it);
@@ -459,7 +459,7 @@  static void t_merged_logs(void)
 		if (err > 0)
 			break;

-		REFTABLE_ALLOC_GROW(out, len + 1, cap);
+		check(!REFTABLE_ALLOC_GROW(out, len + 1, cap));
 		out[len++] = log;
 	}
 	reftable_iterator_destroy(&it);