Re: [PATCH 3/4] Adding a grub_malloc failure check in mmap.c

Sudhakar Kuppusamy <[email protected]>
Newsgroups org.gnu.grub-devel
Message-ID <[email protected]>

> On 28 Oct 2025, at 10:02 PM, Avnish Chouhan <[email protected]> wrote:
> 
> Adding a failure check after calling grub_malloc() as it can lead to
> undefined behavior. If the allocation fails and returns NULL, subsequent
> dereferencing or writing to the pointer will likely result in a runtime
> error such as a segmentation fault.
> 
> Signed-off-by: Avnish Chouhan <[email protected]>

Reviewed-by: Sudhakar Kuppusamy <[email protected]>

Thanks,
Sudhakar  
> ---
> grub-core/mmap/mmap.c | 3 +++
> 1 file changed, 3 insertions(+)
> 
> diff --git a/grub-core/mmap/mmap.c b/grub-core/mmap/mmap.c
> index c8c8312..8f03b77 100644
> --- a/grub-core/mmap/mmap.c
> +++ b/grub-core/mmap/mmap.c
> @@ -242,6 +242,9 @@ grub_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
>  else
>    {
>      struct mm_list *n = grub_malloc (sizeof (*n));
> +              if (n == NULL)
> +                return grub_errno;
> +
>      n->val = ctx.scanline_events[i].memtype;
>      n->present = 1;
>      n->next = present[ctx.scanline_events[i].priority].next;
> -- 
> 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.