From bfeea477748f35e53b508767017c90526fa709c8 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Sat, 31 Aug 2013 02:22:07 -0400 Subject: [PATCH] fix buffer overflows and format issues --- src/chat.c | 15 ++++++++------- src/windows.c | 4 ++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/chat.c b/src/chat.c index 2dd6950..7ec3631 100644 --- a/src/chat.c +++ b/src/chat.c @@ -72,6 +72,9 @@ static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uin if (ctx->friendnum != num) return; + uint8_t nick[TOX_MAX_NAME_LENGTH] = {0}; + tox_getname(m, num, (uint8_t *) &nick); + action[len - 1] = '\0'; wattron(ctx->history, COLOR_PAIR(2)); @@ -79,7 +82,7 @@ static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uin wattroff(ctx->history, COLOR_PAIR(2)); wattron(ctx->history, COLOR_PAIR(5)); - wprintw(ctx->history, "%s\n", action); + wprintw(ctx->history, "* %s %s\n", nick, action); wattroff(ctx->history, COLOR_PAIR(5)); self->blink = true; @@ -187,7 +190,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) #else if (isprint(key)) { #endif - if (ctx->pos != sizeof(ctx->line) - 1) { + if (ctx->pos < MAX_STR_SIZE) { mvwaddstr(self->window, y, x, wc_to_char(key)); ctx->line[ctx->pos++] = key; ctx->line[ctx->pos] = L'\0'; @@ -291,15 +294,13 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) wattroff(ctx->history, COLOR_PAIR(2)); uint8_t selfname[TOX_MAX_NAME_LENGTH]; - int len = tox_getselfname(m, selfname, sizeof(selfname)); - char msg[MAX_STR_SIZE - len - 4]; - snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t *) selfname, action); + tox_getselfname(m, selfname, sizeof(selfname)); wattron(ctx->history, COLOR_PAIR(5)); - wprintw(ctx->history, msg); + wprintw(ctx->history, "* %s %s\n", selfname, action); wattroff(ctx->history, COLOR_PAIR(5)); - if (tox_sendaction(m, ctx->friendnum, (uint8_t *) msg, strlen(msg) + 1) < 0) { + if (tox_sendaction(m, ctx->friendnum, (uint8_t *) action, strlen(action) + 1) == 0) { wattron(ctx->history, COLOR_PAIR(3)); wprintw(ctx->history, " * Failed to send action\n"); wattroff(ctx->history, COLOR_PAIR(3)); diff --git a/src/windows.c b/src/windows.c index e477928..ec6c04a 100644 --- a/src/windows.c +++ b/src/windows.c @@ -120,7 +120,7 @@ int add_window(Tox *m, ToxWindow w) void del_window(ToxWindow *w) { active_window = windows; // Go to prompt screen - + delwin(w->window); memset(w, 0, sizeof(ToxWindow)); @@ -187,7 +187,7 @@ static void draw_bar() move(LINES - 1, 0); attron(COLOR_PAIR(4) | A_BOLD); - printw(" TOXIC " TOXICVER "|"); + printw(" TOXIC " TOXICVER " |"); attroff(COLOR_PAIR(4) | A_BOLD); int i;