Skip to contentSkip to conversation

usb-linux: remove unreachable default in switch statement

PATCH Applied Paul Bolle 4 participants 13 messages Mar 8, 2010 – Mar 17, 2010

lore
Share & tools
lore

Paul Bolle

author
#0 lore raw

PATCH 1/1 usb-linux: remove unreachable default in switch statement

---
 usb-linux.c |    3 ---
 1 files changed, 0 insertions(+), 3 deletions(-)
usb-linux.c +0 -3
index a9c15c6..23155dd 100644
@@ -846,9 +846,6 @@ static int usb_linux_update_endp_table(USBHostDevice *s)
             case 0x03:
                 type = USBDEVFS_URB_TYPE_INTERRUPT;
                 break;
-            default:
-                DPRINTF("usb_host: malformed endpoint type\n");
-                type = USBDEVFS_URB_TYPE_BULK;
             }
             s->endp_table[(devep & 0xf) - 1].type = type;
             s->endp_table[(devep & 0xf) - 1].halted = 0;
Signature · 1 line
1.7.0.1
#1 lore raw
Quoted 11 lines
---
 usb-linux.c |    3 ---
 1 files changed, 0 insertions(+), 3 deletions(-)
usb-linux.c +0 -0
index a9c15c6..23155dd 100644

usb-linux.c static int usb_linux_update_endp_table(USBHostDevice · line 851

Quoted 9 lines
usb-linux.c +0 -3
@@ -846,9 +846,6 @@ static int usb_linux_update_endp_table(USBHostDevice
 *s) case 0x03:
                 type = USBDEVFS_URB_TYPE_INTERRUPT;
                 break;
-            default:
-                DPRINTF("usb_host: malformed endpoint type\n");
-                type = USBDEVFS_URB_TYPE_BULK;
             }
             s->endp_table[(devep & 0xf) - 1].type = type;
             s->endp_table[(devep & 0xf) - 1].halted = 0;

I'd be tempted to replace it by an abort(). If it's provable redundant then the compiler will remove it anyway. If not then we at least get a noisy failure when someone breaks it.

Paul

#2 lore raw
In reply to Paul Bolle
Quoted 2 lines · Paul Bolle

Applied. Thanks.

Regards,

Anthony Liguori

Quoted 19 lines
---
  usb-linux.c |    3 ---
  1 files changed, 0 insertions(+), 3 deletions(-)
usb-linux.c +0 -3
index a9c15c6..23155dd 100644
@@ -846,9 +846,6 @@ static int usb_linux_update_endp_table(USBHostDevice *s)
              case 0x03:
                  type = USBDEVFS_URB_TYPE_INTERRUPT;
                  break;
-            default:
-                DPRINTF("usb_host: malformed endpoint type\n");
-                type = USBDEVFS_URB_TYPE_BULK;
              }
              s->endp_table[(devep&  0xf) - 1].type = type;
              s->endp_table[(devep&  0xf) - 1].halted = 0;
author
#3 lore raw
Quoted 4 lines · Anthony Liguori
Quoted 2 lines · Paul Bolle

Applied. Thanks.

Paul Brook was "tempted to replace it by an abort()" (about one and a half week ago). Did you perhaps miss that message or weren't you tempted to do this?

Regards,

Paul Bolle

#4 lore raw
Quoted 4 lines · Paul Bolle
Quoted 2 lines · Anthony Liguori

On 03/08/2010 06:58 AM, Paul Bolle wrote:

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

Applied. Thanks.

Paul Brook was "tempted to replace it by an abort()" (about one and a half week ago). Did you perhaps miss that message or weren't you tempted to do this?

I missed it, but then again, I don't think the patch was wrong in the first place.

I think we use too many aborts/exits in the device model that can potentially be triggered by guest code.

Regards,

Anthony Liguori

Quoted 6 lines

Regards,

Paul Bolle

#5 lore raw
Quoted 11 lines · Paul Bolle
Quoted 9 lines · Anthony Liguori
Quoted 4 lines · Paul Bolle
Quoted 1 line

