From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.bugs Subject: bug#70122: 29.3.50; transpose-regions can crash Emacs Date: Mon, 01 Apr 2024 16:17:04 +0300 Message-ID: <86ttkl9q9b.fsf@gnu.org> References: <2318820.ElGaqSPkdT@gabor> <86wmph9u1i.fsf@gnu.org> Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="31001"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 70122@debbugs.gnu.org To: braungb88@gmail.com Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Apr 01 15:18:18 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rrHYI-0007r2-EP for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 01 Apr 2024 15:18:18 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rrHY1-00079u-5L; Mon, 01 Apr 2024 09:18:01 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rrHXz-00079m-TP for bug-gnu-emacs@gnu.org; Mon, 01 Apr 2024 09:18:00 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rrHXz-0001Jz-L0 for bug-gnu-emacs@gnu.org; Mon, 01 Apr 2024 09:17:59 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rrHY2-0007p9-Db for bug-gnu-emacs@gnu.org; Mon, 01 Apr 2024 09:18:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Eli Zaretskii Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 01 Apr 2024 13:18:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 70122 X-GNU-PR-Package: emacs Original-Received: via spool by 70122-submit@debbugs.gnu.org id=B70122.171197744230014 (code B ref 70122); Mon, 01 Apr 2024 13:18:02 +0000 Original-Received: (at 70122) by debbugs.gnu.org; 1 Apr 2024 13:17:22 +0000 Original-Received: from localhost ([127.0.0.1]:49826 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rrHXN-0007o2-Nd for submit@debbugs.gnu.org; Mon, 01 Apr 2024 09:17:22 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:36442) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rrHXI-0007nh-Tv for 70122@debbugs.gnu.org; Mon, 01 Apr 2024 09:17:19 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rrHXA-0001AH-E5; Mon, 01 Apr 2024 09:17:08 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date: mime-version; bh=+qJtfMICxq6uUNqhE6Ah06zhdO82+ugU/hC8xMPjRG0=; b=PMGg802MWpcZ u6ekqm6UoKzupKaYtKEG8KvpqvHcvF6HIeOQSq2rfMeKf/Dd6AKeN71YIc0ILqc+C5h1tmTQ5sC5/ aAZsyvTY4/3U7bd1i1GJyI5xvHvca/Lu4twl08rbPnwihVues3GmSrhuqrfP+v6paljCm5ugR0eee fUD3Uqoi0e8ieSw3LZ8ET6eAMNVc5NUSHoDsCh2GKuNll3VFZ9Ivsz/xZpPT5Gs4Dnh2xZ1lrzwVe YzSkuqMkao6KmmRv8deHT7BTrZwhvNRrAfA6MvtGkr8I8IDSBsw8tERKATOHLV9VpneJ4rnSfQA8q 4PI6bSxixeGs8RE1Lsh9Bg==; In-Reply-To: <86wmph9u1i.fsf@gnu.org> (message from Eli Zaretskii on Mon, 01 Apr 2024 14:55:21 +0300) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:282474 Archived-At: > Cc: 70122@debbugs.gnu.org > Date: Mon, 01 Apr 2024 14:55:21 +0300 > From: Eli Zaretskii > > > Please find attached a patch to transpose-regions > > with added tests. (The patch intends to fix typos: > > makes sure lengths in bytes and characters are not confused. > > Thanks, but could you please show the minimal change required to fix > just the particular problem with this scenario (and perhaps explain > the reason for the crash in words), without any cleanup and > typo/confusion fixes? That would make it easier to review the patch, > whereas with what you sent, it is hard to understand what exactly is > being fixed. I came up with the patch below. The problem is that len_mid, which is a difference between byte positions, was sometimes used in calls that expect differences between character positions instead. The patch below passes both your test and the already-existing tests in test/src/editfns-tests.el. WDYT? > > One case (likely for optimization only) has been removed, > > seemed too much trouble to get it right.) > > If you explain the reason for the crash, perhaps we could leave the > optimization alone. I found that len_mid was not used in that branch, so we can leave it alone. Here's the patch: diff --git a/src/editfns.c b/src/editfns.c index 4ccf765..124fd47 100644 --- a/src/editfns.c +++ b/src/editfns.c @@ -4677,7 +4677,8 @@ DEFUN ("transpose-regions", Ftranspose_regions, Stranspose_regions, 4, 5, modify_text (start1, end2); record_change (start1, (end2 - start1)); tmp_interval1 = copy_intervals (cur_intv, start1, len1); - tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid); + tmp_interval_mid = copy_intervals (cur_intv, end1, + start2 - (start1 + len1)); tmp_interval2 = copy_intervals (cur_intv, start2, len2); tmp_interval3 = validate_interval_range (buf, &startr1, &endr2, 0); @@ -4697,7 +4698,8 @@ DEFUN ("transpose-regions", Ftranspose_regions, Stranspose_regions, 4, 5, graft_intervals_into_buffer (tmp_interval1, end2 - len1, len1, current_buffer, 0); graft_intervals_into_buffer (tmp_interval_mid, start1 + len2, - len_mid, current_buffer, 0); + start2 - (start1 + len1), + current_buffer, 0); graft_intervals_into_buffer (tmp_interval2, start1, len2, current_buffer, 0); } @@ -4710,7 +4712,8 @@ DEFUN ("transpose-regions", Ftranspose_regions, Stranspose_regions, 4, 5, modify_text (start1, end2); tmp_interval1 = copy_intervals (cur_intv, start1, len1); - tmp_interval_mid = copy_intervals (cur_intv, end1, len_mid); + tmp_interval_mid = copy_intervals (cur_intv, end1, + start2 - (start1 + len1)); tmp_interval2 = copy_intervals (cur_intv, start2, len2); tmp_interval3 = validate_interval_range (buf, &startr1, &endr2, 0); @@ -4730,7 +4733,8 @@ DEFUN ("transpose-regions", Ftranspose_regions, Stranspose_regions, 4, 5, graft_intervals_into_buffer (tmp_interval1, end2 - len1, len1, current_buffer, 0); graft_intervals_into_buffer (tmp_interval_mid, start1 + len2, - len_mid, current_buffer, 0); + start2 - (start1 + len1), + current_buffer, 0); graft_intervals_into_buffer (tmp_interval2, start1, len2, current_buffer, 0); }