Revision: 6501
Author: ek.kato
Date: Wed Jul 14 23:30:06 2010
Log: * Apply patch to handle EOF or I/O error conditions (Debian bug
  #566791 by Jun Inoue)
        
* emacs/uim-el-helper-agent.c
  - Include errno.h.
  - (read_command) : Change return type and check EOF or error.
  - (main) : Exit if read_command() failed.
* emacs/uim-el-helper-agent.h
  - Update return type of read_command().
* emacs/uim-el-agent.c (main)
  - Include errno.h.
  - (main) : Check EOF or error.

http://code.google.com/p/uim/source/detail?r=6501

Modified:
 /trunk/emacs/uim-el-agent.c
 /trunk/emacs/uim-el-helper-agent.c
 /trunk/emacs/uim-el-helper-agent.h

=======================================
--- /trunk/emacs/uim-el-agent.c Sun Apr  4 20:35:54 2010
+++ /trunk/emacs/uim-el-agent.c Wed Jul 14 23:30:06 2010
@@ -34,7 +34,7 @@
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

-
+#include <errno.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdarg.h>
@@ -658,7 +658,14 @@

        fflush(stdout);

-       fgets(buf, sizeof(buf), stdin);
+       if (fgets(buf, sizeof (buf), stdin) == NULL) {
+         if (feof(stdin))
+           debug_printf(DEBUG_NOTE, "unexpected EOF\n");
+         else
+           debug_printf(DEBUG_ERROR, "failed to read command: %s\n",
+                        strerror (errno));
+         goto QUIT;
+       }

        p1 = buf;
        serial = -1;
=======================================
--- /trunk/emacs/uim-el-helper-agent.c  Sun Apr  4 20:35:54 2010
+++ /trunk/emacs/uim-el-helper-agent.c  Wed Jul 14 23:30:06 2010
@@ -35,6 +35,7 @@
 */


+#include <errno.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdarg.h>
@@ -197,7 +198,10 @@
 }


-static void
+/**
+ * @return 1 if success, 0 if error.
+ */
+static int
 read_command()
 {
   ssize_t len;
@@ -206,8 +210,15 @@
   debug_printf(DEBUG_NOTE, "read command\n");

   do {
-       if ((len = read(STDIN_FILENO, rbuf, sizeof(rbuf) - 1)) == -1)
-         debug_printf(DEBUG_NOTE, "stdin has corrupted\n");
+       len = read(STDIN_FILENO, rbuf, sizeof(rbuf) - 1);
+       if (len == -1) {
+         debug_printf(DEBUG_NOTE, "stdin is corrupt: %s\n", strerror (errno));
+         return 0;
+       }
+       if (len == 0) {
+         debug_printf(DEBUG_NOTE, "unexpected EOF\n");
+         return 0;
+       }

        rbuf[len] = '\0';

@@ -221,6 +232,7 @@

   } while (!command_exists_in_cmdbuf());

+  return 1;
 }


@@ -290,7 +302,8 @@
        debug_printf(DEBUG_NOTE, "data arrive\n");

        if (FD_ISSET(STDIN_FILENO, &rfds)) {
-         read_command();
+         if (!read_command())
+           goto QUIT;
        }

        if (FD_ISSET(helper_fd, &rfds)) {
@@ -307,6 +320,7 @@
        fflush(NULL);
   }

+ QUIT:
   uim_quit();
   return 0;
 }
=======================================
--- /trunk/emacs/uim-el-helper-agent.h  Sun Apr  4 20:35:54 2010
+++ /trunk/emacs/uim-el-helper-agent.h  Wed Jul 14 23:30:06 2010
@@ -49,7 +49,7 @@
 static int command_exists_in_cmdbuf();
 static int process_command();
 static void process_message(char *msg);
-static void read_command();
+static int read_command();
 static void wait_data_arrival(fd_set *rfds);

 void cleanup(void);

Reply via email to