unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Matt Armstrong <matt@rfc20.org>
To: Stefan Monnier <monnier@iro.umontreal.ca>,
	Paul Eggert <eggert@cs.ucla.edu>
Cc: emacs-devel@gnu.org
Subject: Re: master 31544bc: Don’t convert pointer to bool
Date: Sat, 20 Mar 2021 11:21:03 -0700	[thread overview]
Message-ID: <87ft0phek0.fsf@rfc20.org> (raw)
In-Reply-To: <jwvmtux53cu.fsf-monnier+emacs@gnu.org>

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>>     Without this patch, Oracle Studio 12.6 complains about converting
>>     pointer to bool.
>
> Is it right to complain or is that a bug in that tool?
> To my naive intuition, it looks wrong since conversion to bool is
> standard practice when doing `if (ptr) ...`.

In code portable to pre-C99 the diff looks right to me.

Commit 31544bc was about assignment to "bool", not "if (ptr)...", and
the differece is relevant.

"if (ptr)..." and the like has always been valid C.  Instead, the
statememnts test the scalar expression against the zero value, which has
always well defined for pointers.

Essentially, in conditional statements there is an implicit "!= 0"
there.  There is no implicit "!= 0" when converting pointers to integral
types.

In older compilers without _Bool, "bool" usually devolves to char.

  void* p = ...;
  char c = p;

The above code has undefined behavior, since a pointer doesn't fit in a
char.  In practice, most compilers will generate code that assigns the
low order byte of p to b.  This can do things like set b zero for
non-zero pointers and set b to values outside [0, 1].

So, either of these are better:

  char c = !!p;
  char c = p ? true : false;



      parent reply	other threads:[~2021-03-20 18:21 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20210320004804.9441.45535@vcs0.savannah.gnu.org>
     [not found] ` <20210320004805.B7BBF2101A@vcs0.savannah.gnu.org>
2021-03-20 14:05   ` master 31544bc: Don’t convert pointer to bool Stefan Monnier
2021-03-20 17:01     ` Paul Eggert
2021-03-20 18:21     ` Matt Armstrong [this message]

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://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=87ft0phek0.fsf@rfc20.org \
    --to=matt@rfc20.org \
    --cc=eggert@cs.ucla.edu \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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