From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Braun =?UTF-8?Q?G=C3=A1bor?= Newsgroups: gmane.emacs.bugs Subject: bug#70122: 29.3.50; transpose-regions can crash Emacs Date: Fri, 12 Apr 2024 11:42:17 +0200 Message-ID: <3478329.QJadu78ljV@gabor> References: <2318820.ElGaqSPkdT@gabor> <86frw191ht.fsf@gnu.org> <8400498.NyiUUSuA9g@gabor> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="nextPart1888251.CQOukoFCf9" Content-Transfer-Encoding: 7Bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="11336"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 70122@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Apr 12 11:43:15 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 1rvDRC-0002hQ-A2 for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 12 Apr 2024 11:43:14 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rvDQz-0000Zt-1v; Fri, 12 Apr 2024 05:43: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 1rvDQr-0000ZX-Gg for bug-gnu-emacs@gnu.org; Fri, 12 Apr 2024 05:42:54 -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 1rvDQr-00065O-6H for bug-gnu-emacs@gnu.org; Fri, 12 Apr 2024 05:42:53 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rvDR0-00043W-Or for bug-gnu-emacs@gnu.org; Fri, 12 Apr 2024 05:43:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Braun =?UTF-8?Q?G=C3=A1bor?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 12 Apr 2024 09:43: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.171291495915356 (code B ref 70122); Fri, 12 Apr 2024 09:43:02 +0000 Original-Received: (at 70122) by debbugs.gnu.org; 12 Apr 2024 09:42:39 +0000 Original-Received: from localhost ([127.0.0.1]:57907 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rvDQc-0003zb-ND for submit@debbugs.gnu.org; Fri, 12 Apr 2024 05:42:39 -0400 Original-Received: from mail-ed1-x535.google.com ([2a00:1450:4864:20::535]:52702) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rvDQa-0003z4-1s for 70122@debbugs.gnu.org; Fri, 12 Apr 2024 05:42:38 -0400 Original-Received: by mail-ed1-x535.google.com with SMTP id 4fb4d7f45d1cf-56e32b439c5so263608a12.1 for <70122@debbugs.gnu.org>; Fri, 12 Apr 2024 02:42:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712914940; x=1713519740; darn=debbugs.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=F+qMoeKMaOpY+HLufrnHv3R8C3tdj5l1OztXSWXTaTE=; b=TskiJqD6aV/byoGDQQuqzTTsgbAeoIJDvJ0I0Y0DM9+eERx5VSJ3A/iJDeMuYpt0Rp dZqjmz/BSXnAUY22Dbl6F+6D3NDjQPSkox1aaU8lSDMKRxGpK+vjyDNv+gpsDBXu3SFQ h8T3WUpqZ/hNkhV8DLnBMocNwHUDgKD+6bmmY6ea8lVluNLo6xKQqvjEySpS28aeYfYy 6iKTEvn6j0QKClT5ZY4d1cXk5erq2SAl0p3s0YsQ5t2BZWuh/oiHlza/Fty1T0X6zEMl Ee3O/uuyf6dR+x7PZFZths6z2tBcniIcJBhx4u1Wqms5/AZcARSz3nwaA+2zv8vAnNPY w4/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712914940; x=1713519740; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=F+qMoeKMaOpY+HLufrnHv3R8C3tdj5l1OztXSWXTaTE=; b=GA2E6Cjftvz7rWn4rPakHp8Dy6AHXdNlgCiByTBf/FqrfychZEXHX+S5ZeIV4oXCBH 06HCf5psNrYNZY5MZ0VTPdPBjcT1jWM5SALs2flsmP4yIrS7rTB37r5/jLEez+hnNRnB 7jUTHNF77C3stQbifNve0Kd5x/hkgPLLKqKbP9N5jt/IcDflXyUOg/rNTq7yzT8nSUhM izDKjJ5cn0qKmB+h8iAX1UthADdFO60MZBtmiwuZ+9ErLqfV4PDkgbypcMSRhiz44MpA m5O1JLdckf+hY+9eOy5dlSkmEWZrLIN6lffDe4XiFlHidqYsnWp4QHBjNPzilZWQKEeU xOwA== X-Gm-Message-State: AOJu0YzDLG2w3xzyIZxND4FHGO8JKBfoGh4RtjlkF/wmLT14zVX8PUa4 SkSlK6S3jvVGPzyoe+nl1yAceVV9anT8MGBslINqK8SaS14FSJwb80NUIw== X-Google-Smtp-Source: AGHT+IGul3OIEQVf8rL0wqw4a9lMpC5i3RgvJg/mvs5oFFO8u28LjgFe7uBrnD3XdjMPsPbiZNlbJg== X-Received: by 2002:a50:ab15:0:b0:56e:2bf4:fe02 with SMTP id s21-20020a50ab15000000b0056e2bf4fe02mr1695401edc.11.1712914939986; Fri, 12 Apr 2024 02:42:19 -0700 (PDT) Original-Received: from gabor.localnet ([2001:9e8:1565:7c00:3935:88e7:95c6:1e91]) by smtp.gmail.com with ESMTPSA id qh27-20020a170906ecbb00b00a519054d7ddsm1618611ejb.61.2024.04.12.02.42.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 02:42:18 -0700 (PDT) In-Reply-To: <8400498.NyiUUSuA9g@gabor> 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:283128 Archived-At: This is a multi-part message in MIME format. --nextPart1888251.CQOukoFCf9 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Hi, Sorry, I've forgotten to attach the new version of the patch in the previous email. Best wishes, G=C3=A1bor --nextPart1888251.CQOukoFCf9 Content-Disposition: attachment; filename="0001-transpose-regions-fix-wrong-lengths-and-case-of-equa.patch" Content-Transfer-Encoding: quoted-printable Content-Type: text/x-patch; charset="unicode-2-0-utf-8"; name="0001-transpose-regions-fix-wrong-lengths-and-case-of-equa.patch" =46rom fd7f6b9c63f3989863dc789e76fc1dd85f0ab630 Mon Sep 17 00:00:00 2001 =46rom: =3D?UTF-8?q?G=3DC3=3DA1bor=3D20Braun?=3D Date: Wed, 3 Apr 2024 20:03:40 +0200 Subject: [PATCH] transpose-regions: fix wrong lengths and case of equal length. Case len1_byte =3D=3D len2_byte: Adjust everything assuming len1 =3D=3D len2: 1. Adjust intervals even between the two regions, as positions may change. 2. undo entries: make a simple replacement change as in the other branches instead of separately on the two regions. This is to work around set_text_properties_1 and graft_intervals_into_buffer recording text property changes in undo history (which is unwanted here). 3. Adjust positions and position differences which rely on len1 =3D=3D len2. Replace len_mid with end1 - start2 where number of characters is expected instead of number of bytes. =2D-- src/editfns.c | 35 ++++++++++------- test/src/editfns-tests.el | 79 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 14 deletions(-) diff --git a/src/editfns.c b/src/editfns.c index 85f7739df07..dd174e8dfcd 100644 =2D-- a/src/editfns.c +++ b/src/editfns.c @@ -4661,18 +4661,18 @@ DEFUN ("transpose-regions", Ftranspose_regions, Str= anspose_regions, 4, 5, USE_SAFE_ALLOCA; =20 modify_text (start1, end2); =2D record_change (start1, len1); =2D record_change (start2, len2); + record_change (start1, (end2 - start1)); + tmp_interval1 =3D copy_intervals (cur_intv, start1, len1); =2D tmp_interval2 =3D copy_intervals (cur_intv, start2, len2); + /* Intervals in-between need adjustment unless len1 =3D=3D len2. + */ + tmp_interval_mid =3D copy_intervals (cur_intv, end1, + start2 - end1); + tmp_interval2 =3D copy_intervals (cur_intv, start2, len2); =20 =2D tmp_interval3 =3D validate_interval_range (buf, &startr1, &endr1, 0); + tmp_interval3 =3D validate_interval_range (buf, &startr1, &endr2, 0); if (tmp_interval3) =2D set_text_properties_1 (startr1, endr1, Qnil, buf, tmp_interval3); =2D =2D tmp_interval3 =3D validate_interval_range (buf, &startr2, &endr2, 0); =2D if (tmp_interval3) =2D set_text_properties_1 (startr2, endr2, Qnil, buf, tmp_interval3); + set_text_properties_1 (startr1, endr2, Qnil, buf, tmp_interval3); =20 temp =3D SAFE_ALLOCA (len1_byte); start1_addr =3D BYTE_POS_ADDR (start1_byte); @@ -4682,8 +4682,11 @@ DEFUN ("transpose-regions", Ftranspose_regions, Stra= nspose_regions, 4, 5, memcpy (start2_addr, temp, len1_byte); SAFE_FREE (); =20 =2D graft_intervals_into_buffer (tmp_interval1, start2, + graft_intervals_into_buffer (tmp_interval1, end2 - len1, len1, current_buffer, 0); + graft_intervals_into_buffer (tmp_interval_mid, start1 + len2, + start2 - end1, + current_buffer, 0); graft_intervals_into_buffer (tmp_interval2, start1, len2, current_buffer, 0); } @@ -4696,7 +4699,8 @@ DEFUN ("transpose-regions", Ftranspose_regions, Stran= spose_regions, 4, 5, modify_text (start1, end2); record_change (start1, (end2 - start1)); tmp_interval1 =3D copy_intervals (cur_intv, start1, len1); =2D tmp_interval_mid =3D copy_intervals (cur_intv, end1, len_mid); + tmp_interval_mid =3D copy_intervals (cur_intv, end1, + start2 - end1); tmp_interval2 =3D copy_intervals (cur_intv, start2, len2); =20 tmp_interval3 =3D validate_interval_range (buf, &startr1, &endr2, 0); @@ -4716,7 +4720,8 @@ DEFUN ("transpose-regions", Ftranspose_regions, Stran= spose_regions, 4, 5, graft_intervals_into_buffer (tmp_interval1, end2 - len1, len1, current_buffer, 0); graft_intervals_into_buffer (tmp_interval_mid, start1 + len2, =2D len_mid, current_buffer, 0); + start2 - end1, + current_buffer, 0); graft_intervals_into_buffer (tmp_interval2, start1, len2, current_buffer, 0); } @@ -4729,7 +4734,8 @@ DEFUN ("transpose-regions", Ftranspose_regions, Stran= spose_regions, 4, 5, modify_text (start1, end2); =20 tmp_interval1 =3D copy_intervals (cur_intv, start1, len1); =2D tmp_interval_mid =3D copy_intervals (cur_intv, end1, len_mid); + tmp_interval_mid =3D copy_intervals (cur_intv, end1, + start2 - end1); tmp_interval2 =3D copy_intervals (cur_intv, start2, len2); =20 tmp_interval3 =3D validate_interval_range (buf, &startr1, &endr2, 0); @@ -4749,7 +4755,8 @@ DEFUN ("transpose-regions", Ftranspose_regions, Stran= spose_regions, 4, 5, graft_intervals_into_buffer (tmp_interval1, end2 - len1, len1, current_buffer, 0); graft_intervals_into_buffer (tmp_interval_mid, start1 + len2, =2D len_mid, current_buffer, 0); + start2 - end1, + current_buffer, 0); graft_intervals_into_buffer (tmp_interval2, start1, len2, current_buffer, 0); } diff --git a/test/src/editfns-tests.el b/test/src/editfns-tests.el index b3b7da65ad3..7c3e3837c8c 100644 =2D-- a/test/src/editfns-tests.el +++ b/test/src/editfns-tests.el @@ -132,6 +132,85 @@ propertize/error-even-number-of-args "Number of args for `propertize' must be odd." (should-error (propertize "foo" 'bar) :type 'wrong-number-of-arguments)) =20 +;; Tests for `transpose-region' + +(ert-deftest transpose-regions-text-properties-2 () + "Test `transpose-regions' thoroughly with text properties." + (let* ((string1pre (propertize "a\x2013" :test 1)) + (middle (propertize "c\x00e9h" :test 0)) + (string2 (propertize "\x25cf\x25cb" :test 2)) + (bytes1 (string-bytes string1pre)) + (bytes2 (string-bytes string2))) + ;; (cl-assert (< bytes1 bytes2)) + (dotimes (i (+ 3 (- bytes2 bytes1))) + (let ((string1 (concat string1pre + (propertize (make-string i ?X) + :test t)))) + (with-temp-buffer + (insert string1 middle string2) + (buffer-enable-undo) + (transpose-regions + 1 (1+ (length string1)) + (- (point) (length string2)) (point)) + (should (equal-including-properties + (buffer-string) + (concat string2 middle string1))) + (undo-boundary) + (let ((this-command #'undo) + (last-command #'ert)) ; anything but undo + (undo)) + (should (equal-including-properties + (buffer-string) + (concat string1 middle string2)))))))) + +(ert-deftest transpose-regions-text-properties () + "Test `transpose-regions' with text properties. +This test is known to crash Emacs 28.2, 29.2, 29.3." + (with-temp-buffer + (insert (propertize "a" 'face 'font-lock-variable-name-face)) + (insert ":\n") + (insert (propertize "b" 'face 'font-lock-variable-name-face)) + (insert ": \x2113\x2080\n") + (insert (propertize "v" 'face 'font-lock-variable-name-face)) + (insert ": scaling\n") + ;; Move last line to the beginning + (transpose-regions 1 1 10 21) + (should (equal-including-properties + (buffer-string) + (concat + (propertize "v" 'face 'font-lock-variable-name-face) + ": scaling\n" + (propertize "a" 'face 'font-lock-variable-name-face) + ":\n" + (propertize "b" 'face 'font-lock-variable-name-face) + ": \x2113\x2080\n"))))) + +(ert-deftest transpose-regions-equal-size () + "Test `transpose-regions' on regions equal-size regions. +Both the number of characters and bytes are equal of the transposed +regions." + (let* ((string1 (propertize "a\x2013\ bc" :test 1)) + (middle (propertize "R=C5=90T" :test 0)) + (string2 (propertize "f\x2013nd" :test 2))) + (cl-assert (=3D (length string1) (length string2))) + (cl-assert (=3D (string-bytes string1) (string-bytes string2))) + (with-temp-buffer + (insert string1 middle string2) + (buffer-enable-undo) + (transpose-regions + 1 (1+ (length string1)) + (- (point) (length string2)) (point)) + (should (equal-including-properties + (buffer-string) + (concat string2 middle string1))) + (undo-boundary) + (let ((this-command #'undo) + (last-command #'ert)) ; anything but undo + (undo)) + (should (equal-including-properties + (buffer-string) + (concat string1 middle string2)))))) + ;; Tests for bug#5131. (defun transpose-test-reverse-word (start end) "Reverse characters in a word by transposing pairs of characters." =2D-=20 2.39.2 --nextPart1888251.CQOukoFCf9--