git.annexia.org
/
libguestfs.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
4507dd4
)
Don't allocate file chunks on the stack.
author
Richard W.M. Jones
<rjones@redhat.com>
Fri, 24 Apr 2009 12:15:49 +0000
(13:15 +0100)
committer
Richard W.M. Jones
<rjones@redhat.com>
Fri, 24 Apr 2009 12:15:49 +0000
(13:15 +0100)
src/guestfs.c
patch
|
blob
|
history
diff --git
a/src/guestfs.c
b/src/guestfs.c
index
fdf5fed
..
73419a6
100644
(file)
--- a/
src/guestfs.c
+++ b/
src/guestfs.c
@@
-1667,10
+1667,9
@@
send_file_complete_sync (guestfs_h *g)
static int check_for_daemon_cancellation (guestfs_h *g);
static int
static int check_for_daemon_cancellation (guestfs_h *g);
static int
-send_file_chunk_sync (guestfs_h *g, int cancel, const char *buf, size_t len)
+send_file_chunk_sync (guestfs_h *g, int cancel, const char *buf, size_t
buf
len)
{
{
- char data[GUESTFS_MAX_CHUNK_SIZE + 48];
- unsigned datalen;
+ unsigned len;
int sent;
guestfs_chunk chunk;
XDR xdr;
int sent;
guestfs_chunk chunk;
XDR xdr;
@@
-1681,6
+1680,14
@@
send_file_chunk_sync (guestfs_h *g, int cancel, const char *buf, size_t len)
return -1;
}
return -1;
}
+ /* This is probably an internal error. Or perhaps we should just
+ * free the buffer anyway?
+ */
+ if (g->msg_out != NULL) {
+ error (g, "guestfs__send_sync: msg_out should be NULL");
+ return -1;
+ }
+
/* Did the daemon send a cancellation message? */
if (check_for_daemon_cancellation (g)) {
if (g->verbose)
/* Did the daemon send a cancellation message? */
if (check_for_daemon_cancellation (g)) {
if (g->verbose)
@@
-1688,35
+1695,34
@@
send_file_chunk_sync (guestfs_h *g, int cancel, const char *buf, size_t len)
return -2;
}
return -2;
}
+ /* Allocate the chunk buffer. Don't use the stack to avoid
+ * excessive stack usage and unnecessary copies.
+ */
+ g->msg_out = safe_malloc (g, GUESTFS_MAX_CHUNK_SIZE + 4 + 48);
+ xdrmem_create (&xdr, g->msg_out + 4, GUESTFS_MAX_CHUNK_SIZE + 48, XDR_ENCODE);
+
/* Serialize the chunk. */
chunk.cancel = cancel;
/* Serialize the chunk. */
chunk.cancel = cancel;
- chunk.data.data_len = len;
+ chunk.data.data_len =
buf
len;
chunk.data.data_val = (char *) buf;
chunk.data.data_val = (char *) buf;
- if (g->verbose)
- fprintf (stderr,
- "library sending chunk cancel = %d, len = %zu, buf = %p\n",
- cancel, len, buf);
-
- xdrmem_create (&xdr, data, sizeof data, XDR_ENCODE);
if (!xdr_guestfs_chunk (&xdr, &chunk)) {
if (!xdr_guestfs_chunk (&xdr, &chunk)) {
- error (g, "xdr_guestfs_chunk failed (buf = %p, len = %zu)", buf, len);
+ error (g, "xdr_guestfs_chunk failed (buf = %p, buflen = %zu)",
+ buf, buflen);
xdr_destroy (&xdr);
xdr_destroy (&xdr);
-
return -
1;
+
goto cleanup
1;
}
}
-
data
len = xdr_getpos (&xdr);
+ len = xdr_getpos (&xdr);
xdr_destroy (&xdr);
xdr_destroy (&xdr);
- /*
Allocate outgoing message buffer
. */
- g->msg_out = safe_
malloc (g, data
len + 4);
- g->msg_out_size =
data
len + 4;
+ /*
Reduce the size of the outgoing message buffer to the real length
. */
+ g->msg_out = safe_
realloc (g, g->msg_out,
len + 4);
+ g->msg_out_size = len + 4;
g->msg_out_pos = 0;
xdrmem_create (&xdr, g->msg_out, 4, XDR_ENCODE);
g->msg_out_pos = 0;
xdrmem_create (&xdr, g->msg_out, 4, XDR_ENCODE);
- xdr_uint32_t (&xdr, &datalen);
-
- memcpy (g->msg_out + 4, data, datalen);
+ xdr_uint32_t (&xdr, &len);
if (guestfs__switch_to_sending (g) == -1)
goto cleanup1;
if (guestfs__switch_to_sending (g) == -1)
goto cleanup1;