[systemd-devel] [PATCH 5/6] Add hexstr function

Lennart Poettering lennart at poettering.net
Tue Feb 12 18:20:24 PST 2013


On Tue, 12.02.13 10:22, Oleksii Shevchuk (alxchk at gmail.com) wrote:

> Function convers byte array to hex string
> ---
>  src/shared/util.c | 23 +++++++++++++++++++++++
>  src/shared/util.h |  1 +
>  2 files changed, 24 insertions(+)
> 
> diff --git a/src/shared/util.c b/src/shared/util.c
> index 24f9e7e..2a5cbbb 100644
> --- a/src/shared/util.c
> +++ b/src/shared/util.c
> @@ -1368,6 +1368,29 @@ char hexchar(int x) {
>          return table[x & 15];
>  }
>  
> +char * hexstr (const uint8_t *in, size_t count)
> +{

{ please on the same line as the function definition...

> +        char *r = NULL, *i = NULL;
> +
> +        if (!in || !count)
> +                goto finish;

This function should probably handle zero-length blobs too. You probably
want to return an empty string in this case.

> +
> +        r = i = new(char, count * 2 + 1);
> +        if (! r)
> +                goto finish;

goto is not particularly sexy. If there's no reason to use it (because
you don't actually have anything to free on the "finish" label) then
it's nicer to do without goto, and just return directly with NULL.

Lennart

-- 
Lennart Poettering - Red Hat, Inc.


More information about the systemd-devel mailing list