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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions devices/ble_hci/common-hal/_bleio/att.c
Original file line number Diff line number Diff line change
Expand Up @@ -1748,6 +1748,7 @@ void check_att_err(uint8_t err) {
break;
case BT_ATT_ERR_ENCRYPTION_KEY_SIZE:
msg = translate("Encryption key size");
break;
case BT_ATT_ERR_INVALID_ATTRIBUTE_LEN:
msg = translate("Invalid attribute length");
break;
Expand Down
1 change: 1 addition & 0 deletions extmod/re1.5/recursiveloop.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ recursiveloop(char *pc, const char *sp, Subject *input, const char **subp, int n
case Char:
if(*sp != *pc++)
return 0;
/* FALLTHROUGH */
case Any:
sp++;
continue;
Expand Down
13 changes: 11 additions & 2 deletions locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-09-13 22:53+0530\n"
"POT-Creation-Date: 2020-09-13 14:21-0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -1923,7 +1923,7 @@ msgstr ""
msgid "bad format string"
msgstr ""

#: py/binary.c
#: py/binary.c py/objarray.c
msgid "bad typecode"
msgstr ""

Expand Down Expand Up @@ -1976,6 +1976,10 @@ msgstr ""
msgid "bytes > 8 bits not supported"
msgstr ""

#: py/objarray.c
msgid "bytes length not a multiple of item size"
msgstr ""

#: py/objstr.c
msgid "bytes value out of range"
msgstr ""
Expand Down Expand Up @@ -2941,6 +2945,11 @@ msgstr ""
msgid "overflow converting long int to machine word"
msgstr ""

#: py/modstruct.c
#, c-format
msgid "pack expected %d items for packing (got %d)"
msgstr ""

#: shared-bindings/_stage/Layer.c shared-bindings/_stage/Text.c
msgid "palette must be 32 bytes long"
msgstr ""
Expand Down
2 changes: 1 addition & 1 deletion ports/atmel-samd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ SRC_ASF += \
hal/src/hal_mci_sync.c \
hpl/sdhc/hpl_sdhc.c \

$(BUILD)/asf4/$(CHIP_FAMILY)/hpl/sdhc/hpl_sdhc.o: CFLAGS += -Wno-cast-align
$(BUILD)/asf4/$(CHIP_FAMILY)/hpl/sdhc/hpl_sdhc.o: CFLAGS += -Wno-cast-align -Wno-implicit-fallthrough
endif

$(BUILD)/asf4/$(CHIP_FAMILY)/hpl/sercom/hpl_sercom.o: CFLAGS += -Wno-maybe-uninitialized
Expand Down
14 changes: 6 additions & 8 deletions ports/nrf/common-hal/_bleio/PacketBuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,12 @@ STATIC bool packet_buffer_on_ble_client_evt(ble_evt_t *ble_evt, void *param) {
}
break;
}
case BLE_GATTC_EVT_WRITE_CMD_TX_COMPLETE: {
case BLE_GATTC_EVT_WRITE_CMD_TX_COMPLETE:
queue_next_write(self);
break;
}
case BLE_GATTC_EVT_WRITE_RSP: {
case BLE_GATTC_EVT_WRITE_RSP:
queue_next_write(self);
break;
}
default:
return false;
break;
Expand Down Expand Up @@ -171,14 +169,14 @@ STATIC bool packet_buffer_on_ble_server_evt(ble_evt_t *ble_evt, void *param) {
}
break;
}
case BLE_GAP_EVT_DISCONNECTED: {
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: {
break;
case BLE_GATTS_EVT_HVN_TX_COMPLETE:
queue_next_write(self);
}
break;
default:
return false;
break;
Expand Down
9 changes: 5 additions & 4 deletions py/binary.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index) {
break;
case BYTEARRAY_TYPECODE:
case 'B':
case 'x': // value will be discarded
val = ((unsigned char*)p)[index];
break;
case 'h':
Expand Down Expand Up @@ -330,7 +329,11 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **
}
}

mp_binary_set_int(MIN((size_t)size, sizeof(val)), struct_type == '>', p, val);
if (val_type == 'x') {
memset(p, 0, 1);
} else {
mp_binary_set_int(MIN((size_t)size, sizeof(val)), struct_type == '>', p, val);
}
}

