bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: bug-gnulib@gnu.org, Paul Eggert <eggert@cs.ucla.edu>
Subject: Re: [PATCH 1/4] localename: -Wtautological-pointer-compare
Date: Fri, 13 Jan 2023 23:59:02 +0100	[thread overview]
Message-ID: <17910367.MNNF8PUAaN@nimes> (raw)
In-Reply-To: <20230113201704.325290-1-eggert@cs.ucla.edu>

Paul Eggert wrote:
> Problem found by xlclang 16.1 on AIX 7.2.
> * lib/localename.c (duplocale, freelocale):
> Omit unnecessary comparison of non-null args to NULL.

I disagree with this patch.

Compiler warnings are supposed to help us improve the code.

Replacing a function that starts with an entry check — which is a good
practice [1] — with one that operates in garbage-in - garbage-out fashion
is not an improvement; quite the opposite.

We already know how to handle this situation: see
  canonicalize-lgpl.c
  execl.c
  execle.c
  execlp.c
  execve.c
  execvpe.c
  getaddrinfo.c
  getdelim.c
  getpass.c
  glob.c
  random_r.c
  setenv.c
  tsearch.c
  unsetenv.c

[1] https://cwe.mitre.org/data/definitions/20.html


2023-01-13  Bruno Haible  <bruno@clisp.org>

	localename: Fix -Wtautological-pointer-compare warning in a better way.
	* lib/localename.c (duplocale, freelocale): Revert last patch.
	(_GL_ARG_NONNULL): Define to empty.

diff --git a/lib/localename.c b/lib/localename.c
index 5a178c68fe..8fe90e0bf2 100644
--- a/lib/localename.c
+++ b/lib/localename.c
@@ -18,6 +18,12 @@
 /* Native Windows code written by Tor Lillqvist <tml@iki.fi>.  */
 /* Mac OS X code written by Bruno Haible <bruno@clisp.org>.  */
 
+/* Don't use __attribute__ __nonnull__ in this compilation unit.  Otherwise gcc
+   optimizes away the locale == NULL tests below in duplocale() and freelocale(),
+   or xlclang reports -Wtautological-pointer-compare warnings for these tests.
+ */
+#define _GL_ARG_NONNULL(params)
+
 #include <config.h>
 
 /* Specification.  */
@@ -2967,6 +2973,10 @@ duplocale (locale_t locale)
   struct locale_hash_node *node;
   locale_t result;
 
+  if (locale == NULL)
+    /* Invalid argument.  */
+    abort ();
+
   node = (struct locale_hash_node *) malloc (sizeof (struct locale_hash_node));
   if (node == NULL)
     /* errno is set to ENOMEM.  */
@@ -3052,7 +3062,7 @@ void
 freelocale (locale_t locale)
 #undef freelocale
 {
-  if (locale == LC_GLOBAL_LOCALE)
+  if (locale == NULL || locale == LC_GLOBAL_LOCALE)
     /* Invalid argument.  */
     abort ();
 





  parent reply	other threads:[~2023-01-13 22:59 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-13 20:17 [PATCH 1/4] localename: -Wtautological-pointer-compare Paul Eggert
2023-01-13 20:17 ` [PATCH 2/4] Don’t use alloc_size with xlclang 16.1 Paul Eggert
2023-01-13 20:17 ` [PATCH 3/4] assert-h: fix configure comment-out Paul Eggert
2023-01-13 20:17 ` [PATCH 4/4] assert-h: suppress xlclang 16.1 false alarms Paul Eggert
2023-01-13 22:59 ` Bruno Haible [this message]
2023-01-13 23:36   ` [PATCH 1/4] localename: -Wtautological-pointer-compare Paul Eggert
2023-01-14 11:00     ` Bruno Haible
2023-01-15  3:02       ` Paul Eggert
2023-01-15 22:03         ` Bruno Haible
2023-01-16  0:15           ` Paul Eggert

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=17910367.MNNF8PUAaN@nimes \
    --to=bruno@clisp.org \
    --cc=bug-gnulib@gnu.org \
    --cc=eggert@cs.ucla.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).