Didn't attach a patch. Good, because I found a bug. Attaching fixed.
Subject: proper stdio error handling

Correctly detect errors from stdio:
  $ ./xclip < .
  ./xclip: (stdin): Is a directory
  $ ./xclip .
  ./xclip: .: Is a directory
  $ xclip .
  ^C
also, by reordering the loop and the condition this way,
we only allocate more if we know there's more,
and only check for EOF if we encountered an EOF or error.

Bug-Debian: https://bugs.debian.org/1075806
Forwarded: https://github.com/astrand/xclip/pull/153
Last-Update: 2024-07-05

--- xclip-0.13.orig/xclip.c
+++ xclip-0.13/xclip.c
@@ -229,8 +229,9 @@ doIn(Window win, const char *progname)
        }
        else {
            if ((fil_handle = fopen(fil_names[fil_current], "r")) == NULL) {
-               errperror(3, progname, ": ", fil_names[fil_current]
-                   );
+           err:
+               errperror(3, progname, ": ",
+                   fil_number ? fil_names[fil_current] : "(stdin)");
                return EXIT_FAILURE;
            }
            else {
@@ -243,8 +244,17 @@ doIn(Window win, const char *progname)
            }
        }
 
-       fil_current++;
-       while (!feof(fil_handle)) {
+       for (;;) {
+           size_t rd = fread(sel_buf + sel_len, sizeof(char), sel_all - 
sel_len, fil_handle);
+           if (rd != sel_all - sel_len) {
+               if (feof(fil_handle)) {
+                   sel_len += rd;
+                   break;
+               }
+               goto err;
+           }
+           sel_len += rd;
+
            /* If sel_buf is full (used elems =
             * allocated elems)
             */
@@ -256,9 +266,8 @@ doIn(Window win, const char *progname)
                sel_buf = (unsigned char *) xcrealloc(sel_buf, sel_all * 
sizeof(char)
                    );
            }
-           sel_len += fread(sel_buf + sel_len, sizeof(char), sel_all - 
sel_len, fil_handle);
        }
-    } while (fil_current < fil_number);
+    } while (++fil_current < fil_number);
 
     /* remove the last newline character if necessary */
     if (frmnl && sel_len && sel_buf[sel_len - 1] == '\n') {

Attachment: signature.asc
Description: PGP signature

Reply via email to