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

Michal Privoznik posted 3 patches 5 weeks ago

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

Posted by Michal Privoznik 5 weeks ago
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.

Signed-off-by: Michal Privoznik <mprivozn@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);
+    } else {
+        /* This is safe, as we can be only shrinking the memory */
+        ignore_value(VIR_REALLOC_N(boards, nboards));
+    }
 
     *baseBoard = boards;
     *nbaseBoard = nboards;
-- 
2.21.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

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

Posted by Peter Krempa 5 weeks ago
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@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
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

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

Posted by Michal Privoznik 5 weeks ago
On 5/10/19 10:08 AM, Peter Krempa wrote:
> 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.

I'm wondering if it makes sense to modify VIR_REALLOC_N() so that it 
calls VIR_FREE() if count == 0. If so, then this patch is not needed.

Michal

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

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

Posted by Daniel P. Berrangé 5 weeks ago
On Fri, May 10, 2019 at 11:09:32AM +0200, Michal Privoznik wrote:
> On 5/10/19 10:08 AM, Peter Krempa wrote:
> > 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.
> 
> I'm wondering if it makes sense to modify VIR_REALLOC_N() so that it calls
> VIR_FREE() if count == 0. If so, then this patch is not needed.

That would makes its behaviour different from other alocation functions,
which will none the less allocate 1 byte, if you ask for 0 bytes.

Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list