GNU bug report logs - #81504
[PATCH] Restore current buffer after clone-indirect-buffer-hook errors

Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.

Package: emacs; Reported by: Donjuanplatinum <donplat@HIDDEN>; Keywords: patch; dated Mon, 27 Jul 2026 17:48:01 UTC; Maintainer for emacs is bug-gnu-emacs@HIDDEN.

Message received at 81504 <at> debbugs.gnu.org:


Received: (at 81504) by debbugs.gnu.org; 31 Jul 2026 10:27:26 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Fri Jul 31 06:27:26 2026
Received: from localhost ([127.0.0.1]:39963 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1wpkSc-0006Qg-0Y
	for submit <at> debbugs.gnu.org; Fri, 31 Jul 2026 06:27:26 -0400
Received: from fhigh-a1-smtp.messagingengine.com ([103.168.172.152]:54673)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <spwhitton@HIDDEN>)
 id 1wpkSa-0006QS-EC
 for 81504 <at> debbugs.gnu.org; Fri, 31 Jul 2026 06:27:25 -0400
Received: from phl-compute-07.internal (phl-compute-07.internal [10.202.2.47])
 by mailfhigh.phl.internal (Postfix) with ESMTP id 14907140015D;
 Fri, 31 Jul 2026 06:27:19 -0400 (EDT)
Received: from phl-frontend-03 ([10.202.2.162])
 by phl-compute-07.internal (MEProxy); Fri, 31 Jul 2026 06:27:19 -0400
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=spwhitton.name;
 h=cc:cc:content-type:content-type:date:date:from:from
 :in-reply-to:in-reply-to:message-id:mime-version:references
 :reply-to:subject:subject:to:to; s=fm1; t=1785493639; x=
 1785580039; bh=29a2NLZcYpkC3jKM57ilop0jDrdAhIK0nxDVVcxSBqk=; b=R
 Mkd8tvjDkcjnkHMhkgZrgb5UlELnJ6lxp7ePc/n5rhvW2Tr7rKnXVv0pslm4TCLW
 6mpDygQo1EGzb2RGDFEC+MzzG/aM94QSlVo+nKTbYq27g6vy3uuZeESMifIIpJBx
 RJC3ZLJ+1O1NkwH/+4QkZxBkwASdw1dkNh4Mbm8LKmhn83WmFfYmaORH36m6W6B8
 jM1wjFB9rfF8Ufzouh0zOkGNr/V3mlCmN+4RP2nruTL9BbRDzohbRf5SW8H+0ca/
 KMvVmOAMahkuMeYbPkujNmEmd+Mv2i+2jzPkmVsMy48i4QigkuSQ51yfbzS/vSdO
 +bSOUF2M1305EswqVttpg==
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=
 messagingengine.com; h=cc:cc:content-type:content-type:date:date
 :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to
 :message-id:mime-version:references:reply-to:subject:subject:to
 :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=
 1785493639; x=1785580039; bh=29a2NLZcYpkC3jKM57ilop0jDrdAhIK0nxD
 VVcxSBqk=; b=R8H3D2Z5dvC6mjGkkpH1gPzXKa4A2FKPyafZUKh3z4HOurvKmhp
 Ea9r/wLj3hVKOPGa0B/W3aOso+pMLtwTndTetbgokEIgSaKJZekeY6aoLQClM5Vn
 Uwhy2u9IqVB39Ge+rdJIehMLPREQN/fsUxjvActV85RMbFebQz/6sKadIwmqDYye
 9Hij+8lIbJZT0AXjl7I+582TMC7XiWAbYgav1Cr3DVedJX9yuW+5w3ZGI3CpGcCI
 HW5UDwvnC5xhnL5xb8Kh1diQcFMCzfbv8kkXgmXorPL709JtNyKl7TGaIwOXBGCz
 JYsc6hczHHo6k0+8DCW0Nzd19Luu/i3Wolg==
