Skip to content

Commit 814339a

Browse files
authored
Merge pull request #3405 from jepler/implicit-fallthrough-diagnostic
Enable implicit fallthrough diagnostic, note intentional fallthroughs.
2 parents 9409a2c + b3bdd46 commit 814339a

File tree

15 files changed

+68
-74
lines changed

15 files changed

+68
-74
lines changed

devices/ble_hci/common-hal/_bleio/att.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,6 +1748,7 @@ void check_att_err(uint8_t err) {
17481748
break;
17491749
case BT_ATT_ERR_ENCRYPTION_KEY_SIZE:
17501750
msg = translate("Encryption key size");
1751+
break;
17511752
case BT_ATT_ERR_INVALID_ATTRIBUTE_LEN:
17521753
msg = translate("Invalid attribute length");
17531754
break;

extmod/re1.5/recursiveloop.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ recursiveloop(char *pc, const char *sp, Subject *input, const char **subp, int n
2222
case Char:
2323
if(*sp != *pc++)
2424
return 0;
25+
/* FALLTHROUGH */
2526
case Any:
2627
sp++;
2728
continue;

locale/circuitpython.pot

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2020-09-13 22:53+0530\n"
11+
"POT-Creation-Date: 2020-09-13 14:21-0500\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -1923,7 +1923,7 @@ msgstr ""
19231923
msgid "bad format string"
19241924
msgstr ""
19251925

1926-
#: py/binary.c
1926+
#: py/binary.c py/objarray.c
19271927
msgid "bad typecode"
19281928
msgstr ""
19291929

@@ -1976,6 +1976,10 @@ msgstr ""
19761976
msgid "bytes > 8 bits not supported"
19771977
msgstr ""
19781978

1979+
#: py/objarray.c
1980+
msgid "bytes length not a multiple of item size"
1981+
msgstr ""
1982+
19791983
#: py/objstr.c
19801984
msgid "bytes value out of range"
19811985
msgstr ""
@@ -2941,6 +2945,11 @@ msgstr ""
29412945
msgid "overflow converting long int to machine word"
29422946
msgstr ""
29432947

2948+
#: py/modstruct.c
2949+
#, c-format
2950+
msgid "pack expected %d items for packing (got %d)"
2951+
msgstr ""
2952+
29442953
#: shared-bindings/_stage/Layer.c shared-bindings/_stage/Text.c
29452954
msgid "palette must be 32 bytes long"
29462955
msgstr ""

ports/atmel-samd/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ SRC_ASF += \
255255
hal/src/hal_mci_sync.c \
256256
hpl/sdhc/hpl_sdhc.c \
257257

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

261261
$(BUILD)/asf4/$(CHIP_FAMILY)/hpl/sercom/hpl_sercom.o: CFLAGS += -Wno-maybe-uninitialized

ports/nrf/common-hal/_bleio/PacketBuffer.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,12 @@ STATIC bool packet_buffer_on_ble_client_evt(ble_evt_t *ble_evt, void *param) {
129129
}
130130
break;
131131
}
132-
case BLE_GATTC_EVT_WRITE_CMD_TX_COMPLETE: {
132+
case BLE_GATTC_EVT_WRITE_CMD_TX_COMPLETE:
133133
queue_next_write(self);
134134
break;
135-
}
136-
case BLE_GATTC_EVT_WRITE_RSP: {
135+
case BLE_GATTC_EVT_WRITE_RSP:
137136
queue_next_write(self);
138137
break;
139-
}
140138
default:
141139
return false;
142140
break;
@@ -171,14 +169,14 @@ STATIC bool packet_buffer_on_ble_server_evt(ble_evt_t *ble_evt, void *param) {
171169
}
172170
break;
173171
}
174-
case BLE_GAP_EVT_DISCONNECTED: {
172+
case BLE_GAP_EVT_DISCONNECTED:
175173
if (self->conn_handle == ble_evt->evt.gap_evt.conn_handle) {
176174
self->conn_handle = BLE_CONN_HANDLE_INVALID;
177175
}
178-
}
179-
case BLE_GATTS_EVT_HVN_TX_COMPLETE: {
176+
break;
177+
case BLE_GATTS_EVT_HVN_TX_COMPLETE:
180178
queue_next_write(self);
181-
}
179+
break;
182180
default:
183181
return false;
184182
break;

py/binary.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index) {
126126
break;
127127
case BYTEARRAY_TYPECODE:
128128
case 'B':
129-
case 'x': // value will be discarded
130129
val = ((unsigned char*)p)[index];
131130
break;
132131
case 'h':
@@ -330,7 +329,11 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **
330329
}
331330
}
332331

333-
mp_binary_set_int(MIN((size_t)size, sizeof(val)), struct_type == '>', p, val);
332+
if (val_type == 'x') {
333+
memset(p, 0, 1);
334+
} else {
335+
mp_binary_set_int(MIN((size_t)size, sizeof(val)), struct_type == '>', p, val);
336+
}
334337
}
335338