Applied. Thanks.

Paul Brook was "tempted to replace it by an abort()" (about one and a half week ago). Did you perhaps miss that message or weren't you tempted to do this?

Quoted 5 lines

I missed it, but then again, I don't think the patch was wrong in the first place.

I think we use too many aborts/exits in the device model that can potentially be triggered by guest code.

If something should never happen (as in this case) then an abort/assert is completely appropriate. Once things get that screwed up there's no right answer, and the best thing we can do is terminate immediately to try and avoid further damage.

If an assert/abort can be triggered by a guest then you obviously have a bug.

Removing the assert is not the correct solution. You should either fix whatever caused the invalid state to occur, or replace it with an appropriate retry, fallback or guest visible failure.

Paul

#6 lore raw
Quoted 17 lines · Paul Brook
Quoted 17 lines · Paul Bolle
Quoted 13 lines · Anthony Liguori
Quoted 7 lines · Paul Bolle
Quoted 3 lines

Applied. Thanks.

Paul Brook was "tempted to replace it by an abort()" (about one and a half week ago). Did you perhaps miss that message or weren't you tempted to do this?

I missed it, but then again, I don't think the patch was wrong in the first place.

Quoted 8 lines
Quoted 3 lines

I think we use too many aborts/exits in the device model that can potentially be triggered by guest code.

If something should never happen (as in this case) then an abort/assert is completely appropriate. Once things get that screwed up there's no right answer, and the best thing we can do is terminate immediately to try and avoid further damage.

This case was:

switch (foo & 0x03) {
case 0: case 1: case 2: case 3:
default:
}

The default is unreachable. Having it there just introduces more code that serves no purpose. Unless someone does something totally foolish and changes the mask in the switch statement, there's no way it will ever be reachable.

Quoted 2 lines

If an assert/abort can be triggered by a guest then you obviously have a bug.

Agreed.

Quoted 4 lines

Removing the assert is not the correct solution. You should either fix whatever caused the invalid state to occur, or replace it with an appropriate retry, fallback or guest visible failure.

Also agreed.

Regards,

Anthony Liguori

Quoted 2 lines

Paul

#7 lore raw
Quoted 12 lines
Quoted 4 lines

If something should never happen (as in this case) then an abort/assert is completely appropriate. Once things get that screwed up there's no right answer, and the best thing we can do is terminate immediately to try and avoid further damage.

This case was:

switch (foo & 0x03) {
case 0: case 1: case 2: case 3:
default:
}

Quoted 4 lines

The default is unreachable. Having it there just introduces more code that serves no purpose. Unless someone does something totally foolish and changes the mask in the switch statement, there's no way it will ever be reachable.

I mistakenly remembered this was using a symbolic mask rather than a literal 0x03. In that case I'd argue it's much easier to make the dumb error you describe, and the assert can be a handy cluebat.

I guess it's largely personal preference - I prefer to add the default case to make it clear that falling through is never gong to be the right answer.

Paul

#8 lore raw
Quoted 18 lines · Paul Brook
Quoted 12 lines
Quoted 4 lines

If something should never happen (as in this case) then an abort/assert is completely appropriate. Once things get that screwed up there's no right answer, and the best thing we can do is terminate immediately to try and avoid further damage.

This case was:

switch (foo & 0x03) {
case 0: case 1: case 2: case 3:
default:
}

Quoted 4 lines

The default is unreachable. Having it there just introduces more code that serves no purpose. Unless someone does something totally foolish and changes the mask in the switch statement, there's no way it will ever be reachable.

Quoted 6 lines

I mistakenly remembered this was using a symbolic mask rather than a literal
0x03. In that case I'd argue it's much easier to make the dumb error you describe, and the assert can be a handy cluebat.

I guess it's largely personal preference - I prefer to add the default case to make it clear that falling through is never gong to be the right answer.

