From 2f0020b7b8de81f89bfb72e635ea02b94b467dfb Mon Sep 17 00:00:00 2001 From: Ed Schouten Date: Wed, 10 Jan 2018 21:25:00 +0100 Subject: [PATCH] Don't try to turn Argdata into string command line arguments. Rust will always make use of the main() function on startup, even if we make use of Argdata. Optimize this scenario a bit by checking whether the provided Argdata is binary. If so, don't try to convert it to string command line arguments. --- src/libc/program/program_main.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/libc/program/program_main.c b/src/libc/program/program_main.c index 213e28ec..f762f786 100644 --- a/src/libc/program/program_main.c +++ b/src/libc/program/program_main.c @@ -14,14 +14,22 @@ int main(int, char **, char **); // Fallback for when the program does not contain program_main(), so // that it can call into the C standard main() function. noreturn void program_main(const argdata_t *ad) { + // In case input starts with a low-value byte, it is very likely that + // the provided input is structured Argdata. Don't try to parse this + // input as string command line arguments. + size_t argdatalen = + __at_argdatalen > 0 && ((const uint8_t *)__at_argdata)[0] >= ' ' + ? __at_argdatalen + : 0; + // Copy the argument data over to a writable null-terminated buffer. - char arg[__at_argdatalen + 1]; - memcpy(arg, __at_argdata, __at_argdatalen); - arg[__at_argdatalen] = '\0'; + char arg[argdatalen + 1]; + memcpy(arg, __at_argdata, argdatalen); + arg[argdatalen] = '\0'; // Count the number of arguments. size_t argc = 0; - for (size_t i = 0; i <= __at_argdatalen; ++i) + for (size_t i = 0; i <= argdatalen; ++i) if (arg[i] == '\0') ++argc;