[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH 2/2] maint: pacify GCC 6 with -Wnull-dereference
From: |
Pádraig Brady |
Subject: |
Re: [PATCH 2/2] maint: pacify GCC 6 with -Wnull-dereference |
Date: |
Wed, 27 Jul 2016 10:35:37 +0100 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 |
On 27/07/16 08:33, Bernhard Voelker wrote:
> src/id.c:249:29: error: potential null pointer dereference \
> [-Werror=null-dereference]
> pw_name = xstrdup (pwd->pw_name);
> ~~~^~~~~~~~~
> src/whoami.c:89:11: error: potential null pointer dereference \
> [-Werror=null-dereference]
> puts (pw->pw_name);
> ~~^~~~~~~~~
>
> * src/id.c (main): Explicitly exit with EXIT_FAILURE after an eror to
> help gcc-6 to detect that the dereferenced pointer is valid.
> * src/whoami.c (main): Likewise.
> ---
> src/id.c | 5 ++++-
> src/whoami.c | 7 +++++--
> 2 files changed, 9 insertions(+), 3 deletions(-)
>
> diff --git a/src/id.c b/src/id.c
> index 218ee5a..35cbeb5 100644
> --- a/src/id.c
> +++ b/src/id.c
> @@ -245,7 +245,10 @@ main (int argc, char **argv)
> }
> }
> if (pwd == NULL)
> - error (EXIT_FAILURE, 0, _("%s: no such user"), quote (spec));
> + {
> + error (0, 0, _("%s: no such user"), quote (spec));
> + exit (EXIT_FAILURE);
> + }
> pw_name = xstrdup (pwd->pw_name);
> ruid = euid = pwd->pw_uid;
> rgid = egid = pwd->pw_gid;
> diff --git a/src/whoami.c b/src/whoami.c
> index e58c575..972cd55 100644
> --- a/src/whoami.c
> +++ b/src/whoami.c
> @@ -84,8 +84,11 @@ main (int argc, char **argv)
> uid = geteuid ();
> pw = (uid == NO_UID && errno ? NULL : getpwuid (uid));
> if (!pw)
> - error (EXIT_FAILURE, errno, _("cannot find name for user ID %lu"),
> - (unsigned long int) uid);
> + {
> + error (0, errno, _("cannot find name for user ID %lu"),
> + (unsigned long int) uid);
> + exit (EXIT_FAILURE);
> + }
> puts (pw->pw_name);
> return EXIT_SUCCESS;
> }
>
Hrm, I wonder should we have an errorx to wrap error (EXIT_FAILURE, ...)
and that can be marked as noreturn? I.E. stick this somewhere?
#include <assert.h>
#include "verror.h"
static inline void
_GL_ATTRIBUTE_FORMAT ((__printf__, 3, 4))
ATTRIBUTE_NORETURN
errorx (int status, int errnum, const char *fmt, ...)
{
assert (status);
va_list ap;
va_start (ap, fmt);
verror (status, errnum, fmt, ap);
va_end (ap);
}