X-ME-Sender: <xms:hnhsaqcLLYsY4zuLt4P7PGhAyg92NzhRSKXrtB_Akm2UmhsMsTlLDQ>
 <xme:hnhsaqOfLSPAsa-zm5_Tk_tZwYfSEyue_9HC2-XYn4DaBbYheQO9TfeD_Z2L5p96K
 2TrznFt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<xmx:hnhsat8pdvMcXxvw_6r5mps_0oBJJP0J8li5clvrIn29nwzNUL9v5Q>
 <xmx:h3hsagwLGQ65utlqV-fUSr7P6vIAAwj3OWUrtCESwaCO8QHGXcw2A9Mc>
Feedback-ID: i62564b17:Fastmail
Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri,
 31 Jul 2026 06:27:18 -0400 (EDT)
Received: by melete.silentflame.com (Postfix, from userid 1000)
 id A3F977E80E3; Fri, 31 Jul 2026 11:27:17 +0100 (BST)
From: Sean Whitton <spwhitton@HIDDEN>
To: Donjuanplatinum <donplat@HIDDEN>
Subject: Re: bug#81504: [PATCH] Restore current buffer after
 clone-indirect-buffer-hook errors
In-Reply-To: <871pckha6a.fsf@HIDDEN>
References: <87ik60mijp.fsf@HIDDEN>
 <87jyqcacd5.fsf@HIDDEN> <871pckha6a.fsf@HIDDEN>
Date: Fri, 31 Jul 2026 11:27:17 +0100
Message-ID: <875x1v8nei.fsf@HIDDEN>
MIME-Version: 1.0
Content-Type: text/plain
X-Spam-Score: -0.7 (/)
X-Debbugs-Envelope-To: 81504
Cc: donplat@HIDDEN, 81504 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.7 (-)

Donjuanplatinum [30/Jul  9:35pm +08] wrote:
> Sean Whitton <spwhitton@HIDDEN> writes:
>
>> Donjuanplatinum via "Bug reports for GNU Emacs, the Swiss army knife of text editors" [28/Jul  1:47am +08] wrote:
>>> Version: 31.0.50
>>> Base-Commit: 810e9d675ba484dabade1a9f30201d2848c5e2ea
>>>
>>> In `make-indirect-buffer` with clone argument, if
>>> `clone-indirect-buffer-hook` return an error, `current-buffer` will not
>>> be restored.
>>>
>>> To make the error in emacs -Q:
>>>
>>>
>>>    (progn
>>>       (generate-new-buffer "base")
>>>       (with-current-buffer "base" (insert "text"))
>>>       (set-buffer "base")
>>>       (let ((clone-indirect-buffer-hook
>>>              (list (lambda () (error "hook error")))))
>>>         (condition-case nil
>>>             (make-indirect-buffer "base" "ind" t)
>>>           (error nil)))
>>>       (buffer-name (current-buffer)))
>>>
>>> Expected: "base"
>>> But get: "ind"
>>>
>>> The Clone path in Fmake_indirect_buffer use set_buffer_internal_1 to
>>> restore the current-buffer, but when clone-indirect-buffer-hook return
>>> an error, it will skip the set_buffer_internal_1.
>>>
>>> The attached patch replaces it with record_unwind_current_buffer so that
>>> it will following the standard specpdl pattern to restore the current-buffer.
>>
>> Should we be restoring the current buffer here?  I'm not sure there is
>> any particular reason why we should, and calling code can always use
>> with-current-buffer or save-current-buffer if it needs to ensure that
>> it's restored?
> Thank you for taking a look.
>
> I think the function already intends to restore the current buffer on
> the normal path. Otherwise, why does it save old_b and call
> set_buffer_internal_1 (old_b) before returning?
>
> My patch only makes the error path behave same with the normal path.

I see what you mean.

I can't install the full patch because I don't think you've assigned
copyright for contributions to Emacs to the FSF.  If you'd be interested
in doing that we can start that process?