void mp_binary_set_val_array(char typecode, void *p, mp_uint_t index, mp_obj_t val_in) {
Expand Down Expand Up @@ -379,8 +382,6 @@ void mp_binary_set_val_array_from_int(char typecode, void *p, mp_uint_t index, m
case 'B':
((unsigned char*)p)[index] = val;
break;
case 'x':
((unsigned char*)p)[index] = 0;
case 'h':
((short*)p)[index] = val;
break;
Expand Down
1 change: 1 addition & 0 deletions py/circuitpy_defns.mk
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ BASE_CFLAGS = \
-fsingle-precision-constant \
-fno-strict-aliasing \
-Wdouble-promotion \
-Wimplicit-fallthrough=2 \
-Wno-endif-labels \
-Wstrict-prototypes \
-Werror-implicit-function-declaration \
Expand Down
17 changes: 10 additions & 7 deletions py/modstruct.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,21 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(struct_unpack_from_obj, 2, 3, struct_unpack_

// This function assumes there is enough room in p to store all the values
STATIC void struct_pack_into_internal(mp_obj_t fmt_in, byte *p, size_t n_args, const mp_obj_t *args) {
size_t size;
size_t count = calc_size_items(mp_obj_str_get_str(fmt_in), &size);
if (count != n_args) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_ValueError(NULL);
#else
mp_raise_ValueError_varg(translate("pack expected %d items for packing (got %d)"), count, n_args);
#endif
}
const char *fmt = mp_obj_str_get_str(fmt_in);
char fmt_type = get_fmt_type(&fmt);

size_t i;
for (i = 0; i < n_args;) {
mp_uint_t cnt = 1;
if (*fmt == '\0') {
// more arguments given than used by format string; CPython raises struct.error here
break;
}
if (unichar_isdigit(*fmt)) {
cnt = get_fmt_num(&fmt);
}
Expand All @@ -208,8 +213,7 @@ STATIC void struct_pack_into_internal(mp_obj_t fmt_in, byte *p, size_t n_args, c
memset(p + to_copy, 0, cnt - to_copy);
p += cnt;
} else {
// If we run out of args then we just finish; CPython would raise struct.error
while (cnt-- && i < n_args) {
while (cnt--) {
mp_binary_set_val(fmt_type, *fmt, args[i], &p);
// Pad bytes don't have a corresponding argument.
if (*fmt != 'x') {
Expand All @@ -222,7 +226,6 @@ STATIC void struct_pack_into_internal(mp_obj_t fmt_in, byte *p, size_t n_args, c
}

STATIC mp_obj_t struct_pack(size_t n_args, const mp_obj_t *args) {
// TODO: "The arguments must match the values required by the format exactly."
mp_int_t size = MP_OBJ_SMALL_INT_VALUE(struct_calcsize(args[0]));
vstr_t vstr;
vstr_init_len(&vstr, size);
Expand Down
7 changes: 6 additions & 1 deletion py/objarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ STATIC void array_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t

#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
STATIC mp_obj_array_t *array_new(char typecode, size_t n) {
if (typecode == 'x') {
mp_raise_ValueError(translate("bad typecode"));
}
int typecode_size = mp_binary_get_size('@', typecode, NULL);
mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
#if MICROPY_PY_BUILTINS_BYTEARRAY && MICROPY_PY_ARRAY
Expand Down Expand Up @@ -126,8 +129,10 @@ STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
|| (MICROPY_PY_BUILTINS_BYTEARRAY && MP_OBJ_IS_TYPE(initializer, &mp_type_bytearray)))))
&& mp_get_buffer(initializer, &bufinfo, MP_BUFFER_READ)) {
// construct array from raw bytes
// we round-down the len to make it a multiple of sz (CPython raises error)
size_t sz = mp_binary_get_size('@', typecode, NULL);
if (bufinfo.len % sz) {
mp_raise_ValueError(translate("bytes length not a multiple of item size"));
}
size_t len = bufinfo.len / sz;
mp_obj_array_t *o = array_new(typecode, len);
memcpy(o->items, bufinfo.buf, len * sz);
Expand Down
1 change: 1 addition & 0 deletions py/objset.c
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ STATIC mp_obj_t set_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
return MP_OBJ_NEW_SMALL_INT(hash);
}
#endif
/* FALLTHROUGH */
default: return MP_OBJ_NULL; // op not supported
}
}
Expand Down
25 changes: 25 additions & 0 deletions tests/basics/struct1.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@
# network byte order
print(struct.pack('!i', 123))

# too short / too long arguments
buf = bytearray(b'>>>123<<<')
try:
struct.pack_into('bb', buf, 0, 3)
except:
print('struct.error')

try:
struct.pack_into('bb', buf, 0, 3, 1, 4)
except:
print('struct.error')

try:
struct.pack('bb', 3)
except:
print('struct.error')

try:
struct.pack('bb', 3, 1, 4)
except:
print('struct.error')

# check that we get an error if the buffer is too small
try:
struct.unpack('I', b'\x00\x00\x00')
Expand Down Expand Up @@ -96,3 +118,6 @@
print(struct.unpack_from('<b', buf, -11))
except:
print('struct.error')

# check padding bytes
print(struct.pack("xb", 3))
41 changes: 0 additions & 41 deletions tests/basics/struct_micropython.py

This file was deleted.

2 changes: 0 additions & 2 deletions tests/basics/struct_micropython.py.exp

This file was deleted.

6 changes: 0 additions & 6 deletions tests/misc/non_compliant.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,6 @@
except NotImplementedError:
print('NotImplementedError')

# struct pack with too many args, not checked by uPy
print(struct.pack('bb', 1, 2, 3))

# struct pack with too few args, not checked by uPy
print(struct.pack('bb', 1))

# array slice assignment with unsupported RHS
try:
bytearray(4)[0:1] = [1, 2]
Expand Down
2 changes: 0 additions & 2 deletions tests/misc/non_compliant.py.exp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ NotImplementedError
NotImplementedError
NotImplementedError
NotImplementedError
b'\x01\x02'
b'\x01\x00'
NotImplementedError
AttributeError
TypeError
Expand Down