All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] hw/display/xenfb: Replace unreachable code by abort()
@ 2025-07-29 11:12 Markus Armbruster
  2025-07-29 11:26 ` Philippe Mathieu-Daudé
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Markus Armbruster @ 2025-07-29 11:12 UTC (permalink / raw)
  To: qemu-devel
  Cc: sstabellini, anthony, paul, edgar.iglesias, xen-devel,
	qemu-trivial

xenfb_mouse_event() has a switch statement whose controlling
expression move->axis is an enum InputAxis.  The enum values are
INPUT_AXIS_X and INPUT_AXIS_Y, encoded as 0 and 1.  The switch has a
case for both axes.  In addition, it has an unreachable default label.
This convinces Coverity that move->axis can be greater than 1.  It
duly reports a buffer overrun when it is used to subscript an array
with two elements.

Replace the unreachable code by abort().

Resolves: Coverity CID 1613906
Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 hw/display/xenfb.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/hw/display/xenfb.c b/hw/display/xenfb.c
index 22822fecea..5e6c691779 100644
--- a/hw/display/xenfb.c
+++ b/hw/display/xenfb.c
@@ -283,8 +283,7 @@ static void xenfb_mouse_event(DeviceState *dev, QemuConsole *src,
                 scale = surface_height(surface) - 1;
                 break;
             default:
-                scale = 0x8000;
-                break;
+                abort();
             }
             xenfb->axis[move->axis] = move->value * scale / 0x7fff;
         }
-- 
2.49.0



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH] hw/display/xenfb: Replace unreachable code by abort()
  2025-07-29 11:12 [PATCH] hw/display/xenfb: Replace unreachable code by abort() Markus Armbruster
@ 2025-07-29 11:26 ` Philippe Mathieu-Daudé
  2025-07-29 12:16   ` Markus Armbruster
  2025-10-13 11:10 ` Markus Armbruster
  2025-10-14  8:36 ` Peter Maydell
  2 siblings, 1 reply; 13+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-07-29 11:26 UTC (permalink / raw)
  To: Markus Armbruster, qemu-devel
  Cc: sstabellini, anthony, paul, edgar.iglesias, xen-devel,
	qemu-trivial

On 29/7/25 13:12, Markus Armbruster wrote:
> xenfb_mouse_event() has a switch statement whose controlling
> expression move->axis is an enum InputAxis.  The enum values are
> INPUT_AXIS_X and INPUT_AXIS_Y, encoded as 0 and 1.  The switch has a
> case for both axes.  In addition, it has an unreachable default label.
> This convinces Coverity that move->axis can be greater than 1.  It
> duly reports a buffer overrun when it is used to subscript an array
> with two elements.
> 
> Replace the unreachable code by abort().
> 
> Resolves: Coverity CID 1613906
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>   hw/display/xenfb.c | 3 +--
>   1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/hw/display/xenfb.c b/hw/display/xenfb.c
> index 22822fecea..5e6c691779 100644
> --- a/hw/display/xenfb.c
> +++ b/hw/display/xenfb.c
> @@ -283,8 +283,7 @@ static void xenfb_mouse_event(DeviceState *dev, QemuConsole *src,
>                   scale = surface_height(surface) - 1;
>                   break;
>               default:
> -                scale = 0x8000;
> -                break;
> +                abort();

We prefer GLib g_assert_not_reached() over abort() because it displays
the file, line number & function before aborting.

>               }
>               xenfb->axis[move->axis] = move->value * scale / 0x7fff;
>           }



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] hw/display/xenfb: Replace unreachable code by abort()
  2025-07-29 11:26 ` Philippe Mathieu-Daudé
@ 2025-07-29 12:16   ` Markus Armbruster
  2025-07-29 12:59     ` Philippe Mathieu-Daudé
  0 siblings, 1 reply; 13+ messages in thread
From: Markus Armbruster @ 2025-07-29 12:16 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé
  Cc: qemu-devel, sstabellini, anthony, paul, edgar.iglesias, xen-devel,
	qemu-trivial

Philippe Mathieu-Daudé <philmd@linaro.org> writes:

