1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-26 15:03:27 +01:00

Fix a bunch of misc bugs and corner cases

This commit is contained in:
Jfreegman 2015-11-08 03:57:01 -05:00
parent 14a8bdb874
commit fa0e645a79
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
12 changed files with 99 additions and 50 deletions

View File

@ -169,8 +169,9 @@ void terminate_audio()
void read_device_callback(const int16_t* captured, uint32_t size, void* data) void read_device_callback(const int16_t* captured, uint32_t size, void* data)
{ {
TOXAV_ERR_SEND_FRAME error; TOXAV_ERR_SEND_FRAME error;
uint32_t friend_number = *((uint32_t*)data); /* TODO: Or pass an array of call_idx's */ uint32_t friend_number = *((uint32_t *)data); /* TODO: Or pass an array of call_idx's */
int64_t sample_count = CallControl.audio_sample_rate * CallControl.audio_frame_duration / 1000; int64_t sample_count = ((int64_t) CallControl.audio_sample_rate) * \
((int64_t) CallControl.audio_frame_duration) / 1000;
if ( sample_count <= 0 || toxav_audio_send_frame(CallControl.av, friend_number, if ( sample_count <= 0 || toxav_audio_send_frame(CallControl.av, friend_number,
captured, sample_count, captured, sample_count,

View File

@ -114,7 +114,7 @@ int complete_line(ToxWindow *self, const void *list, int n_items, int size)
tmp[ctx->pos] = '\0'; tmp[ctx->pos] = '\0';
const char *s = dir_search ? strchr(tmp, '\"') : strrchr(tmp, ' '); const char *s = dir_search ? strchr(tmp, '\"') : strrchr(tmp, ' ');
char *sub = malloc(strlen(ubuf) + 1); char *sub = calloc(1, strlen(ubuf) + 1);
if (sub == NULL) if (sub == NULL)
exit_toxic_err("failed in complete_line", FATALERR_MEMORY); exit_toxic_err("failed in complete_line", FATALERR_MEMORY);
@ -142,14 +142,14 @@ int complete_line(ToxWindow *self, const void *list, int n_items, int size)
} }
int s_len = strlen(sub); int s_len = strlen(sub);
const char *str;
int n_matches = 0; int n_matches = 0;
char matches[n_items][MAX_STR_SIZE]; char matches[n_items][MAX_STR_SIZE];
int i = 0; int i = 0;
/* put all list matches in matches array */ /* put all list matches in matches array */
for (i = 0; i < n_items; ++i) { for (i = 0; i < n_items; ++i) {
str = &L[i * size]; char str[MAX_CMDNAME_SIZE + 1];
snprintf(str, sizeof(str), "%s", &L[i * size]);
if (strncasecmp(str, sub, s_len) == 0) if (strncasecmp(str, sub, s_len) == 0)
strcpy(matches[n_matches++], str); strcpy(matches[n_matches++], str);
@ -165,10 +165,11 @@ int complete_line(ToxWindow *self, const void *list, int n_items, int size)
char match[MAX_STR_SIZE]; char match[MAX_STR_SIZE];
get_str_match(self, match, matches, n_matches); get_str_match(self, match, matches, n_matches);
size_t match_len = strlen(match);
if (dir_search) { if (dir_search) {
if (n_matches == 1) if (n_matches == 1)
endchrs = char_rfind(match, '.', strlen(match)) ? "\"" : "/"; endchrs = char_rfind(match, '.', match_len) ? "\"" : "/";
else else
endchrs = ""; endchrs = "";
} else if (n_matches > 1) { } else if (n_matches > 1) {
@ -177,18 +178,21 @@ int complete_line(ToxWindow *self, const void *list, int n_items, int size)
/* put match in correct spot in buf and append endchars */ /* put match in correct spot in buf and append endchars */
int n_endchrs = strlen(endchrs); int n_endchrs = strlen(endchrs);
int m_len = strlen(match);
int strt = ctx->pos - s_len; int strt = ctx->pos - s_len;
int diff = m_len - s_len + n_endchrs; int diff = match_len - s_len + n_endchrs;
if (ctx->len + diff >= MAX_STR_SIZE) if (ctx->len + diff >= MAX_STR_SIZE)
return -1; return -1;
char tmpend[MAX_STR_SIZE]; char tmpend[MAX_STR_SIZE];
snprintf(tmpend, sizeof(tmpend), "%s", &ubuf[ctx->pos]); snprintf(tmpend, sizeof(tmpend), "%s", &ubuf[ctx->pos]);
if (match_len + n_endchrs + strlen(tmpend) >= sizeof(ubuf))
return -1;
strcpy(&ubuf[strt], match); strcpy(&ubuf[strt], match);
strcpy(&ubuf[strt + m_len], endchrs); strcpy(&ubuf[strt + match_len], endchrs);
strcpy(&ubuf[strt + m_len + n_endchrs], tmpend); strcpy(&ubuf[strt + match_len + n_endchrs], tmpend);
/* convert to widechar and copy back to original buf */ /* convert to widechar and copy back to original buf */
wchar_t newbuf[MAX_STR_SIZE]; wchar_t newbuf[MAX_STR_SIZE];

View File

@ -795,6 +795,10 @@ static void init_infobox(ToxWindow *self)
int x2, y2; int x2, y2;
getmaxyx(self->window, y2, x2); getmaxyx(self->window, y2, x2);
if (y2 <= 0 || x2 <= 0)
return;
(void) y2; (void) y2;
memset(&ctx->infobox, 0, sizeof(struct infobox)); memset(&ctx->infobox, 0, sizeof(struct infobox));
@ -901,7 +905,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
getyx(self->window, y, x); getyx(self->window, y, x);
getmaxyx(self->window, y2, x2); getmaxyx(self->window, y2, x2);
if (x2 <= 0) if (y2 <= 0 || x2 <= 0)
return; return;
if (self->help->active) { if (self->help->active) {
@ -999,6 +1003,9 @@ static void chat_onDraw(ToxWindow *self, Tox *m)
int x2, y2; int x2, y2;
getmaxyx(self->window, y2, x2); getmaxyx(self->window, y2, x2);
if (y2 <= 0 || x2 <= 0)
return;
ChatContext *ctx = self->chatwin; ChatContext *ctx = self->chatwin;
pthread_mutex_lock(&Winthread.lock); pthread_mutex_lock(&Winthread.lock);
@ -1123,6 +1130,10 @@ static void chat_onInit(ToxWindow *self, Tox *m)
curs_set(1); curs_set(1);
int x2, y2; int x2, y2;
getmaxyx(self->window, y2, x2); getmaxyx(self->window, y2, x2);
if (y2 <= 0 || x2 <= 0)
exit_toxic_err("failed in chat_onInit", FATALERR_CURSES);
self->x = x2; self->x = x2;
/* Init statusbar info */ /* Init statusbar info */

View File

@ -31,6 +31,7 @@
#include "toxic.h" #include "toxic.h"
#include "configdir.h" #include "configdir.h"
#include "misc_tools.h"
/* get the user's home directory */ /* get the user's home directory */
void get_home_dir(char *home, int size) void get_home_dir(char *home, int size)
@ -69,8 +70,8 @@ char *get_user_config_dir(void)
char home[NSS_BUFLEN_PASSWD] = {0}; char home[NSS_BUFLEN_PASSWD] = {0};
get_home_dir(home, sizeof(home)); get_home_dir(home, sizeof(home));
char *user_config_dir; char *user_config_dir = NULL;
size_t len; size_t len = 0;
# if defined(__APPLE__) # if defined(__APPLE__)
len = strlen(home) + strlen("/Library/Application Support") + 1; len = strlen(home) + strlen("/Library/Application Support") + 1;
@ -82,9 +83,9 @@ char *get_user_config_dir(void)
snprintf(user_config_dir, len, "%s/Library/Application Support", home); snprintf(user_config_dir, len, "%s/Library/Application Support", home);
# else /* __APPLE__ */ # else /* __APPLE__ */
const char *tmp; const char *tmp = getenv("XDG_CONFIG_HOME");
if (!(tmp = getenv("XDG_CONFIG_HOME"))) { if (tmp == NULL) {
len = strlen(home) + strlen("/.config") + 1; len = strlen(home) + strlen("/.config") + 1;
user_config_dir = malloc(len); user_config_dir = malloc(len);
@ -98,6 +99,11 @@ char *get_user_config_dir(void)
# endif /* __APPLE__ */ # endif /* __APPLE__ */
if (!file_exists(user_config_dir)) {
free(user_config_dir);
return NULL;
}
return user_config_dir; return user_config_dir;
} }

View File

@ -238,7 +238,7 @@ int load_blocklist(char *path)
memcpy(&tmp, data + i * sizeof(BlockedFriend), sizeof(BlockedFriend)); memcpy(&tmp, data + i * sizeof(BlockedFriend), sizeof(BlockedFriend));
Blocked.list[i].active = true; Blocked.list[i].active = true;
Blocked.list[i].num = i; Blocked.list[i].num = i;
Blocked.list[i].namelength = ntohs(tmp.namelength); Blocked.list[i].namelength = MIN(TOXIC_MAX_NAME_LENGTH, ntohs(tmp.namelength));
memcpy(Blocked.list[i].name, tmp.name, Blocked.list[i].namelength + 1); memcpy(Blocked.list[i].name, tmp.name, Blocked.list[i].namelength + 1);
memcpy(Blocked.list[i].pub_key, tmp.pub_key, TOX_PUBLIC_KEY_SIZE); memcpy(Blocked.list[i].pub_key, tmp.pub_key, TOX_PUBLIC_KEY_SIZE);

View File

@ -210,6 +210,9 @@ void redraw_groupchat_win(ToxWindow *self)
getmaxyx(stdscr, y2, x2); getmaxyx(stdscr, y2, x2);
y2 -= 2; y2 -= 2;
if (y2 <= 0 || x2 <= 0)
return;
if (ctx->sidebar) { if (ctx->sidebar) {
delwin(ctx->sidebar); delwin(ctx->sidebar);
ctx->sidebar = NULL; ctx->sidebar = NULL;
@ -559,7 +562,7 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
getyx(self->window, y, x); getyx(self->window, y, x);
getmaxyx(self->window, y2, x2); getmaxyx(self->window, y2, x2);
if (x2 <= 0) if (x2 <= 0 || y2 <= 0)
return; return;
if (self->help->active) { if (self->help->active) {
@ -649,6 +652,9 @@ static void groupchat_onDraw(ToxWindow *self, Tox *m)
int x2, y2; int x2, y2;
getmaxyx(self->window, y2, x2); getmaxyx(self->window, y2, x2);
if (x2 <= 0 || y2 <= 0)
return;
ChatContext *ctx = self->chatwin; ChatContext *ctx = self->chatwin;
pthread_mutex_lock(&Winthread.lock); pthread_mutex_lock(&Winthread.lock);
@ -713,6 +719,9 @@ static void groupchat_onInit(ToxWindow *self, Tox *m)
int x2, y2; int x2, y2;
getmaxyx(self->window, y2, x2); getmaxyx(self->window, y2, x2);
if (x2 <= 0 || y2 <= 0)
exit_toxic_err("failed in groupchat_onInit", FATALERR_CURSES);
ChatContext *ctx = self->chatwin; ChatContext *ctx = self->chatwin;
ctx->history = subwin(self->window, y2 - CHATBOX_HEIGHT + 1, x2 - SIDEBAR_WIDTH - 1, 0, 0); ctx->history = subwin(self->window, y2 - CHATBOX_HEIGHT + 1, x2 - SIDEBAR_WIDTH - 1, 0, 0);

View File

@ -60,6 +60,9 @@ static void help_init_window(ToxWindow *self, int height, int width)
int y2, x2; int y2, x2;
getmaxyx(stdscr, y2, x2); getmaxyx(stdscr, y2, x2);
if (y2 <= 0 || x2 <= 0)
return;
height = MIN(height, y2); height = MIN(height, y2);
width = MIN(width, x2); width = MIN(width, x2);

View File

@ -202,12 +202,12 @@ size_t write_lookup_data(void *data, size_t size, size_t nmemb, void *user_point
struct Recv_Data *recv_data = (struct Recv_Data *) user_pointer; struct Recv_Data *recv_data = (struct Recv_Data *) user_pointer;
size_t real_size = size * nmemb; size_t real_size = size * nmemb;
if (real_size > MAX_RECV_LOOKUP_DATA_SIZE) if (real_size >= MAX_RECV_LOOKUP_DATA_SIZE)
return 0; return 0;
memcpy(&recv_data->data, data, real_size); memcpy(&recv_data->data, data, real_size);
recv_data->size = real_size; recv_data->size = real_size;
recv_data->data[real_size] = 0; recv_data->data[real_size] = '\0';
return real_size; return real_size;
} }

View File

@ -185,7 +185,7 @@ static void prompt_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
getyx(self->window, y, x); getyx(self->window, y, x);
getmaxyx(self->window, y2, x2); getmaxyx(self->window, y2, x2);
if (x2 <= 0) if (x2 <= 0 || y2 <= 0)
return; return;
/* ignore non-menu related input if active */ /* ignore non-menu related input if active */
@ -256,6 +256,9 @@ static void prompt_onDraw(ToxWindow *self, Tox *m)
int x2, y2; int x2, y2;
getmaxyx(self->window, y2, x2); getmaxyx(self->window, y2, x2);
if (y2 <= 0 || x2 <= 0)
return;
ChatContext *ctx = self->chatwin; ChatContext *ctx = self->chatwin;
pthread_mutex_lock(&Winthread.lock); pthread_mutex_lock(&Winthread.lock);
@ -431,6 +434,10 @@ void prompt_init_statusbar(ToxWindow *self, Tox *m)
{ {
int x2, y2; int x2, y2;
getmaxyx(self->window, y2, x2); getmaxyx(self->window, y2, x2);
if (y2 <= 0 || x2 <= 0)
exit_toxic_err("failed in prompt_init_statusbar", FATALERR_CURSES);
(void) y2; (void) y2;
/* Init statusbar info */ /* Init statusbar info */
@ -488,6 +495,9 @@ static void prompt_onInit(ToxWindow *self, Tox *m)
int y2, x2; int y2, x2;
getmaxyx(self->window, y2, x2); getmaxyx(self->window, y2, x2);
if (y2 <= 0 || x2 <= 0)
exit_toxic_err("failed in prompt_onInit", FATALERR_CURSES);
ChatContext *ctx = self->chatwin; ChatContext *ctx = self->chatwin;
ctx->history = subwin(self->window, y2 - CHATBOX_HEIGHT + 1, x2, 0, 0); ctx->history = subwin(self->window, y2 - CHATBOX_HEIGHT + 1, x2, 0, 0);
ctx->linewin = subwin(self->window, CHATBOX_HEIGHT, x2, y2 - CHATBOX_HEIGHT, 0); ctx->linewin = subwin(self->window, CHATBOX_HEIGHT, x2, y2 - CHATBOX_HEIGHT, 0);

View File

@ -88,6 +88,7 @@ typedef enum _FATAL_ERRS {
FATALERR_PROXY = -10, /* Tox network failed to init using a proxy */ FATALERR_PROXY = -10, /* Tox network failed to init using a proxy */
FATALERR_ENCRYPT = -11, /* Data file encryption failure */ FATALERR_ENCRYPT = -11, /* Data file encryption failure */
FATALERR_TOX_INIT = -12, /* Tox instance failed to initialize */ FATALERR_TOX_INIT = -12, /* Tox instance failed to initialize */
FATALERR_CURSES = -13, /* Unrecoverable Ncurses error */
} FATAL_ERRS; } FATAL_ERRS;
/* Fixes text color problem on some terminals. /* Fixes text color problem on some terminals.

View File

@ -153,9 +153,10 @@ int stop_video_transmission(Call *call, int friend_number)
CallControl.video_bit_rate = 0; CallControl.video_bit_rate = 0;
toxav_bit_rate_set(CallControl.av, friend_number, -1, CallControl.video_bit_rate, NULL); toxav_bit_rate_set(CallControl.av, friend_number, -1, CallControl.video_bit_rate, NULL);
if ( call->vin_idx != -1 ) if ( call->vin_idx != -1 ) {
close_video_device(vdt_input, call->vin_idx); close_video_device(vdt_input, call->vin_idx);
call->vin_idx = -1; call->vin_idx = -1;
}
return 0; return 0;
} }
@ -190,12 +191,12 @@ void callback_recv_video_starting(uint32_t friend_number)
{ {
return; return;
Call* this_call = &CallControl.calls[friend_number]; // Call* this_call = &CallControl.calls[friend_number];
if ( this_call->vout_idx != -1 ) // if ( this_call->vout_idx != -1 )
return; // return;
open_primary_video_device(vdt_output, &this_call->vout_idx); // open_primary_video_device(vdt_output, &this_call->vout_idx);
} }
void callback_recv_video_end(uint32_t friend_number) void callback_recv_video_end(uint32_t friend_number)
{ {
@ -243,37 +244,37 @@ void cmd_video(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[M
{ {
return; // TODO: Fix video return; // TODO: Fix video
const char *error_str; // const char *error_str;
Call* this_call = &CallControl.calls[self->num]; // Call* this_call = &CallControl.calls[self->num];
if ( argc != 0 ) { // if ( argc != 0 ) {
error_str = "Unknown arguments."; // error_str = "Unknown arguments.";
goto on_error; // goto on_error;
} // }
if ( !CallControl.av ) { // if ( !CallControl.av ) {
error_str = "ToxAV not supported!"; // error_str = "ToxAV not supported!";
goto on_error; // goto on_error;
} // }
if ( !self->stb->connection ) { // if ( !self->stb->connection ) {
error_str = "Friend is offline."; // error_str = "Friend is offline.";
goto on_error; // goto on_error;
} // }
if ( !self->is_call ) { // if ( !self->is_call ) {
error_str = "Not in call!"; // error_str = "Not in call!";
goto on_error; // goto on_error;
} // }
if ( this_call->vin_idx == -1 ) // if ( this_call->vin_idx == -1 )
callback_video_starting(self->num); // callback_video_starting(self->num);
else // else
callback_video_end(self->num); // callback_video_end(self->num);
return; // return;
on_error: // on_error:
print_err (self, error_str); // print_err (self, error_str);
} }
void cmd_list_video_devices(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) void cmd_list_video_devices(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])

View File

@ -400,6 +400,9 @@ void on_window_resize(void)
getmaxyx(stdscr, y2, x2); getmaxyx(stdscr, y2, x2);
y2 -= 2; y2 -= 2;
if (y2 <= 0 || x2 <= 0)
return;
size_t i; size_t i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { for (i = 0; i < MAX_WINDOWS_NUM; ++i) {