[virt-tools-list] [vhostmd 1/5] Extend vu_buffer struct, init buffer allocated by buffer_grow

Jim Fehlig jfehlig at suse.com
Wed Sep 26 20:43:06 UTC 2018


On 8/30/18 4:11 AM, Michael Trapp wrote:
> With the virtio implementation vu_buffer is used for socket IO
> and therefore information about the current position after a
> write is required.
> buffer_grow uses realloc to extend the buffer, due to the fact that
> realloc doesn't guarantee that allocated memory is initialized there
> should be a memset.

In theory these two changes are only slightly related and should be separate 
patches, but in practice they are trivial and I'm fine with both in a single patch.

> ---
>   include/util.h | 1 +
>   vhostmd/util.c | 4 +++-
>   2 files changed, 4 insertions(+), 1 deletion(-)

Reviewed-by: Jim Fehlig <jfehlig at suse.com>

Regards,
Jim

> 
> diff --git a/include/util.h b/include/util.h
> index 3a21e09..c0bd19a 100644
> --- a/include/util.h
> +++ b/include/util.h
> @@ -42,6 +42,7 @@ typedef enum {
>   typedef struct _vu_buffer {
>       unsigned int size;
>       unsigned int use;
> +    unsigned int pos;
>       char *content;
>   }vu_buffer;
>   
> diff --git a/vhostmd/util.c b/vhostmd/util.c
> index 70fc804..c0f8f2b 100644
> --- a/vhostmd/util.c
> +++ b/vhostmd/util.c
> @@ -53,6 +53,7 @@ static int buffer_grow(vu_buffer *buf, int len)
>           return -1;
>   
>       buf->size = size;
> +    memset(&buf->content[buf->use], 0, len);
>       return 0;
>   }
>   
> @@ -151,7 +152,7 @@ int vu_buffer_create(vu_buffer **buf, int len)
>      *buf = calloc(1, sizeof(vu_buffer));
>      if (*buf == NULL)
>         return -1;
> -
> +
>      return buffer_grow(*buf, len);
>   }
>   
> @@ -232,6 +233,7 @@ void vu_buffer_erase(vu_buffer *buf)
>      if (buf) {
>         memset(buf->content, '\0', buf->size);
>         buf->use = 0;
> +      buf->pos = 0;
>      }
>   }
>   
> 




More information about the virt-tools-list mailing list