Re: [PATCH 4/4] osdep/linux/getroot: add missing strdup failure checks

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

> On 10 Nov 2025, at 8:27 PM, Avnish Chouhan <[email protected]> wrote:
> 
> Adding a failure check in strdup. If strdup() fails,
> it returns NULL and passing NULL to a strchr() can
> lead to segmentation faults or undefined behavior.
> 
> Signed-off-by: Avnish Chouhan <[email protected]>

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

Thanks,
Sudhakar  
> ---
> grub-core/osdep/linux/getroot.c | 10 ++++++++++
> 1 file changed, 10 insertions(+)
> 
> diff --git a/grub-core/osdep/linux/getroot.c b/grub-core/osdep/linux/getroot.c
> index 38fe110..d71c373 100644
> --- a/grub-core/osdep/linux/getroot.c
> +++ b/grub-core/osdep/linux/getroot.c
> @@ -991,6 +991,8 @@ grub_util_get_raid_grub_dev (const char *os_dev)
>       char *p, *q;
> 
>       p = strdup (os_dev + sizeof ("/dev/md_d") - 1);
> +      if (p == NULL)
> +        return NULL;
> 
>       q = strchr (p, 'p');
>       if (q)
> @@ -1006,6 +1008,8 @@ grub_util_get_raid_grub_dev (const char *os_dev)
>       char *p, *q;
> 
>       p = strdup (os_dev + sizeof ("/dev/md/d") - 1);
> +      if (p == NULL)
> +        return NULL;
> 
>       q = strchr (p, 'p');
>       if (q)
> @@ -1019,6 +1023,8 @@ grub_util_get_raid_grub_dev (const char *os_dev)
>       char *p , *q;
> 
>       p = strdup (os_dev + sizeof ("/dev/md") - 1);
> +      if (p == NULL)
> +        return NULL;
> 
>       q = strchr (p, 'p');
>       if (q)
> @@ -1032,6 +1038,8 @@ grub_util_get_raid_grub_dev (const char *os_dev)
>       char *p , *q;
> 
>       p = strdup (os_dev + sizeof ("/dev/md/") - 1);
> +      if (p == NULL)
> +        return NULL;
> 
>       q = strchr (p, 'p');
>       if (q)
> @@ -1046,6 +1054,8 @@ grub_util_get_raid_grub_dev (const char *os_dev)
>       char *p , *q;
> 
>       p = strdup (os_dev + sizeof ("/dev/md/") - 1);
> +      if (p == NULL)
> +        return NULL;
> 
>       q = strchr (p, 'p');
>       if (q)
> -- 
> 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.