In the meantime I've installed the C changes, because it does not seem
essential to have a test for this.  But after copyright assignment we
can install the test anyway.

-- 
Sean Whitton




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#81504; Package emacs. Full text available.

Message received at 81504 <at> debbugs.gnu.org:


Received: (at 81504) by debbugs.gnu.org; 30 Jul 2026 13:36:12 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Thu Jul 30 09:36:12 2026
Received: from localhost ([127.0.0.1]:58990 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1wpQvj-0000sD-S8
	for submit <at> debbugs.gnu.org; Thu, 30 Jul 2026 09:36:12 -0400
Received: from mail.barrensea.org ([198.12.121.168]:55398)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <donplat@HIDDEN>)
 id 1wpQvf-0000rx-T9
 for 81504 <at> debbugs.gnu.org; Thu, 30 Jul 2026 09:36:09 -0400
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=barrensea.org;
 s=mail; t=1785418566;
 bh=acny4ksEAe0pPmXWa5H3b/+m/4r/R9SQ8apRDrYAQhc=;
 h=From:To:Cc:Subject:In-Reply-To:References;
 b=jzxKHm/zqCJuZAGqCnFSk3IwBIcfaJhGynUL0pOKiqIiwOxugnYbx3jnaRpfT4Z/E
 SFjTGtxZxjQVC1EY9ewXsIwmh4tC3iqeymxd6yzfMLxtZvnGQxs2FrE6VKDhmTVymu
 OH+tkwHgVo88d+KZutTrg78PcXY2Ol4b6r2XmkSEgE8HLRKlPX0gWJfsGgiM6PKlAm
 R8t7zNT+4/1k+qRhBXX9OBJwjVKIyeqvDIZvHO7xKTiH0Mb7dp1UkHgeebTbbxg93I
 c+yIGNyG1kkg0s7aLcqs+cAKfI4upbR/qaH5GTa/YT13aTurtr9WVv/dZxLye7S+WA
 yiHVniyN1qebg==
From: Donjuanplatinum <donplat@HIDDEN>
To: Sean Whitton <spwhitton@HIDDEN>
Subject: Re: bug#81504: [PATCH] Restore current buffer after
 clone-indirect-buffer-hook errors
In-Reply-To: <87jyqcacd5.fsf@HIDDEN> (Sean Whitton's message
 of "Thu, 30 Jul 2026 13:30:30 +0100")
References: <87ik60mijp.fsf@HIDDEN>
 <87jyqcacd5.fsf@HIDDEN>
Date: Thu, 30 Jul 2026 21:35:57 +0800
Message-ID: <871pckha6a.fsf@HIDDEN>
MIME-Version: 1.0
Content-Type: text/plain
X-Spam-Score: 0.0 (/)
X-Debbugs-Envelope-To: 81504
Cc: donplat@HIDDEN, 81504 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

Sean Whitton <spwhitton@HIDDEN> writes:

> Donjuanplatinum via "Bug reports for GNU Emacs, the Swiss army knife of text editors" [28/Jul  1:47am +08] wrote:
>> Version: 31.0.50
>> Base-Commit: 810e9d675ba484dabade1a9f30201d2848c5e2ea
>>
>> In `make-indirect-buffer` with clone argument, if
>> `clone-indirect-buffer-hook` return an error, `current-buffer` will not
>> be restored.
>>
>> To make the error in emacs -Q:
>>
>>
>>    (progn
>>       (generate-new-buffer "base")
>>       (with-current-buffer "base" (insert "text"))
>>       (set-buffer "base")
>>       (let ((clone-indirect-buffer-hook
>>              (list (lambda () (error "hook error")))))
>>         (condition-case nil
>>             (make-indirect-buffer "base" "ind" t)
>>           (error nil)))
>>       (buffer-name (current-buffer)))
>>
>> Expected: "base"
>> But get: "ind"
>>
>> The Clone path in Fmake_indirect_buffer use set_buffer_internal_1 to
>> restore the current-buffer, but when clone-indirect-buffer-hook return
>> an error, it will skip the set_buffer_internal_1.
>>
>> The attached patch replaces it with record_unwind_current_buffer so that
>> it will following the standard specpdl pattern to restore the current-buffer.
>
> Should we be restoring the current buffer here?  I'm not sure there is
> any particular reason why we should, and calling code can always use
> with-current-buffer or save-current-buffer if it needs to ensure that
> it's restored?
Thank you for taking a look.