> On 29/7/25 13:12, Markus Armbruster wrote:
>> xenfb_mouse_event() has a switch statement whose controlling
>> expression move->axis is an enum InputAxis.  The enum values are
>> INPUT_AXIS_X and INPUT_AXIS_Y, encoded as 0 and 1.  The switch has a
>> case for both axes.  In addition, it has an unreachable default label.
>> This convinces Coverity that move->axis can be greater than 1.  It
>> duly reports a buffer overrun when it is used to subscript an array
>> with two elements.
>> Replace the unreachable code by abort().
>> Resolves: Coverity CID 1613906
>> Signed-off-by: Markus Armbruster <armbru@redhat.com>
>> ---
>>   hw/display/xenfb.c | 3 +--
>>   1 file changed, 1 insertion(+), 2 deletions(-)
>> diff --git a/hw/display/xenfb.c b/hw/display/xenfb.c
>> index 22822fecea..5e6c691779 100644
>> --- a/hw/display/xenfb.c
>> +++ b/hw/display/xenfb.c
>> @@ -283,8 +283,7 @@ static void xenfb_mouse_event(DeviceState *dev, QemuConsole *src,
>>                   scale = surface_height(surface) - 1;
>>                   break;
>>               default:
>> -                scale = 0x8000;
>> -                break;
>> +                abort();
>
> We prefer GLib g_assert_not_reached() over abort() because it displays
> the file, line number & function before aborting.

The purpose of this line is to tell the compiler we can't get there,
with the least amount of ceremony.

We have ~600 calls of abort().

Whoever merges this: feel free to replace by g_assert_not_reached().

>>               }
>>               xenfb->axis[move->axis] = move->value * scale / 0x7fff;
>>           }



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] hw/display/xenfb: Replace unreachable code by abort()
  2025-07-29 12:16   ` Markus Armbruster
@ 2025-07-29 12:59     ` Philippe Mathieu-Daudé
  2025-07-29 13:16       ` Daniel P. Berrangé
  0 siblings, 1 reply; 13+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-07-29 12:59 UTC (permalink / raw)
  To: Markus Armbruster
  Cc: qemu-devel, sstabellini, anthony, paul, edgar.iglesias, xen-devel,
	qemu-trivial

On 29/7/25 14:16, Markus Armbruster wrote:
> Philippe Mathieu-Daudé <philmd@linaro.org> writes:
> 
>> On 29/7/25 13:12, Markus Armbruster wrote:
>>> xenfb_mouse_event() has a switch statement whose controlling
>>> expression move->axis is an enum InputAxis.  The enum values are
>>> INPUT_AXIS_X and INPUT_AXIS_Y, encoded as 0 and 1.  The switch has a
>>> case for both axes.  In addition, it has an unreachable default label.
>>> This convinces Coverity that move->axis can be greater than 1.  It
>>> duly reports a buffer overrun when it is used to subscript an array
>>> with two elements.
>>> Replace the unreachable code by abort().
>>> Resolves: Coverity CID 1613906
>>> Signed-off-by: Markus Armbruster <armbru@redhat.com>
>>> ---
>>>    hw/display/xenfb.c | 3 +--
>>>    1 file changed, 1 insertion(+), 2 deletions(-)
>>> diff --git a/hw/display/xenfb.c b/hw/display/xenfb.c
>>> index 22822fecea..5e6c691779 100644
>>> --- a/hw/display/xenfb.c
>>> +++ b/hw/display/xenfb.c
>>> @@ -283,8 +283,7 @@ static void xenfb_mouse_event(DeviceState *dev, QemuConsole *src,
>>>                    scale = surface_height(surface) - 1;
>>>                    break;
>>>                default:
>>> -                scale = 0x8000;
>>> -                break;
>>> +                abort();
>>
>> We prefer GLib g_assert_not_reached() over abort() because it displays
>> the file, line number & function before aborting.
> 
> The purpose of this line is to tell the compiler we can't get there,
> with the least amount of ceremony.
> 
> We have ~600 calls of abort().

And ~1600 of g_assert_not_reached() =)

$ git grep -w 'abort();' | wc -l
      556
$ git grep -w 'g_assert_not_reached();' | wc -l
     1551

> Whoever merges this: feel free to replace by g_assert_not_reached().
> 
>>>                }
>>>                xenfb->axis[move->axis] = move->value * scale / 0x7fff;
>>>            }
> 



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] hw/display/xenfb: Replace unreachable code by abort()
  2025-07-29 12:59     ` Philippe Mathieu-Daudé
