From ba3cac5803fec37a2145679e2d73746302e59776 Mon Sep 17 00:00:00 2001
From: Joshua Saxby <joshua.a.saxby@gmail.com>
Date: Sat, 30 Nov 2019 15:40:02 +0000
Subject: [PATCH] Put argv count and length in macros

---
 userprog/argument_parsing.h | 7 +++++++
 userprog/parse_arguments.c  | 4 ++--
 userprog/process.c          | 8 +++-----
 3 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/userprog/argument_parsing.h b/userprog/argument_parsing.h
index d5cef69..7a490ec 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 a78d203..5687b4a 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 5dfa0a2..5fbfb38 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
 
-- 
GitLab