From c4702985a502eb80c3eae37f2b5b38fdb7e385e5 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 23 Sep 2013 04:30:24 -0400 Subject: [PATCH 1/2] made setfriendname part of public api --- toxcore/Messenger.c | 6 +++--- toxcore/Messenger.h | 7 +++++++ toxcore/tox.c | 14 ++++++++++++++ toxcore/tox.h | 10 ++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index b5da12ba..9601f2a2 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -352,13 +352,13 @@ static int m_sendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t le * return 0 if success. * return -1 if failure. */ -static int setfriendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t len) +int setfriendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t length) { if (friend_not_valid(m, friendnumber)) return -1; - m->friendlist[friendnumber].name_length = len; - memcpy(m->friendlist[friendnumber].name, name, len); + m->friendlist[friendnumber].name_length = length; + memcpy(m->friendlist[friendnumber].name, name, length); return 0; } diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 2b29896c..1f0361b1 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -246,6 +246,13 @@ uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, ui */ int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length); +/* Set the name and name_length of a friend. + * + * return 0 if success. + * return -1 if failure. + */ +int setfriendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t length); + /* Set our nickname. * name must be a string of maximum MAX_NAME_LENGTH length. * length must be at least 1 byte. diff --git a/toxcore/tox.c b/toxcore/tox.c index fa53e693..1d7118be 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -153,6 +153,20 @@ int tox_sendaction(void *tox, int friendnumber, uint8_t *action, uint32_t length return m_sendaction(m, friendnumber, action, length); } +/* Set friendnumber's nickname. + * name must be a string of maximum MAX_NAME_LENGTH length. + * length must be at least 1 byte. + * length is the length of name with the NULL terminator. + * + * return 0 if success. + * return -1 if failure. + */ +int tox_setfriendname(void *tox, int friendnumber, uint8_t *name, uint16_t length) +{ + Messenger *m = tox; + return setfriendname(m, friendnumber, name, length); +} + /* Set our nickname. * name must be a string of maximum MAX_NAME_LENGTH length. * length must be at least 1 byte. diff --git a/toxcore/tox.h b/toxcore/tox.h index 9c810418..db3621dc 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -214,6 +214,16 @@ uint32_t tox_sendmessage_withid(Tox *tox, int friendnumber, uint32_t theid, uint */ int tox_sendaction(Tox *tox, int friendnumber, uint8_t *action, uint32_t length); +/* Set friendnumber's nickname. + * name must be a string of maximum MAX_NAME_LENGTH length. + * length must be at least 1 byte. + * length is the length of name with the NULL terminator. + * + * return 0 if success. + * return -1 if failure. + */ +int tox_setfriendname(void *tox, int friendnumber, uint8_t *name, uint16_t length); + /* Set our nickname. * name must be a string of maximum MAX_NAME_LENGTH length. * length must be at least 1 byte. From 23e03b0ba94df9640a978529edaf75b412dbf8a2 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 23 Sep 2013 15:40:25 -0400 Subject: [PATCH 2/2] check length of name --- toxcore/Messenger.c | 3 +++ toxcore/Messenger.h | 3 +++ 2 files changed, 6 insertions(+) diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 9601f2a2..0efe20ce 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -357,6 +357,9 @@ int setfriendname(Messenger *m, int friendnumber, uint8_t *name, uint16_t length if (friend_not_valid(m, friendnumber)) return -1; + if (length > MAX_NAME_LENGTH || length == 0) + return -1; + m->friendlist[friendnumber].name_length = length; memcpy(m->friendlist[friendnumber].name, name, length); return 0; diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 1f0361b1..0ff14de4 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -247,6 +247,9 @@ uint32_t m_sendmessage_withid(Messenger *m, int friendnumber, uint32_t theid, ui int m_sendaction(Messenger *m, int friendnumber, uint8_t *action, uint32_t length); /* Set the name and name_length of a friend. + * name must be a string of maximum MAX_NAME_LENGTH length. + * length must be at least 1 byte. + * length is the length of name with the NULL terminator. * * return 0 if success. * return -1 if failure.