Skip to contentSkip to conversation

HID: uclogic: Add NULL check in uclogic_input_configured

PATCH Superseded Henry Martin 3 participants 5 messages Apr 1, 2025 – Apr 24, 2025 HID

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 HID: uclogic: Add NULL check in uclogic_input_configured

devm_kasprintf() returns NULL when memory allocation fails. Currently, uclogic_input_configured() does not check for this case, which results in a NULL pointer dereference.

Add NULL check after devm_kasprintf() to prevent this issue.

Fixes dd613a4e45f8 ("HID: uclogic: Correct devm device reference for hidinput input_dev name")

---
V1 -> V2:
Simplify the handing of the condition "suffix" with if/else suggested by Markus.
The current implementation (directly returning -ENOMEM) is reasonable because:
1. pen_input is just a cached pointer - no resources are allocated
2. evbit modification is a software-only configuration (hardware-independent)
3. No critical state needs rollback on memory allocation failure

 drivers/hid/hid-uclogic-core.c | 28 +++++++++++-----------------
 1 file changed, 11 insertions(+), 17 deletions(-)
drivers/hid/hid-uclogic-core.c +11 -17
index d8008933c052..83625ec6a55a 100644
@@ -118,35 +118,29 @@ static int uclogic_input_configured(struct hid_device *hdev,
 		}
 	}
 
-	if (!suffix) {
+	if (!suffix && hi->report->maxfield > 0) {
 		field = hi->report->field[0];
 
-		switch (field->application) {
-		case HID_GD_KEYBOARD:
+		if (field->application == HID_GD_KEYBOARD)
 			suffix = "Keyboard";
-			break;
-		case HID_GD_MOUSE:
+		else if (field->application == HID_GD_MOUSE)
 			suffix = "Mouse";
-			break;
-		case HID_GD_KEYPAD:
+		else if (field->application == HID_GD_KEYPAD)
 			suffix = "Pad";
-			break;
-		case HID_DG_PEN:
-		case HID_DG_DIGITIZER:
+		else if (field->application == HID_DG_PEN || field->application == HID_DG_DIGITIZER)
 			suffix = "Pen";
-			break;
-		case HID_CP_CONSUMER_CONTROL:
+		else if (field->application == HID_CP_CONSUMER_CONTROL)
 			suffix = "Consumer Control";
-			break;
-		case HID_GD_SYSTEM_CONTROL:
+		else if (field->application == HID_GD_SYSTEM_CONTROL)
 			suffix = "System Control";
-			break;
-		}
 	}
 
-	if (suffix)
+	if (suffix) {
 		hi->input->name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
 						 "%s %s", hdev->name, suffix);
+		if (!hi->input->name)
+			return -ENOMEM;
+	}
 
 	return 0;
 }
Signature · 1 line
2.34.1
#1 lore raw
Quoted 1 line

devm_kasprintf() return NULL if memory allocation fails. Currently,

…
call? failed?

Quoted 1 line

Add NULL check after devm_kasprintf() to prevent this issue.

Do you propose to improve this function implementation a bit more?

…

Quoted 3 lines

---
V1 -> V2:
Simplify the handing of the condition "suffix" with if/else suggested by …

* I imagine that there is a need to offer such adjustments in separate update steps.
https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.14#n81

* Can another tag become more helpful?
https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.14#n598

Quoted 1 line

The current implementation (directly returning -ENOMEM) is reasonable because:

…

How much will such information influence the refinement of the change description?

How do you think about to append parentheses to a function name also in the summary phrase?

Regards,
Markus

#2 lore raw

PATCH v3 1/1 HID: uclogic: Add NULL check in uclogic_input_configured()

devm_kasprintf() returns NULL when memory allocation fails. Currently, uclogic_input_configured() does not check for this case, which results in a NULL pointer dereference.

Add NULL check after devm_kasprintf() to prevent this issue.

Fixes dd613a4e45f8 ("HID: uclogic: Correct devm device reference for hidinput input_dev name")

---
V2 -> V3: Kept the original switch structure, only optimized the outer conditional checks while maintaining safety requirements. V1 -> V2: Simplify the handing of the condition "suffix" with if/else.

 drivers/hid/hid-uclogic-core.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/hid/hid-uclogic-core.c +4 -3
index d8008933c052..321c43fb06ae 100644
@@ -142,11 +142,12 @@ static int uclogic_input_configured(struct hid_device *hdev,
 			suffix = "System Control";
 			break;
 		}
-	}
-
-	if (suffix)
+	} else {
 		hi->input->name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
 						 "%s %s", hdev->name, suffix);
+		if (!hi->input->name)
+			return -ENOMEM;
+	}
 
 	return 0;
 }
Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH v3] HID: uclogic: Add NULL check in uclogic_input_configured()

Quoted 1 line

devm_kasprintf() return NULL if memory allocation fails. Currently,

…
call? failed?

Quoted 1 line

Add NULL check after devm_kasprintf() to prevent this issue.

Do you propose to improve this function implementation a bit more?

…

Quoted 4 lines

---
V2 -> V3: Kept the original switch structure, only optimized the outer conditional checks while maintaining safety requirements. V1 -> V2: Simplify the handing of the condition "suffix" with if/else.

* Under which circumstances will you begin to care more to offer such adjustments in separate update steps?
https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.14#n81

* Can another tag become more helpful?
https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.14#n598

Can an other control flow variant be a bit nicer finally?

	if (suffix) {
…
	} else {
…
	}

Regards,
Markus

maintainer Applied
#4 lore raw

Re: [PATCH v3] HID: uclogic: Add NULL check in uclogic_input_configured()

In reply to Henry Martin
Quoted 8 lines · Henry Martin

devm_kasprintf() returns NULL when memory allocation fails. Currently, uclogic_input_configured() does not check for this case, which results in a NULL pointer dereference.

Add NULL check after devm_kasprintf() to prevent this issue.

Fixes dd613a4e45f8 ("HID: uclogic: Correct devm device reference for hidinput input_dev name")

Applied, thanks.

Signature · 2 lines
Jiri Kosina
SUSE Labs