Skip to content

Enable implicit fallthrough diagnostic, note intentional fallthroughs. #3405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 14, 2020

Conversation

jepler
Copy link

@jepler jepler commented Sep 12, 2020

This is cumulative with #3404 since that also fixed a case of an unintentional fallthrough.

It's considered a common mistake to forget 'break' between cases of a case statement. Help the compiler help us find them in the first place.

While checking whether we can enable -Wimplicit-fallthrough, I encountered
a diagnostic in mp_binary_set_val_array_from_int which led to discovering
the following bug:
```
>>> struct.pack("xb", 3)
b'\x03\x03'
```
That is, the next value (3) was used as the value of a padding byte, while
standard Python always fills "x" bytes with zeros.  I initially thought
this had to do with the unintentional fallthrough, but it doesn't.
Instead, this code would relate to an array.array with a typecode of
padding ('x'), which is ALSO not desktop Python compliant:
```
>>> array.array('x', (1, 2, 3))
array('x', [1, 0, 0])
```
Possibly this is dead code that used to be shared between struct-setting
and array-setting, but it no longer is.

I also discovered that the argument list length for struct.pack
and struct.pack_into were not checked, and that the length of binary data
passed to array.array was not checked to be a multiple of the element
size.

I have corrected all of these to conform more closely to standard Python
and revised some tests where necessary.  Some tests for micropython-specific
behavior that does not conform to standard Python and is not present
in CircuitPython was deleted outright.
@jepler jepler marked this pull request as draft September 12, 2020 19:19
I investigated these cases and confirmed that the fallthrough behavior
was intentional.
If any diagnostics occur, we will want to either add `/* FALLTHROUGH */`
or `break;` as appropriate.  I only tested a few builds (trinket m0
and metro m4 express)
@jepler jepler force-pushed the implicit-fallthrough-diagnostic branch from 5b287b7 to 5729097 Compare September 12, 2020 20:11
.. there is an instance of it that looks like a "true positive", but it only
affects sdhc transfers that are not a multiple of 4 bytes, which I don't think
happens. (sd card blocks are always 512 bytes)  I can fix this in our
asf4 repo but that would mean this should be deferred until after adafruit#3384 is
merged, since that also touches asf4 very invasively by adding a whole new
chip family.
@jepler jepler marked this pull request as ready for review September 13, 2020 17:54
Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes so far look fine, but why did you only enable it for atmel-samd, instead of enabling in in py/circuitpy_defns.mk in BASE_CFLAGS?

@jepler
Copy link
Author

jepler commented Sep 13, 2020

For better or for worse, we apply more stringent warning flags to the atmel-samd port than to other ports. If you like, I can move it and we can find out how much more is caught with a wider net.

@dhalbert
Copy link
Collaborator

I think it's a good idea, since it could catch a number of port-specific bugs. The main issue I had with the nRF port and its hal layers is that it does #if on undefined preprocessor macros.

For better or for worse, we apply more stringent warning flags to the atmel-samd port than to other ports.

I looked in atmel-samd/Makefile and didn't see a lot of this. What are you thinking of?

@jepler
Copy link
Author

jepler commented Sep 13, 2020

Sorry for not being more specific, rather I have a general feeling of hitting some "wait, why didn't the compiler tell me" problems when not in the atmel sam port

…diagnostic

Conflict in locale/circuitpython.pot resolved with 'make translate'
@jepler
Copy link
Author

jepler commented Sep 13, 2020

cxd56 and mimxrt10xx don't enable -Werror.

@jepler
Copy link
Author

jepler commented Sep 13, 2020

@dhalbert happy you're here, are these FALLTHROUGHs or missing break;s?

common-hal/_bleio/PacketBuffer.c: In function 'packet_buffer_on_ble_server_evt':
common-hal/_bleio/PacketBuffer.c:175:16: error: this statement may fall through [-Werror=implicit-fallthrough=]
  175 |             if (self->conn_handle == ble_evt->evt.gap_evt.conn_handle) {
      |                ^
common-hal/_bleio/PacketBuffer.c:179:9: note: here
  179 |         case BLE_GATTS_EVT_HVN_TX_COMPLETE: {
      |         ^~~~
common-hal/_bleio/PacketBuffer.c:180:13: error: this statement may fall through [-Werror=implicit-fallthrough=]
  180 |             queue_next_write(self);
      |             ^~~~~~~~~~~~~~~~~~~~~~
common-hal/_bleio/PacketBuffer.c:182:9: note: here
  182 |         default:
      |         ^~~~~~~
        }
        case BLE_GAP_EVT_DISCONNECTED: {
            if (self->conn_handle == ble_evt->evt.gap_evt.conn_handle) {
                self->conn_handle = BLE_CONN_HANDLE_INVALID;
            }
        }
        case BLE_GATTS_EVT_HVN_TX_COMPLETE: {
            queue_next_write(self);
        }
        default:
            return false;
            break;

@dhalbert
Copy link
Collaborator

@dhalbert happy you're here, are these FALLTHROUGHs or missing break;s?

Those are bugs! Both those cases are missing break.

@dhalbert
Copy link
Collaborator

Also you could remove the extraneous curly braces, which should only be there if there's a local declaration in the case arm. Thanks.

@jepler jepler requested a review from dhalbert September 14, 2020 01:41
Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for adding this warning. It was clearly worth it!

@jepler jepler merged commit 814339a into adafruit:main Sep 14, 2020
@jepler jepler deleted the implicit-fallthrough-diagnostic branch November 3, 2021 21:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants