all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Paul Eggert <eggert@cs.ucla.edu>
To: YAMAMOTO Mitsuharu <mituharu@math.s.chiba-u.ac.jp>,
	 "Herbert J. Skuhra" <herbert@oslo.ath.cx>
Cc: emacs-devel@gnu.org
Subject: Re: [Emacs-diffs] master bc78ff2 2/2: Use 0 for Qnil
Date: Wed, 07 Jan 2015 23:05:31 -0800	[thread overview]
Message-ID: <54AE2C3B.5030807@cs.ucla.edu> (raw)
In-Reply-To: <wl4ms1zvzh.wl%mituharu@math.s.chiba-u.ac.jp>

[-- Attachment #1: Type: text/plain, Size: 492 bytes --]

YAMAMOTO Mitsuharu wrote:
> With the above macros, XUNTAG (a, type) is expanded to:
>
>    ((void *) ((char *) (0) - (type) + (intptr_t) XLI (a)))
>
> It is relative to the NULL pointer and clang seems to treat its
> dereference as an undefined behavior.

Thanks for diagnosing that.  This is arguably a bug in clang, but it's easy 
enough to work around as you suggest.  I reproduced the problem with Fedora 21 
x86-64 (clang 3.5.0) and installed the attached patch, which fixed it for me.


[-- Attachment #2: 0001-Port-Qnil-0-XUNTAG-to-clang.patch --]
[-- Type: text/x-patch, Size: 2082 bytes --]

From 8d025fa15493be4079d05629e9ca8b7ae325fc58 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Wed, 7 Jan 2015 23:02:01 -0800
Subject: [PATCH] Port Qnil==0 XUNTAG to clang

clang has undefined behavior if the program subtracts an integer
from (char *) 0.  Problem reported by YAMAMOTO Mitsuharu in:
http://lists.gnu.org/archive/html/emacs-devel/2015-01/msg00132.html
* lisp.h (lisp_h_XUNTAG) [USE_LSB_TAG]:
(XUNTAG) [!USE_LSB_TAG]: Port to clang 3.5.0.
---
 src/ChangeLog | 7 +++++++
 src/lisp.h    | 5 +++--
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index eae16c2..817483b 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,12 @@
 2015-01-08  Paul Eggert  <eggert@cs.ucla.edu>
 
+	Port Qnil==0 XUNTAG to clang
+	clang has undefined behavior if the program subtracts an integer
+	from (char *) 0.  Problem reported by YAMAMOTO Mitsuharu in:
+	http://lists.gnu.org/archive/html/emacs-devel/2015-01/msg00132.html
+	* lisp.h (lisp_h_XUNTAG) [USE_LSB_TAG]:
+	(XUNTAG) [!USE_LSB_TAG]: Port to clang 3.5.0.
+
 	Port GFileMonitor * hack to Qnil==0 platforms
 	Reported by Glenn Morris in: http://bugs.gnu.org/15880#112
 	* gfilenotify.c (monitor_to_lisp, lisp_to_monitor): New functions.
diff --git a/src/lisp.h b/src/lisp.h
index 1f18b5e..b9263f8 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -368,7 +368,7 @@ error !;
 # define lisp_h_XFASTINT(a) XINT (a)
 # define lisp_h_XINT(a) (XLI (a) >> INTTYPEBITS)
 # define lisp_h_XTYPE(a) ((enum Lisp_Type) (XLI (a) & ~VALMASK))
-# define lisp_h_XUNTAG(a, type) XUNTAGBASE (a, type, 0)
+# define lisp_h_XUNTAG(a, type) ((void *) (intptr_t) (XLI (a) - (type)))
 # define lisp_h_XUNTAGBASE(a, type, base) \
     ((void *) ((char *) (base) - (type) + (intptr_t) XLI (a)))
 #endif
@@ -905,7 +905,8 @@ XUNTAGBASE (Lisp_Object a, int type, void *base)
 INLINE void *
 XUNTAG (Lisp_Object a, int type)
 {
-  return XUNTAGBASE (a, type, 0);
+  intptr_t i = USE_LSB_TAG ? XLI (a) - type : XLI (a) & VALMASK;
+  return (void *) i;
 }
 
 #endif /* ! USE_LSB_TAG */
-- 
2.1.0


      reply	other threads:[~2015-01-08  7:05 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20150105181510.19723.18782@vcs.savannah.gnu.org>
     [not found] ` <E1Y8CBN-0005MY-1J@vcs.savannah.gnu.org>
2015-01-07 21:57   ` [Emacs-diffs] master bc78ff2 2/2: Use 0 for Qnil Herbert J. Skuhra
2015-01-08  2:10     ` Paul Eggert
2015-01-08  2:29       ` Perry E. Metzger
2015-01-08  5:23     ` YAMAMOTO Mitsuharu
2015-01-08  7:05       ` Paul Eggert [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

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

  git send-email \
    --in-reply-to=54AE2C3B.5030807@cs.ucla.edu \
    --to=eggert@cs.ucla.edu \
    --cc=emacs-devel@gnu.org \
    --cc=herbert@oslo.ath.cx \
    --cc=mituharu@math.s.chiba-u.ac.jp \
    /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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.