From unknown Fri Aug 08 22:23:35 2025 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Mailer: MIME-tools 5.509 (Entity 5.509) Content-Type: text/plain; charset=utf-8 From: bug#10146 <10146@debbugs.gnu.org> To: bug#10146 <10146@debbugs.gnu.org> Subject: Status: error in aput in assoc.el, Emacs 24, 23, and 22 (at least) Reply-To: bug#10146 <10146@debbugs.gnu.org> Date: Sat, 09 Aug 2025 05:23:35 +0000 retitle 10146 error in aput in assoc.el, Emacs 24, 23, and 22 (at least) reassign 10146 emacs submitter 10146 Christopher Genovese severity 10146 normal thanks From debbugs-submit-bounces@debbugs.gnu.org Sun Nov 27 01:00:54 2011 Received: (at submit) by debbugs.gnu.org; 27 Nov 2011 06:00:54 +0000 Received: from localhost ([127.0.0.1] helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1RUXnE-0007UD-Iq for submit@debbugs.gnu.org; Sun, 27 Nov 2011 01:00:54 -0500 Received: from eggs.gnu.org ([140.186.70.92]) by debbugs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1RUXin-0007N6-0T for submit@debbugs.gnu.org; Sun, 27 Nov 2011 00:56:14 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RUXh3-0007yQ-9R for submit@debbugs.gnu.org; Sun, 27 Nov 2011 00:54:26 -0500 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on eggs.gnu.org X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_LOW,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from lists.gnu.org ([140.186.70.17]:56693) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RUXh3-0007yL-7y for submit@debbugs.gnu.org; Sun, 27 Nov 2011 00:54:25 -0500 Received: from eggs.gnu.org ([140.186.70.92]:35942) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RUXh2-0000xa-0M for bug-gnu-emacs@gnu.org; Sun, 27 Nov 2011 00:54:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RUXh0-0007y1-I5 for bug-gnu-emacs@gnu.org; Sun, 27 Nov 2011 00:54:23 -0500 Received: from mail-pz0-f44.google.com ([209.85.210.44]:64817) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RUXh0-0007xo-5f for bug-gnu-emacs@gnu.org; Sun, 27 Nov 2011 00:54:22 -0500 Received: by pzk33 with SMTP id 33so9110456pzk.3 for ; Sat, 26 Nov 2011 21:54:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:from:date:message-id:subject:to:content-type; bh=x1IIeV85e0MFpMKPwyQDC26LVXCEgGFboh+AlwE1Qlg=; b=xmHMUiqkoui84OKzcq73p2zBbYx+vXd+fGsb/tGgFnijr0cShdTESclclhS3GsF1MK z/EgxhVJgxBRtsyjdh+qG4Yh4Et3BDxY+vyjPzrxuIfU16Pi820jOT/BuR3UIkPZ7J9U KkezCVJUJo7VgIKXEH81Tj6ORRD5yInMVzqNI= Received: by 10.68.44.133 with SMTP id e5mr3052348pbm.44.1322373260156; Sat, 26 Nov 2011 21:54:20 -0800 (PST) MIME-Version: 1.0 Received: by 10.142.231.15 with HTTP; Sat, 26 Nov 2011 21:53:59 -0800 (PST) From: Christopher Genovese Date: Sun, 27 Nov 2011 00:53:59 -0500 Message-ID: Subject: error in aput in assoc.el, Emacs 24, 23, and 22 (at least) To: bug-gnu-emacs@gnu.org Content-Type: multipart/alternative; boundary=bcaec520f4736ea0b804b2b10437 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 140.186.70.17 X-Spam-Score: -5.9 (-----) X-Debbugs-Envelope-To: submit X-Mailman-Approved-At: Sun, 27 Nov 2011 01:00:47 -0500 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.11 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: debbugs-submit-bounces@debbugs.gnu.org Errors-To: debbugs-submit-bounces@debbugs.gnu.org X-Spam-Score: -5.9 (-----) --bcaec520f4736ea0b804b2b10437 Content-Type: text/plain; charset=ISO-8859-1 The function aput in assoc.el (within lisp/emacs-lisp) has two errors. 1. In the third case of the cond (see original below), when the alist is not null, the key is already in the list (and thus at the head), and the value is not null, the return value is incorrect. The original returns the result of the setcar -- that is, the new entry -- rather than the modified alist as it should. 2. The documentation string indicates that nil is returned when the list is nil, but this is incorrect. The modified list is returned, which will include the new key-value pair. I have included below, in order: 1) the original function for reference, 2) a simple case showing the problem, and 3) a fixed version of the function. Only two minor changes are required. (FWIW, I'm running on Mac OS X 10.5, Emacs 24 and 23 nextstep and Emacs 22 carbon.) ;; Original version (from the current emacs24 trunk) (defun aput (alist-symbol key &optional value) "Inserts a key-value pair into an alist. The alist is referenced by ALIST-SYMBOL. The key-value pair is made from KEY and optionally, VALUE. Returns the altered alist or nil if ALIST is nil. If the key-value pair referenced by KEY can be found in the alist, and VALUE is supplied non-nil, then the value of KEY will be set to VALUE. If VALUE is not supplied, or is nil, the key-value pair will not be modified, but will be moved to the head of the alist. If the key-value pair cannot be found in the alist, it will be inserted into the head of the alist (with value nil if VALUE is nil or not supplied)." (lexical-let ((elem (aelement key value)) alist) (asort alist-symbol key) (setq alist (symbol-value alist-symbol)) (cond ((null alist) (set alist-symbol elem)) ((anot-head-p alist key) (set alist-symbol (nconc elem alist))) (value (setcar alist (car elem))) (t alist)))) ;; Simple case illustrating the problem (require 'assoc) (progn (setq tmp-alist '((a . 1) (b . 2) (c . 3) (d . 4))) (setq tmp-ret1 (aput 'tmp-alist 'e 5)) ; no problem (prin1 tmp-ret1) (print "\n") (setq tmp-ret2 (aput 'tmp-alist 'b 10)) ; oops (prin1 tmp-ret2)) ;; Fixed version of the function ;; ;; Changes: ;; + The `values' case of the cond needs to return the ;; alist *not* the value returned by setcar ;; + The docstring indicating the return value has been ;; corrected. The original was inaccurate about the ;; case where the alist is nil. ;; ;; Note this needs (require 'cl) as in assoc.el for lexical-let (defun aput (alist-symbol key &optional value) "Inserts a key-value pair into an alist. The alist is referenced by ALIST-SYMBOL. The key-value pair is made from KEY and optionally, VALUE. Returns the altered alist. If the key-value pair referenced by KEY can be found in the alist, and VALUE is supplied non-nil, then the value of KEY will be set to VALUE. If VALUE is not supplied, or is nil, the key-value pair will not be modified, but will be moved to the head of the alist. If the key-value pair cannot be found in the alist, it will be inserted into the head of the alist (with value nil if VALUE is nil or not supplied)." (lexical-let ((elem (aelement key value)) alist) (asort alist-symbol key) (setq alist (symbol-value alist-symbol)) (cond ((null alist) (set alist-symbol elem)) ((anot-head-p alist key) (set alist-symbol (nconc elem alist))) (value (setcar alist (car elem)) alist) (t alist)))) --bcaec520f4736ea0b804b2b10437 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable The function aput in assoc.el=A0 (within lisp/emacs-lisp) has two errors.
1. In the third case of the cond (see original below), when the alist= is not
=A0=A0=A0 null, the key is already in the list (and thus at the = head), and the value is not null,
=A0=A0=A0 the return value is incorrect.

