From 1bbd50aac748e599c42dcd41836a0568fd20ca40 Mon Sep 17 00:00:00 2001 From: jfreegman Date: Mon, 2 Nov 2020 18:08:54 -0500 Subject: [PATCH] Fix a few issues - realloc needs to be error checked - use correct format specifiers - make sure optarg and DATA_FILE aren't null before using them --- src/avatars.c | 2 +- src/chat.c | 4 ++-- src/chat_commands.c | 4 ++-- src/friendlist.c | 2 +- src/line_info.c | 2 +- src/misc_tools.c | 2 +- src/term_mplex.c | 14 ++++++++++++-- src/toxic.c | 10 +++++++++- 8 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/avatars.c b/src/avatars.c index 428e62a..21fbceb 100644 --- a/src/avatars.c +++ b/src/avatars.c @@ -92,7 +92,7 @@ int avatar_send(Tox *m, uint32_t friendnum) } if (err != TOX_ERR_FILE_SEND_OK) { - fprintf(stderr, "tox_file_send failed for friendnumber %d (error %d)\n", friendnum, err); + fprintf(stderr, "tox_file_send failed for friendnumber %u (error %d)\n", friendnum, err); return -1; } diff --git a/src/chat.c b/src/chat.c index bf9ddca..49f2a07 100644 --- a/src/chat.c +++ b/src/chat.c @@ -502,7 +502,7 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, uint32_t friendnum, uint /* transfer is accepted */ if (ft->state == FILE_TRANSFER_PENDING) { ft->state = FILE_TRANSFER_STARTED; - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File transfer [%d] for '%s' accepted.", + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File transfer [%zu] for '%s' accepted.", ft->index, ft->file_name); char progline[MAX_STR_SIZE]; init_progress_bar(progline); @@ -660,7 +660,7 @@ static void chat_onFileRecv(ToxWindow *self, Tox *m, uint32_t friendnum, uint32_ } } - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Type '/savefile %d' to accept the file transfer.", ft->index); + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Type '/savefile %zu' to accept the file transfer.", ft->index); ft->file_size = file_size; snprintf(ft->file_path, sizeof(ft->file_path), "%s", file_path); diff --git a/src/chat_commands.c b/src/chat_commands.c index 0e857f4..393c46f 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -103,7 +103,7 @@ void cmd_groupinvite(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*a return; } - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invited contact to Group %d.", groupnum); + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invited contact to Group %lu.", groupnum); } void cmd_join_group(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) @@ -189,7 +189,7 @@ void cmd_savefile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv goto on_recv_error; } - line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Saving file [%d] as: '%s'", idx, ft->file_path); + line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Saving file [%zu] as: '%s'", idx, ft->file_path); /* prep progress bar line */ char progline[MAX_STR_SIZE]; diff --git a/src/friendlist.c b/src/friendlist.c index a7122ae..f3f6352 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -407,7 +407,7 @@ static void friendlist_onConnectionChange(ToxWindow *self, Tox *m, uint32_t num, ++Friends.num_online; if (avatar_send(m, num) == -1) { - fprintf(stderr, "avatar_send failed for friend %d\n", num); + fprintf(stderr, "avatar_send failed for friend %u\n", num); } } diff --git a/src/line_info.c b/src/line_info.c index cea57b6..96641cc 100644 --- a/src/line_info.c +++ b/src/line_info.c @@ -420,7 +420,7 @@ void line_info_print(ToxWindow *self) } } - wprintw(win, "\n", line->msg); + wprintw(win, "\n"); break; case SYS_MSG: diff --git a/src/misc_tools.c b/src/misc_tools.c index 6d756d6..44511f0 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -553,7 +553,7 @@ void set_window_title(ToxWindow *self, const char *title, int len) char cpy[TOXIC_MAX_NAME_LENGTH + 1]; if (self->is_groupchat) { /* keep groupnumber in title */ - snprintf(cpy, sizeof(cpy), "%d %s", self->num, title); + snprintf(cpy, sizeof(cpy), "%u %s", self->num, title); } else { snprintf(cpy, sizeof(cpy), "%s", title); } diff --git a/src/term_mplex.c b/src/term_mplex.c index 1420549..0dbe4ee 100644 --- a/src/term_mplex.c +++ b/src/term_mplex.c @@ -100,9 +100,19 @@ static char *read_into_dyn_buffer(FILE *stream) int length = dyn_buffer_size + strlen(input_ptr); if (dyn_buffer) { - dyn_buffer = (char *) realloc(dyn_buffer, length); + char *tmp = realloc(dyn_buffer, length); + + if (tmp == NULL) { + return NULL; + } + + dyn_buffer = tmp; } else { - dyn_buffer = (char *) malloc(length); + dyn_buffer = malloc(length); + + if (dyn_buffer == NULL) { + return NULL; + } } strcpy(dyn_buffer + dyn_buffer_size - 1, input_ptr); diff --git a/src/toxic.c b/src/toxic.c index 2e5c442..38d732b 100644 --- a/src/toxic.c +++ b/src/toxic.c @@ -1106,23 +1106,31 @@ static void parse_args(int argc, char *argv[]) break; case 'f': + if (optarg == NULL) { + queue_init_message("Invalid argument for option: %d", opt); + break; + } + arg_opts.use_custom_data = 1; if (DATA_FILE) { free(DATA_FILE); + DATA_FILE = NULL; } if (BLOCK_FILE) { free(BLOCK_FILE); + BLOCK_FILE = NULL; } DATA_FILE = malloc(strlen(optarg) + 1); - strcpy(DATA_FILE, optarg); if (DATA_FILE == NULL) { exit_toxic_err("failed in parse_args", FATALERR_MEMORY); } + strcpy(DATA_FILE, optarg); + BLOCK_FILE = malloc(strlen(optarg) + strlen("-blocklist") + 1); if (BLOCK_FILE == NULL) {