@ 2025-07-29 13:16       ` Daniel P. Berrangé
  0 siblings, 0 replies; 13+ messages in thread
From: Daniel P. Berrangé @ 2025-07-29 13:16 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé
  Cc: Markus Armbruster, qemu-devel, sstabellini, anthony, paul,
	edgar.iglesias, xen-devel, qemu-trivial

On Tue, Jul 29, 2025 at 02:59:46PM +0200, Philippe Mathieu-Daudé wrote:
> On 29/7/25 14:16, Markus Armbruster wrote:
> > Philippe Mathieu-Daudé <philmd@linaro.org> writes:
> > 
> > > On 29/7/25 13:12, Markus Armbruster wrote:
> > > > xenfb_mouse_event() has a switch statement whose controlling
> > > > expression move->axis is an enum InputAxis.  The enum values are
> > > > INPUT_AXIS_X and INPUT_AXIS_Y, encoded as 0 and 1.  The switch has a
> > > > case for both axes.  In addition, it has an unreachable default label.
> > > > This convinces Coverity that move->axis can be greater than 1.  It
> > > > duly reports a buffer overrun when it is used to subscript an array
> > > > with two elements.
> > > > Replace the unreachable code by abort().
> > > > Resolves: Coverity CID 1613906
> > > > Signed-off-by: Markus Armbruster <armbru@redhat.com>
> > > > ---
> > > >    hw/display/xenfb.c | 3 +--
> > > >    1 file changed, 1 insertion(+), 2 deletions(-)
> > > > diff --git a/hw/display/xenfb.c b/hw/display/xenfb.c
> > > > index 22822fecea..5e6c691779 100644
> > > > --- a/hw/display/xenfb.c
> > > > +++ b/hw/display/xenfb.c
> > > > @@ -283,8 +283,7 @@ static void xenfb_mouse_event(DeviceState *dev, QemuConsole *src,
> > > >                    scale = surface_height(surface) - 1;
> > > >                    break;
> > > >                default:
> > > > -                scale = 0x8000;
> > > > -                break;
> > > > +                abort();
> > > 
> > > We prefer GLib g_assert_not_reached() over abort() because it displays
> > > the file, line number & function before aborting.
> > 
> > The purpose of this line is to tell the compiler we can't get there,
> > with the least amount of ceremony.
> > 
> > We have ~600 calls of abort().
> 
> And ~1600 of g_assert_not_reached() =)
> 
> $ git grep -w 'abort();' | wc -l
>      556
> $ git grep -w 'g_assert_not_reached();' | wc -l
>     1551

Sounds like we could create a gitlab issue for "replace abort with
g_assert_not_reached" that could be a easy on-ramp for someone to
start contributing.

With 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 :|



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] hw/display/xenfb: Replace unreachable code by abort()
  2025-07-29 11:12 [PATCH] hw/display/xenfb: Replace unreachable code by abort() Markus Armbruster
  2025-07-29 11:26 ` Philippe Mathieu-Daudé
@ 2025-10-13 11:10 ` Markus Armbruster
  2025-10-13 14:14   ` Philippe Mathieu-Daudé
  2025-10-13 19:17   ` Bernhard Beschow
  2025-10-14  8:36 ` Peter Maydell
  2 siblings, 2 replies; 13+ messages in thread
From: Markus Armbruster @ 2025-10-13 11:10 UTC (permalink / raw)
  To: qemu-devel
  Cc: sstabellini, anthony, paul, edgar.iglesias, xen-devel,
	qemu-trivial

Ping?

Markus Armbruster <armbru@redhat.com> writes:

