[PATCH v2 07/10] qdev: Abort if the root machine container is missing

Philippe Mathieu-Daudé posted 10 patches 6 years ago
Maintainers: David Gibson <david@gibson.dropbear.id.au>, Paolo Bonzini <pbonzini@redhat.com>, Richard Henderson <rth@twiddle.net>, Peter Maydell <peter.maydell@linaro.org>, Eduardo Habkost <ehabkost@redhat.com>, Marcelo Tosatti <mtosatti@redhat.com>, Cornelia Huck <cohuck@redhat.com>, David Hildenbrand <david@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>
[PATCH v2 07/10] qdev: Abort if the root machine container is missing
Posted by Philippe Mathieu-Daudé 6 years ago
The QEMU device API (qdev) relies on having the '/machine'
container always available.

If it is missing, QEMU will later crash dereferencing a NULL
pointer, we will get a SEGV, open a debugger, look at the
backtrace, and figure out we messed with QOM.
Or we can use g_assert() which abort, displaying the filename
and line number, so we can quickly open our favorite IDE.
Prefer the later, to save time to developers.

Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
v2: New patch
---
 hw/core/qdev.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/hw/core/qdev.c b/hw/core/qdev.c
index 58e87d336d..d30cf6320b 100644
--- a/hw/core/qdev.c
+++ b/hw/core/qdev.c
@@ -1143,6 +1143,7 @@ Object *qdev_get_machine(void)
 
     if (dev == NULL) {
         dev = container_get(object_get_root(), "/machine");
+        g_assert(dev != NULL);
     }
 
     return dev;
-- 
2.21.1


Re: [PATCH v2 07/10] qdev: Abort if the root machine container is missing
Posted by Markus Armbruster 6 years ago
Philippe Mathieu-Daudé <philmd@redhat.com> writes:

> The QEMU device API (qdev) relies on having the '/machine'
> container always available.
>
> If it is missing, QEMU will later crash dereferencing a NULL
> pointer, we will get a SEGV, open a debugger, look at the
> backtrace, and figure out we messed with QOM.
> Or we can use g_assert() which abort, displaying the filename
> and line number, so we can quickly open our favorite IDE.
> Prefer the later, to save time to developers.
>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
> ---
> v2: New patch
> ---
>  hw/core/qdev.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/hw/core/qdev.c b/hw/core/qdev.c
> index 58e87d336d..d30cf6320b 100644
> --- a/hw/core/qdev.c
> +++ b/hw/core/qdev.c
> @@ -1143,6 +1143,7 @@ Object *qdev_get_machine(void)
>  
>      if (dev == NULL) {
>          dev = container_get(object_get_root(), "/machine");
> +        g_assert(dev != NULL);
>      }
>  
>      return dev;

container_get()'s contract promises it won't return null.  I think the
assertion belongs there instead.