diff --git a/src/command/command.c b/src/command/command.c index b09c91fb..49f84ad0 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -1169,7 +1169,6 @@ cmd_autocomplete(char *input, int *size) { int i = 0; char *found = NULL; - char *auto_msg = NULL; char inp_cpy[*size]; // autocomplete command @@ -1180,8 +1179,7 @@ cmd_autocomplete(char *input, int *size) inp_cpy[i] = '\0'; found = autocomplete_complete(commands_ac, inp_cpy); if (found != NULL) { - auto_msg = (char *) malloc(strlen(found) + 1); - strcpy(auto_msg, found); + char *auto_msg = strdup(found); inp_replace_input(input, auto_msg, size); free(auto_msg); free(found); diff --git a/src/profanity.c b/src/profanity.c index 31684217..cc95875a 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -192,10 +192,10 @@ process_input(char *inp) // habdle command if input starts with a '/' } else if (inp[0] == '/') { - char inp_cpy[strlen(inp) + 1]; - strcpy(inp_cpy, inp); + char *inp_cpy = strdup(inp); char *command = strtok(inp_cpy, " "); result = cmd_execute(command, inp); + free(inp_cpy); // call a default handler if input didn't start with '/' } else { diff --git a/src/tools/autocomplete.c b/src/tools/autocomplete.c index aa96fcc6..e82103ac 100644 --- a/src/tools/autocomplete.c +++ b/src/tools/autocomplete.c @@ -158,10 +158,7 @@ autocomplete_complete(Autocomplete ac, gchar *search_str) // first search attempt if (ac->last_found == NULL) { - ac->search_str = - (gchar *) malloc((strlen(search_str) + 1) * sizeof(gchar)); - strcpy(ac->search_str, search_str); - + ac->search_str = strdup(search_str); found = _search_from(ac, ac->items); return found; @@ -188,12 +185,14 @@ autocomplete_param_with_func(char *input, int *size, char *command, autocomplete_func func) { char *found = NULL; - char *auto_msg = NULL; + GString *auto_msg = NULL; + char *result = NULL; char inp_cpy[*size]; int i; char command_cpy[strlen(command) + 2]; sprintf(command_cpy, "%s ", command); int len = strlen(command_cpy); + if ((strncmp(input, command_cpy, len) == 0) && (*size > len)) { for(i = len; i < *size; i++) { inp_cpy[i-len] = input[i]; @@ -201,14 +200,15 @@ autocomplete_param_with_func(char *input, int *size, char *command, inp_cpy[(*size) - len] = '\0'; found = func(inp_cpy); if (found != NULL) { - auto_msg = (char *) malloc(len + strlen(found) + 1); - strcpy(auto_msg, command_cpy); - strcat(auto_msg, found); + auto_msg = g_string_new(command_cpy); + g_string_append(auto_msg, found); free(found); + result = auto_msg->str; + g_string_free(auto_msg, FALSE); } } - return auto_msg; + return result; } char * @@ -216,7 +216,8 @@ autocomplete_param_with_ac(char *input, int *size, char *command, Autocomplete ac) { char *found = NULL; - char *auto_msg = NULL; + GString *auto_msg = NULL; + char *result = NULL; char inp_cpy[*size]; int i; char *command_cpy = malloc(strlen(command) + 2); @@ -229,15 +230,16 @@ autocomplete_param_with_ac(char *input, int *size, char *command, inp_cpy[(*size) - len] = '\0'; found = autocomplete_complete(ac, inp_cpy); if (found != NULL) { - auto_msg = (char *) malloc(len + strlen(found) + 1); - strcpy(auto_msg, command_cpy); - strcat(auto_msg, found); + auto_msg = g_string_new(command_cpy); + g_string_append(auto_msg, found); free(found); + result = auto_msg->str; + g_string_free(auto_msg, FALSE); } } free(command_cpy); - return auto_msg; + return result; } char * diff --git a/src/ui/core.c b/src/ui/core.c index 4d917563..c926d633 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -869,18 +869,15 @@ static void _ui_print_system_msg_from_recipient(const char * const from, const char *message) { int num = 0; - char from_cpy[strlen(from) + 1]; - char *bare_jid; if (from == NULL || message == NULL) return; - strcpy(from_cpy, from); - bare_jid = strtok(from_cpy, "/"); + Jid *jid = jid_create(from); - ProfWin *window = wins_get_by_recipient(bare_jid); + ProfWin *window = wins_get_by_recipient(jid->barejid); if (window == NULL) { - window = wins_new(bare_jid, WIN_CHAT); + window = wins_new(jid->barejid, WIN_CHAT); if (window != NULL) { num = wins_get_num(window); status_bar_active(num); @@ -892,12 +889,14 @@ _ui_print_system_msg_from_recipient(const char * const from, const char *message } win_print_time(window, '-'); - wprintw(window->win, "*%s %s\n", bare_jid, message); + wprintw(window->win, "*%s %s\n", jid->barejid, message); // this is the current window if (wins_is_current(window)) { wins_update_virtual_current(); } + + jid_destroy(jid); } static void diff --git a/src/ui/inputwin.c b/src/ui/inputwin.c index b7c5270d..5b5fb381 100644 --- a/src/ui/inputwin.c +++ b/src/ui/inputwin.c @@ -226,7 +226,7 @@ static void _inp_replace_input(char *input, const char * const new_input, int *size) { int display_size; - strcpy(input, new_input); + strncpy(input, new_input, INP_WIN_MAX); *size = strlen(input); display_size = g_utf8_strlen(input, *size); inp_win_reset(); diff --git a/src/ui/notifier.c b/src/ui/notifier.c index 959bf8f3..e36edf69 100644 --- a/src/ui/notifier.c +++ b/src/ui/notifier.c @@ -193,14 +193,14 @@ _notify(const char * const message, int timeout, nid.uVersion = NOTIFYICON_VERSION; //nid.uCallbackMessage = WM_MYMESSAGE; nid.hIcon = LoadIcon(NULL, IDI_APPLICATION); - strcpy(nid.szTip, "Tray Icon"); + strncpy(nid.szTip, "Tray Icon", 10); nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; Shell_NotifyIcon(NIM_ADD, &nid); // For a Ballon Tip nid.uFlags = NIF_INFO; - strcpy(nid.szInfoTitle, "Profanity"); // Title - strcpy(nid.szInfo, message); // Copy Tip + strncpy(nid.szInfoTitle, "Profanity", 10); // Title + strncpy(nid.szInfo, message, 256); // Copy Tip nid.uTimeout = timeout; // 3 Seconds nid.dwInfoFlags = NIIF_INFO; diff --git a/src/ui/statusbar.c b/src/ui/statusbar.c index eb39467b..bbb3b781 100644 --- a/src/ui/statusbar.c +++ b/src/ui/statusbar.c @@ -277,8 +277,7 @@ _status_bar_print_message(const char * const msg) if (message != NULL) { free(message); } - message = (char *) malloc(strlen(msg) + 1); - strcpy(message, msg); + message = strdup(msg); mvwprintw(status_bar, 0, 10, message); int cols = getmaxx(stdscr); diff --git a/tests/test_roster_list.c b/tests/test_roster_list.c index a5612072..5c767531 100644 --- a/tests/test_roster_list.c +++ b/tests/test_roster_list.c @@ -156,8 +156,7 @@ void find_first_exists(void **state) roster_add("Dave", NULL, NULL, NULL, FALSE); roster_add("Bob", NULL, NULL, NULL, FALSE); - char *search = (char *) malloc(2 * sizeof(char)); - strcpy(search, "B"); + char *search = strdup("B"); char *result = roster_find_contact(search); assert_string_equal("Bob", result);