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#69709: `sort` interface improvement and universal ordering predicate Date: Sun, 14 Apr 2024 19:26:05 +0300 Message-ID: <86cyqrnc6a.fsf@gnu.org> References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="24813"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 69709@debbugs.gnu.org, mattias.engdegard@gmail.com To: Aris Spathis , Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Apr 14 18:27:11 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 1rw2hD-0006Du-5L for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 14 Apr 2024 18:27:11 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rw2gx-0005MI-C2; Sun, 14 Apr 2024 12:26:55 -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 1rw2gv-0005M0-OG for bug-gnu-emacs@gnu.org; Sun, 14 Apr 2024 12:26:53 -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 1rw2gv-0006vq-BJ for bug-gnu-emacs@gnu.org; Sun, 14 Apr 2024 12:26:53 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rw2h6-0000UE-AR for bug-gnu-emacs@gnu.org; Sun, 14 Apr 2024 12:27:04 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Eli Zaretskii Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 14 Apr 2024 16:27:04 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 69709 X-GNU-PR-Package: emacs Original-Received: via spool by 69709-submit@debbugs.gnu.org id=B69709.17131119881557 (code B ref 69709); Sun, 14 Apr 2024 16:27:04 +0000 Original-Received: (at 69709) by debbugs.gnu.org; 14 Apr 2024 16:26:28 +0000 Original-Received: from localhost ([127.0.0.1]:36044 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rw2gU-0000Oq-IE for submit@debbugs.gnu.org; Sun, 14 Apr 2024 12:26:28 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:52200) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rw2gS-0000NW-2P for 69709@debbugs.gnu.org; Sun, 14 Apr 2024 12:26:25 -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 1rw2gB-0006sP-8E; Sun, 14 Apr 2024 12:26:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-version:References:Subject:In-Reply-To:To:From: Date; bh=8iwWIhA2s0YeIvRbA1yu2Am3GPxbo4HE1+xO5LAX3IE=; b=a6q++HjmZz8bJTJCuK5G HIvmoCg5FjV/GlprCqsFWIIZXH2U6xy7ZzeMJmLa49etGzBowrg+UPhScGes2SFP1W81faOUiO7ce ENpvtaY09l/9pATT5o+1txGwowLddOiSHKYmJ8xUaK40BOv7lvJCz+y44PVoq7qUJKx4nzg7rUDbh 5t3yMi0rHyibJSRp+gMNWINalPhtXPKJGg/qw4tynBDi6bZDfyhuYj97Eu0gzAZO+B4Gv2YPYVjbJ wA17EcM5yGMMVvYq3JAX4G8nu42F3EBak1gE602sbNAELZd/Zr8h7FBvCU8885BfkDIRLF7gjSGzZ 6O8qVrl+dJ9YHw==; In-Reply-To: (message from Aris Spathis on Sun, 14 Apr 2024 17:03:11 +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:283294 Archived-At: > Cc: 69709@debbugs.gnu.org > From: Aris Spathis > Date: Sun, 14 Apr 2024 17:03:11 +0300 > > Thank you for your excellent work on `sort` and related functionality! > > Unfortunately, the new `sort` implementation occasionally crashes with a > segfault. The following code reproduces that in current master: > > (dotimes (i 500) > (sort (make-list 128 42) > :key (lambda (n) (make-list i n)))) > > It happens for inputs of length >= `MERGESTATE_TEMP_SIZE / 2` (= 128 > currently) along with a non-NIL `:key` function. In such cases, a > `Lisp_Object` array is explicitly heap-allocated to store the keys, which is > never marked against GC. This would not be a problem if not for the fact that > the `:key` function call may trigger GC. > > I'm attaching a patch with a proposed solution, consisting of three changes: > > 1. Allocate with `xzalloc` to have the new array initialized to `Qnil`. This > ensures its objects can be marked properly. > > 2. Mark `ms->allocated_keys` in `merge_markmem`. We don't need to check that > `ms->allocated_keys != NULL`, as `merge_register_cleanup` is only called > under this exact condition. > > 3. Move the computation of keys (which may trigger a GC) after `merge_init`, > which registers `merge_markmem`. > > I hope this is useful. Thanks, I took the liberty of CC'ing Mattias using his "usual" address, in the hope that it will help bring this to his attention sooner.