From 5b5f1735ff2d530790e7f23b2f6b91b69d03def4 Mon Sep 17 00:00:00 2001 From: Gary Date: Fri, 13 Dec 2019 11:59:14 -0800 Subject: [PATCH] acrnboot: fix the parsing hv_cmdline to correctly handle the case of containing trailing whitespaces The pointer variable 'start' should be checked against NULL right after detected it is not pointer to a space character, otherwise the pointer variable 'end' must hold the wrong address right after NULL if the cmdline containing trailing whitespaces and deference the wrong address out of cmdline string. this parsing code also been optimized and simplified. Tracked-On: projectacrn#4250 Signed-off-by: Gary --- hypervisor/boot/cmdline.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/hypervisor/boot/cmdline.c b/hypervisor/boot/cmdline.c index 4e6c59df1..3e3a63d4b 100644 --- a/hypervisor/boot/cmdline.c +++ b/hypervisor/boot/cmdline.c @@ -17,7 +17,7 @@ int32_t parse_hv_cmdline(void) { const char *start; - const char *end; + const char *end = NULL; struct multiboot_info *mbi = NULL; if (boot_regs[0] != MULTIBOOT_INFO_MAGIC) { @@ -35,20 +35,20 @@ int32_t parse_hv_cmdline(void) start = (char *)hpa2hva_early((uint64_t)mbi->mi_cmdline); dev_dbg(ACRN_DBG_PARSE, "hv cmdline: %s", start); - do { - while (*start == ' ') + while ((start != NULL) && ((*start) != '\0')) { + while ((*start) == ' ') start++; + if ((*start) != '\0') { + end = start + 1; + while ((*end != ' ') && ((*end) != '\0')) + end++; - end = start + 1; - while ((*end != ' ') && ((*end) != '\0')) - end++; - - if (!handle_dbg_cmd(start, (int32_t)(end - start))) { - /* if not handled by handle_dbg_cmd, it can be handled further */ + if (!handle_dbg_cmd(start, (int32_t)(end - start))) { + /* if not handled by handle_dbg_cmd, it can be handled further */ + } + start = end; } - start = end + 1; - - } while (((*end) != '\0') && ((*start) != '\0')); + } return 0; }