> xenfb_mouse_event() has a switch statement whose controlling
> expression move->axis is an enum InputAxis.  The enum values are
> INPUT_AXIS_X and INPUT_AXIS_Y, encoded as 0 and 1.  The switch has a
> case for both axes.  In addition, it has an unreachable default label.
> This convinces Coverity that move->axis can be greater than 1.  It
> duly reports a buffer overrun when it is used to subscript an array
> with two elements.
>
> Replace the unreachable code by abort().
>
> Resolves: Coverity CID 1613906
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>  hw/display/xenfb.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/hw/display/xenfb.c b/hw/display/xenfb.c
> index 22822fecea..5e6c691779 100644
> --- a/hw/display/xenfb.c
> +++ b/hw/display/xenfb.c
> @@ -283,8 +283,7 @@ static void xenfb_mouse_event(DeviceState *dev, QemuConsole *src,
>                  scale = surface_height(surface) - 1;
>                  break;
>              default:
> -                scale = 0x8000;
> -                break;
> +                abort();
>              }
>              xenfb->axis[move->axis] = move->value * scale / 0x7fff;
>          }



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] hw/display/xenfb: Replace unreachable code by abort()
  2025-10-13 11:10 ` Markus Armbruster
@ 2025-10-13 14:14   ` Philippe Mathieu-Daudé
  2025-10-13 19:17   ` Bernhard Beschow
  1 sibling, 0 replies; 13+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-10-13 14:14 UTC (permalink / raw)
  To: Markus Armbruster, qemu-devel
  Cc: sstabellini, anthony, paul, edgar.iglesias, xen-devel,
	qemu-trivial

On 13/10/25 13:10, Markus Armbruster wrote:
> Ping?
> 
> Markus Armbruster <armbru@redhat.com> writes:
> 
>> xenfb_mouse_event() has a switch statement whose controlling
>> expression move->axis is an enum InputAxis.  The enum values are
>> INPUT_AXIS_X and INPUT_AXIS_Y, encoded as 0 and 1.  The switch has a
>> case for both axes.  In addition, it has an unreachable default label.
>> This convinces Coverity that move->axis can be greater than 1.  It
>> duly reports a buffer overrun when it is used to subscript an array
>> with two elements.
>>
>> Replace the unreachable code by abort().
>>
>> Resolves: Coverity CID 1613906
>> Signed-off-by: Markus Armbruster <armbru@redhat.com>
>> ---
>>   hw/display/xenfb.c | 3 +--
>>   1 file changed, 1 insertion(+), 2 deletions(-)

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>

and queued with g_assert_not_reached(), thanks!


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] hw/display/xenfb: Replace unreachable code by abort()
  2025-10-13 11:10 ` Markus Armbruster
  2025-10-13 14:14   ` Philippe Mathieu-Daudé
@ 2025-10-13 19:17   ` Bernhard Beschow
  2025-10-14  7:42     ` Markus Armbruster
  1 sibling, 1 reply; 13+ messages in thread
From: Bernhard Beschow @ 2025-10-13 19:17 UTC (permalink / raw)
  To: qemu-devel, Markus Armbruster
  Cc: sstabellini, anthony, paul, edgar.iglesias, xen-devel,
	qemu-trivial



Am 13. Oktober 2025 11:10:45 UTC schrieb Markus Armbruster <armbru@redhat.com>:
>Ping?
>
>Markus Armbruster <armbru@redhat.com> writes:
>
>> xenfb_mouse_event() has a switch statement whose controlling
>> expression move->axis is an enum InputAxis.  The enum values are
>> INPUT_AXIS_X and INPUT_AXIS_Y, encoded as 0 and 1.  The switch has a
>> case for both axes.  In addition, it has an unreachable default label.
>> This convinces Coverity that move->axis can be greater than 1.  It
>> duly reports a buffer overrun when it is used to subscript an array
>> with two elements.
>>
>> Replace the unreachable code by abort().
>>
>> Resolves: Coverity CID 1613906
>> Signed-off-by: Markus Armbruster <armbru@redhat.com>
>> ---
>>  hw/display/xenfb.c | 3 +--
>>  1 file changed, 1 insertion(+), 2 deletions(-)
>>
>> diff --git a/hw/display/xenfb.c b/hw/display/xenfb.c
>> index 22822fecea..5e6c691779 100644
>> --- a/hw/display/xenfb.c
>> +++ b/hw/display/xenfb.c
>> @@ -283,8 +283,7 @@ static void xenfb_mouse_event(DeviceState *dev, QemuConsole *src,
>>                  scale = surface_height(surface) - 1;
>>                  break;
>>              default:
>> -                scale = 0x8000;
>> -                break;
>> +                abort();

Don't we prefer g_assert_not_reached() these days, for more expressiveness?

Best regards,
Bernhard

>>              }
>>              xenfb->axis[move->axis] = move->value * scale / 0x7fff;
>>          }
>
>


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] hw/display/xenfb: Replace unreachable code by abort()
  2025-10-13 19:17   ` Bernhard Beschow
