From: Ihor Radchenko <yantar92@gmail.com> To: emacs-orgmode@gnu.org, Nicolas Goaziou <mail@nicolasgoaziou.fr> Subject: [PATCH] oc-basic: Detect malformed bibtex bibliographies Date: Wed, 20 Apr 2022 20:28:13 +0800 [thread overview] Message-ID: <bc8e6b99805af23c77020c3ce2d3f2495f420dad.1650457377.git.yantar92@gmail.com> (raw) 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
next reply other threads:[~2022-04-20 12:29 UTC|newest] Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top 2022-04-20 12:28 Ihor Radchenko [this message] 2022-04-20 12:38 ` Bruce D'Arcus 2022-04-20 12:44 ` John Kitchin 2022-04-20 13:02 ` Ihor Radchenko
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style List information: https://www.orgmode.org/ * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bc8e6b99805af23c77020c3ce2d3f2495f420dad.1650457377.git.yantar92@gmail.com \ --to=yantar92@gmail.com \ --cc=emacs-orgmode@gnu.org \ --cc=mail@nicolasgoaziou.fr \ --subject='Re: [PATCH] oc-basic: Detect malformed bibtex bibliographies' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Code repositories for project(s) associated with this inbox: https://git.savannah.gnu.org/cgit/emacs/org-mode.git This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).