Skip to contentSkip to conversation

linux/ofpath: Add missing strdup failure checks

PATCH Avnish Chouhan 2 participants 2 messages Nov 19, 2025 1 message not in the archive

lore
Share & tools
lore

Avnish Chouhan

author Reviewed-by
#1 lore raw
In reply to a message not in the archive
Quoted 9 lines · On 2025-11-19 17:41, [email protected] wrote:

Message: 1
Date: Wed, 19 Nov 2025 15:30:47 +0530
From: Sudhakar Kuppusamy <[email protected]>
To: [email protected]
Cc: Sudhakar Kuppusamy <[email protected]>, [email protected],
[email protected], [email protected]
Subject: [PATCH v1] linux/ofpath: Add missing strdup failure checks
Message-ID: <[email protected]>

Quoted 7 lines

Segmentation faults or undefined behaviour may result from a null pointer dereference in strip_trailing_digits and grub_util_devname_to_ofpath if strdup() fails. Therefore, I added a NULL check to fix this.

---

Regards,
Avnish Chouhan

Quoted 50 lines
 grub-core/osdep/linux/ofpath.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
grub-core/osdep/linux/ofpath.c +12 -2
index a6153d359..a3747b248 100644
@@ -695,6 +695,9 @@ strip_trailing_digits (const char *p)
   char *new, *end;
 
   new = strdup (p);
+  if (new == NULL)
+    return NULL;
+
   end = new + strlen(new) - 1;
   while (end >= new)
     {
@@ -709,13 +712,18 @@ strip_trailing_digits (const char *p)
 char *
 grub_util_devname_to_ofpath (const char *sys_devname)
 {
-  char *name_buf, *device, *devnode, *devicenode, *ofpath;
+  char *name_buf, *device, *devnode, *devicenode, *ofpath = NULL;
 
   name_buf = xrealpath (sys_devname);
 
   device = get_basename (name_buf);
   devnode = strip_trailing_digits (name_buf);
+  if (devnode == NULL)
+    goto devnode_fail;
+
   devicenode = strip_trailing_digits (device);
+  if (devicenode == NULL)
+    goto devicenode_fail;
 
   if (device[0] == 'h' && device[1] == 'd')
     ofpath = of_path_of_ide(name_buf, device, devnode, devicenode);
@@ -741,8 +749,10 @@ grub_util_devname_to_ofpath (const char 
 *sys_devname)
       ofpath = NULL;
     }
 
-  free (devnode);
   free (devicenode);
+ devicenode_fail:
+  free (devnode);
+ devnode_fail:
   free (name_buf);
 
   return ofpath;
Quoted 2 lines

--
2.50.1 (Apple Git-155)

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Reviewed-by
#2 lore raw
Quoted 15 lines · Avnish Chouhan
Quoted 14 lines · On 2025-11-19 17:41, [email protected] wrote:

Message: 1
Date: Wed, 19 Nov 2025 15:30:47 +0530
From: Sudhakar Kuppusamy <[email protected]>
To: [email protected]
Cc: Sudhakar Kuppusamy <[email protected]>, [email protected],
[email protected], [email protected]
Subject: [PATCH v1] linux/ofpath: Add missing strdup failure checks
Message-ID: <[email protected]>

Segmentation faults or undefined behaviour may result from a null pointer
dereference in strip_trailing_digits and grub_util_devname_to_ofpath if strdup() fails. Therefore, I added a NULL check to fix this.

Quoted 5 lines
Quoted 3 lines

---

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel