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

A few small fixes

- Fix race condition in draw_peer()
- Handle realloc_peer_list() error
- Remove dead code in cmd_conference()
- Reduce scope of a few variable variables
- Fix possible buffer truncation in api.c
This commit is contained in:
jfreegman 2020-11-19 14:21:45 -05:00
parent 0554bf0240
commit 312b38d253
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
9 changed files with 40 additions and 41 deletions

View File

@ -175,36 +175,36 @@ void cmd_run(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX
void invoke_autoruns(WINDOW *window, ToxWindow *self)
{
struct dirent *dir;
char abspath_buf[PATH_MAX + 1], err_buf[PATH_MAX + 1];
size_t path_len;
DIR *d;
FILE *fp;
char abspath_buf[PATH_MAX + 256];
char err_buf[PATH_MAX + 128];
if (user_settings->autorun_path[0] == '\0') {
return;
}
d = opendir(user_settings->autorun_path);
DIR *d = opendir(user_settings->autorun_path);
if (d == NULL) {
snprintf(err_buf, PATH_MAX + 1, "Autorun path does not exist: %s", user_settings->autorun_path);
snprintf(err_buf, sizeof(err_buf), "Autorun path does not exist: %s", user_settings->autorun_path);
api_display(err_buf);
return;
}
struct dirent *dir = NULL;
cur_window = window;
self_window = self;
while ((dir = readdir(d)) != NULL) {
path_len = strlen(dir->d_name);
size_t path_len = strlen(dir->d_name);
if (!strcmp(dir->d_name + path_len - 3, ".py")) {
snprintf(abspath_buf, PATH_MAX + 1, "%s%s", user_settings->autorun_path, dir->d_name);
fp = fopen(abspath_buf, "r");
snprintf(abspath_buf, sizeof(abspath_buf), "%s%s", user_settings->autorun_path, dir->d_name);
FILE *fp = fopen(abspath_buf, "r");
if (fp == NULL) {
snprintf(err_buf, PATH_MAX + 1, "Invalid path: %s", abspath_buf);
snprintf(err_buf, sizeof(err_buf), "Invalid path: %s", abspath_buf);
api_display(err_buf);
continue;
}

View File

@ -916,7 +916,7 @@ void stop_current_call(ToxWindow *self)
*/
static void realloc_calls(uint32_t n)
{
if (n <= 0) {
if (n == 0) {
free(CallControl.calls);
CallControl.calls = NULL;
return;

View File

@ -504,8 +504,6 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, uint32_t friendnum, uint
return;
}
char msg[MAX_STR_SIZE];
switch (control) {
case TOX_FILE_CONTROL_RESUME: {
/* transfer is accepted */
@ -531,6 +529,7 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, uint32_t friendnum, uint
}
case TOX_FILE_CONTROL_CANCEL: {
char msg[MAX_STR_SIZE];
snprintf(msg, sizeof(msg), "File transfer for '%s' was aborted.", ft->file_name);
close_file_transfer(self, m, ft, -1, msg, notif_error);
break;

View File

@ -160,7 +160,7 @@ static void init_conference_logging(ToxWindow *self, Tox *m, uint32_t conference
char conference_id[TOX_CONFERENCE_ID_SIZE];
tox_conference_get_id(m, conferencenum, (uint8_t *) conference_id);
if (log_init(ctx->log, conferences[conferencenum].title, my_id, conference_id, LOG_TYPE_CHAT) != 0) {
if (log_init(ctx->log, conferences[self->num].title, my_id, conference_id, LOG_TYPE_CHAT) != 0) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Warning: Log failed to initialize.");
return;
}
@ -656,7 +656,11 @@ static void update_peer_list(Tox *m, uint32_t conferencenum, uint32_t num_peers,
memcpy(old_peer_list, chat->peer_list, old_num_peers * sizeof(ConferencePeer));
}
realloc_peer_list(chat, num_peers);
if (realloc_peer_list(chat, num_peers) != 0) {
free(old_peer_list);
fprintf(stderr, "Warning: realloc_peer_list() failed in update_peer_list()\n");
return;
}
for (uint32_t i = 0; i < num_peers; ++i) {
ConferencePeer *peer = &chat->peer_list[i];
@ -971,15 +975,9 @@ static void draw_peer(ToxWindow *self, Tox *m, ChatContext *ctx, uint32_t i)
const uint32_t peernum = conferences[self->num].name_list[peer_idx].peernum;
const bool is_self = tox_conference_peer_number_is_ours(m, self->num, peernum, NULL);
const bool audio = conferences[self->num].audio_enabled;
pthread_mutex_unlock(&Winthread.lock);
/* truncate nick to fit in side panel without modifying list */
char tmpnick[TOX_MAX_NAME_LENGTH];
int maxlen = SIDEBAR_WIDTH - 2 - 2 * audio;
if (audio) {
#ifdef AUDIO
pthread_mutex_lock(&Winthread.lock);
const ConferencePeer *peer = peer_in_conference(self->num, peernum);
const bool audio_active = is_self
? !timed_out(conferences[self->num].last_sent_audio, 2)
@ -996,8 +994,14 @@ static void draw_peer(ToxWindow *self, Tox *m, ChatContext *ctx, uint32_t i)
wattroff(ctx->sidebar, aud_attr);
waddch(ctx->sidebar, ' ');
#endif
} else {
pthread_mutex_unlock(&Winthread.lock);
}
/* truncate nick to fit in side panel without modifying list */
char tmpnick[TOX_MAX_NAME_LENGTH];
int maxlen = SIDEBAR_WIDTH - 2 - 2 * audio;
pthread_mutex_lock(&Winthread.lock);
memcpy(tmpnick, &conferences[self->num].name_list[peer_idx].name, maxlen);
pthread_mutex_unlock(&Winthread.lock);

View File

@ -807,7 +807,7 @@ static void delete_blocked_friend(uint32_t bnum)
/* deletes contact from friendlist and puts in blocklist */
void block_friend(Tox *m, uint32_t fnum)
{
if (Friends.num_friends <= 0) {
if (Friends.num_friends == 0) {
return;
}

View File

@ -389,9 +389,6 @@ void cmd_conference(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*ar
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Audio support disabled by compile-time option.");
return;
#endif
} else {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Unknown conference type %d", type);
return;
}
if (init_conference_win(m, conferencenum, type, NULL, 0) == -1) {

View File

@ -368,10 +368,10 @@ int rename_logfile(const char *src, const char *dest, const char *selfkey, const
if (rename(newpath, new_backup) != 0) {
goto on_error;
}
} else {
if (rename(oldpath, newpath) != 0) {
goto on_error;
}
}
if (rename(oldpath, newpath) != 0) {
goto on_error;
}
if (log != NULL) {

View File

@ -114,25 +114,23 @@ static PyObject *python_api_get_status_message(PyObject *self, PyObject *args)
static PyObject *python_api_get_all_friends(PyObject *self, PyObject *args)
{
size_t i, ii;
FriendsList friends;
PyObject *cur, *ret;
char pubkey_buf[TOX_PUBLIC_KEY_SIZE * 2 + 1];
char pubkey_buf[TOX_PUBLIC_KEY_SIZE * 2 + 1];
if (!PyArg_ParseTuple(args, "")) {
return NULL;
}
friends = api_get_friendslist();
ret = PyList_New(0);
PyObject *ret = PyList_New(0);
for (i = 0; i < friends.num_friends; i++) {
for (ii = 0; ii < TOX_PUBLIC_KEY_SIZE; ii++) {
for (size_t i = 0; i < friends.num_friends; i++) {
for (size_t ii = 0; ii < TOX_PUBLIC_KEY_SIZE; ii++) {
snprintf(pubkey_buf + ii * 2, 3, "%02X", friends.list[i].pub_key[ii] & 0xff);
}
pubkey_buf[TOX_PUBLIC_KEY_SIZE * 2] = '\0';
cur = Py_BuildValue("(s,s)", friends.list[i].name, pubkey_buf);
PyObject *cur = Py_BuildValue("(s,s)", friends.list[i].name, pubkey_buf);
PyList_Append(ret, cur);
}
@ -264,14 +262,14 @@ PyMODINIT_FUNC PyInit_toxic_api(void)
void terminate_python(void)
{
struct python_registered_func *cur, *old;
if (python_commands.name != NULL) {
free(python_commands.name);
}
struct python_registered_func *cur = NULL;
for (cur = python_commands.next; cur != NULL;) {
old = cur;
struct python_registered_func *old = cur;
cur = cur->next;
free(old->name);
free(old);

View File

@ -235,11 +235,12 @@ void *event_loop(void *p)
UNUSED_VAR(p); /* DINDUNOTHIN */
XEvent event;
int pending;
while (Xtra.display) {
/* NEEDMOEVENTSFODEMPROGRAMS */
int pending = 0;
XLockDisplay(Xtra.display);
if ((pending = XPending(Xtra.display))) {