bug-gnulib
[Top][All Lists]
Advanced

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

Re: Port getprogname module to SCO OpenServer


From: Bruno Haible
Subject: Re: Port getprogname module to SCO OpenServer
Date: Thu, 01 Oct 2020 17:59:22 +0200
User-agent: KMail/5.1.3 (Linux/4.4.0-189-generic; KDE/5.18.0; x86_64; ; )

Hi Benji,

> I just made it read from /proc/<pid>/cmdline to get the command name. 
> The patch is below. Comments are welcome. Thanks!

Thanks for the patch. I have a couple of improvement suggestions, though:

> +  char buf[50];
> +  char *ret;
> +  int fd;
> +  int pathlen;

Can you try to minimize the scope of local variables? Listing all the
local variables upfront is BSD style and leads to code that is hard to
understand.

> +    {
> +      size_t n = read (fd, buf, 49);
> +      if (n > 0)
> +        {
> +          buf[49] = '\0'; /* Guarantee null-termination */
> +          pathlen = strlen (buf);

If n < 49, this call to strlen may read uninitialized memory, no?
Better put a NUL in buf[n], not buf[49], then.

> +          ret = malloc (pathlen + 1);
> +          if (ret)
> +            {
> +              int i;
> +              int pathsep = 0;
> +              for (i = 0; i < pathlen; i++)
> +                {
> +                  if (buf[i] == '/')
> +                    {
> +                      pathsep = i;
> +                    }
> +                }
> +              strcpy (ret, buf + pathsep + 1);

Can't this code be simplified by calling strrchr (buf, '/') ?

> +              return ret;

Is the size pathlen + 1 really needed for ret? It looks like you need
only strlen (buf + pathsep + 1) + 1 bytes.

> +          }
> +        }
> +      close (fd);
> +    }
> +  return "?";
>   # else

Bruno




reply via email to

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