Hi Ilya,

On Wed, Dec 25, 2024 at 10:10:12PM +0100, Ilia Shipitsin wrote:
> This defect was found by the coccinelle script "unchecked-strdup.cocci".
> It can be backported to all supported branches.
> ---
>  src/compression.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/src/compression.c b/src/compression.c
> index a4464e09b..edf5553c1 100644
> --- a/src/compression.c
> +++ b/src/compression.c
> @@ -119,6 +119,8 @@ int comp_append_type(struct comp_type **types, const char 
> *type)
>               return 1;
>       comp_type->name_len = strlen(type);
>       comp_type->name = strdup(type);
> +     if (!comp_type->name)
> +             return 1;
>       comp_type->next = *types;
>       *types = comp_type;
>       return 0;

For this one we must free comp_type before returning, since it was
allocated earlier. As in the previous series it could also be done
using jumps:

    comp_type = calloc();
    if (!comp_type)
        goto fail;
    ...
    comp_type->name = strdup(type);
    if (!comp_type->name)
        goto fail_free_comp_type;
    ...
    return 0;
fail_free_comp_type:
    free(comp_type);
fail:
    return 1;

The function is short so either solutions are fine to me, that's as
you prefer.

Thanks!
Willy


Reply via email to