@ 2025-10-14  7:42     ` Markus Armbruster
  0 siblings, 0 replies; 13+ messages in thread
From: Markus Armbruster @ 2025-10-14  7:42 UTC (permalink / raw)
  To: Bernhard Beschow
  Cc: qemu-devel, sstabellini, anthony, paul, edgar.iglesias, xen-devel,
	qemu-trivial

Bernhard Beschow <shentey@gmail.com> writes:

> Am 13. Oktober 2025 11:10:45 UTC schrieb Markus Armbruster <armbru@redhat.com>:
>>Ping?
>>
>>Markus Armbruster <armbru@redhat.com> writes:
>>
>>> xenfb_mouse_event() has a switch statement whose controlling
>>> expression move->axis is an enum InputAxis.  The enum values are
>>> INPUT_AXIS_X and INPUT_AXIS_Y, encoded as 0 and 1.  The switch has a
>>> case for both axes.  In addition, it has an unreachable default label.
>>> This convinces Coverity that move->axis can be greater than 1.  It
>>> duly reports a buffer overrun when it is used to subscript an array
>>> with two elements.
>>>
>>> Replace the unreachable code by abort().
>>>
>>> Resolves: Coverity CID 1613906
>>> Signed-off-by: Markus Armbruster <armbru@redhat.com>
>>> ---
>>>  hw/display/xenfb.c | 3 +--
>>>  1 file changed, 1 insertion(+), 2 deletions(-)
>>>
>>> diff --git a/hw/display/xenfb.c b/hw/display/xenfb.c
>>> index 22822fecea..5e6c691779 100644
>>> --- a/hw/display/xenfb.c
>>> +++ b/hw/display/xenfb.c
>>> @@ -283,8 +283,7 @@ static void xenfb_mouse_event(DeviceState *dev, QemuConsole *src,
>>>                  scale = surface_height(surface) - 1;
>>>                  break;
>>>              default:
>>> -                scale = 0x8000;
>>> -                break;
>>> +                abort();
>
> Don't we prefer g_assert_not_reached() these days, for more expressiveness?

See https://lore.kernel.org/qemu-devel/87v7nbdwfx.fsf@pond.sub.org/

[...]



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] hw/display/xenfb: Replace unreachable code by abort()
  2025-07-29 11:12 [PATCH] hw/display/xenfb: Replace unreachable code by abort() Markus Armbruster
  2025-07-29 11:26 ` Philippe Mathieu-Daudé
  2025-10-13 11:10 ` Markus Armbruster
@ 2025-10-14  8:36 ` Peter Maydell
  2025-10-14 12:59   ` Peter Maydell
  2 siblings, 1 reply; 13+ messages in thread
From: Peter Maydell @ 2025-10-14  8:36 UTC (permalink / raw)
  To: Markus Armbruster
  Cc: qemu-devel, sstabellini, anthony, paul, edgar.iglesias, xen-devel,
	qemu-trivial

On Tue, 29 Jul 2025 at 12:14, Markus Armbruster <armbru@redhat.com> wrote:
>
> xenfb_mouse_event() has a switch statement whose controlling
> expression move->axis is an enum InputAxis.  The enum values are
> INPUT_AXIS_X and INPUT_AXIS_Y, encoded as 0 and 1.  The switch has a
> case for both axes.  In addition, it has an unreachable default label.
> This convinces Coverity that move->axis can be greater than 1.  It
> duly reports a buffer overrun when it is used to subscript an array
> with two elements.

I think also that Coverity gets confused by QAPI's convention
in generated code of defining enumerations like this:

typedef enum InputAxis {
    INPUT_AXIS_X,
    INPUT_AXIS_Y,
    INPUT_AXIS__MAX,
} InputAxis;

Coverity thinks that INPUT_AXIS__MAX might be a valid
value it can see in move->axis, because we defined the
enum that way.

In theory I suppose that since the __MAX value is only
there to be an array or enumeration bound that we could
emit code that #defines it rather than making it part
of the enum.

thanks
-- PMM


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] hw/display/xenfb: Replace unreachable code by abort()
  2025-10-14  8:36 ` Peter Maydell
