[libvirt] [PATCH v7 02/23] snapshot: Allow NULL to virDomainSnapshotObjGetDef

Eric Blake posted 23 patches 6 years, 10 months ago
There is a newer version of this series
[libvirt] [PATCH v7 02/23] snapshot: Allow NULL to virDomainSnapshotObjGetDef
Posted by Eric Blake 6 years, 10 months ago
Doing so can simplify some callers.

Signed-off-by: Eric Blake <eblake@redhat.com>
---
 src/conf/virdomainsnapshotobjlist.h | 2 +-
 src/conf/snapshot_conf.c            | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/conf/virdomainsnapshotobjlist.h b/src/conf/virdomainsnapshotobjlist.h
index b83f7a4ba9..12b574b4ff 100644
--- a/src/conf/virdomainsnapshotobjlist.h
+++ b/src/conf/virdomainsnapshotobjlist.h
@@ -87,7 +87,7 @@ int virDomainListSnapshots(virDomainSnapshotObjListPtr snapshots,
 static inline virDomainSnapshotDefPtr
 virDomainSnapshotObjGetDef(virDomainMomentObjPtr obj)
 {
-    return (virDomainSnapshotDefPtr) obj->def;
+    return obj ? (virDomainSnapshotDefPtr) obj->def : NULL;
 }

 #endif /* LIBVIRT_VIRDOMAINSNAPSHOTOBJLIST_H */
diff --git a/src/conf/snapshot_conf.c b/src/conf/snapshot_conf.c
index 4ce120451e..8e4f3d9410 100644
--- a/src/conf/snapshot_conf.c
+++ b/src/conf/snapshot_conf.c
@@ -967,7 +967,7 @@ virDomainSnapshotRedefinePrep(virDomainPtr domain,
     }

     other = virDomainSnapshotFindByName(vm->snapshots, def->common.name);
-    otherdef = other ? virDomainSnapshotObjGetDef(other) : NULL;
+    otherdef = virDomainSnapshotObjGetDef(other);
     check_if_stolen = other && otherdef->common.dom;
     if (virDomainSnapshotRedefineValidate(def, domain->uuid, other, xmlopt,
                                           flags) < 0) {
-- 
2.20.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v7 02/23] snapshot: Allow NULL to virDomainSnapshotObjGetDef
Posted by Daniel P. Berrangé 6 years, 10 months ago
On Wed, Mar 27, 2019 at 05:10:33AM -0500, Eric Blake wrote:
> Doing so can simplify some callers.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>
> ---
>  src/conf/virdomainsnapshotobjlist.h | 2 +-
>  src/conf/snapshot_conf.c            | 2 +-
>  2 files changed, 2 insertions(+), 2 deletions(-)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


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
Re: [libvirt] [PATCH v7 02/23] snapshot: Allow NULL to virDomainSnapshotObjGetDef
Posted by Eric Blake 6 years, 10 months ago
On 3/27/19 5:10 AM, Eric Blake wrote:
> Doing so can simplify some callers.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>
> ---
>  src/conf/virdomainsnapshotobjlist.h | 2 +-
>  src/conf/snapshot_conf.c            | 2 +-
>  2 files changed, 2 insertions(+), 2 deletions(-)

Peter reported that at -O2, this makes gcc unhappy. I'll probably just
revert this one (the coding convenience savings for one or two callers
do not outweigh the potential NULL dereference logic to be checked at
all other callers).

/home/pipo/libvirt/src/qemu/qemu_driver.c: In function
'qemuDomainSnapshotCreateXML':
/home/pipo/libvirt/src/qemu/qemu_driver.c:15389:26: error: potential
null pointer dereference [-Werror=null-dereference]
     bool memory = snapdef->memory == VIR_DOMAIN_SNAPSHOT_LOCATION_EXTERNAL;
                   ~~~~~~~^~~~~~~~
/home/pipo/libvirt/src/qemu/qemu_driver.c:15389:26: error: potential
null pointer dereference [-Werror=null-dereference]
In file included from /home/pipo/libvirt/src/util/virbuffer.h:27,
                 from /home/pipo/libvirt/src/conf/capabilities.h:27,
                 from /home/pipo/libvirt/src/conf/domain_conf.h:32,
                 from /home/pipo/libvirt/src/qemu/qemu_agent.h:26,
                 from /home/pipo/libvirt/src/qemu/qemu_driver.c:40:
/home/pipo/libvirt/src/util/viralloc.h:125:34: error: potential null
pointer dereference [-Werror=null-dereference]
 # define VIR_ALLOC_N(ptr, count) virAllocN(&(ptr), sizeof(*(ptr)),
(count), true, \

^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                            VIR_FROM_THIS, __FILE__,
__FUNCTION__, __LINE__)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/pipo/libvirt/src/qemu/qemu_driver.c:15103:9: note: in expansion of
macro 'VIR_ALLOC_N'
     if (VIR_ALLOC_N(ret, snapdef->ndisks) < 0)
         ^~~~~~~~~~~
/home/pipo/libvirt/src/qemu/qemu_driver.c:15798:45: error: null pointer
dereference [-Werror=null-dereference]
             virDomainSnapshotObjGetDef(snap)->memory ==
VIR_DOMAIN_SNAPSHOT_LOCATION_EXTERNAL) {
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~


> 
> diff --git a/src/conf/virdomainsnapshotobjlist.h b/src/conf/virdomainsnapshotobjlist.h
> index b83f7a4ba9..12b574b4ff 100644
> --- a/src/conf/virdomainsnapshotobjlist.h
> +++ b/src/conf/virdomainsnapshotobjlist.h
> @@ -87,7 +87,7 @@ int virDomainListSnapshots(virDomainSnapshotObjListPtr snapshots,
>  static inline virDomainSnapshotDefPtr
>  virDomainSnapshotObjGetDef(virDomainMomentObjPtr obj)
>  {
> -    return (virDomainSnapshotDefPtr) obj->def;
> +    return obj ? (virDomainSnapshotDefPtr) obj->def : NULL;
>  }
> 
>  #endif /* LIBVIRT_VIRDOMAINSNAPSHOTOBJLIST_H */
> diff --git a/src/conf/snapshot_conf.c b/src/conf/snapshot_conf.c
> index 4ce120451e..8e4f3d9410 100644
> --- a/src/conf/snapshot_conf.c
> +++ b/src/conf/snapshot_conf.c
> @@ -967,7 +967,7 @@ virDomainSnapshotRedefinePrep(virDomainPtr domain,
>      }
> 
>      other = virDomainSnapshotFindByName(vm->snapshots, def->common.name);
> -    otherdef = other ? virDomainSnapshotObjGetDef(other) : NULL;
> +    otherdef = virDomainSnapshotObjGetDef(other);
>      check_if_stolen = other && otherdef->common.dom;
>      if (virDomainSnapshotRedefineValidate(def, domain->uuid, other, xmlopt,
>                                            flags) < 0) {
> 

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org

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