Skip to content

Commit cb811bd

Browse files
Avnish ChouhanDaniel Kiper
authored andcommitted
normal/cmdline: Add grub_calloc() failure check and fix hist_lines state loss
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>
1 parent 8a850f4 commit cb811bd

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

grub-core/normal/cmdline.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ grub_err_t
4242
grub_set_history (int newsize)
4343
{
4444
grub_uint32_t **old_hist_lines = hist_lines;
45+
4546
hist_lines = grub_calloc (newsize, sizeof (grub_uint32_t *));
47+
if (hist_lines == NULL)
48+
{
49+
/* We need to restore hist_lines to avoid memory leak and state loss. */
50+
hist_lines = old_hist_lines;
51+
return grub_errno;
52+
}
4653

4754
/* Copy the old lines into the new buffer. */
4855
if (old_hist_lines)

0 commit comments

Comments
 (0)