From 9a741aba3d9040d2bb367db79e9021ba6abc12dd Mon Sep 17 00:00:00 2001
From: Werner Koch <wk@gnupg.org>
Date: Fri, 6 Sep 2024 15:46:41 +0200
Subject: [PATCH] gpg: Make --no-literal work again for -c and --store.

* g10/dearmor.c (dearmor_file): Check for errors of iobuf_copy.
(enarmor_file): Ditto.
* g10/encrypt.c (encrypt_simple): Fix error check of iobuf_copy
(encrypt_crypt): Use iobuf_copy.
--

Fixes-commit: 756c0bd5d89bd0a773f844fbc2ec508c1a36c63d
GnuPG-bug-id: 5852
---
 g10/dearmor.c | 12 ++++++++++++
 g10/encrypt.c | 35 +++++++++++++++++------------------
 2 files changed, 29 insertions(+), 18 deletions(-)

diff --git a/g10/dearmor.c b/g10/dearmor.c
index f6bb59ef6..667888362 100644
--- a/g10/dearmor.c
+++ b/g10/dearmor.c
@@ -67,6 +67,12 @@ dearmor_file( const char *fname )
 	goto leave;
 
     iobuf_copy (out, inp);
+    if ((rc = iobuf_error (inp)))
+      log_error (_("error reading '%s': %s\n"),
+                 iobuf_get_fname_nonnull (inp), gpg_strerror (rc));
+    else if ((rc = iobuf_error (out)))
+      log_error (_("error writing '%s': %s\n"),
+                 iobuf_get_fname_nonnull (out), gpg_strerror (rc));
 
   leave:
     if( rc )
@@ -115,6 +121,12 @@ enarmor_file( const char *fname )
     push_armor_filter ( afx, out );
 
     iobuf_copy (out, inp);
+    if ((rc = iobuf_error (inp)))
+      log_error (_("error reading '%s': %s\n"),
+                 iobuf_get_fname_nonnull (inp), gpg_strerror (rc));
+    else if ((rc = iobuf_error (out)))
+      log_error (_("error writing '%s': %s\n"),
+                 iobuf_get_fname_nonnull (out), gpg_strerror (rc));
 
   leave:
     if( rc )
diff --git a/g10/encrypt.c b/g10/encrypt.c
index 3fc10a7b8..cc8f37fe2 100644
--- a/g10/encrypt.c
+++ b/g10/encrypt.c
@@ -633,9 +633,13 @@ encrypt_simple (const char *filename, int mode, int use_seskey)
     {
       /* User requested not to create a literal packet, so we copy the
          plain data.  */
-      rc = iobuf_copy (out, inp);
-      if (rc)
-        log_error ("copying input to output failed: %s\n", gpg_strerror (rc));
+      iobuf_copy (out, inp);
+      if ((rc = iobuf_error (inp)))
+        log_error (_("error reading '%s': %s\n"),
+                   iobuf_get_fname_nonnull (inp), gpg_strerror (rc));
+      else if ((rc = iobuf_error (out)))
+        log_error (_("error writing '%s': %s\n"),
+                   iobuf_get_fname_nonnull (out), gpg_strerror (rc));
     }
 
   /* Finish the stuff.  */
@@ -760,8 +764,8 @@ write_symkey_enc (STRING2KEY *symkey_s2k, aead_algo_t aead_algo,
  * The caller may provide a checked list of public keys in
  * PROVIDED_PKS; if not the function builds a list of keys on its own.
  *
- * Note that FILEFD is currently only used by cmd_encrypt in the
- * not yet finished server.c.
+ * Note that FILEFD and OUTPUTFD are currently only used by
+ * cmd_encrypt in the not yet finished server.c.
  */
 int
 encrypt_crypt (ctrl_t ctrl, int filefd, const char *filename,
@@ -996,19 +1000,14 @@ encrypt_crypt (ctrl_t ctrl, int filefd, const char *filename,
     {
       /* User requested not to create a literal packet, so we copy the
          plain data. */
-      byte copy_buffer[4096];
-      int  bytes_copied;
-      while ((bytes_copied = iobuf_read (inp, copy_buffer, 4096)) != -1)
-        {
-          rc = iobuf_write (out, copy_buffer, bytes_copied);
-          if (rc)
-            {
-              log_error ("copying input to output failed: %s\n",
-                         gpg_strerror (rc));
-              break;
-            }
-        }
-      wipememory (copy_buffer, 4096); /* Burn the buffer. */
+      iobuf_copy (out, inp);
+      if ((rc = iobuf_error (inp)))
+        log_error (_("error reading '%s': %s\n"),
+                   iobuf_get_fname_nonnull (inp), gpg_strerror (rc));
+      else if ((rc = iobuf_error (out)))
+        log_error (_("error writing '%s': %s\n"),
+                   iobuf_get_fname_nonnull (out), gpg_strerror (rc));
+
     }
 
   /* Finish the stuff. */
-- 
2.33.0

