Skip to content

Commit 1827170

Browse files
committed
usb: gadget: f_midi: Annotate struct f_midi with __counted_by
Prepare for the coming implementation by GCC and Clang of the __counted_by attribute. Flexible array members annotated with __counted_by can have their accesses bounds-checked at run-time checking via CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for strcpy/memcpy-family functions). As found with Coccinelle[1], add __counted_by for struct f_midi. Additionally, since the element count member must be set before accessing the annotated flexible array member, move its initialization earlier. [1] https://github.com/kees/kernel-tools/blob/trunk/coccinelle/examples/counted_by.cocci Cc: Greg Kroah-Hartman <[email protected]> Cc: John Keeping <[email protected]> Cc: Peter Chen <[email protected]> Cc: Hulk Robot <[email protected]> Cc: Allen Pais <[email protected]> Cc: Will McVicker <[email protected]> Cc: Davidlohr Bueso <[email protected]> Cc: Zhang Qilong <[email protected]> Cc: [email protected] Reviewed-by: "Gustavo A. R. Silva" <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent c7c4ac7 commit 1827170

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

drivers/usb/gadget/function/f_midi.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ struct f_midi {
9999
unsigned int in_last_port;
100100
unsigned char free_ref;
101101

102-
struct gmidi_in_port in_ports_array[/* in_ports */];
102+
struct gmidi_in_port in_ports_array[] __counted_by(in_ports);
103103
};
104104

105105
static inline struct f_midi *func_to_midi(struct usb_function *f)
@@ -1349,6 +1349,7 @@ static struct usb_function *f_midi_alloc(struct usb_function_instance *fi)
13491349
status = -ENOMEM;
13501350
goto setup_fail;
13511351
}
1352+
midi->in_ports = opts->in_ports;
13521353

13531354
for (i = 0; i < opts->in_ports; i++)
13541355
midi->in_ports_array[i].cable = i;
@@ -1359,7 +1360,6 @@ static struct usb_function *f_midi_alloc(struct usb_function_instance *fi)
13591360
status = -ENOMEM;
13601361
goto midi_free;
13611362
}
1362-
midi->in_ports = opts->in_ports;
13631363
midi->out_ports = opts->out_ports;
13641364
midi->index = opts->index;
13651365
midi->buflen = opts->buflen;

0 commit comments

Comments
 (0)