=A0=A0=A0 The original retu= rns the result of the setcar -- that is, the new entry -- rather than
= =A0=A0=A0 the modified alist as it should.

2. The documentation stri= ng indicates that nil is returned when the list is nil,
=A0=A0=A0 but this is incorrect. The modified list is returned, which will = include the
=A0=A0=A0 new key-value pair.

I have included below, = in order: 1) the original function for reference, 2) a simple
case show= ing the problem, and 3) a fixed version of the function. Only two
minor changes are required.

(FWIW, I'm running on Mac OS X 10.5,= Emacs 24 and 23 nextstep
and Emacs 22 carbon.)


;; Original v= ersion (from the current emacs24 trunk)

(defun aput (alist-symbol ke= y &optional value)
=A0 "Inserts a key-value pair into an alist.
The alist is reference= d by ALIST-SYMBOL.=A0 The key-value pair is made
from KEY and optionally= , VALUE.=A0 Returns the altered alist or nil if
ALIST is nil.

If = the key-value pair referenced by KEY can be found in the alist, and
VALUE is supplied non-nil, then the value of KEY will be set to VALUE.
I= f VALUE is not supplied, or is nil, the key-value pair will not be
modif= ied, but will be moved to the head of the alist.=A0 If the key-value
pair cannot be found in the alist, it will be inserted into the head
of the alist (with value nil if VALUE is nil or not supplied)."
=A0= (lexical-let ((elem (aelement key value))
=A0=A0=A0 =A0=A0=A0 alist)=A0=A0=A0 (asort alist-symbol key)
=A0=A0=A0 (setq alist (symbol-value = alist-symbol))
=A0=A0=A0 (cond ((null alist) (set alist-symbol elem)) =A0=A0=A0 =A0 ((anot-head-p alist key) (set alist-symbol (nconc elem alist)= ))
=A0=A0=A0 =A0 (value (setcar alist (car elem)))
=A0=A0=A0 =A0 (t a= list))))

