summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/packed.c30
1 files changed, 16 insertions, 14 deletions
diff --git a/src/common/packed.c b/src/common/packed.c
index ca6e816..d956a42 100644
--- a/src/common/packed.c
+++ b/src/common/packed.c
@@ -296,16 +296,17 @@ bool read_packed_buffer(packed_buffer *pbuf, int fd)
result = safe_read(fd, &used, sizeof(uint32_t));
- if (!result)
- init_packed_buffer(pbuf);
-
- else
+ if (result)
{
- pbuf->allocated = sizeof(uint32_t) + used;
- pbuf->data = malloc(pbuf->allocated * sizeof(uint8_t));
+ assert(pbuf->pos == sizeof(uint32_t));
+
+ if ((pbuf->pos + used) > pbuf->allocated)
+ {
+ pbuf->allocated = pbuf->pos + used;
+ pbuf->data = realloc(pbuf->data, pbuf->allocated * sizeof(uint8_t));
+ }
pbuf->used = used;
- pbuf->pos = sizeof(uint32_t);
result = safe_read(fd, pbuf->data + pbuf->pos, used);
@@ -363,16 +364,17 @@ bool recv_packed_buffer(packed_buffer *pbuf, int fd)
result = safe_recv(fd, &used, sizeof(uint32_t), 0);
- if (!result)
- init_packed_buffer(pbuf);
-
- else
+ if (result)
{
- pbuf->allocated = sizeof(uint32_t) + used;
- pbuf->data = malloc(pbuf->allocated * sizeof(uint8_t));
+ assert(pbuf->pos == sizeof(uint32_t));
+
+ if ((pbuf->pos + used) > pbuf->allocated)
+ {
+ pbuf->allocated = pbuf->pos + used;
+ pbuf->data = realloc(pbuf->data, pbuf->allocated * sizeof(uint8_t));
+ }
pbuf->used = used;
- pbuf->pos = sizeof(uint32_t);
result = safe_recv(fd, pbuf->data + pbuf->pos, used, 0);