Skip to contentSkip to conversation

Fixing a memory leak in serial.c

PATCH In review Avnish Chouhan 3 participants 3 messages Oct 28, 2025 – Nov 5, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fixing a memory leak in serial.c

grub_zalloc() allocates memory for port. And if the allocation for port->name fails, the function returns NULL without freeing the previously allocated port memory. This results in a memory leak. To avoid this, we must free port before returning.

---
 grub-core/term/ieee1275/serial.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
grub-core/term/ieee1275/serial.c +5 -1
index ac2a8f8..b3a8a7c 100644
@@ -236,7 +236,11 @@ add_port (struct ofserial_hash_ent *ent)
 			    + grub_strlen (ent->shortest));
   port->elem = ent;
   if (!port->name)
-    return NULL;
+    {
+      grub_free (port);
+      return NULL;
+    }
+
   ptr = grub_stpcpy (port->name, "ieee1275/");
   grub_strcpy (ptr, ent->shortest);
 
Signature · 7 lines
2.47.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Avnish Chouhan

grub_zalloc() allocates memory for port. And if the allocation for port->name fails, the function returns NULL without freeing the previously allocated port memory. This results in a memory leak. To avoid this, we must free port before returning.

Thanks,
Sudhakar

Quoted 29 lines

---
grub-core/term/ieee1275/serial.c | 6 +++++-

1 file changed, 5 insertions(+), 1 deletion(-)
grub-core/term/ieee1275/serial.c +5 -1
index ac2a8f8..b3a8a7c 100644
@@ -236,7 +236,11 @@ add_port (struct ofserial_hash_ent *ent)
   + grub_strlen (ent->shortest));
  port->elem = ent;
  if (!port->name)
-    return NULL;
+    {
+      grub_free (port);
+      return NULL;
+    }
+
  ptr = grub_stpcpy (port->name, "ieee1275/");
  grub_strcpy (ptr, ent->shortest);
 
Signature · 7 lines
2.47.1


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

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

Reviewed-by on v1 1/1
#2 lore raw
In reply to Avnish Chouhan
Quoted 6 lines · Avnish Chouhan

grub_zalloc() allocates memory for port. And if the allocation for port->name fails, the function returns NULL without freeing the previously allocated port memory. This results in a memory leak. To avoid this, we must free port before returning.

Daniel

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