Skip to contentSkip to conversation

hw/display/xenfb: Replace unreachable code by abort()

PATCH In review Markus Armbruster 5 participants 13 messages Jul 29, 2025 – Oct 15, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/display/xenfb: Replace unreachable code by abort()

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

---
 hw/display/xenfb.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
hw/display/xenfb.c +1 -2
index 22822fecea..5e6c691779 100644
@@ -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;
         }
Signature · 1 line
2.49.0
#1 lore raw
Quoted 21 lines · Markus Armbruster

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

---
  hw/display/xenfb.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
hw/display/xenfb.c +0 -0
index 22822fecea..5e6c691779 100644

hw/display/xenfb.c static void xenfb_mouse_event(DeviceState *dev, QemuConsole *src, · line 286

Quoted 6 lines
hw/display/xenfb.c +1 -2
@@ -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.

Quoted 3 lines
hw/display/xenfb.c +0 -0
              }
              xenfb->axis[move->axis] = move->value * scale / 0x7fff;
          }
#2 lore raw
Quoted 20 lines · Philippe Mathieu-Daudé
Quoted 19 lines · Markus Armbruster

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

---
  hw/display/xenfb.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
hw/display/xenfb.c +0 -0
index 22822fecea..5e6c691779 100644
@@ -283,8 +283,7 @@ static void xenfb_mouse_event(DeviceState *dev, QemuConsole *src,
                  scale = surface_height(surface) - 1;
Quoted 8 lines

hw/display/xenfb.c static void xenfb_mouse_event(DeviceState *dev, QemuConsole *src, · line 286

Quoted 5 lines
hw/display/xenfb.c +1 -2
                  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().

Quoted 3 lines
Quoted 3 lines
hw/display/xenfb.c +0 -0
              }
              xenfb->axis[move->axis] = move->value * scale / 0x7fff;
          }
#3 lore raw
Quoted 31 lines · Markus Armbruster
Quoted 20 lines · Philippe Mathieu-Daudé
Quoted 19 lines · Markus Armbruster

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

---
   hw/display/xenfb.c | 3 +--
   1 file changed, 1 insertion(+), 2 deletions(-)
hw/display/xenfb.c +0 -0
index 22822fecea..5e6c691779 100644
@@ -283,8 +283,7 @@ static void xenfb_mouse_event(DeviceState *dev, QemuConsole *src,
                   scale = surface_height(surface) - 1;
Quoted 8 lines

hw/display/xenfb.c static void xenfb_mouse_event(DeviceState *dev, QemuConsole *src, · line 286

Quoted 5 lines
hw/display/xenfb.c +1 -2
                   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.

Quoted 4 lines

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
Quoted 6 lines

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

Quoted 3 lines
Quoted 3 lines
hw/display/xenfb.c +0 -0
               }
               xenfb->axis[move->axis] = move->value * scale / 0x7fff;
           }
#4 lore raw
Quoted 35 lines · Philippe Mathieu-Daudé
Quoted 34 lines · Markus Armbruster
Quoted 20 lines · Philippe Mathieu-Daudé
Quoted 19 lines · Markus Armbruster

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

---
   hw/display/xenfb.c | 3 +--
   1 file changed, 1 insertion(+), 2 deletions(-)
hw/display/xenfb.c +0 -0
index 22822fecea..5e6c691779 100644
@@ -283,8 +283,7 @@ static void xenfb_mouse_event(DeviceState *dev, QemuConsole *src,
                   scale = surface_height(surface) - 1;
Quoted 8 lines

hw/display/xenfb.c static void xenfb_mouse_event(DeviceState *dev, QemuConsole *src, · line 286

Quoted 5 lines
hw/display/xenfb.c +1 -2
                   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.

Quoted 8 lines
Quoted 1 line

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

Signature · 3 lines
|: 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 :|
#5 lore raw
In reply to Markus Armbruster

Ping?

Quoted 30 lines · Markus Armbruster

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

---
 hw/display/xenfb.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
hw/display/xenfb.c +1 -2
index 22822fecea..5e6c691779 100644
@@ -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;
         }
#6 lore raw
Quoted 19 lines · Markus Armbruster

Ping?

Quoted 15 lines · Markus Armbruster

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

---
  hw/display/xenfb.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)

and queued with g_assert_not_reached(), thanks!

#7 lore raw
In reply to Markus Armbruster

Am 13. Oktober 2025 11:10:45 UTC schrieb Markus Armbruster <[email protected]>:

Quoted 20 lines

Ping?

Quoted 16 lines · Markus Armbruster

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

---
 hw/display/xenfb.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
Quoted 11 lines

hw/display/xenfb.c static void xenfb_mouse_event(DeviceState *dev, QemuConsole *src, · line 286

Quoted 11 lines
hw/display/xenfb.c +1 -2
index 22822fecea..5e6c691779 100644
@@ -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

Quoted 5 lines
Quoted 3 lines
hw/display/xenfb.c +0 -0
             }
             xenfb->axis[move->axis] = move->value * scale / 0x7fff;
         }
#8 lore raw
Quoted 21 lines · Bernhard Beschow

Am 13. Oktober 2025 11:10:45 UTC schrieb Markus Armbruster <[email protected]>:

Quoted 20 lines

Ping?

Quoted 16 lines · Markus Armbruster

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

---
 hw/display/xenfb.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
Quoted 13 lines
Quoted 11 lines

hw/display/xenfb.c static void xenfb_mouse_event(DeviceState *dev, QemuConsole *src, · line 286

Quoted 11 lines
hw/display/xenfb.c +1 -2
index 22822fecea..5e6c691779 100644
@@ -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/[email protected]/

[...]

#9 lore raw
In reply to Markus Armbruster
Quoted 8 lines · Markus Armbruster

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

#10 lore raw
Quoted 20 lines · Peter Maydell
Quoted 8 lines · Markus Armbruster

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;

Quoted 8 lines

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

#11 lore raw
Quoted 38 lines · Peter Maydell
Quoted 20 lines · Peter Maydell
Quoted 8 lines · Markus Armbruster

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;

Quoted 8 lines

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.

Quoted 3 lines

(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/[email protected]/

#12 lore raw
Quoted 22 lines · Philippe Mathieu-Daudé
Quoted 21 lines · Peter Maydell
Quoted 20 lines · Peter Maydell
Quoted 8 lines · Markus Armbruster

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;

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

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.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

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.

Quoted 6 lines
Quoted 6 lines

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.

Quoted 9 lines
Quoted 6 lines

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/[email protected]/