From f65446c93a8c033b704cac46c49bad92d72316d1 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Thu, 17 Sep 2026 12:35:15 +0000 Subject: fix stack size issue, replaced by heap when input is too long, stack isn't large enough --- include/job.h | 2 +- src/eval.cc | 31 +++++++++++++++++-------------- src/job.cc | 2 +- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/include/job.h b/include/job.h index 130bc5d..1ef63b5 100644 --- a/include/job.h +++ b/include/job.h @@ -51,7 +51,7 @@ struct job_t { /* The job struct */ void clearjob(struct job_t *job = _jobs); void initjobs(struct job_t *job = _jobs); int maxjid(struct job_t *job = _jobs); -int addjob(pid_t pid, int state, char *cmdline, struct job_t *job = _jobs); +int addjob(pid_t pid, int state, const char *cmdline, struct job_t *job = _jobs); int deletejob(pid_t pid, struct job_t *job = _jobs); pid_t fgpid(struct job_t *job = _jobs); struct job_t *getjobpid(pid_t pid, struct job_t *job = _jobs); diff --git a/src/eval.cc b/src/eval.cc index 716ca66..75c1d27 100644 --- a/src/eval.cc +++ b/src/eval.cc @@ -41,21 +41,24 @@ int eval_exe(const string &s, bool is_async, const T_lexer *lexer_instance, size loggerln("eval_exe: receive {}", s); bool task_type_is_subshell = false; - char raw_input[MAXLINE]; - char **argv; - char cmd_with_path[MAXLINE]; + const char *raw_input = nullptr; + char **argv = nullptr; + const char *unified_cmd_c = nullptr; + string unified_cmd, s_wrap_eliminated; // subshell type if (lexer_instance != nullptr) { task_type_is_subshell = true; - strcpy(raw_input, "SUBSHELL"); + unified_cmd = "SUBSHELL"; + raw_input = unified_cmd.c_str(); } // command type else { task_type_is_subshell = false; - strcpy(raw_input, s.c_str()); + s_wrap_eliminated = s.substr(0, s.find('\n')); // eliminate \n + raw_input = s_wrap_eliminated.c_str(); argv = parseline(s); @@ -63,8 +66,8 @@ int eval_exe(const string &s, bool is_async, const T_lexer *lexer_instance, size if (exe_bultin_command(argv)) return 0; - string cmd = find_cmd(argv[0]); - strcpy(cmd_with_path, cmd.c_str()); + unified_cmd = find_cmd(argv[0]); + unified_cmd_c = unified_cmd.c_str(); } // block_io signals can stop shell @@ -97,43 +100,43 @@ int eval_exe(const string &s, bool is_async, const T_lexer *lexer_instance, size } // normal command - execve(cmd_with_path, argv, environ); + execve(unified_cmd_c, argv, environ); // execve: if success, never returns - wrap_eliminator(raw_input); cout << raw_input << flush; switch (errno) { case ENOTDIR: println(": A component of the path prefix is not a directory."); - exit(errno); + _exit(errno); break; case ENAMETOOLONG: println(": A component of a pathname exceeded 255 characters, or an entire path name exceeded 1023 characters."); - exit(errno); + _exit(errno); break; case ENOENT: println(": The new process file does not exist."); - exit(errno); + _exit(errno); break; case EACCES: println(": The new process file mode denies execute permission."); - exit(errno); + _exit(errno); break; default: println(": Error, code: {}", errno); - exit(errno); + _exit(errno); } } // parent operator delete(argv); + argv = nullptr; exit_required_pid = pid; diff --git a/src/job.cc b/src/job.cc index e4f9617..8e9e6cc 100644 --- a/src/job.cc +++ b/src/job.cc @@ -33,7 +33,7 @@ int maxjid(struct job_t *job) } /* addjob - Add a job to the job list */ -int addjob(pid_t pid, int state, char *cmdline, struct job_t *job) +int addjob(pid_t pid, int state, const char *cmdline, struct job_t *job) { int i; -- cgit v1.3.1-4-g156e