summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2014-01-10 11:12:21 +0100
committerGravatar Nikias Bassen2014-01-10 11:12:21 +0100
commitc3a37c778ca404beb5e6acd78658a2467d18f3d6 (patch)
tree3683e833c7cca0113b83c7d480f198343167dd00
parent3ab20d711f8a64833e6dcad13766fcf2912ff2e2 (diff)
downloadusbmuxd-c3a37c778ca404beb5e6acd78658a2467d18f3d6.tar.gz
usbmuxd-c3a37c778ca404beb5e6acd78658a2467d18f3d6.tar.bz2
client: fix realloc in send_pkt() that made the buffer smaller instead of larger
-rw-r--r--src/client.c14
1 files changed, 10 insertions, 4 deletions
diff --git a/src/client.c b/src/client.c
index 5a70edb..330a902 100644
--- a/src/client.c
+++ b/src/client.c
@@ -176,10 +176,16 @@ static int send_pkt(struct mux_client *client, uint32_t tag, enum usbmuxd_msgtyp
uint32_t available = client->ob_capacity - client->ob_size;
/* the output buffer _should_ be large enough, but just in case */
if(available < hdr.length) {
- uint32_t needed_buffer = hdr.length;
- usbmuxd_log(LL_DEBUG, "Enlarging client %d output buffer %d -> %d", client->fd, client->ob_capacity, needed_buffer);
- client->ob_buf = realloc(client->ob_buf, needed_buffer);
- client->ob_capacity = needed_buffer;
+ unsigned char* new_buf;
+ uint32_t new_size = ((client->ob_capacity + hdr.length + 4096) / 4096) * 4096;
+ usbmuxd_log(LL_DEBUG, "%s: Enlarging client %d output buffer %d -> %d", __func__, client->fd, client->ob_capacity, new_size);
+ new_buf = realloc(client->ob_buf, new_size);
+ if (!new_buf) {
+ usbmuxd_log(LL_FATAL, "%s: Failed to realloc.\n", __func__);
+ return -1;
+ }
+ client->ob_buf = new_buf;
+ client->ob_capacity = new_size;
}
memcpy(client->ob_buf + client->ob_size, &hdr, sizeof(hdr));
if(payload && payload_length)