-
Notifications
You must be signed in to change notification settings - Fork 167
init: Fix args processing #599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,7 +37,6 @@ | |
| #define KRUN_FOOTER_LEN 12 | ||
| #define CMDLINE_SECRET_PATH "/sfs/secrets/coco/cmdline" | ||
| #define CONFIG_FILE_PATH "/.krun_config.json" | ||
| #define MAX_ARGS 32 | ||
| #define MAX_PASS_SIZE 512 | ||
| #define MAX_TOKENS 16384 | ||
|
|
||
|
|
@@ -623,32 +622,42 @@ static char **config_parse_args(char *data, jsmntok_t *token) | |
| char *arg, *value; | ||
| char **argv; | ||
| int len; | ||
| int i, j; | ||
| int i; | ||
| const int n_args = token->size; | ||
|
|
||
| argv = malloc(MAX_ARGS * sizeof(char *)); | ||
| j = 0; | ||
| argv = malloc((n_args + 1) * sizeof(char *)); | ||
| if (!argv) { | ||
| perror("malloc(config_parse_args)"); | ||
| return NULL; | ||
| } | ||
|
|
||
| for (i = 0; i < token->size; i++) { | ||
| for (i = 0; i < n_args; i++) { | ||
| targ = &token[i + 1]; | ||
|
|
||
| value = data + targ->start; | ||
| len = targ->end - targ->start; | ||
|
|
||
| arg = malloc(len + 1); | ||
| if (!arg) { | ||
| perror("malloc(config_parse_args arg)"); | ||
| while (--i >= 0) | ||
| free(argv[i]); | ||
| free(argv); | ||
| return NULL; | ||
|
mz-pdm marked this conversation as resolved.
|
||
| } | ||
| memcpy(arg, value, len); | ||
| arg[len] = '\0'; | ||
|
|
||
| unescape_string(arg, len); | ||
|
|
||
| argv[j] = arg; | ||
| j++; | ||
| argv[i] = arg; | ||
| } | ||
|
|
||
| if (j == 0) { | ||
| if (i == 0) { | ||
| free(argv); | ||
| argv = NULL; | ||
| } else { | ||
| argv[j] = NULL; | ||
| argv[i] = NULL; | ||
| } | ||
|
|
||
| return argv; | ||
|
|
@@ -692,14 +701,24 @@ char **concat_entrypoint_argv(char **entrypoint, char **config_argv) | |
| { | ||
| char **argv; | ||
| int i, j; | ||
| int n_args = 0; | ||
|
|
||
| for (i = 0; entrypoint[i]; i++) | ||
| n_args++; | ||
| for (j = 0; config_argv[j]; j++) | ||
| n_args++; | ||
|
|
||
| argv = malloc(MAX_ARGS * sizeof(char *)); | ||
| argv = malloc((n_args + 1) * sizeof(char *)); | ||
| if (!argv) { | ||
| perror("malloc(concat_entrypoint_argv)"); | ||
| return NULL; | ||
| } | ||
|
|
||
| for (i = 0; i < MAX_ARGS && entrypoint[i]; i++) { | ||
| for (i = 0; entrypoint[i]; i++) { | ||
| argv[i] = entrypoint[i]; | ||
| } | ||
|
|
||
| for (j = 0; j < MAX_ARGS && config_argv[j]; i++, j++) { | ||
| for (j = 0; config_argv[j]; i++, j++) { | ||
| argv[i] = config_argv[j]; | ||
|
mz-pdm marked this conversation as resolved.
|
||
| } | ||
|
Comment on lines
+706
to
723
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are two issues in this function:
A more robust and efficient implementation would check for char **argv;
int entrypoint_len = 0;
int config_argv_len = 0;
while (entrypoint[entrypoint_len]) {
entrypoint_len++;
}
while (config_argv[config_argv_len]) {
config_argv_len++;
}
int n_args = entrypoint_len + config_argv_len;
argv = malloc((n_args + 1) * sizeof(char *));
if (!argv) {
perror("malloc");
return NULL;
}
memcpy(argv, entrypoint, entrypoint_len * sizeof(char *));
memcpy(argv + entrypoint_len, config_argv, config_argv_len * sizeof(char *));
argv[n_args] = NULL;
return argv;Note that
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may not be a bad idea but it introduces unnecessary changes to the problem being fixed and I consider the original code a bit better readable. As for malloc() return value, this is handled in the newly added commit. |
||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.