@ 2025-10-14 12:59   ` Peter Maydell
  2025-10-14 15:19     ` Philippe Mathieu-Daudé
  0 siblings, 1 reply; 13+ messages in thread
From: Peter Maydell @ 2025-10-14 12:59 UTC (permalink / raw)
  To: Markus Armbruster
  Cc: qemu-devel, sstabellini, anthony, paul, edgar.iglesias, xen-devel,
	qemu-trivial

On Tue, 14 Oct 2025 at 09:36, Peter Maydell <peter.maydell@linaro.org> wrote:
>
> On Tue, 29 Jul 2025 at 12:14, Markus Armbruster <armbru@redhat.com> wrote:
> >
> > xenfb_mouse_event() has a switch statement whose controlling
> > expression move->axis is an enum InputAxis.  The enum values are
> > INPUT_AXIS_X and INPUT_AXIS_Y, encoded as 0 and 1.  The switch has a
> > case for both axes.  In addition, it has an unreachable default label.
> > This convinces Coverity that move->axis can be greater than 1.  It
> > duly reports a buffer overrun when it is used to subscript an array
> > with two elements.
>
> I think also that Coverity gets confused by QAPI's convention
> in generated code of defining enumerations like this:
>
> typedef enum InputAxis {
>     INPUT_AXIS_X,
>     INPUT_AXIS_Y,
>     INPUT_AXIS__MAX,
> } InputAxis;
>
> Coverity thinks that INPUT_AXIS__MAX might be a valid
> value it can see in move->axis, because we defined the
> enum that way.
>
> In theory I suppose that since the __MAX value is only
> there to be an array or enumeration bound that we could
> emit code that #defines it rather than making it part
> of the enum.

Also, there's an argument that this function should
ignore unknown input-axis enum values. If we ever in future
extend this to support a Z-axis, it would be better to
ignore the events we can't send, the same way we already
do for unknown INPUT_EVENT_KIND_BTN button types, rather
than aborting. But it's not very important, so the
g_assert_not_reached() will do.

(In some other languages, we'd get a compile failure for
adding a new value to the enum that we didn't handle.
But not in C :-))

thanks
-- PMM


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] hw/display/xenfb: Replace unreachable code by abort()
  2025-10-14 12:59   ` Peter Maydell
@ 2025-10-14 15:19     ` Philippe Mathieu-Daudé
  2025-10-15  5:50       ` Markus Armbruster
  0 siblings, 1 reply; 13+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-10-14 15:19 UTC (permalink / raw)
  To: Peter Maydell, Markus Armbruster
  Cc: qemu-devel, sstabellini, anthony, paul, edgar.iglesias, xen-devel,
	qemu-trivial

On 14/10/25 14:59, Peter Maydell wrote:
> On Tue, 14 Oct 2025 at 09:36, Peter Maydell <peter.maydell@linaro.org> wrote:
>>
>> On Tue, 29 Jul 2025 at 12:14, Markus Armbruster <armbru@redhat.com> wrote:
>>>
>>> xenfb_mouse_event() has a switch statement whose controlling
>>> expression move->axis is an enum InputAxis.  The enum values are
>>> INPUT_AXIS_X and INPUT_AXIS_Y, encoded as 0 and 1.  The switch has a
>>> case for both axes.  In addition, it has an unreachable default label.
>>> This convinces Coverity that move->axis can be greater than 1.  It
>>> duly reports a buffer overrun when it is used to subscript an array
>>> with two elements.
>>
>> I think also that Coverity gets confused by QAPI's convention
>> in generated code of defining enumerations like this:
>>
>> typedef enum InputAxis {
>>      INPUT_AXIS_X,
>>      INPUT_AXIS_Y,
>>      INPUT_AXIS__MAX,
>> } InputAxis;
>>
>> Coverity thinks that INPUT_AXIS__MAX might be a valid
>> value it can see in move->axis, because we defined the
>> enum that way.
>>
>> In theory I suppose that since the __MAX value is only
>> there to be an array or enumeration bound that we could
>> emit code that #defines it rather than making it part
>> of the enum.
> 
> Also, there's an argument that this function should
> ignore unknown input-axis enum values. If we ever in future
> extend this to support a Z-axis, it would be better to
> ignore the events we can't send, the same way we already
> do for unknown INPUT_EVENT_KIND_BTN button types, rather
> than aborting. But it's not very important, so the
> g_assert_not_reached() will do.
> 
> (In some other languages, we'd get a compile failure for
> adding a new value to the enum that we didn't handle.
> But not in C :-))

See this thread where it was discussed (until I gave up...):
https://lore.kernel.org/qemu-devel/873564spze.fsf@pond.sub.org/


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] hw/display/xenfb: Replace unreachable code by abort()
  2025-10-14 15:19     ` Philippe Mathieu-Daudé
