Skip to content

Commit

Permalink
Fix realloc issue
Browse files Browse the repository at this point in the history
  • Loading branch information
baglayan committed Mar 24, 2024
1 parent 56f2cd2 commit 1b9b319
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cppcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ jobs:
- name: Static Analysis with cppcheck
run: |
sudo apt-get install cppcheck
cppcheck --enable=all --suppress=missingIncludeSystem .
cppcheck --enable=all --suppress=missingIncludeSystem --error-exitcode=1 .
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ hwsh
hwsh.o
hwsh_rewrite.c
hwsh.dSYM
.DS_Store

/.vscode
16 changes: 11 additions & 5 deletions hwsh.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ size_t history_count = 0;
int main_interactive(char line[], char *command);
int main_batch(char line[], char *command, FILE *batch_file);
int hwsh_exec(char *command);
int hwsh_builtin_command(char *command, char *first_arg);
int hwsh_builtin_command(const char *command, char *first_arg);
void hwsh_command_chdir(char *path);
void hwsh_command_history(char **history, size_t history_count, size_t history_start);
char *hwsh_util_get_username(void);
Expand Down Expand Up @@ -233,7 +233,7 @@ int hwsh_exec(char *command)
new_parallel_cmd->args = NULL;

char *arg_save = NULL;
char *arg = strtok_r(parallel_cmd, " ", &arg_save);
const char *arg = strtok_r(parallel_cmd, " ", &arg_save);

while (arg) {
new_parallel_cmd->args = realloc(new_parallel_cmd->args, (new_parallel_cmd->argc + 1) * sizeof(char *));
Expand All @@ -251,7 +251,13 @@ int hwsh_exec(char *command)
logger(LOG_LEX, "==SEMICOLON==");
}

clusters = realloc(clusters, (num_clusters + 1) * sizeof(pipe_cluster_t *));
pipe_cluster_t **new_clusters = realloc(clusters, (num_clusters + 1) * sizeof(pipe_cluster_t *));
if (new_clusters == NULL) {
free(clusters);
perror("realloc");
} else {
clusters = new_clusters;
}
clusters[num_clusters++] = new_cluster;

cluster = strtok_r(NULL, "|", &pipe_save);
Expand Down Expand Up @@ -379,7 +385,7 @@ void hwsh_command_history(char **_history, size_t _history_count, size_t _histor
}
}

int hwsh_builtin_command(char *command, char *firstArg)
int hwsh_builtin_command(const char *command, char *firstArg)
{
if (strcmp(command, "quit") == 0) {
exit(0);
Expand All @@ -406,7 +412,7 @@ void hwsh_command_chdir(char *path)

char *hwsh_util_get_username(void)
{
struct passwd *_username = getpwuid(getuid());
const struct passwd *_username = getpwuid(getuid());
size_t username_len = strlen(_username->pw_name);
char *username = (char *)malloc(sizeof(char) * (username_len + 1));
strcpy(username, _username->pw_name);
Expand Down

0 comments on commit 1b9b319

Please sign in to comment.