From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id iHBdIpf8X2JxcQEAbAwnHQ (envelope-from ) for ; Wed, 20 Apr 2022 14:29:11 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id 4IRzIpf8X2L5AgEA9RJhRA (envelope-from ) for ; Wed, 20 Apr 2022 14:29:11 +0200 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 09FB2C52E for ; Wed, 20 Apr 2022 14:29:11 +0200 (CEST) Received: from localhost ([::1]:35370 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nh9SI-0006Os-5X for larch@yhetil.org; Wed, 20 Apr 2022 08:29:10 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53062) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nh9Qd-0005jD-4o for emacs-orgmode@gnu.org; Wed, 20 Apr 2022 08:27:27 -0400 Received: from mail-pj1-x1034.google.com ([2607:f8b0:4864:20::1034]:43626) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nh9Qb-0001Du-8C for emacs-orgmode@gnu.org; Wed, 20 Apr 2022 08:27:26 -0400 Received: by mail-pj1-x1034.google.com with SMTP id j8-20020a17090a060800b001cd4fb60dccso1864855pjj.2 for ; Wed, 20 Apr 2022 05:27:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:message-id:date:mime-version; bh=NKcqVHUidaJih7bJHojGRlhmdCjvVwfbjbww4wYG5Aw=; b=fjcYrcqKRvpfjbDPhvzqgkcg4j2F5jYpBCELQ1QzkOyCAj3z1QqFqGjnBOH53ZXyiO PvLTTTl6KDja0Z8JKkydtLl/lmdI4RXeITnmHqdNpiTMCIgbxHmE+/F6TV0pHfbV0fe3 gR7dnDnQqEq6BMZKaW5ut+TZpjzboxj+uZY45AzIiGkAjvxFlP6QF2L0Z6ig9SM1EoiZ QQlnLSkwLyPX31v/N+p423tpl52QuHO8d9Xth30pJAzRh/lKKKXVZYZLzAmm6X2daNmh 8VLitTWxn2wh7iXh7pJcO5608v0kKAwjLeioW2PUJtu1vB0W6ByZ3cNoMKj66hehZC/X sU1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:message-id:date:mime-version; bh=NKcqVHUidaJih7bJHojGRlhmdCjvVwfbjbww4wYG5Aw=; b=lqQnAF6NpMCwNTrmyGEZmwsvekJ1SMi1EkiycPKHBwSh2TaHa8i/YdFaxFs9dcT+Ie ydJI5VAR87bwfYNFrhq8tUx76Re5m5+KWg5nrezgzmI0Ugw3ugde4VhSCLvrLi00Rznx WDoXhfIuqjoVS6NFAVUKGTET7h6JQFwSrqyfL/GRb+mpIbqS+IB8qE73yMru/tw7UZh0 o/jO6XpTTwzhJcy8r6WwV1J+UrEQRIr32kwQli+92Tp1M7+YFwJ2KTQFFYtlZsmE5qKC d9rZx/EqxZNn2C/l9NpD86Y+UdUVt+JeyqJ48xtyqJrBlLiyqagIacL3jXEPItvS+na1 tGDg== X-Gm-Message-State: AOAM532UBJ4uILuPla/+WuRxZB7NJMV2Wm4UJm8ngxNK9r/g0P0HG/Y9 SqNHMFYCJZDIS1VcWNNCJUmaxR4ftR/zOA== X-Google-Smtp-Source: ABdhPJwi4BjUPwvdwq6pjjRE2RmgTma3kth+Denth2XH4MLYmCnaJ5BcljTuX/9GZDg9fo6Q8lw0qw== X-Received: by 2002:a17:902:e0ca:b0:158:cc0a:44f7 with SMTP id e10-20020a170902e0ca00b00158cc0a44f7mr19655374pla.70.1650457642502; Wed, 20 Apr 2022 05:27:22 -0700 (PDT) Received: from localhost ([64.32.23.62]) by smtp.gmail.com with ESMTPSA id w7-20020aa79547000000b0050ad0e82e6dsm565036pfq.215.2022.04.20.05.27.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Apr 2022 05:27:21 -0700 (PDT) From: Ihor Radchenko To: emacs-orgmode@gnu.org, Nicolas Goaziou Subject: [PATCH] oc-basic: Detect malformed bibtex bibliographies Message-Id: Date: Wed, 20 Apr 2022 20:28:13 +0800 MIME-Version: 1.0 Content-Type: text/plain Received-SPF: pass client-ip=2607:f8b0:4864:20::1034; envelope-from=yantar92@gmail.com; helo=mail-pj1-x1034.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 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, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN X-Migadu-To: larch@yhetil.org X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1650457751; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=NKcqVHUidaJih7bJHojGRlhmdCjvVwfbjbww4wYG5Aw=; b=d8qH/MDOueMJgNhz0fNbGAvVoy2r7t9to33pAXhtsSxa7FjZDiN31nsI/UynTSYioTrEv/ rs0XrcWqXm8bwGFGev/mh0VnJUZ9omTrniXaQcHw6n2hGKl380FsdLNlWrOPt+czh1nR1s X1KrDFB2pUKU5oRn2IaREN3NRkeOFy+GR8ZotowGslJxBUCLn4Sz2vW+/RYpORkpyDnVm3 8BP8T3roY4PUN4g9528uJ6OfenVRHh/VIjpD0+PEp7EKKc4oJwLhm+g63AFVzHQsUAgC+b BXGiC81QXCucwb5c1FNCbuzFK2P/7z4dj8jyS7TWcou12Lmze6NKqbx9Flrfaw== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1650457751; a=rsa-sha256; cv=none; b=FBUsc/3SvRN9Yu7DODxggcEcULpayBPwO06XzjIsls306Ae1xCTXERV33NwzZ/lafqN5/b 9WsA1227/8wdaFJ6cd4fULHzzM47lixzb34N+aSWN17zfjJa4XYwELwN6h2MqdMUy4LuLx k4KRbzIMrypc5U7bwDlN3ZOK+HxxPGAHm0g5KgOKR9XpaWeyob9NH2P49RfqKyeJdymIhX gTjjOKEJDUezul0nKs/2ZEm/Huvb+j6MFCw54Ma7J1PjYu05iOZYmfk0P2kfmHvmAkQCep WEVVakECYgw9Z4KTQ9Zvq8WnOaQytQ1ehYllHu1MQeN0RFi5I3LAWd73049COg== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=fjcYrcqK; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: -4.84 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=fjcYrcqK; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: 09FB2C52E X-Spam-Score: -4.84 X-Migadu-Scanner: scn0.migadu.com X-TUID: sxntA1a1o5IC There is an edge case triggering infinite loop in oc-basic. It is caused by bibtex-map-entries (used in org-cite-basic--parse-bibtex) when ran on a malformed bibtex buffer [1]. The proposed patch validates the bibtex buffer before processing and throws an error if issues are found. `bibtex-validate` also conveniently displays a list of errors with clickable links to problematic lines. I believe that it is useful for the users to see such issues instead of, say, failing silently on malformed bibliographies. WDYT? Best, Ihor [1] https://debbugs.gnu.org/cgi/bugreport.cgi?bug=55036 * lisp/oc-basic.el (org-cite-basic--parse-bibtex): Validate the bibliography before parsing. Display list of issues if any (via `bibtex-validate`). (org-cite-basic--parse-bibliography): Set buffer file name needed by `bibtex-validate`. Empty the cache in case of error. --- lisp/oc-basic.el | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/lisp/oc-basic.el b/lisp/oc-basic.el index 873986d07..79f7a4844 100644 --- a/lisp/oc-basic.el +++ b/lisp/oc-basic.el @@ -214,6 +214,10 @@ (defun org-cite-basic--parse-bibtex (dialect) (let ((entries (make-hash-table :test #'equal)) (bibtex-sort-ignore-string-entries t)) (bibtex-set-dialect dialect t) + ;; Throw an error if bibliography is malformed. + (unless (bibtex-validate) + (user-error "Malformed bibliography at %S" + (or (buffer-file-name) (current-buffer)))) (bibtex-map-entries (lambda (key &rest _) ;; Normalize entries: field names are turned into symbols @@ -258,21 +262,27 @@ (defun org-cite-basic--parse-bibliography (&optional info) (when (or (org-file-has-changed-p file) (not (gethash file org-cite-basic--file-id-cache))) (insert-file-contents file) + (setf (buffer-file-name) file) (puthash file (org-buffer-hash) org-cite-basic--file-id-cache)) - (let* ((file-id (cons file (gethash file org-cite-basic--file-id-cache))) - (entries - (or (cdr (assoc file-id org-cite-basic--bibliography-cache)) - (let ((table - (pcase (file-name-extension file) - ("json" (org-cite-basic--parse-json)) - ("bib" (org-cite-basic--parse-bibtex 'biblatex)) - ("bibtex" (org-cite-basic--parse-bibtex 'BibTeX)) - (ext - (user-error "Unknown bibliography extension: %S" - ext))))) - (push (cons file-id table) org-cite-basic--bibliography-cache) - table)))) - (push (cons file entries) results))))) + (unwind-protect + (condition-case nil + (unwind-protect + (let* ((file-id (cons file (gethash file org-cite-basic--file-id-cache))) + (entries + (or (cdr (assoc file-id org-cite-basic--bibliography-cache)) + (let ((table + (pcase (file-name-extension file) + ("json" (org-cite-basic--parse-json)) + ("bib" (org-cite-basic--parse-bibtex 'biblatex)) + ("bibtex" (org-cite-basic--parse-bibtex 'BibTeX)) + (ext + (user-error "Unknown bibliography extension: %S" + ext))))) + (push (cons file-id table) org-cite-basic--bibliography-cache) + table)))) + (push (cons file entries) results)) + (setf (buffer-file-name) nil)) + (error (setq org-cite-basic--file-id-cache nil))))))) (when info (plist-put info :cite-basic/bibliography results)) results))) -- 2.35.1 -- Ihor Radchenko, PhD, Center for Advancing Materials Performance from the Nanoscale (CAMP-nano) State Key Laboratory for Mechanical Behavior of Materials, Xi'an Jiaotong University, Xi'an, China Email: yantar92@gmail.com, ihor_radchenko@alumni.sutd.edu.sg