Skip to content

Commit

Permalink
Fix segfault caused by allocating large arrays on the stack
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielVukelich committed Nov 12, 2016
1 parent 958b2a2 commit fe1282d
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions vsh.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ int main(int argc, char** argv, char** envp){
running = 1;
char pwd[DIR_STR_SIZ];
const long ARG_MAX = sysconf(_SC_ARG_MAX);
char cmdbuf[ARG_MAX];
char* cmdbuf = malloc(sizeof(char) * ARG_MAX);
//This holds all the args that a child could be passed.
//ARG_MAX is the maximum length of an argument, so there
//can never be more than ARG_MAX/2 arguments (assuming there
//are ARG_MAX arguments of 1 char each separated by spaces)
char* child_argv[ARG_MAX / 2];
char** child_argv = malloc(sizeof(char) * (ARG_MAX / 2));
last_argv = child_argv;

char user_prompt = '$';
Expand All @@ -35,7 +35,7 @@ int main(int argc, char** argv, char** envp){


while(running){

//Get the current working directory. If an error occurs, then
//set the pwd string to empty
if(getcwd(pwd, DIR_STR_SIZ) != pwd){
Expand All @@ -55,8 +55,8 @@ int main(int argc, char** argv, char** envp){
//Get our argc and fill our argv
int child_argc = get_args(args, child_argv);

//See if the command we've been given should be
//executed internally. If not, assume it's a
//See if the command we've been given should be
//executed internally. If not, assume it's a
//program
run_builtin_command(child_argc, child_argv);
if(!builtin_cmd_found){
Expand All @@ -67,18 +67,19 @@ int main(int argc, char** argv, char** envp){
}
wait(NULL);
}

//If the user ran a command built in to the shell
//(like cd), and it had an error, print that error
if(builtin_cmd_found && last_cmd_code){
print_error();
}
}

free_args(args);

}
puts("Exiting");

free(cmdbuf);
free(child_argv);
deinit_builtin_commands();

return 0;
Expand Down

0 comments on commit fe1282d

Please sign in to comment.