Subject: Re: [PATCH iproute2] tc class: Don't fail if there is no default names file

From: Daniel Borkmann <[email-masked]>
Date: Tue, 17 Mar 2015 15:32:39 +0100

On 03/17/2015 02:41 PM, Vadim Kochan wrote:
> From: Vadim Kochan <[email-masked]>
>
> Added the following changes in one patch:
>
> 1) Split db_names_init into alloc & load funcs
>
> 2) Added checks for out of memory with new alloc helpers
>
> 3) Ignore if there is no default class names file
>
> 3) Changed default class names path cls_names -> tc_cls
>
> Signed-off-by: Vadim Kochan <[email-masked]>
...
> diff --git a/lib/utils.c b/lib/utils.c
> index 9cda268..d119a69 100644
> --- a/lib/utils.c
> +++ b/lib/utils.c
> @@ -904,3 +904,29 @@ char *int_to_str(int val, char *buf)
>   	sprintf(buf, "%d", val);
>   	return buf;
>   }
> +
> +void *xmalloc(size_t size)
> +{
> +	void *ptr = malloc(size);
> +	if (!ptr)
> +		fprintf(stderr, "xmalloc: Out of memory\n");
> +
> +	return ptr;
> +}
> +
> +void xfree(void *ptr)
> +{
> +	if (!ptr)
> +		fprintf(stderr, "xfree: ptr is NULL\n");
> +	else
> +		free(ptr);
> +}
> +
> +char *xstrdup(char *str)
> +{
> +	char *ptr = strdup(str);
> +	if (!ptr)
> +		fprintf(stderr, "xstrdup: Out of memory\n");
> +
> +	return ptr;
> +}

When you add xmalloc and friends, the expectation is rather
different then what you've implemented : i.e. xmalloc dies
in case the allocation fails, so that xmalloc() et al users
don't have to care about the NULL check.

Simple example:

void *xmalloc(size_t size)
{
	void *ptr;

	ptr = malloc(size);
	if (unlikely(ptr == NULL))
		panic("xmalloc: out of memory (allocating %zu bytes)\n",
		      size);

	return ptr;
}

For a more elaborate example, you can also have a look at git,
e.g. wrapper.c +84, but a simpler form is sufficient here.

I think there are two possible ways forward: either use normal
malloc() and check for NULL, or you could add in a different set
xmalloc() et al, and convert also other users from iproute2 that
don't have an error recovery, iow do abort() or the like.

Other than that, would be good if you split up this patch into
a set.