I think the function already intends to restore the current buffer on
the normal path. Otherwise, why does it save old_b and call
set_buffer_internal_1 (old_b) before returning?

My patch only makes the error path behave same with the normal path.




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#81504; Package emacs. Full text available.

Message received at 81504 <at> debbugs.gnu.org:


Received: (at 81504) by debbugs.gnu.org; 30 Jul 2026 12:30:44 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Thu Jul 30 08:30:43 2026
Received: from localhost ([127.0.0.1]:58700 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1wpPuK-00052t-QI
	for submit <at> debbugs.gnu.org; Thu, 30 Jul 2026 08:30:43 -0400
Received: from flow-a2-smtp.messagingengine.com ([103.168.172.137]:38683)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <spwhitton@HIDDEN>)
 id 1wpPuG-0004rb-Nq
 for 81504 <at> debbugs.gnu.org; Thu, 30 Jul 2026 08:30:38 -0400
Received: from phl-compute-04.internal (phl-compute-04.internal [10.202.2.44])
 by mailflow.phl.internal (Postfix) with ESMTP id 7DA591380226;
 Thu, 30 Jul 2026 08:30:31 -0400 (EDT)
Received: from phl-frontend-03 ([10.202.2.162])
 by phl-compute-04.internal (MEProxy); Thu, 30 Jul 2026 08:30:31 -0400
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=spwhitton.name;
 h=cc:content-type:content-type:date:date:from:from:in-reply-to
 :in-reply-to:message-id:mime-version:references:reply-to:subject
 :subject:to:to; s=fm1; t=1785414631; x=1785421831; bh=RhKWCrXbe2
 tv4la+woUMdvYt4T6iYTzGb8Nn9monxzo=; b=uo8iWKt7Se9L0fvEf/9WpGkLTj
 yh57e586ON7U8p8/WzvoH65msTu5/ejfj2WlT5K688u1GXBE2v8CiIqhFbnLhgo1
 zq5p2wGES6lQ/rCq0e8rONEzdWZ3cSO9jd3wZtUZubmvJ6L+Gk7wTbRbbNMaOyvi
 /LaECdi9i7b4L21yito254rEh28fuIH5DNEFVJYR0fPWB/zGJItrC8q3tzZA1loE
 OKZbj8queZ4A0il/+fLveTxERDnNzpEBeZo7kWqvJMSd94wwC6GVIfRSVqHFpq7z
 zmw3Exz4R+lfurdxb6hefSIfQyb2QdkMr7NumOnn9rhMtpzUQ2kkHiSLdCWQ==
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=
 messagingengine.com; h=cc:content-type:content-type:date:date
 :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to
 :message-id:mime-version:references:reply-to:subject:subject:to
 :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=
 1785414631; x=1785421831; bh=RhKWCrXbe2tv4la+woUMdvYt4T6iYTzGb8N
 n9monxzo=; b=OBzQJoZrBwwidmu08YMBodtMjuXKwYjMAFs07dafdHxnxmLpPez
 UJQhCGIve8YvwaVbwv1K7ZzouSuifShuPeSVe3fGm1LmcNCEQ8+m8W8u175NKSza
 g0Lt0mlJTEgIdByDlElzJ3SzUK2nYs62cqYxhC1sRxQHPUlH2XINuBlyMWKtRDsl
 oxWnFASWVwUvaRJ+AT1yLDvmbGh6oFWJljt6MkyZsjUbf+EfrsvJsSfGTWhXmvKI
 IpuDS/DEA5QkEr69MFNZFohiWFBwK2kLJv5ZsQqGhntN2dQU5E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Proxy: <xmx:50NragpitbQUWHtkmiO9M3w5ZxKPd6VkL4MggRc6L9e0NYo9rwmTOg>
 <xmx:50Nravk3Tt4Dzz8oXzxxjlIw9n3eJ6sIq_GXfzgvdKAHVsO63BLOSQ>
 <xmx:50NranJ2uxKRdCd9WzxsFUIRt4liasRMvF_-fPkkc3R74ucoSkng0g>
 <xmx:50NrakyN6E7gFkYLRQZU46Nl80gE7bV_ghPz-TXC8Y6xNCkGHI3VoQ>
 <xmx:50NrapG-Og0OJe-lZ9i3OA3dknBt3_Qax0Rwu6swPa_-Wcp3wMeUINp7>
