bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* [PATCH] Fix pointer comparison
@ 2019-02-28 12:22 Michal Privoznik
  2019-03-03 18:45 ` Bruno Haible
  0 siblings, 1 reply; 2+ messages in thread
From: Michal Privoznik @ 2019-02-28 12:22 UTC (permalink / raw)
  To: bug-gnulib

While generally it might be safe to compare pointer against
literal 0, it may make one think that the pointer is an int.
Use explicit comparison against NULL (or !ptr).

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 lib/alloca.c         | 10 +++++-----
 lib/backup-find.c    |  2 +-
 lib/bitset/list.c    |  2 +-
 lib/bitset/table.c   |  2 +-
 lib/getloadavg.c     | 12 ++++++------
 lib/obstack.c        | 10 +++++-----
 lib/obstack.h        |  2 +-
 lib/timevar.c        |  2 +-
 tests/test-regex.c   |  2 +-
 tests/test-tsearch.c |  2 +-
 10 files changed, 23 insertions(+), 23 deletions(-)

diff --git a/lib/alloca.c b/lib/alloca.c
index ee0f01886..d0476d53e 100644
--- a/lib/alloca.c
+++ b/lib/alloca.c
@@ -350,16 +350,16 @@ i00afunc (long *address)
   /* There must be at least one stack segment.  Therefore it is
      a fatal error if "trailer" is null.  */
 
-  if (trailer == 0)
+  if (trailer == NULL)
     abort ();
 
   /* Discard segments that do not contain our argument address.  */
 
-  while (trailer != 0)
+  while (trailer != NULL)
     {
       block = (long *) trailer->this_address;
       size = trailer->this_size;
-      if (block == 0 || size == 0)
+      if (block == NULL || size == 0)
         abort ();
       trailer = (struct stk_trailer *) trailer->link;
       if ((block <= address) && (address < (block + size)))
@@ -371,7 +371,7 @@ i00afunc (long *address)
 
   result = address - block;
 
-  if (trailer == 0)
+  if (trailer == NULL)
     {
       return result;
     }
@@ -383,7 +383,7 @@ i00afunc (long *address)
       result += trailer->this_size;
       trailer = (struct stk_trailer *) trailer->link;
     }
-  while (trailer != 0);
+  while (trailer != NULL);
 
   /* We are done.  Note that if you present a bogus address (one
      not in any segment), you will get a different number back, formed
diff --git a/lib/backup-find.c b/lib/backup-find.c
index c038ae6dc..66e4c9b31 100644
--- a/lib/backup-find.c
+++ b/lib/backup-find.c
@@ -70,7 +70,7 @@ ARGMATCH_VERIFY (backup_args, backup_types);
 enum backup_type
 get_version (char const *context, char const *version)
 {
-  if (version == 0 || *version == 0)
+  if (version == NULL || *version == 0)
     return numbered_existing_backups;
   else
     return XARGMATCH (context, version, backup_args, backup_types);
diff --git a/lib/bitset/list.c b/lib/bitset/list.c
index f42edb8ea..0b0a91ead 100644
--- a/lib/bitset/list.c
+++ b/lib/bitset/list.c
@@ -95,7 +95,7 @@ lbitset_elt_alloc (void)
 {
   lbitset_elt *elt;
 
-  if (lbitset_free_list != 0)
+  if (lbitset_free_list != NULL)
     {
       elt = lbitset_free_list;
       lbitset_free_list = elt->next;
diff --git a/lib/bitset/table.c b/lib/bitset/table.c
index 8351cf784..90b5f1858 100644
--- a/lib/bitset/table.c
+++ b/lib/bitset/table.c
@@ -174,7 +174,7 @@ tbitset_elt_alloc (void)
 {
   tbitset_elt *elt;
 
-  if (tbitset_free_list != 0)
+  if (tbitset_free_list != NULL)
     {
       elt = tbitset_free_list;
       tbitset_free_list = EBITSET_NEXT (elt);
diff --git a/lib/getloadavg.c b/lib/getloadavg.c
index 353664777..e9b3b5350 100644
--- a/lib/getloadavg.c
+++ b/lib/getloadavg.c
@@ -424,17 +424,17 @@ getloadavg (double loadavg[], int nelem)
   int saved_errno;
 
   kc = kstat_open ();
-  if (kc == 0)
+  if (!kc)
     return -1;
   ksp = kstat_lookup (kc, "unix", 0, "system_misc");
-  if (ksp == 0)
+  if (!ksp)
     return -1;
   if (kstat_read (kc, ksp, 0) == -1)
     return -1;
 
 
   kn = kstat_data_lookup (ksp, "avenrun_1min");
-  if (kn == 0)
+  if (!kn)
     {
       /* Return -1 if no load average information is available.  */
       nelem = 0;
@@ -447,14 +447,14 @@ getloadavg (double loadavg[], int nelem)
   if (nelem >= 2)
     {
       kn = kstat_data_lookup (ksp, "avenrun_5min");
-      if (kn != 0)
+      if (kn)
         {
           loadavg[elem++] = (double) kn->value.ul / FSCALE;
 
           if (nelem >= 3)
             {
               kn = kstat_data_lookup (ksp, "avenrun_15min");
-              if (kn != 0)
+              if (kn)
                 loadavg[elem++] = (double) kn->value.ul / FSCALE;
             }
         }
@@ -895,7 +895,7 @@ getloadavg (double loadavg[], int nelem)
       /* We pass 0 for the kernel, corefile, and swapfile names
          to use the currently running kernel.  */
       kd = kvm_open (0, 0, 0, O_RDONLY, 0);
-      if (kd != 0)
+      if (kd)
         {
           /* nlist the currently running kernel.  */
           kvm_nlist (kd, name_list);
diff --git a/lib/obstack.c b/lib/obstack.c
index 6949111e4..49e934d45 100644
--- a/lib/obstack.c
+++ b/lib/obstack.c
@@ -247,12 +247,12 @@ _obstack_allocated_p (struct obstack *h, void *obj)
   /* We use >= rather than > since the object cannot be exactly at
      the beginning of the chunk but might be an empty object exactly
      at the end of an adjacent chunk.  */
-  while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
+  while (lp != NULL && ((void *) lp >= obj || (void *) (lp)->limit < obj))
     {
       plp = lp->prev;
       lp = plp;
     }
-  return lp != 0;
+  return lp != NULL;
 }
 
 /* Free objects in obstack H, including OBJ and everything allocate
@@ -268,7 +268,7 @@ _obstack_free (struct obstack *h, void *obj)
   /* We use >= because there cannot be an object at the beginning of a chunk.
      But there can be an empty object at that address
      at the end of another chunk.  */
-  while (lp != 0 && ((void *) lp >= obj || (void *) (lp)->limit < obj))
+  while (lp != NULL && ((void *) lp >= obj || (void *) (lp)->limit < obj))
     {
       plp = lp->prev;
       call_freefun (h, lp);
@@ -283,7 +283,7 @@ _obstack_free (struct obstack *h, void *obj)
       h->chunk_limit = lp->limit;
       h->chunk = lp;
     }
-  else if (obj != 0)
+  else if (obj != NULL)
     /* obj is not in any of the chunks! */
     abort ();
 }
@@ -294,7 +294,7 @@ _obstack_memory_used (struct obstack *h)
   struct _obstack_chunk *lp;
   _OBSTACK_SIZE_T nbytes = 0;
 
-  for (lp = h->chunk; lp != 0; lp = lp->prev)
+  for (lp = h->chunk; lp != NULL; lp = lp->prev)
     {
       nbytes += lp->limit - (char *) lp;
     }
diff --git a/lib/obstack.h b/lib/obstack.h
index 811de588a..7430d48e4 100644
--- a/lib/obstack.h
+++ b/lib/obstack.h
@@ -316,7 +316,7 @@ extern int obstack_exit_failure;
 # define obstack_empty_p(OBSTACK)					      \
   __extension__								      \
     ({ struct obstack const *__o = (OBSTACK);				      \
-       (__o->chunk->prev == 0						      \
+       (__o->chunk->prev == NULL						      \
         && __o->next_free == __PTR_ALIGN ((char *) __o->chunk,		      \
                                           __o->chunk->contents,		      \
                                           __o->alignment_mask)); })
diff --git a/lib/timevar.c b/lib/timevar.c
index 440ba0c01..af28082c7 100644
--- a/lib/timevar.c
+++ b/lib/timevar.c
@@ -295,7 +295,7 @@ timevar_print (FILE *fp)
 
   /* Update timing information in case we're calling this from GDB.  */
 
-  if (fp == 0)
+  if (fp == NULL)
     fp = stderr;
 
   /* What time is it?  */
diff --git a/tests/test-regex.c b/tests/test-regex.c
index 2ffad4c0f..9192c79f7 100644
--- a/tests/test-regex.c
+++ b/tests/test-regex.c
@@ -156,7 +156,7 @@ main (void)
   re_set_syntax (RE_SYNTAX_POSIX_EGREP | RE_NO_EMPTY_RANGES);
   memset (&regex, 0, sizeof regex);
   s = re_compile_pattern ("a[b-a]", 6, &regex);
-  if (s == 0)
+  if (!s)
     {
       result |= 8;
       regfree (&regex);
diff --git a/tests/test-tsearch.c b/tests/test-tsearch.c
index 808a82700..6d544c338 100644
--- a/tests/test-tsearch.c
+++ b/tests/test-tsearch.c
@@ -223,7 +223,7 @@ mangle_tree (enum order how, enum action what, void **root, int lag)
                   error = 1;
                 }
               elem = tsearch (x + j, root, cmp_fn);
-              if (elem == 0
+              if (!elem
                   || tfind (x + j, (void *const *) root, cmp_fn) == NULL)
                 {
                   fputs ("Couldn't find element after it was added.\n",
-- 
2.19.2



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] Fix pointer comparison
  2019-02-28 12:22 [PATCH] Fix pointer comparison Michal Privoznik
@ 2019-03-03 18:45 ` Bruno Haible
  0 siblings, 0 replies; 2+ messages in thread
From: Bruno Haible @ 2019-03-03 18:45 UTC (permalink / raw)
  To: bug-gnulib; +Cc: Michal Privoznik

Hi Michal,

> it may make one think that the pointer is an int.
> Use explicit comparison against NULL (or !ptr).

I agree with you. Use of '0' to designate a NULL pointer is confusing
to most of us.

>  lib/alloca.c         | 10 +++++-----
>  tests/test-tsearch.c |  2 +-

Applied.

>  lib/getloadavg.c     | 12 ++++++------

Here I preferred to write NULL instead of the boolean negation operator.

>  lib/backup-find.c    |  2 +-
>  lib/obstack.c        | 10 +++++-----
>  lib/obstack.h        |  2 +-
>  tests/test-regex.c   |  2 +-

Paul may want to handle these.

>  lib/bitset/list.c    |  2 +-
>  lib/bitset/table.c   |  2 +-
>  lib/timevar.c        |  2 +-

Akim may want to handle these.

Bruno



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-03-03 18:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-28 12:22 [PATCH] Fix pointer comparison Michal Privoznik
2019-03-03 18:45 ` Bruno Haible

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).