]> git.ipfire.org Git - thirdparty/grub.git/commitdiff
normal/cmdline: Add grub_calloc() failure check and fix hist_lines state loss
authorAvnish Chouhan <avnish@linux.ibm.com>
Thu, 20 Nov 2025 08:11:41 +0000 (13:41 +0530)
committerDaniel Kiper <daniel.kiper@oracle.com>
Fri, 21 Nov 2025 19:15:38 +0000 (20:15 +0100)
If grub_calloc() fails hist_lines becomes NULL. It means we loose the
reference to the previously allocated hist_lines and leak memory. With
this change on failure hist_lines still points to the old memory. So,
no leak, no state corruption.

Signed-off-by: Avnish Chouhan <avnish@linux.ibm.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
grub-core/normal/cmdline.c

index 9c6d9ade970312758db92e8f267df879704b21a7..0875250be0d892a38fd7d57d29c578b5df6e1416 100644 (file)
@@ -42,7 +42,14 @@ grub_err_t
 grub_set_history (int newsize)
 {
   grub_uint32_t **old_hist_lines = hist_lines;
+
   hist_lines = grub_calloc (newsize, sizeof (grub_uint32_t *));
+  if (hist_lines == NULL)
+    {
+      /* We need to restore hist_lines to avoid memory leak and state loss. */
+      hist_lines = old_hist_lines;
+      return grub_errno;
+    }
 
   /* Copy the old lines into the new buffer.  */
   if (old_hist_lines)