Feedback-ID: i62564b17:Fastmail
Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu,
 30 Jul 2026 08:30:31 -0400 (EDT)
Received: by melete.silentflame.com (Postfix, from userid 1000)
 id 2DEDE7E7B73; Thu, 30 Jul 2026 13:30:30 +0100 (BST)
From: Sean Whitton <spwhitton@HIDDEN>
To: Donjuanplatinum <donplat@HIDDEN>, 81504 <at> debbugs.gnu.org
Subject: Re: bug#81504: [PATCH] Restore current buffer after
 clone-indirect-buffer-hook errors
In-Reply-To: <87ik60mijp.fsf@HIDDEN>
References: <87ik60mijp.fsf@HIDDEN>
Date: Thu, 30 Jul 2026 13:30:30 +0100
Message-ID: <87jyqcacd5.fsf@HIDDEN>
MIME-Version: 1.0
Content-Type: text/plain
X-Spam-Score: -0.7 (/)
X-Debbugs-Envelope-To: 81504
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.7 (-)

Donjuanplatinum via "Bug reports for GNU Emacs, the Swiss army knife of text editors" [28/Jul  1:47am +08] wrote:
> Version: 31.0.50
> Base-Commit: 810e9d675ba484dabade1a9f30201d2848c5e2ea
>
> In `make-indirect-buffer` with clone argument, if
> `clone-indirect-buffer-hook` return an error, `current-buffer` will not
> be restored.
>
> To make the error in emacs -Q:
>
>
>    (progn
>       (generate-new-buffer "base")
>       (with-current-buffer "base" (insert "text"))
>       (set-buffer "base")
>       (let ((clone-indirect-buffer-hook
>              (list (lambda () (error "hook error")))))
>         (condition-case nil
>             (make-indirect-buffer "base" "ind" t)
>           (error nil)))
>       (buffer-name (current-buffer)))
>
> Expected: "base"
> But get: "ind"
>
> The Clone path in Fmake_indirect_buffer use set_buffer_internal_1 to
> restore the current-buffer, but when clone-indirect-buffer-hook return
> an error, it will skip the set_buffer_internal_1.
>
> The attached patch replaces it with record_unwind_current_buffer so that
> it will following the standard specpdl pattern to restore the current-buffer.

Should we be restoring the current buffer here?  I'm not sure there is
any particular reason why we should, and calling code can always use
with-current-buffer or save-current-buffer if it needs to ensure that
it's restored?

-- 
Sean Whitton




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#81504; Package emacs. Full text available.

Message received at submit <at> debbugs.gnu.org:


