From aed7d2b8a99af1fdd26a34e75151421f97bf2927 Mon Sep 17 00:00:00 2001 From: Windrow14 <90297720+Windrow14@users.noreply.github.com> Date: Wed, 9 Oct 2024 13:29:38 +0800 Subject: [PATCH] fsutils/mkmbr/mkmbr.c: fix null pointer access when not all device space is defined in mbr argv[argn] is accessed out of range when there are neither four partitions are specified nor the last partition is of auto size. Add a number of partition variable based on input argument number. Signed-off-by: Yinzhe Wu Reviewed-by: Yuezhang Mo Reviewed-by: Jacky Cao Tested-by: Yinzhe Wu --- fsutils/mkmbr/mkmbr.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/fsutils/mkmbr/mkmbr.c b/fsutils/mkmbr/mkmbr.c index 9c0251233..4c468c5cf 100644 --- a/fsutils/mkmbr/mkmbr.c +++ b/fsutils/mkmbr/mkmbr.c @@ -111,8 +111,10 @@ int main(int argc, FAR char *argv[]) int argn; int fd; int ret; + int nr_part; - if (argc < 4 || argc % 2 != 0) + nr_part = (argc - 2) / 2; + if (argc < 4 || argc % 2 != 0 || nr_part > 4) { print_usage(); return EINVAL; @@ -133,7 +135,7 @@ int main(int argc, FAR char *argv[]) next = 1; bsize = st.st_size / 512; mbr_init(&mbr); - for (partn = 0; partn < 4 && next < bsize; partn++) + for (partn = 0; partn < nr_part && next < bsize; partn++) { argn = 2 * (partn + 1); if (!strcmp(argv[argn], "auto"))