diff --git a/src/chat.c b/src/chat.c index fd1b885..f1ec13c 100644 --- a/src/chat.c +++ b/src/chat.c @@ -316,8 +316,14 @@ static void execute(ToxWindow *self, ChatContext *ctx, StatusBar *statusbar, Tox return; } - nick++; - tox_setname(m, nick, strlen(nick) + 1); + int len = strlen(++nick); + + if (len > TOXIC_MAX_NAME_LENGTH) { + nick[TOXIC_MAX_NAME_LENGTH] = L'\0'; + len = TOXIC_MAX_NAME_LENGTH; + } + + tox_setname(m, nick, len+1); prompt_update_nick(self->prompt, nick); wprintw(ctx->history, "Nickname set to: %s\n", nick); } diff --git a/src/prompt.c b/src/prompt.c index 420bfef..f65d08a 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -384,10 +384,19 @@ void cmd_nick(ToxWindow *self, Tox *m, int argc, char **argv) return; } - if (nick[0] == '\"') - nick[strlen(++nick)-1] = L'\0'; + int len = strlen(nick); - tox_setname(m, nick, strlen(nick) + 1); + if (nick[0] == '\"') { + ++nick; + nick[--len-1] = L'\0'; + } + + if (len > TOXIC_MAX_NAME_LENGTH) { + nick[TOXIC_MAX_NAME_LENGTH] = L'\0'; + len = TOXIC_MAX_NAME_LENGTH; + } + + tox_setname(m, nick, len+1); prompt_update_nick(self, nick); store_data(m, DATA_FILE); diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 41f6b14..27c786b 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -16,12 +16,9 @@ #define MAX_FRIENDS_NUM 100 #define MAX_STR_SIZE 256 #define KEY_SIZE_BYTES 32 - -/* number of permanent default windows */ -#define N_DEFAULT_WINS 3 - +#define TOXIC_MAX_NAME_LENGTH 30 /* Not to be confused with TOX_MAX_NAME_LENGTH */ +#define N_DEFAULT_WINS 3 /* number of permanent default windows */ #define UNKNOWN_NAME "Unknown" - #define EXIT_SUCCESS 0 #define EXIT_FAILURE 1 diff --git a/src/windows.c b/src/windows.c index 7ff5c8d..f61768b 100644 --- a/src/windows.c +++ b/src/windows.c @@ -81,6 +81,11 @@ void on_action(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) { + if (length >= TOXIC_MAX_NAME_LENGTH) { /* length includes null byte */ + string[TOXIC_MAX_NAME_LENGTH] = L'\0'; + length = TOXIC_MAX_NAME_LENGTH+1; + } + int i; for (i = 0; i < MAX_WINDOWS_NUM; ++i) {