From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: =?UTF-8?Q?=C5=A0t=C4=9Bp=C3=A1n_?= =?UTF-8?Q?N=C4=9Bmec?= Newsgroups: gmane.emacs.bugs Subject: bug#39980: [PATCH] gnus-shorten-url: Improve and avoid args-out-of-range error Date: Sat, 28 Mar 2020 15:17:56 +0100 Message-ID: <87imioo963.fsf@gmail.com> References: <20200308090630.41238-1-stepnem@gmail.com> <87pndf17v5.fsf@gnus.org> <87zhcinbdl.fsf@gmail.com> <87d09ezudv.fsf@gnus.org> <83zhciby6s.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="69483"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Notmuch/0.29.3 (https://notmuchmail.org) Emacs/28.0.50 (x86_64-pc-linux-gnu) Cc: 39980@debbugs.gnu.org To: Eli Zaretskii , Lars Ingebrigtsen Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Mar 28 15:18:15 2020 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 1jICHt-000Hyx-Sn for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 28 Mar 2020 15:18:13 +0100 Original-Received: from localhost ([::1]:53520 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jICHs-0002ch-VZ for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 28 Mar 2020 10:18:12 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:33133) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jICHk-0002cO-5M for bug-gnu-emacs@gnu.org; Sat, 28 Mar 2020 10:18:05 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jICHi-0003YI-IH for bug-gnu-emacs@gnu.org; Sat, 28 Mar 2020 10:18:04 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:57171) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1jICHi-0003YB-EB for bug-gnu-emacs@gnu.org; Sat, 28 Mar 2020 10:18:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1jICHi-0006bq-A5 for bug-gnu-emacs@gnu.org; Sat, 28 Mar 2020 10:18:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: =?UTF-8?Q?=C5=A0t=C4=9Bp=C3=A1n_?= =?UTF-8?Q?N=C4=9Bmec?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 28 Mar 2020 14:18:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 39980 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 39980-submit@debbugs.gnu.org id=B39980.158540505825370 (code B ref 39980); Sat, 28 Mar 2020 14:18:02 +0000 Original-Received: (at 39980) by debbugs.gnu.org; 28 Mar 2020 14:17:38 +0000 Original-Received: from localhost ([127.0.0.1]:34911 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jICHK-0006b7-5P for submit@debbugs.gnu.org; Sat, 28 Mar 2020 10:17:38 -0400 Original-Received: from mail-lf1-f43.google.com ([209.85.167.43]:33911) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jICHI-0006as-00 for 39980@debbugs.gnu.org; Sat, 28 Mar 2020 10:17:36 -0400 Original-Received: by mail-lf1-f43.google.com with SMTP id e7so10219771lfq.1 for <39980@debbugs.gnu.org>; Sat, 28 Mar 2020 07:17:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:in-reply-to:references:user-agent:date :message-id:mime-version; bh=rur5Sh1NPqixPwS88F5ICrMw+X1IkvZM1vb+A0e00yA=; b=QgsAhpO5WvYC6fKJuxzyaPAOlhSqQi1iY+wfxJfxgRBDax48i0+C3qZF8Z8UMTh73i 8dOmyIV1xro4EpiZGq1gajhx4x3IOG8MPik2CP86aaGiT84y5XGqXvpvDKq95rK+9rAQ TZX+y6py4u/d8hmUWtBzrYUvRSEJyKac4Rafj4jrdrRh0nFqlwn1Vtkq30HjrH+CPJX1 pg4LrpLlx+/cmbwnNV4hnKepw1aZ83k0nPLx5osm+q+K0mizBB+HZfPNxebSjqO4mz3q Rb0evS1dw7F7FGEnpARXXf6Dx6LHUThIcwXFEvQKFphqo7lJ5UiZS66MjWAdhovTyfwM MHQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:in-reply-to:references :user-agent:date:message-id:mime-version; bh=rur5Sh1NPqixPwS88F5ICrMw+X1IkvZM1vb+A0e00yA=; b=mdEU5h/gbpGwpVSgyS4WGtKCalUe71nazK1ndI/OvRcEVbSvv2yYKNCMVtbI6HkJd2 p3G+aGmBChcqAZnBjoxEM3dDzdFdenNyCZuhhzx7zj4zqeWaLcJuM4TtREn/CcDr6Y0X QsfJTBUl4BfvM/AccVKKsTWC5iCpeVJeJDZ2x7iF4B6HqWHnR9Ni4uK47J5axRTcCyYV z74HvUNWJvckRfOMUJLXuAanWa0zwV4LS5sJiGvT2w2fxHhsvweG+3K3oYBise6ZXIbw LkeuJgXPLFXyPlBf0kJUQZNdEU2Al4lAmRZsg/J5XZPd280ma2x2St75rbMWJoR5gApA jENw== X-Gm-Message-State: AGi0PuZu4PYfAmkkH3JgNXmt6srqZpxybx9Sv9snjidJTM8oX391fIuD wgU0OSdZl28ziK2zbHbdTvU= X-Google-Smtp-Source: APiQypJ1UveEarB6blVXaT5h1CFcNY8c8mykWMyi52pNvnEs7yrpb4Gj3cxO05ZeegGy2A0R6zVruQ== X-Received: by 2002:a05:6512:3195:: with SMTP id i21mr2822352lfe.186.1585405049970; Sat, 28 Mar 2020 07:17:29 -0700 (PDT) Original-Received: from localhost ([185.112.167.47]) by smtp.gmail.com with ESMTPSA id i18sm4564334lfe.15.2020.03.28.07.17.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Mar 2020 07:17:29 -0700 (PDT) In-Reply-To: <83zhciby6s.fsf@gnu.org> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 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" Xref: news.gmane.io gmane.emacs.bugs:177818 Archived-At: --=-=-= Content-Type: text/plain Considering the opinions expressed here and in https://lists.gnu.org/archive/html/emacs-devel/2020-03/msg00541.html I have reverted to defun and autoload the function instead to prevent having to load subr-x at runtime whenever gnus-sum is loaded, as it is currently only used there by `gnus-summary-browse-url'. Revised patch attached. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-gnus-shorten-url-Improve-and-avoid-args-out-of-range.patch >From b6a2daee2e5be0c4aec6d13fe55b6d9df343a07a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0t=C4=9Bp=C3=A1n=20N=C4=9Bmec?= Date: Sat, 7 Mar 2020 18:26:44 +0100 Subject: [PATCH] gnus-shorten-url: Improve and avoid args-out-of-range error 'gnus-shorten-url' (used by 'gnus-summary-browse-url') ignored fragment identifiers and didn't check substring bounds, in some cases leading to runtime errors, e.g.: (gnus-shorten-url "https://some.url.with/path/and#also_a_long_target" 40) ;; => Lisp error: (args-out-of-range "/path/and" -18 nil) This commit makes it account for #fragments and fixes faulty string computation, reusing existing helper function. (bug#39980) * lisp/vc/ediff-init.el (ediff-truncate-string-left): Rename to 'string-truncate-left'. * lisp/emacs-lisp/subr-x.el (string-truncate-left): Move here. * lisp/vc/ediff-mult.el (ediff-meta-insert-file-info1) (ediff-draw-dir-diffs): 'ediff-draw-dir-diffs' renamed to 'string-truncate-left'. * lisp/gnus/gnus-sum.el (gnus-shorten-url): Fix args-out-of-range error, don't drop #fragments, use 'string-truncate-left'. --- lisp/emacs-lisp/subr-x.el | 9 +++++++++ lisp/gnus/gnus-sum.el | 14 +++++++------- lisp/vc/ediff-init.el | 10 ---------- lisp/vc/ediff-mult.el | 9 ++++----- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el index 044c9aada0..9f96ac50d1 100644 --- a/lisp/emacs-lisp/subr-x.el +++ b/lisp/emacs-lisp/subr-x.el @@ -236,6 +236,15 @@ string-trim TRIM-LEFT and TRIM-RIGHT default to \"[ \\t\\n\\r]+\"." (string-trim-left (string-trim-right string trim-right) trim-left)) +;;;###autoload +(defun string-truncate-left (string length) + "Truncate STRING to LENGTH, replacing initial surplus with \"...\"." + (let ((strlen (length string))) + (if (<= strlen length) + string + (setq length (max 0 (- length 3))) + (concat "..." (substring string (max 0 (- strlen 1 length))))))) + (defsubst string-blank-p (string) "Check whether STRING is either empty or only whitespace. The following characters count as whitespace here: space, tab, newline and diff --git a/lisp/gnus/gnus-sum.el b/lisp/gnus/gnus-sum.el index a47e657623..6f367692dd 100644 --- a/lisp/gnus/gnus-sum.el +++ b/lisp/gnus/gnus-sum.el @@ -9494,15 +9494,15 @@ gnus-collect-urls (delete-dups urls))) (defun gnus-shorten-url (url max) - "Return an excerpt from URL." + "Return an excerpt from URL not exceeding MAX characters." (if (<= (length url) max) url - (let ((parsed (url-generic-parse-url url))) - (concat (url-host parsed) - "..." - (substring (url-filename parsed) - (- (length (url-filename parsed)) - (max (- max (length (url-host parsed))) 0))))))) + (let* ((parsed (url-generic-parse-url url)) + (host (url-host parsed)) + (rest (concat (url-filename parsed) + (when-let ((target (url-target parsed))) + (concat "#" target))))) + (concat host (string-truncate-left rest (- max (length host))))))) (defun gnus-summary-browse-url (&optional external) "Scan the current article body for links, and offer to browse them. diff --git a/lisp/vc/ediff-init.el b/lisp/vc/ediff-init.el index e59d4b57b5..da6509b7cb 100644 --- a/lisp/vc/ediff-init.el +++ b/lisp/vc/ediff-init.el @@ -1510,16 +1510,6 @@ ediff-strip-last-dir (setq dir (substring dir 0 pos))) (ediff-abbreviate-file-name (file-name-directory dir)))) -(defun ediff-truncate-string-left (str newlen) - ;; leave space for ... on the left - (let ((len (length str)) - substr) - (if (<= len newlen) - str - (setq newlen (max 0 (- newlen 3))) - (setq substr (substring str (max 0 (- len 1 newlen)))) - (concat "..." substr)))) - (defsubst ediff-nonempty-string-p (string) (and (stringp string) (not (string= string "")))) diff --git a/lisp/vc/ediff-mult.el b/lisp/vc/ediff-mult.el index fee87e8352..2b1b07927f 100644 --- a/lisp/vc/ediff-mult.el +++ b/lisp/vc/ediff-mult.el @@ -113,7 +113,6 @@ ediff-mult (require 'ediff-wind) (require 'ediff-util) - ;; meta-buffer (ediff-defvar-local ediff-meta-buffer nil "") (ediff-defvar-local ediff-parent-meta-buffer nil "") @@ -1172,7 +1171,7 @@ ediff-meta-insert-file-info1 ;; abbreviate the file name, if file exists (if (and (not (stringp fname)) (< file-size -1)) "-------" ; file doesn't exist - (ediff-truncate-string-left + (string-truncate-left (ediff-abbreviate-file-name fname) max-filename-width))))))) @@ -1266,7 +1265,7 @@ ediff-draw-dir-diffs (if (= (mod membership-code ediff-membership-code1) 0) ; dir1 (let ((beg (point))) (insert (format "%-27s" - (ediff-truncate-string-left + (string-truncate-left (ediff-abbreviate-file-name (if (file-directory-p (concat dir1 file)) (file-name-as-directory file) @@ -1281,7 +1280,7 @@ ediff-draw-dir-diffs (if (= (mod membership-code ediff-membership-code2) 0) ; dir2 (let ((beg (point))) (insert (format "%-26s" - (ediff-truncate-string-left + (string-truncate-left (ediff-abbreviate-file-name (if (file-directory-p (concat dir2 file)) (file-name-as-directory file) @@ -1295,7 +1294,7 @@ ediff-draw-dir-diffs (if (= (mod membership-code ediff-membership-code3) 0) ; dir3 (let ((beg (point))) (insert (format " %-25s" - (ediff-truncate-string-left + (string-truncate-left (ediff-abbreviate-file-name (if (file-directory-p (concat dir3 file)) (file-name-as-directory file) -- 2.26.0 --=-=-=--