From 8d86ee202a9bb553375f56ae1d2944818112b68b Mon Sep 17 00:00:00 2001 From: Nigel Croxon Date: Wed, 30 Jul 2014 15:04:44 -0400 Subject: [PATCH] From: David Decotigny Date: Mon, 28 Jul 2014 21:01:35 -0700 Subject: [PATCH 1/3] Fix cmdline parser The cmdline parser would not return the correct number of args, would allocate one too many. Also make it clear from the declaration that we expect a suitably lare argv. Signed-off-by: David Decotigny Signed-off-by: Nigel Croxon --- gnu-efi-3.0/inc/ParseCmdLine.h | 5 ++--- gnu-efi-3.0/lib/ParseCmdLine.c | 33 +++++++++++++++------------------ 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/gnu-efi-3.0/inc/ParseCmdLine.h b/gnu-efi-3.0/inc/ParseCmdLine.h index 899b70d..dd795ab 100644 --- a/gnu-efi-3.0/inc/ParseCmdLine.h +++ b/gnu-efi-3.0/inc/ParseCmdLine.h @@ -7,6 +7,5 @@ #define MAX_ARGS 512 - -int ParseCmdLine(CHAR16 *argv[], CHAR16 *buf, int len); - +/* buf will be updated in-place */ +INTN ParseCmdLine(CHAR16 *argv[MAX_ARGS], CHAR16 *buf, UINTN len); diff --git a/gnu-efi-3.0/lib/ParseCmdLine.c b/gnu-efi-3.0/lib/ParseCmdLine.c index 7aa8e7c..706ba47 100644 --- a/gnu-efi-3.0/lib/ParseCmdLine.c +++ b/gnu-efi-3.0/lib/ParseCmdLine.c @@ -11,28 +11,25 @@ -int ParseCmdLine(CHAR16 *argv[], CHAR16 *LoadOptions, int LoadOptionSize) +INTN ParseCmdLine(CHAR16 *argv[MAX_ARGS], CHAR16 *buf, UINTN len) { - CHAR16 *buf = LoadOptions; - int len = LoadOptionSize; - int i; // Index into buf + CHAR16 *arg_start, *c; + INTN argc = 0; - int argc = 0; // Count of argv entries - argv[argc++] = LoadOptions ? LoadOptions: L"##NoName##"; + if (len < 2) + return -1; - for ( i = 0; len > 0; len-= 2, i++) { - if ( buf[i] == L' ' ) { - // end of current argv entry, start next? - buf[i] = L'\0'; - do { - len -= 2; - i++; - } while( (buf[i] == L' ') && (len > 0) ); - argv[argc] = &buf[i]; - if ( len > 0 ) argc++; - } + buf[(len / 2) - 1] = L'\0'; + for (c = arg_start = buf ; *c ; ++c) { + if (*c == L' ') { + *c = L'\0'; + if (argc < MAX_ARGS) argv[argc++] = arg_start; + arg_start = c + 1; + } } - argv[argc] = NULL; + + if ((*arg_start != L'\0') && (argc < MAX_ARGS)) + argv[argc++] = arg_start; return argc; }