Skip to contentSkip to conversation

normal/cmdline: fix hist_lines state loss and grub calloc failure check

PATCH In review Avnish Chouhan 3 participants 5 messages Nov 20, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 normal/cmdline: fix hist_lines state loss and grub calloc failure check

Adding a fix for hist_lines memory leak and state loss. In current code, we overwrite hist_lines before checking the allocation result. If grub_calloc fails, hist_lines becomes NULL and we loose the reference to the previously allocated hist_lines. With this new change. On failure, hist_lines remains pointing to the old valid memory. No leak, no state corruption.

Along with this, adding a failure check in grub_calloc(). If grub_calloc fails, (e.g., due to memory allocation failure), it returns NULL. Then, passing hist_lines (which would be NULL) to grub_memmove() will result in a null pointer dereference, and can cause an undefined behavior.

---
 grub-core/normal/cmdline.c | 7 +++++++
 1 file changed, 7 insertions(+)
grub-core/normal/cmdline.c +7 -0
index 9c6d9ad..14a40a4 100644
@@ -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)
Signature · 7 lines
2.47.1


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

Adding a fix for hist_lines memory leak and state loss. In current code, we overwrite hist_lines before checking the allocation result. If grub_calloc fails, hist_lines becomes NULL and we loose the reference to the previously allocated hist_lines. With this new change. On failure, hist_lines remains pointing to the old valid memory. No leak, no state corruption.

Quoted 5 lines

Along with this, adding a failure check in grub_calloc(). If grub_calloc fails, (e.g., due to memory allocation failure), it returns NULL. Then, passing hist_lines (which would be NULL) to grub_memmove() will result in a null pointer dereference, and can cause an undefined behavior.

Sorry, Avnish. I don’t understand the above commit message. Could you please rephrase it.

Thanks,
Sudhakar

Quoted 33 lines

---
grub-core/normal/cmdline.c | 7 +++++++

1 file changed, 7 insertions(+)
grub-core/normal/cmdline.c +7 -0
index 9c6d9ad..14a40a4 100644
@@ -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)
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

#2 lore raw
Quoted 15 lines · Sudhakar Kuppusamy
Quoted 9 lines · Avnish Chouhan

Adding a fix for hist_lines memory leak and state loss. In current code, we overwrite hist_lines before checking the allocation result. If grub_calloc fails, hist_lines becomes NULL and we loose the reference to the previously allocated hist_lines. With this new change. On failure, hist_lines remains pointing to the old valid memory. No leak, no state corruption.

Quoted 5 lines

Along with this, adding a failure check in grub_calloc(). If grub_calloc fails, (e.g., due to memory allocation failure), it returns NULL. Then, passing hist_lines (which would be NULL) to grub_memmove() will result in a null pointer dereference, and can cause an undefined behavior.

Quoted 5 lines

Sorry, Avnish. I don’t understand the above commit message. Could you please rephrase it.

Thanks,
Sudhakar

This commit message clearly defines the problem and the intended purpose of this patch!

Quoted 30 lines
Quoted 30 lines

---
grub-core/normal/cmdline.c | 7 +++++++

1 file changed, 7 insertions(+)
grub-core/normal/cmdline.c +4 -0
index 9c6d9ad..14a40a4 100644
@@ -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 */

grub-core/normal/cmdline.c +3 -1
+      hist_lines = old_hist_lines;
+      return grub_errno;
+    }
 
  /* Copy the old lines into the new buffer.  */
  if (old_hist_lines)
--

2.47.1

Quoted 4 lines
Quoted 4 lines

_______________________________________________
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
#4 lore raw
In reply to Avnish Chouhan
Quoted 7 lines · Avnish Chouhan

Adding a fix for hist_lines memory leak and state loss. In current code, we overwrite hist_lines before checking the allocation result. If grub_calloc fails, hist_lines becomes NULL and we loose the reference to the previously allocated hist_lines. With this new change. On failure, hist_lines remains pointing to the old valid memory. No leak, no state corruption.

Quoted 7 lines

Along with this, adding a failure check in grub_calloc(). If grub_calloc fails, (e.g., due to memory allocation failure), it returns NULL. Then, passing hist_lines (which would be NULL) to grub_memmove() will result in a null pointer dereference, and can cause an undefined behavior.

Daniel

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

#3 lore raw
In reply to Avnish Chouhan
Quoted 17 lines · Avnish Chouhan
Quoted 12 lines · Sudhakar Kuppusamy
Quoted 12 lines · Avnish Chouhan

Adding a fix for hist_lines memory leak and state loss. In current code, we overwrite hist_lines before checking the allocation result. If grub_calloc fails, hist_lines becomes NULL and we loose the reference to the previously allocated hist_lines. With this new change. On failure, hist_lines remains pointing to the old valid memory. No leak, no state corruption. Along with this, adding a failure check in grub_calloc(). If grub_calloc fails, (e.g., due to memory allocation failure), it returns NULL. Then, passing hist_lines (which would be NULL) to grub_memmove() will result in a null pointer dereference, and can cause an undefined behavior.

Quoted 6 lines
Quoted 4 lines

Sorry, Avnish. I don’t understand the above commit message. Could you please rephrase it.
Thanks,
Sudhakar

This commit message clearly defines the problem and the intended purpose of this patch!

In the first message you defines the problem and also mentioned the solution. The second message is confusing to me.

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines

---
grub-core/normal/cmdline.c | 7 +++++++

1 file changed, 7 insertions(+)
grub-core/normal/cmdline.c +7 -0
index 9c6d9ad..14a40a4 100644
@@ -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;
+    }
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
grub-core/normal/cmdline.c +0 -1
 /* Copy the old lines into the new buffer.  */
 if (old_hist_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