;; Simple case illustrating the problem
(require 'as= soc)
(progn
=A0 (setq tmp-alist '((a . 1)
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 (b . 2)
=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 (c . 3)
=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 (d . 4)))
=A0 (setq tmp-re= t1
=A0=A0=A0=A0=A0=A0=A0 (aput 'tmp-alist 'e 5))=A0 ; no problem=
=A0 (prin1 tmp-ret1)

=A0 (print "\n")

=A0 (setq tmp-ret2
=A0=A0=A0=A0=A0=A0=A0 (aput 'tmp-alist 'b= 10))=A0=A0=A0 ; oops
=A0 (prin1 tmp-ret2))

;; Fixed version of t= he function
;;
;; Changes:
;;=A0=A0 + The `values' case of the= cond needs to return the
;;=A0=A0=A0=A0 alist *not* the value returned by setcar
;;=A0=A0 + The d= ocstring indicating the return value has been
;;=A0=A0=A0=A0 corrected. = The original was inaccurate about the
;;=A0=A0=A0=A0 case where the alis= t is nil.
;;
;; Note this needs (require 'cl) as in assoc.el for = lexical-let

(defun aput (alist-symbol key &optional value)
=A0 "Inserts= a key-value pair into an alist.
The alist is referenced by ALIST-SYMBOL= .=A0 The key-value pair is made
from KEY and optionally, VALUE.=A0 Retur= ns the altered alist.

If the key-value pair referenced by KEY can be found in the alist, and<= br>VALUE is supplied non-nil, then the value of KEY will be set to VALUE.If VALUE is not supplied, or is nil, the key-value pair will not be
modified, but will be moved to the head of the alist.=A0 If the key-valuepair cannot be found in the alist, it will be inserted into the head
o= f the alist (with value nil if VALUE is nil or not supplied)."
=A0 = (lexical-let ((elem (aelement key value))
=A0=A0=A0 =A0=A0=A0 alist)
=A0=A0=A0 (asort alist-symbol key)
=A0=A0= =A0 (setq alist (symbol-value alist-symbol))
=A0=A0=A0 (cond ((null alis= t)
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 (set alist-symbol elem))
=A0=A0=A0 = =A0 ((anot-head-p alist key)
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 (set alist-s= ymbol (nconc elem alist)))
=A0=A0=A0 =A0 (value
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 (setcar alist (car e= lem))
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 alist)
=A0=A0=A0 =A0 (t alist)))= )

--bcaec520f4736ea0b804b2b10437-- From debbugs-submit-bounces@debbugs.gnu.org Mon Dec 05 17:27:34 2011 Received: (at 10146-done) by debbugs.gnu.org; 5 Dec 2011 22:27:34 +0000 Received: from localhost ([127.0.0.1] helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1RXh0Y-0003xi-0B for submit@debbugs.gnu.org; Mon, 05 Dec 2011 17:27:34 -0500 Received: from pruche.dit.umontreal.ca ([132.204.246.22]) by debbugs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1RXh0V-0003xb-Uu for 10146-done@debbugs.gnu.org; Mon, 05 Dec 2011 17:27:32 -0500 Received: from pastel.home (lechon.iro.umontreal.ca [132.204.27.242]) by pruche.dit.umontreal.ca (8.14.1/8.14.1) with ESMTP id pB5MQuA6025033; Mon, 5 Dec 2011 17:26:56 -0500 Received: by pastel.home (Postfix, from userid 20848) id 485FE593C0; Mon, 5 Dec 2011 17:26:56 -0500 (EST) From: Stefan Monnier To: Christopher Genovese Subject: Re: bug#10146: error in aput in assoc.el, Emacs 24, 23, and 22 (at least) Message-ID: References: Date: Mon, 05 Dec 2011 17:26:56 -0500 In-Reply-To: (Christopher Genovese's message of "Sun, 27 Nov 2011 00:53:59 -0500") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.91 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-NAI-Spam-Flag: NO X-NAI-Spam-Threshold: 5 X-NAI-Spam-Score: 0 X-NAI-Spam-Rules: 1 Rules triggered RV4062=0 X-NAI-Spam-Version: 2.2.0.9309 : core <4062> : streams <708111> : uri <1022407> X-Spam-Score: -4.6 (----) X-Debbugs-Envelope-To: 10146-done Cc: 10146-done@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.11 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: debbugs-submit-bounces@debbugs.gnu.org Errors-To: debbugs-submit-bounces@debbugs.gnu.org X-Spam-Score: -4.6 (----) > The function aput in assoc.el (within lisp/emacs-lisp) has two errors. Thanks, installed. Note that assoc.el is very little used and I'd be happy to deprecate it, among other things because it does not use a "assoc-" prefix and is hence not clean w.r.t namespace. So if you have good reasons to use it, I'd be happy to hear them, Stefan From unknown Fri Aug 08 22:23:35 2025 Received: (at fakecontrol) by fakecontrolmessage; To: internal_control@debbugs.gnu.org From: Debbugs Internal Request Subject: Internal Control Message-Id: bug archived. Date: Tue, 03 Jan 2012 12:24:04 +0000 User-Agent: Fakemail v42.6.9 # This is a fake control message. # # The action: # bug archived. thanks # This fakemail brought to you by your local debbugs # administrator