@ 2025-10-15  5:50       ` Markus Armbruster
  0 siblings, 0 replies; 13+ messages in thread
From: Markus Armbruster @ 2025-10-15  5:50 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé
  Cc: Peter Maydell, qemu-devel, sstabellini, anthony, paul,
	edgar.iglesias, xen-devel, qemu-trivial

Philippe Mathieu-Daudé <philmd@linaro.org> writes:

> On 14/10/25 14:59, Peter Maydell wrote:
>> On Tue, 14 Oct 2025 at 09:36, Peter Maydell <peter.maydell@linaro.org> wrote:
>>>
>>> On Tue, 29 Jul 2025 at 12:14, Markus Armbruster <armbru@redhat.com> wrote:
>>>>
>>>> xenfb_mouse_event() has a switch statement whose controlling
>>>> expression move->axis is an enum InputAxis.  The enum values are
>>>> INPUT_AXIS_X and INPUT_AXIS_Y, encoded as 0 and 1.  The switch has a
>>>> case for both axes.  In addition, it has an unreachable default label.
>>>> This convinces Coverity that move->axis can be greater than 1.  It
>>>> duly reports a buffer overrun when it is used to subscript an array
>>>> with two elements.
>>>
>>> I think also that Coverity gets confused by QAPI's convention
>>> in generated code of defining enumerations like this:
>>>
>>> typedef enum InputAxis {
>>>      INPUT_AXIS_X,
>>>      INPUT_AXIS_Y,
>>>      INPUT_AXIS__MAX,
>>> } InputAxis;
>>>
>>> Coverity thinks that INPUT_AXIS__MAX might be a valid
>>> value it can see in move->axis, because we defined the
>>> enum that way.
>>>
>>> In theory I suppose that since the __MAX value is only
>>> there to be an array or enumeration bound

Correct.

>>>                                           that we could
>>> emit code that #defines it rather than making it part
>>> of the enum.

I'd love that, but it's harder than it has any right to be; see the
message Philippe referred to below.

>> Also, there's an argument that this function should
>> ignore unknown input-axis enum values. If we ever in future
>> extend this to support a Z-axis, it would be better to
>> ignore the events we can't send, the same way we already
>> do for unknown INPUT_EVENT_KIND_BTN button types, rather
>> than aborting.

No objection.

>>                But it's not very important, so the
>> g_assert_not_reached() will do.
>> 
>> (In some other languages, we'd get a compile failure for
>> adding a new value to the enum that we didn't handle.
>> But not in C :-))
>
> See this thread where it was discussed (until I gave up...):
> https://lore.kernel.org/qemu-devel/873564spze.fsf@pond.sub.org/



^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2025-10-15  5:51 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-29 11:12 [PATCH] hw/display/xenfb: Replace unreachable code by abort() Markus Armbruster
2025-07-29 11:26 ` Philippe Mathieu-Daudé
2025-07-29 12:16   ` Markus Armbruster
2025-07-29 12:59     ` Philippe Mathieu-Daudé
2025-07-29 13:16       ` Daniel P. Berrangé
2025-10-13 11:10 ` Markus Armbruster
2025-10-13 14:14   ` Philippe Mathieu-Daudé
2025-10-13 19:17   ` Bernhard Beschow
2025-10-14  7:42     ` Markus Armbruster
2025-10-14  8:36 ` Peter Maydell
2025-10-14 12:59   ` Peter Maydell
2025-10-14 15:19     ` Philippe Mathieu-Daudé
2025-10-15  5:50       ` Markus Armbruster

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.