summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2018-06-02 11:49:55 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2018-06-02 11:49:55 (GMT)
commit7ebe360e85dceb8db580fd76ec8f4d6865e8a5aa (patch)
tree9057208c869117f1b2d141b725f284ac59c120cb /src/common
parent30111e5cf6ff5a7766296ac2579a98c16e7cc7c1 (diff)
Fixed a memory leak when reading packed buffers.
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);