commands/ls: Fix NULL dereference

The grub_strrchr() may return NULL when the dirname do not contain "/".
This can happen on broken filesystems.

Reported-by: B Horn <b@horn.uk>
Signed-off-by: B Horn <b@horn.uk>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
This commit is contained in:
B Horn 2024-05-12 11:08:23 +01:00 committed by Daniel Kiper
parent 05be856a8c
commit 0bf56bce47

View File

@ -241,7 +241,11 @@ grub_ls_list_files (char *dirname, int longlist, int all, int human)
grub_file_close (file);
p = grub_strrchr (dirname, '/') + 1;
p = grub_strrchr (dirname, '/');
if (p == NULL)
goto fail;
++p;
ctx.dirname = grub_strndup (dirname, p - dirname);
if (ctx.dirname == NULL)
goto fail;