Skip to content

Commit

Permalink
Don't try to turn Argdata into string command line arguments.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
EdSchouten committed Jan 10, 2018
1 parent e0971bd commit 2f0020b
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/libc/program/program_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 2f0020b

Please sign in to comment.