336339
void mp_binary_set_val_array(char typecode, void *p, mp_uint_t index, mp_obj_t val_in) {
@@ -379,8 +382,6 @@ void mp_binary_set_val_array_from_int(char typecode, void *p, mp_uint_t index, m
379382
case 'B':
380383
((unsigned char*)p)[index] = val;
381384
break;
382-
case 'x':
383-
((unsigned char*)p)[index] = 0;
384385
case 'h':
385386
((short*)p)[index] = val;
386387
break;

py/circuitpy_defns.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ BASE_CFLAGS = \
3131
-fsingle-precision-constant \
3232
-fno-strict-aliasing \
3333
-Wdouble-promotion \
34+
-Wimplicit-fallthrough=2 \
3435
-Wno-endif-labels \
3536
-Wstrict-prototypes \
3637
-Werror-implicit-function-declaration \

py/modstruct.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,21 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(struct_unpack_from_obj, 2, 3, struct_unpack_
183183

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

189198
size_t i;
190199
for (i = 0; i < n_args;) {
191200
mp_uint_t cnt = 1;
192-
if (*fmt == '\0') {
193-
// more arguments given than used by format string; CPython raises struct.error here
194-
break;
195-
}
196201
if (unichar_isdigit(*fmt)) {
197202
cnt = get_fmt_num(&fmt);
198203
}
@@ -208,8 +213,7 @@ STATIC void struct_pack_into_internal(mp_obj_t fmt_in, byte *p, size_t n_args, c
208213
memset(p + to_copy, 0, cnt - to_copy);
209214
p += cnt;
210215
} else {
211-
// If we run out of args then we just finish; CPython would raise struct.error
212-
while (cnt-- && i < n_args) {
216+
while (cnt--) {
213217
mp_binary_set_val(fmt_type, *fmt, args[i], &p);
214218
// Pad bytes don't have a corresponding argument.
215219
if (*fmt != 'x') {
@@ -222,7 +226,6 @@ STATIC void struct_pack_into_internal(mp_obj_t fmt_in, byte *p, size_t n_args, c
222226
}
223227

224228
STATIC mp_obj_t struct_pack(size_t n_args, const mp_obj_t *args) {
225-
// TODO: "The arguments must match the values required by the format exactly."
226229
mp_int_t size = MP_OBJ_SMALL_INT_VALUE(struct_calcsize(args[0]));
227230
vstr_t vstr;
228231
vstr_init_len(&vstr, size);

py/objarray.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ STATIC void array_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t
9797

9898
#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
9999
STATIC mp_obj_array_t *array_new(char typecode, size_t n) {
100+
if (typecode == 'x') {
101+
mp_raise_ValueError(translate("bad typecode"));
102+
}
100103
int typecode_size = mp_binary_get_size('@', typecode, NULL);
101104
mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
102105
#if MICROPY_PY_BUILTINS_BYTEARRAY && MICROPY_PY_ARRAY
@@ -126,8 +129,10 @@ STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
126129
|| (MICROPY_PY_BUILTINS_BYTEARRAY && MP_OBJ_IS_TYPE(initializer, &mp_type_bytearray)))))
127130
&& mp_get_buffer(initializer, &bufinfo, MP_BUFFER_READ)) {
128131
// construct array from raw bytes
129-
// we round-down the len to make it a multiple of sz (CPython raises error)
130132
size_t sz = mp_binary_get_size('@', typecode, NULL);
133+
if (bufinfo.len % sz) {
134+
mp_raise_ValueError(translate("bytes length not a multiple of item size"));
135+
}
131136
size_t len = bufinfo.len / sz;
132137
mp_obj_array_t *o = array_new(typecode, len);
133138
memcpy(o->items, bufinfo.buf, len * sz);

py/objset.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ STATIC mp_obj_t set_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
450450
return MP_OBJ_NEW_SMALL_INT(hash);
451451
}
452452
#endif
453+
/* FALLTHROUGH */
453454
default: return MP_OBJ_NULL; // op not supported
454455
}
455456
}

tests/basics/struct1.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,28 @@
3939
# network byte order
4040
print(struct.pack('!i', 123))
4141

42+
# too short / too long arguments
43+
buf = bytearray(b'>>>123<<<')
44+
try:
45+
struct.pack_into('bb', buf, 0, 3)
46+
except:
47+
print('struct.error')
48+
49+
try:
50+
struct.pack_into('bb', buf, 0, 3, 1, 4)
51+
except:
52+
print('struct.error')
53+
54+
try:
55+
struct.pack('bb', 3)
56+
except:
57+
print('struct.error')
58+
59+
try:
60+
struct.pack('bb', 3, 1, 4)
61+
except:
62+
print('struct.error')
63+
4264
# check that we get an error if the buffer is too small
4365
try:
4466
struct.unpack('I', b'\x00\x00\x00')
@@ -96,3 +118,6 @@
96118
print(struct.unpack_from('<b', buf, -11))
97119
except:
98120
print('struct.error')
121+
122+
# check padding bytes
123+
print(struct.pack("xb", 3))

tests/basics/struct_micropython.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

tests/basics/struct_micropython.py.exp

Lines changed: 0 additions & 2 deletions
This file was deleted.

tests/misc/non_compliant.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,6 @@
105105
except NotImplementedError:
106106
print('NotImplementedError')
107107

108-
# struct pack with too many args, not checked by uPy
109-
print(struct.pack('bb', 1, 2, 3))
110-
111-
# struct pack with too few args, not checked by uPy
112-
print(struct.pack('bb', 1))
113-
114108
# array slice assignment with unsupported RHS
115109
try:
116110
bytearray(4)[0:1] = [1, 2]

tests/misc/non_compliant.py.exp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ NotImplementedError
1414
NotImplementedError
1515
NotImplementedError
1616
NotImplementedError
17-
b'\x01\x02'
18-
b'\x01\x00'
1917
NotImplementedError
2018
AttributeError
2119
TypeError

0 commit comments

Comments
 (0)