Received: (at submit) by debbugs.gnu.org; 27 Jul 2026 17:47:41 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Mon Jul 27 13:47:41 2026
Received: from localhost ([127.0.0.1]:39331 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1woPQS-0008OP-W3
	for submit <at> debbugs.gnu.org; Mon, 27 Jul 2026 13:47:41 -0400
Received: from lists1p.gnu.org ([2001:470:142::17]:38672)
 by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.84_2) (envelope-from <donplat@HIDDEN>)
 id 1woPQP-0008Nj-Lo
 for submit <at> debbugs.gnu.org; Mon, 27 Jul 2026 13:47:38 -0400
Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <donplat@HIDDEN>)
 id 1woPQ7-0003vN-GT
 for bug-gnu-emacs@HIDDEN; Mon, 27 Jul 2026 13:47:21 -0400
Received: from mail.barrensea.org ([198.12.121.168])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <donplat@HIDDEN>)
 id 1woPQ5-00059G-SM
 for bug-gnu-emacs@HIDDEN; Mon, 27 Jul 2026 13:47:19 -0400
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=barrensea.org;
 s=mail; t=1785174428;
 bh=PeUK4K1NyFxg7bwT4DoWA0Jc9DWTAfCE+DS3NNMWnKc=; h=From:To:Subject;
 b=PfR47R6MBMAOkpnZ7DWz80+GJGPYMFhHYX2Go/SW8tZbAoPNPUac6Q7XAMKVO2Bs/
 80dVxgt+W3lZjK0qqpiKulyeUu4vZu7dLYaNmebh1HXyuKUkfkVV3AMjNXuubAYzc2
 hWr8DCxOk2LUUEdG++hYuJEoGyBnnmw/05kGYbHCLzkRRED0txbwoRDkGx07Iarzdq
 X7RrRJz5flOkBPdgRmVFbBOl/dq7lKA8Y749tIxIR3ZulgiJbAKAaYRe7zJm05U0VT
 RHcwBnHZh/W1nt+PkBDe5/VjQ84vPtZoRGTx1w7lwWaRpqw2bUM3DbvrvNAfZnoie5
 QysrKVj5MuHWg==
From: Donjuanplatinum <donplat@HIDDEN>
To: bug-gnu-emacs@HIDDEN
Subject: [PATCH] Restore current buffer after clone-indirect-buffer-hook errors
Date: Tue, 28 Jul 2026 01:47:06 +0800
Message-ID: <87ik60mijp.fsf@HIDDEN>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
Received-SPF: pass client-ip=198.12.121.168;
 envelope-from=donplat@HIDDEN; helo=mail.barrensea.org
X-Spam_score_int: -20
X-Spam_score: -2.1
X-Spam_bar: --
X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,
 DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001,
 SPF_PASS=-0.001 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-Spam-Score: 0.9 (/)
X-Debbugs-Envelope-To: submit
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -0.1 (/)

--=-=-=
Content-Type: text/plain


Version: 31.0.50
Base-Commit: 810e9d675ba484dabade1a9f30201d2848c5e2ea

In `make-indirect-buffer` with clone argument, if
`clone-indirect-buffer-hook` return an error, `current-buffer` will not
be restored.

To make the error in emacs -Q:


   (progn
      (generate-new-buffer "base")
      (with-current-buffer "base" (insert "text"))
      (set-buffer "base")
      (let ((clone-indirect-buffer-hook
             (list (lambda () (error "hook error")))))
        (condition-case nil
            (make-indirect-buffer "base" "ind" t)
          (error nil)))
      (buffer-name (current-buffer)))

Expected: "base"
But get: "ind"

The Clone path in Fmake_indirect_buffer use set_buffer_internal_1 to
restore the current-buffer, but when clone-indirect-buffer-hook return
an error, it will skip the set_buffer_internal_1.

The attached patch replaces it with record_unwind_current_buffer so that
it will following the standard specpdl pattern to restore the current-buffer.

--=-=-=
Content-Type: text/x-patch
Content-Disposition: attachment;
 filename=0001-Restore-current-buffer-after-clone-indirect-buffer-h.patch

