Subject: bug#31072: 27.0.50; Assertion failure in defvar

From: Philipp Stephani <[email-masked]>
Date: Fri, 06 Apr 2018 15:57:40 +0000

Philipp <[email-masked]> schrieb am Fr., 6. Apr. 2018 um 03:25 Uhr:

>
> Loading the following file triggers an assertion failure:
>
> ;;; -*- lexical-binding: t; -*-
> (defvar 1)
>
> $ emacs -Q -batch -l /tmp/crash.el
> eval.c:772: Emacs fatal error: assertion failed: SYMBOLP (sym)
> Fatal error 6: Abort trapAbort trap: 6
>
> This is because Fdefvar lacks a CHECK_SYMBOL in the second branch
> (lexical binding but only one argument)
>

Here is a patch.
From e4e301c8228e6d29cec6b44d86da47a0db8f3e0c Mon Sep 17 00:00:00 2001
From: Philipp Stephani <[email-masked]>
Date: Fri, 6 Apr 2018 17:55:59 +0200
Subject: [PATCH] Avoid undefined behavior in 'defvar' (Bug#31072)

* src/eval.c (Fdefvar): Check that first argument is a symbol.
* test/src/eval-tests.el (defvar/bug31072): New unit test.
---
 src/eval.c             | 2 ++
 test/src/eval-tests.el | 4 ++++
 2 files changed, 6 insertions(+)

diff --git a/src/eval.c b/src/eval.c
index a6e1d86c4a..90d8c33518 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -737,6 +737,8 @@ usage: (defvar SYMBOL &optional INITVALUE DOCSTRING)  */)
   sym = XCAR (args);
   tail = XCDR (args);
 
+  CHECK_SYMBOL (sym);
+
   if (!NILP (tail))
     {
       if (!NILP (XCDR (tail)) && !NILP (XCDR (XCDR (tail))))
diff --git a/test/src/eval-tests.el b/test/src/eval-tests.el
index 59da6b7cc3..319dd91c86 100644
--- a/test/src/eval-tests.el
+++ b/test/src/eval-tests.el
@@ -113,4 +113,8 @@ eval-tests--exceed-specbind-limit
         (signal-hook-function #'ignore))
     (should-error (eval-tests--exceed-specbind-limit))))
 
+(ert-deftest defvar/bug31072 ()
+  "Check that Bug#31072 is fixed."
+  (should-error (eval '(defvar 1) t) :type 'wrong-type-argument))
+
 ;;; eval-tests.el ends here
-- 
2.17.0