aboutsummaryrefslogtreecommitdiffstats
path: root/lib-src
diff options
context:
space:
mode:
authorChong Yidong <[email protected]>2012-05-02 21:06:08 +0800
committerChong Yidong <[email protected]>2012-05-02 21:06:08 +0800
commit687d464f5c60cfe135436dab3dc29620ea8d1f95 (patch)
treecfb029be2384fa7d42125e4048c7b3768bc04951 /lib-src
parentc635b6d5458afe1e86f044a407a0331ee75dc816 (diff)
Backport Bug#11374 fix from trunk
Diffstat (limited to 'lib-src')
-rw-r--r--lib-src/ChangeLog5
-rw-r--r--lib-src/emacsclient.c40
2 files changed, 26 insertions, 19 deletions
diff --git a/lib-src/ChangeLog b/lib-src/ChangeLog
index 8e07193ae0..90fd722141 100644
--- a/lib-src/ChangeLog
+++ b/lib-src/ChangeLog
@@ -1,4 +1,7 @@
-2012-05-02 Jim Meyering <[email protected]>
+2012-05-02 Jim Meyering <[email protected]>
+
+ * emacsclient.c (send_to_emacs): Avoid invalid strcpy upon partial
+ send (Bug#11374).
* lib-src/pop.c (pop_stat, pop_list, pop_multi_first, pop_last):
NUL-terminate the error buffer (Bug#11372).
diff --git a/lib-src/emacsclient.c b/lib-src/emacsclient.c
index 48b4384d48..931f6d10c5 100644
--- a/lib-src/emacsclient.c
+++ b/lib-src/emacsclient.c
@@ -119,6 +119,8 @@ char *(getcwd) (char *, size_t);
# define IF_LINT(Code) /* empty */
#endif
+#define min(x, y) (((x) < (y)) ? (x) : (y))
+
/* Name used to invoke this program. */
const char *progname;
@@ -783,33 +785,35 @@ sock_err_message (const char *function_name)
static void
send_to_emacs (HSOCKET s, const char *data)
{
- while (data)
+ size_t dlen;
+
+ if (!data)
+ return;
+
+ dlen = strlen (data);
+ while (*data)
{
- size_t dlen = strlen (data);
- if (dlen + sblen >= SEND_BUFFER_SIZE)
- {
- int part = SEND_BUFFER_SIZE - sblen;
- strncpy (&send_buffer[sblen], data, part);
- data += part;
- sblen = SEND_BUFFER_SIZE;
- }
- else if (dlen)
- {
- strcpy (&send_buffer[sblen], data);
- data = NULL;
- sblen += dlen;
- }
- else
- break;
+ size_t part = min (dlen, SEND_BUFFER_SIZE - sblen);
+ memcpy (&send_buffer[sblen], data, part);
+ data += part;
+ sblen += part;
if (sblen == SEND_BUFFER_SIZE
|| (sblen > 0 && send_buffer[sblen-1] == '\n'))
{
int sent = send (s, send_buffer, sblen, 0);
+ if (sent < 0)
+ {
+ message (TRUE, "%s: failed to send %d bytes to socket: %s\n",
+ progname, sblen, strerror (errno));
+ fail ();
+ }
if (sent != sblen)
- strcpy (send_buffer, &send_buffer[sent]);
+ memmove (send_buffer, &send_buffer[sent], sblen - sent);
sblen -= sent;
}
+
+ dlen -= part;
}
}