From 89ce33acdd96a76f6efb831183349ea7d3be406f Mon Sep 17 00:00:00 2001
From: Donjuanplatinum <donplat@HIDDEN>
Date: Tue, 28 Jul 2026 01:25:56 +0800
Subject: [PATCH] Restore current buffer after clone-indirect-buffer-hook
 errors

When the CLONE argument is non-nil, Fmake_indirect_buffer switches to
the new indirect buffer and runs clone-indirect-buffer-hook. If the hook
return an error, the set_buffer_internal_1 restore is skipped, so
current_buffer pointing to the indirect buffer.

Use record_unwind_current_buffer to ensure the original buffer can be
restored in normal and error paths.

* src/buffer.c (Fmake_indirect_buffer): Replace set_buffer_internal_1 to
specpdl unwind in the clone path.

* test/src/buffer-tests.el (test-make-indirect-buffer-clone-hook): Add
this condition test.
---
 src/buffer.c             |  8 ++++++--
 test/src/buffer-tests.el | 17 +++++++++++++++++
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/src/buffer.c b/src/buffer.c
index 0a2bff27c11..423c0ce7905 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -937,7 +937,9 @@ DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
     }
   else
     {
-      struct buffer *old_b = current_buffer;
+      /* Arrange for the current buffer to be restored later,
+	 even if an exit occurs. */
+      specpdl_ref count = SPECPDL_INDEX ();
 
       clone_per_buffer_values (b->base_buffer, b);
       bset_filename (b, Qnil);
@@ -946,6 +948,7 @@ DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
       bset_backed_up (b, Qnil);
       bset_local_minor_modes (b, Qnil);
       bset_auto_save_file_name (b, Qnil);
+      record_unwind_current_buffer ();
       set_buffer_internal_1 (b);
       Fset (Qbuffer_save_without_query, Qnil);
       Fset (Qbuffer_file_number, Qnil);
@@ -955,7 +958,8 @@ DEFUN ("make-indirect-buffer", Fmake_indirect_buffer, Smake_indirect_buffer,
 	 variable copies for list variables that might be mangled due
 	 to destructive operations in the indirect buffer. */
       run_hook (Qclone_indirect_buffer_hook);
-      set_buffer_internal_1 (old_b);
+      /* Restore the original buffer.  */
+      unbind_to (count, Qnil);
     }
 
   run_buffer_list_update_hook (b);
diff --git a/test/src/buffer-tests.el b/test/src/buffer-tests.el
index 3fae18011fb..fc6ba43b3d7 100644
--- a/test/src/buffer-tests.el
+++ b/test/src/buffer-tests.el
@@ -1478,6 +1478,23 @@ test-make-indirect-buffer-1
           (kill-buffer indirect))))))
 
 
+(ert-deftest test-make-indirect-buffer-clone-hook ()
+  (let ((base-buf (generate-new-buffer "base")))
+    (unwind-protect
+        (progn
+          (with-current-buffer base-buf
+            (insert "sample text"))
+          (set-buffer base-buf)
+          (let ((clone-indirect-buffer-hook
+                 (list (lambda ()
+                         (error "clone hook error")))))
+            (should-error
+             (make-indirect-buffer base-buf "indirect" t)))
+          (should (eq (current-buffer) base-buf)))
+      (ignore-errors (kill-buffer "indirect"))
+      (when (buffer-live-p base-buf)
+        (kill-buffer base-buf)))))
+
 
 ;; +==========================================================================+
 ;; | buffer-swap-text
-- 
2.55.0


--=-=-=--




Acknowledgement sent to Donjuanplatinum <donplat@HIDDEN>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs@HIDDEN. Full text available.
Report forwarded to bug-gnu-emacs@HIDDEN:
bug#81504; Package emacs. Full text available.
Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.
Last modified: Fri, 31 Jul 2026 10:30:02 UTC

GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997 nCipher Corporation Ltd, 1994-97 Ian Jackson.