From 969fa07d2862f1fbf5c799c6a1ac646160044d6f Mon Sep 17 00:00:00 2001 From: Renge Date: Sat, 16 Apr 2022 10:04:08 -0400 Subject: [PATCH] fix: check input boundary --- hw4/src/jobs.c | 13 +++++++++++++ hw4/src/store.c | 9 +++++++++ 2 files changed, 22 insertions(+) diff --git a/hw4/src/jobs.c b/hw4/src/jobs.c index 26af050..0916627 100644 --- a/hw4/src/jobs.c +++ b/hw4/src/jobs.c @@ -224,6 +224,9 @@ int jobs_show(FILE *file) { * value returned is the job ID assigned to the pipeline. */ int jobs_run(PIPELINE *pline) { + if (!pline) + return -1; + pline = copy_pipeline(pline); // return -1 if no place for new job if (job_current_size + 1 >= MAX_JOBS) @@ -423,6 +426,8 @@ char **jobs_get_args(ARG *args) { * or -1 if any error occurs that makes it impossible to wait for the specified job. */ int jobs_wait(int jobid) { + if (jobid >= MAX_JOBS || jobid < 0) + return -1; if (!job_data_array[jobid]) return -1; if (job_data_array[jobid]->status == COMPLETED || @@ -458,6 +463,8 @@ int jobs_wait(int jobid) { * has terminated, or -1 if the job has not yet terminated or if any other error occurs. */ int jobs_poll(int jobid) { + if (jobid >= MAX_JOBS || jobid < 0) + return -1; if (!job_data_array[jobid]) return -1; if (job_data_array[jobid]->status != COMPLETED && @@ -481,6 +488,8 @@ int jobs_poll(int jobid) { * @return 0 if the job was successfully expunged, -1 if the job could not be expunged. */ int jobs_expunge(int jobid) { + if (jobid >= MAX_JOBS || jobid < 0) + return -1; if (!job_data_array[jobid]) return -1; if (job_data_array[jobid]->status != COMPLETED && @@ -523,6 +532,8 @@ int jobs_expunge(int jobid) { * error occurred. */ int jobs_cancel(int jobid) { + if (jobid >= MAX_JOBS || jobid < 0) + return -1; if (!job_data_array[jobid]) return -1; if (job_data_array[jobid]->status == COMPLETED || @@ -554,6 +565,8 @@ int jobs_cancel(int jobid) { * output available, otherwise NULL. */ char *jobs_get_output(int jobid) { + if (jobid >= MAX_JOBS || jobid < 0) + return NULL; if (!job_data_array[jobid]) return NULL; if (job_data_array[jobid]->status != COMPLETED && diff --git a/hw4/src/store.c b/hw4/src/store.c index 16596cb..413c11c 100644 --- a/hw4/src/store.c +++ b/hw4/src/store.c @@ -30,6 +30,8 @@ * otherwise NULL. */ char *store_get_string(char *var) { + if (!var) + return -1; store_data *data = store_get_data(var); if (data) return data->value; @@ -50,6 +52,8 @@ char *store_get_string(char *var) { * otherwise 0 is returned. */ int store_get_int(char *var, long *valp) { + if (!var || !valp) + return -1; store_data *data = store_get_data(var); if (data) { // is NULL @@ -91,6 +95,9 @@ int store_get_int(char *var, long *valp) { * un-set. */ int store_set_string(char *var, char *val) { + if (!var) + return -1; + store_data* data = store_get_data(var); // if want to unset a value that does not exist if (!data && !val) @@ -133,6 +140,8 @@ int store_set_string(char *var, char *val) { * @param val The value to set. */ int store_set_int(char *var, long val) { + if (!var) + return -1; // if val is 0, set var to 0 if (val == 0) return store_set_string(var, "0");