Skip to contentSkip to conversation

HID: hiddev: return -ENOMEM when kmalloc failed

PATCH In review Yang Li 2 participants 2 messages Apr 26, 2021

Subsystems (3)
lore
Share & tools
lore

Yang Li

author
#0 lore raw

PATCH 1/1 HID: hiddev: return -ENOMEM when kmalloc failed

The driver is using -1 instead of the -ENOMEM defined macro to specify that a buffer allocation failed. Using the correct error code is more intuitive.

Smatch tool warning:
drivers/hid/usbhid/hiddev.c:894 hiddev_connect() warn: returning -1 instead of -ENOMEM is sloppy

No functional change, just more standardized.

---
 drivers/hid/usbhid/hiddev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hid/usbhid/hiddev.c +1 -1
index 45e0b1c..88020f3 100644
@@ -891,7 +891,7 @@ int hiddev_connect(struct hid_device *hid, unsigned int force)
 	}
 
 	if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL)))
-		return -1;
+		return -ENOMEM;
 
 	init_waitqueue_head(&hiddev->wait);
 	INIT_LIST_HEAD(&hiddev->list);
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 21 lines · Yang Li

The driver is using -1 instead of the -ENOMEM defined macro to specify that a buffer allocation failed. Using the correct error code is more intuitive.

Smatch tool warning:
drivers/hid/usbhid/hiddev.c:894 hiddev_connect() warn: returning -1 instead of -ENOMEM is sloppy

No functional change, just more standardized.

---
 drivers/hid/usbhid/hiddev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hid/usbhid/hiddev.c +0 -0
index 45e0b1c..88020f3 100644

drivers/hid/usbhid/hiddev.c int hiddev_connect(struct hid_device *hid, unsigned int force) · line 894

Quoted 5 lines
drivers/hid/usbhid/hiddev.c +1 -1
@@ -891,7 +891,7 @@ int hiddev_connect(struct hid_device *hid, unsigned int force)
 	}
 
 	if (!(hiddev = kzalloc(sizeof(struct hiddev), GFP_KERNEL)))
-		return -1;
+		return -ENOMEM;

Please try to understand the code that you're changing based on feedback from some tool.

All other error paths here return -1 and the return value of this function is only compared to zero.

How is changing only one of these paths an improvement in any way?

drivers/hid/usbhid/hiddev.c int hiddev_connect(struct hid_device *hid, unsigned int force) · line 897

Quoted 3 lines
drivers/hid/usbhid/hiddev.c +0 -0
 	init_waitqueue_head(&hiddev->wait);
 	INIT_LIST_HEAD(&hiddev->list);

Johan