diff --git a/userprog/argument_parsing.h b/userprog/argument_parsing.h index d5cef69f63d1e20c7ab9ee15d643ee9390064401..7a490ec47caf6d4edf8d95d78de0c10528da0a53 100644 --- a/userprog/argument_parsing.h +++ b/userprog/argument_parsing.h @@ -1,6 +1,13 @@ #ifndef USERPROG_ARGUMENT_PARSING_H #define USERPROG_ARGUMENT_PARSING_H + +// some macros for argument length and count limits +// TODO: decide how many arguments we want --is 255 the standard number? +#define USERPROG_ARGV_COUNT 255 +// TODO: clarify what a sensible value for this is +#define USERPROG_RAW_ARGV_LENGTH 255 + /* * Given a string containing the command invoking the program `command_line` * and an array of C strings (pointer to `char* argv[]`), parse `command_line` diff --git a/userprog/parse_arguments.c b/userprog/parse_arguments.c index a78d20359c02e9b4a3eeb44ab8d81d62b2ae159d..5687b4a1419ffc50d249c87f472686575d6da0d7 100644 --- a/userprog/parse_arguments.c +++ b/userprog/parse_arguments.c @@ -1,4 +1,3 @@ -#include <stdio.h> #include <string.h> #include "userprog/argument_parsing.h" @@ -13,7 +12,8 @@ int parse_arguments(const char* command_line, char**argv) { argc++; // now, parse token-by-token for any additional arguments char* token = strtok_r(NULL, " ", &state_pointer); - while (token != NULL) { + // continue while still tokens to read and maximum arg count is not reached + while (token != NULL && argc < USERPROG_ARGV_COUNT) { argv[argc] = token; argc++; token = strtok_r(NULL, " ", &state_pointer); diff --git a/userprog/process.c b/userprog/process.c index 5dfa0a2f6c3b0b86b962614e100b91ab00d5f353..5fbfb38e05f7d429a994a2e87d71221068c56531 100644 --- a/userprog/process.c +++ b/userprog/process.c @@ -232,12 +232,10 @@ load (const char *command, void (**eip) (void), void **esp) int i; // extract arguments (including file name) - // NOTE: decide how many arguments we want --is 255 the standard number? - char* argv[255]; + char* argv[USERPROG_ARGV_COUNT]; // copy the command string so we have a separate copy to manipulate - // generous length limit based on 255 arguments that are 15 chars long each - char command_copy[255]; - strlcpy(command_copy, command, 255); + char command_copy[USERPROG_RAW_ARGV_LENGTH]; + strlcpy(command_copy, command, USERPROG_RAW_ARGV_LENGTH); int argc = parse_arguments(command_copy, argv); char* file_name = argv[0]; // file name is always argument 0