Skip to contentSkip to conversation

USB: dummy-hcd: Fix uninitialized array use in init()

PATCH In review Bui Quang Minh 2 participants 6 messages Dec 4, 2020 – Dec 6, 2020 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 USB: dummy-hcd: Fix uninitialized array use in init()

This error path

	err_add_pdata:
		for (i = 0; i < mod_data.num; i++)
			kfree(dum[i]);

can be triggered when not all dum's elements are initialized.

Fix this by initializing all dum's elements to NULL.

---
 drivers/usb/gadget/udc/dummy_hcd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/gadget/udc/dummy_hcd.c +1 -1
index 0eeaead..a2cf009 100644
@@ -2734,7 +2734,7 @@ static int __init init(void)
 {
 	int	retval = -ENOMEM;
 	int	i;
-	struct	dummy *dum[MAX_NUM_UDC];
+	struct	dummy *dum[MAX_NUM_UDC] = {};
 
 	if (usb_disabled())
 		return -ENODEV;
Signature · 1 line
2.7.4
Acked-by on v1 1/1
#1 lore raw
Quoted 20 lines · Bui Quang Minh

This error path

	err_add_pdata:
		for (i = 0; i < mod_data.num; i++)
			kfree(dum[i]);

can be triggered when not all dum's elements are initialized.

Fix this by initializing all dum's elements to NULL.

---
 drivers/usb/gadget/udc/dummy_hcd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/gadget/udc/dummy_hcd.c +0 -0
index 0eeaead..a2cf009 100644

drivers/usb/gadget/udc/dummy_hcd.c static int __init init(void) · line 2740

Quoted 8 lines
drivers/usb/gadget/udc/dummy_hcd.c +1 -1
@@ -2734,7 +2734,7 @@ static int __init init(void)
 {
 	int	retval = -ENOMEM;
 	int	i;
-	struct	dummy *dum[MAX_NUM_UDC];
+	struct	dummy *dum[MAX_NUM_UDC] = {};
 
 	if (usb_disabled())
 		return -ENODEV;

Does this initialization end up using less memory than an explicit memset() call?

Alan Stern

#2 lore raw

Vào Th 6, 4 thg 12, 2020 vào lúc 23:12 Alan Stern <[email protected]> đã viết:

Quoted 2 lines

Does this initialization end up using less memory than an explicit memset() call?

You mean speed? In my opinion, there is no difference in speed between 2 ways. When I compile this array initialization using gcc 5.4.0, this initialization becomes
mov instructions when MAX_NUM_UDC=2 and becomes rep stos when MAX_NUM_UDC=32. I think it makes no difference when comparing with memset()

Thanks,
Quang Minh

#3 lore raw
Quoted 6 lines · Minh Bùi Quang

Vào Th 6, 4 thg 12, 2020 vào lúc 23:12 Alan Stern <[email protected]> đã viết:

Quoted 2 lines

Does this initialization end up using less memory than an explicit memset() call?

You mean speed?

No, I mean memory space.

A memset call requires a certain amount of instruction space (to push the arguments and make the call) but no static data space. Initialization requires some instruction space (to copy the data) and static data space as well (to hold the data that is to be copied).

Alan Stern

Quoted 8 lines

In my opinion, there is no difference in speed between 2 ways. When I compile this array initialization using gcc 5.4.0, this initialization becomes
mov instructions when MAX_NUM_UDC=2 and becomes rep stos when MAX_NUM_UDC=32. I think it makes no difference when comparing with memset()

Thanks,
Quang Minh

#4 lore raw
Quoted 8 lines · Alan Stern
Quoted 6 lines · Minh Bùi Quang

Vào Th 6, 4 thg 12, 2020 vào lúc 23:12 Alan Stern <[email protected]> đã viết:

Quoted 2 lines

Does this initialization end up using less memory than an explicit memset() call?

You mean speed?

Quoted 9 lines

No, I mean memory space.

A memset call requires a certain amount of instruction space (to push the arguments and make the call) but no static data space. Initialization requires some instruction space (to copy the data) and static data space as well (to hold the data that is to be copied).

Alan Stern

Thank you for your clarification, I didn't think about it before.

As I check when compiling the code, with MAX_NUM_UDC=32 the initialization becomes

xor eax,eax
mov ecx,0x40
rep stos DWORD PTR es:[rdi],eax

With MAX_NUM_UDC=2, the initialization becomes

        mov    QWORD PTR [rbp-0x30],0x0
        mov    QWORD PTR [rbp-0x28],0x0

As I see, initialization does not require additional static data space. Am I right?

Thanks,
Quang Minh

#5 lore raw
Quoted 28 lines · Bui Quang Minh
Quoted 8 lines · Alan Stern
Quoted 6 lines · Minh Bùi Quang

Vào Th 6, 4 thg 12, 2020 vào lúc 23:12 Alan Stern <[email protected]> đã viết:

Quoted 2 lines

Does this initialization end up using less memory than an explicit memset() call?

You mean speed?

Quoted 9 lines

No, I mean memory space.

A memset call requires a certain amount of instruction space (to push the arguments and make the call) but no static data space. Initialization requires some instruction space (to copy the data) and static data space as well (to hold the data that is to be copied).

Alan Stern

Thank you for your clarification, I didn't think about it before.

As I check when compiling the code, with MAX_NUM_UDC=32 the initialization becomes

xor eax,eax
mov ecx,0x40
rep stos DWORD PTR es:[rdi],eax

Quoted 7 lines

With MAX_NUM_UDC=2, the initialization becomes

        mov    QWORD PTR [rbp-0x30],0x0
        mov    QWORD PTR [rbp-0x28],0x0

As I see, initialization does not require additional static data space. Am I right?

Ah, okay, good. I didn't realize the compiler was smart enough to do this. Thanks for checking.

Alan Stern