Skip to contentSkip to conversation

linux/ofpath: Add missing strdup failure checks

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

lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive
Quoted 33 lines · On 2025-11-18 22:31, [email protected] wrote:

Message: 2
Date: Tue, 18 Nov 2025 19:55:39 +0530
From: Sudhakar Kuppusamy <[email protected]>
To: [email protected]
Cc: Sudhakar Kuppusamy <[email protected]>, [email protected],
[email protected], [email protected]
Subject: [PATCH] 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.

---
 grub-core/osdep/linux/ofpath.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
grub-core/osdep/linux/ofpath.c +3 -0
index a6153d359..ade5220db 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)
     {

grub-core/osdep/linux/ofpath.c grub_util_devname_to_ofpath (const char · line 722

Quoted 5 lines
grub-core/osdep/linux/ofpath.c +1 -0
@@ -715,7 +718,15 @@ grub_util_devname_to_ofpath (const char 
 *sys_devname)
 
   device = get_basename (name_buf);
   devnode = strip_trailing_digits (name_buf);
+  if (devnode == NULL)

Hi Sudhakar,

This change will lead to a memory leak. You need to add "free (name_buf);" here before retuning!

grub-core/osdep/linux/ofpath.c grub_util_devname_to_ofpath (const char · line 728

Quoted 6 lines
grub-core/osdep/linux/ofpath.c +5 -0
+    return NULL;
+
   devicenode = strip_trailing_digits (device);
+  if (devicenode == NULL)
+    {
+      free (devnode);

And here too, "free (name_buf);"......

Thank you!

Regards,
Avnish Chouhan

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

#2 lore raw
Quoted 42 lines · Avnish Chouhan
Quoted 29 lines · On 2025-11-18 22:31, [email protected] wrote:

Message: 2
Date: Tue, 18 Nov 2025 19:55:39 +0530
From: Sudhakar Kuppusamy <[email protected]>
To: [email protected]
Cc: Sudhakar Kuppusamy <[email protected]>, [email protected], [email protected], [email protected]
Subject: [PATCH] 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.

---
grub-core/osdep/linux/ofpath.c | 11 +++++++++++

1 file changed, 11 insertions(+)
grub-core/osdep/linux/ofpath.c +3 -0
index a6153d359..ade5220db 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)
    {

grub-core/osdep/linux/ofpath.c grub_util_devname_to_ofpath (const char *sys_devname) · line 720

Quoted 3 lines
grub-core/osdep/linux/ofpath.c +1 -0
@@ -715,7 +718,15 @@ grub_util_devname_to_ofpath (const char *sys_devname)
  device = get_basename (name_buf);
  devnode = strip_trailing_digits (name_buf);
+  if (devnode == NULL)

Hi Sudhakar,

This change will lead to a memory leak. You need to add "free (name_buf);" here before retuning!

Quoted 9 lines

grub-core/osdep/linux/ofpath.c grub_util_devname_to_ofpath (const char *sys_devname) · line 726

Quoted 6 lines
grub-core/osdep/linux/ofpath.c +5 -0
+    return NULL;
+
  devicenode = strip_trailing_digits (device);
+  if (devicenode == NULL)
+    {
+      free (devnode);

And here too, "free (name_buf);"......

Thank you Avnish for the review. I will fix it on v1.

Thanks,
Sudhakar

Quoted 4 lines

Thank you!

Regards,
Avnish Chouhan

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

#3 lore raw
Quoted 46 lines · Sudhakar Kuppusamy
Quoted 46 lines · Avnish Chouhan
Quoted 29 lines · On 2025-11-18 22:31, [email protected] wrote:

Message: 2
Date: Tue, 18 Nov 2025 19:55:39 +0530
From: Sudhakar Kuppusamy <[email protected]>
To: [email protected]
Cc: Sudhakar Kuppusamy <[email protected]>, [email protected], [email protected], [email protected]
Subject: [PATCH] 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.

---
grub-core/osdep/linux/ofpath.c | 11 +++++++++++

1 file changed, 11 insertions(+)
grub-core/osdep/linux/ofpath.c +3 -0
index a6153d359..ade5220db 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)
    {

grub-core/osdep/linux/ofpath.c grub_util_devname_to_ofpath (const char *sys_devname) · line 720

Quoted 3 lines
grub-core/osdep/linux/ofpath.c +1 -0
@@ -715,7 +718,15 @@ grub_util_devname_to_ofpath (const char *sys_devname)
  device = get_basename (name_buf);
  devnode = strip_trailing_digits (name_buf);
+  if (devnode == NULL)

Hi Sudhakar,

This change will lead to a memory leak. You need to add "free (name_buf);" here before retuning!

Quoted 6 lines
grub-core/osdep/linux/ofpath.c +5 -0
+    return NULL;
+
  devicenode = strip_trailing_digits (device);
+  if (devicenode == NULL)
+    {
+      free (devnode);
Quoted 4 lines
Quoted 2 lines

And here too, "free (name_buf);"......

Thank you Avnish for the review. I will fix it on v1.

This patch is in. So, please provide a fix on top of it.

Daniel

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

#4 lore raw
Quoted 51 lines · Daniel Kiper
Quoted 46 lines · Sudhakar Kuppusamy
Quoted 46 lines · Avnish Chouhan
Quoted 29 lines · On 2025-11-18 22:31, [email protected] wrote:

Message: 2
Date: Tue, 18 Nov 2025 19:55:39 +0530
From: Sudhakar Kuppusamy <[email protected]>
To: [email protected]
Cc: Sudhakar Kuppusamy <[email protected]>, [email protected], [email protected], [email protected]
Subject: [PATCH] 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.

---
grub-core/osdep/linux/ofpath.c | 11 +++++++++++

1 file changed, 11 insertions(+)
grub-core/osdep/linux/ofpath.c +3 -0
index a6153d359..ade5220db 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)
   {

grub-core/osdep/linux/ofpath.c grub_util_devname_to_ofpath (const char *sys_devname) · line 720

Quoted 3 lines
grub-core/osdep/linux/ofpath.c +1 -0
@@ -715,7 +718,15 @@ grub_util_devname_to_ofpath (const char *sys_devname)
 device = get_basename (name_buf);
 devnode = strip_trailing_digits (name_buf);
+  if (devnode == NULL)

Hi Sudhakar,

This change will lead to a memory leak. You need to add "free (name_buf);" here before retuning!

Quoted 6 lines
grub-core/osdep/linux/ofpath.c +5 -0
+    return NULL;
+
 devicenode = strip_trailing_digits (device);
+  if (devicenode == NULL)
+    {
+      free (devnode);
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

And here too, "free (name_buf);"......

Thank you Avnish for the review. I will fix it on v1.

This patch is in. So, please provide a fix on top of it.

Daniel

Hi Daniel,

I could not see this patch in https://cgit.git.savannah.gnu.org/cgit/grub.git/

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

#5 lore raw
Quoted 53 lines · Sudhakar Kuppusamy
Quoted 53 lines · Daniel Kiper
Quoted 50 lines · Sudhakar Kuppusamy
Quoted 39 lines · Avnish Chouhan
Quoted 29 lines · On 2025-11-18 22:31, [email protected] wrote:

Message: 2
Date: Tue, 18 Nov 2025 19:55:39 +0530
From: Sudhakar Kuppusamy <[email protected]>
To: [email protected]
Cc: Sudhakar Kuppusamy <[email protected]>, [email protected], [email protected], [email protected]
Subject: [PATCH] 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.

---
grub-core/osdep/linux/ofpath.c | 11 +++++++++++

1 file changed, 11 insertions(+)
grub-core/osdep/linux/ofpath.c +3 -0
index a6153d359..ade5220db 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)
   {

grub-core/osdep/linux/ofpath.c grub_util_devname_to_ofpath (const char *sys_devname) · line 720

Quoted 3 lines
grub-core/osdep/linux/ofpath.c +1 -0
@@ -715,7 +718,15 @@ grub_util_devname_to_ofpath (const char *sys_devname)
 device = get_basename (name_buf);
 devnode = strip_trailing_digits (name_buf);
+  if (devnode == NULL)

Hi Sudhakar,

This change will lead to a memory leak. You need to add "free (name_buf);" here before retuning!

Quoted 9 lines

grub-core/osdep/linux/ofpath.c grub_util_devname_to_ofpath (const char *sys_devname) · line 726

Quoted 6 lines
grub-core/osdep/linux/ofpath.c +5 -0
+    return NULL;
+
 devicenode = strip_trailing_digits (device);
+  if (devicenode == NULL)
+    {
+      free (devnode);

And here too, "free (name_buf);"......

Thank you Avnish for the review. I will fix it on v1.

Quoted 7 lines
Quoted 3 lines

This patch is in. So, please provide a fix on top of it.

Daniel

Hi Daniel,

I could not see this patch in https://cgit.git.savannah.gnu.org/cgit/grub.git/

Errr... Sorry, I mixed up the patches. Ignore my comment then.

Daniel

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