Subject: Fix epa-file-insert-file-contents substring error

From: juntaka <[email-masked]>
Date: Mon, 12 Apr 2021 04:20:13 +0000

Hi

I think substring occurs error when TO is longer than its content.
This patch will fix the substring bug in epa-file-insert-file-contents.

The last commit this line was 2008, so no one seems not using it, though ;)

Thanks,

From da85a87962cad14fb6dee231fe019fadafa1107e Mon Sep 17 00:00:00 2001
From: Junya Takahashi <[email-masked]>
Date: Mon, 12 Apr 2021 13:09:35 +0900
Subject: [PATCH] Fix args-out-of-range error in epa-file-insert-file-contents

Fix error below:

$ echo -n test > test
$ gpg -c test
elisp> (epa-file-insert-file-contents "test.gpg" nil 0 5)
       ;; => Args out of range: "test", 0, 5
---
 lisp/epa-file.el | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lisp/epa-file.el b/lisp/epa-file.el
index e46e3684c8..33bf5adabe 100644
--- a/lisp/epa-file.el
+++ b/lisp/epa-file.el
@@ -198,7 +198,9 @@ epa-file-insert-file-contents
                       (mapcar #'car (epg-context-result-for
                                      context 'encrypted-to)))
 	  (if (or beg end)
-	      (setq string (substring string (or beg 0) end)))
+              (setq string (substring string
+                                      (or beg 0)
+                                      (and end (min end (length string))))))
 	  (save-excursion
 	    ;; If visiting, bind off buffer-file-name so that
 	    ;; file-locking will not ask whether we should
-- 
2.31.1