[libvirt] [PATCH 2/3] virSysinfoParseX86BaseBoard: Free memory upfront if no board detected

Peter Krempa pkrempa at redhat.com
Fri May 10 08:08:19 UTC 2019


On Fri, May 10, 2019 at 09:52:27 +0200, Michal Privoznik wrote:
> If no board was detected then VIR_REALLOC_N() done at the end of
> the function will actually free the memory (because nborads ==
> 0), but @boards will be set to a non-NULL pointer. This makes it
> unnecessary harder for a caller to see if any board was detected.

Ah, yeah. Allocating 0 bytes of memory may get you a pointer.

> 
> Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
> ---
>  src/util/virsysinfo.c | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/src/util/virsysinfo.c b/src/util/virsysinfo.c
> index 9e60d1a553..6c3adc23ab 100644
> --- a/src/util/virsysinfo.c
> +++ b/src/util/virsysinfo.c
> @@ -844,8 +844,12 @@ virSysinfoParseX86BaseBoard(const char *base,
>              nboards--;
>      }
>  
> -    /* This is safe, as we can be only shrinking the memory */
> -    ignore_value(VIR_REALLOC_N(boards, nboards));
> +    if (nboards == 0) {
> +        VIR_FREE(boards);

ACK
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20190510/05ead0a9/attachment-0001.sig>


More information about the libvir-list mailing list