This breaks build (gcc 4.3.2):
CC usb-linux.o
cc1: warnings being treated as errors /src/qemu/usb-linux.c: In function 'usb_linux_update_endp_table':
/src/qemu/usb-linux.c:759: error: 'type' may be used uninitialized in this function

#9 lore raw
Quoted 28 lines · Blue Swirl
Quoted 19 lines · Paul Brook
Quoted 13 lines
Quoted 5 lines

If something should never happen (as in this case) then an abort/assert

is completely appropriate. Once things get that screwed up there's no right answer, and the best thing we can do is terminate immediately to try and avoid further damage.

This case was:

switch (foo& 0x03) {
case 0: case 1: case 2: case 3:
default:
}

Quoted 4 lines

The default is unreachable. Having it there just introduces more code that serves no purpose. Unless someone does something totally foolish and changes the mask in the switch statement, there's no way it will ever be reachable.

Quoted 7 lines

I mistakenly remembered this was using a symbolic mask rather than a literal
0x03. In that case I'd argue it's much easier to make the dumb error you describe, and the assert can be a handy cluebat.

I guess it's largely personal preference - I prefer to add the default case to make it clear that falling through is never gong to be the right answer.

Quoted 7 lines

This breaks build (gcc 4.3.2):
CC usb-linux.o
cc1: warnings being treated as errors /src/qemu/usb-linux.c: In function 'usb_linux_update_endp_table':
/src/qemu/usb-linux.c:759: error: 'type' may be used uninitialized in this function

That's unfortunate. I'll revert.

Regards,

Anthony Liguori

author
#10 lore raw
Quoted 10 lines · Anthony Liguori
Quoted 7 lines · Blue Swirl

This breaks build (gcc 4.3.2):
CC usb-linux.o
cc1: warnings being treated as errors /src/qemu/usb-linux.c: In function 'usb_linux_update_endp_table':
/src/qemu/usb-linux.c:759: error: 'type' may be used uninitialized in this function

That's unfortunate. I'll revert.

I can't reproduce this with gcc-4.4.3-8.fc13.i686 (which I'm currently running). The patch was tested and submitted when I was running gcc-4.4.3-6.fc13.i686.

Regards,

Paul Bolle

#11 lore raw
Quoted 12 lines · Paul Bolle
Quoted 10 lines · Anthony Liguori
Quoted 8 lines · Blue Swirl

This breaks build (gcc 4.3.2):
CC usb-linux.o
cc1: warnings being treated as errors /src/qemu/usb-linux.c: In function 'usb_linux_update_endp_table':
/src/qemu/usb-linux.c:759: error: 'type' may be used uninitialized in this function

Quoted 6 lines
Quoted 2 lines

That's unfortunate. I'll revert.

I can't reproduce this with gcc-4.4.3-8.fc13.i686 (which I'm currently running). The patch was tested and submitted when I was running gcc-4.4.3-6.fc13.i686.

Yeah, it worked for me, but clearly older versions of gcc are less smart about calculating ranges.

Regards,

Anthony Liguori

Quoted 6 lines

Regards,

Paul Bolle

#12 lore raw
Quoted 20 lines · Anthony Liguori
Quoted 18 lines · Paul Bolle
Quoted 15 lines · Anthony Liguori
Quoted 9 lines · Blue Swirl

This breaks build (gcc 4.3.2):
CC usb-linux.o
cc1: warnings being treated as errors /src/qemu/usb-linux.c: In function 'usb_linux_update_endp_table':
/src/qemu/usb-linux.c:759: error: 'type' may be used uninitialized in this function

That's unfortunate. I'll revert.

Quoted 8 lines
Quoted 5 lines

I can't reproduce this with gcc-4.4.3-8.fc13.i686 (which I'm currently running). The patch was tested and submitted when I was running gcc-4.4.3-6.fc13.i686.

Yeah, it worked for me, but clearly older versions of gcc are less smart
about calculating ranges.

Actually OpenBSD gcc (3.4.4) has no problems.