poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH 3/6] ios: Check ios->handler_normalize for memory allocation


From: Tim Rühsen
Subject: Re: [PATCH 3/6] ios: Check ios->handler_normalize for memory allocation error.
Date: Fri, 8 May 2020 09:13:24 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.8.0


On 07.05.20 18:42, Jose E. Marchesi wrote:
>     
>      # gnulib modules used in libpoke/.
>      libpoke_modules="
>     +  errno
>        fstat
>        gcd
>        gettext-h
> 
> Should we be using the global `errno' at all in a library?

module errno makes #include <errno> reliable.

> 
>     diff --git a/libpoke/ios.c b/libpoke/ios.c
>     index ebf5f5bc..74fd3719 100644
>     --- a/libpoke/ios.c
>     +++ b/libpoke/ios.c
>     @@ -26,6 +26,7 @@
>      #include <limits.h>
>      #define _(str) gettext (str)
>      #include <streq.h>
>     +#include <errno.h>
>     
>      #include "pk-utils.h"
>      #include "ios.h"
>     @@ -120,7 +121,7 @@ ios_open (const char *handler, uint64_t flags, int 
> set_cur)
>      {
>        struct ios *io = NULL;
>        struct ios_dev_if **dev_if = NULL;
>     -  int error, ret;
>     +  int error = IOD_ERROR, ret;
>     
>        /* Allocate and initialize the new IO space.  */
>        io = xmalloc (sizeof (struct ios));
>     @@ -130,11 +131,14 @@ ios_open (const char *handler, uint64_t flags, int 
> set_cur)
>     
>        /* Look for a device interface suitable to operate on the given
>           handler.  */
>     +  errno = 0;
>        for (dev_if = ios_dev_ifs; *dev_if; ++dev_if)
>          {
>            io->handler = (*dev_if)->handler_normalize (handler);
>            if (io->handler)
>              break;
>     +      if (errno == ENOMEM)
>     +        goto error;
>          }
>     
>        if (*dev_if == NULL)
> 
> Why would be want to check for `errno' after calling handler_normalize?
> The function does not return any error code.

handler_normalize may return NULL for two cases: no memory and some
asprintf error. Or ios_dev_mem_open return NULL if mio is NULL or (with
NBD) if the URL type is wrong.
That's why I set errno=0 and then check for ENOMEM.

ios_dev_file_handler_normalize uses asprintf:
asprintf man page "If memory  allocation wasn't possible, or some other
error occurs, these functions will return -1"

ios_dev_nbd_handler_normalize returns NULL if 'handler' has the wrong
URL type.

Tim

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]