diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/Messenger.c | 28 | ||||
-rw-r--r-- | core/Messenger.h | 8 |
2 files changed, 23 insertions, 13 deletions
diff --git a/core/Messenger.c b/core/Messenger.c index 86056df8..f77b4491 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -41,6 +41,8 @@ typedef struct { | |||
41 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | 41 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; |
42 | 42 | ||
43 | static uint8_t self_name[MAX_NAME_LENGTH]; | 43 | static uint8_t self_name[MAX_NAME_LENGTH]; |
44 | static uint16_t self_name_length; | ||
45 | |||
44 | static uint8_t *self_userstatus; | 46 | static uint8_t *self_userstatus; |
45 | static uint16_t self_userstatus_len; | 47 | static uint16_t self_userstatus_len; |
46 | 48 | ||
@@ -191,13 +193,16 @@ int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length) | |||
191 | return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1); | 193 | return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1); |
192 | } | 194 | } |
193 | 195 | ||
194 | /* send a name packet to friendnumber */ | 196 | /* send a name packet to friendnumber |
195 | static int m_sendname(int friendnumber, uint8_t * name) | 197 | length is the length with the NULL terminator*/ |
198 | static int m_sendname(int friendnumber, uint8_t * name, uint16_t length) | ||
196 | { | 199 | { |
200 | if(length > MAX_NAME_LENGTH || length == 0) | ||
201 | return 0; | ||
197 | uint8_t temp[MAX_NAME_LENGTH + 1]; | 202 | uint8_t temp[MAX_NAME_LENGTH + 1]; |
198 | memcpy(temp + 1, name, MAX_NAME_LENGTH); | 203 | memcpy(temp + 1, name, length); |
199 | temp[0] = PACKET_ID_NICKNAME; | 204 | temp[0] = PACKET_ID_NICKNAME; |
200 | return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, MAX_NAME_LENGTH + 1); | 205 | return write_cryptpacket(friendlist[friendnumber].crypt_connection_id, temp, length + 1); |
201 | } | 206 | } |
202 | 207 | ||
203 | /* set the name of a friend | 208 | /* set the name of a friend |
@@ -213,13 +218,16 @@ static int setfriendname(int friendnumber, uint8_t * name) | |||
213 | 218 | ||
214 | /* Set our nickname | 219 | /* Set our nickname |
215 | name must be a string of maximum MAX_NAME_LENGTH length. | 220 | name must be a string of maximum MAX_NAME_LENGTH length. |
221 | length must be at least 1 byte | ||
222 | length is the length of name with the NULL terminator | ||
216 | return 0 if success | 223 | return 0 if success |
217 | return -1 if failure */ | 224 | return -1 if failure */ |
218 | int setname(uint8_t * name, uint16_t length) | 225 | int setname(uint8_t * name, uint16_t length) |
219 | { | 226 | { |
220 | if (length > MAX_NAME_LENGTH) | 227 | if (length > MAX_NAME_LENGTH || length == 0) |
221 | return -1; | 228 | return -1; |
222 | memcpy(self_name, name, length); | 229 | memcpy(self_name, name, length); |
230 | self_name_length = length; | ||
223 | uint32_t i; | 231 | uint32_t i; |
224 | for (i = 0; i < numfriends; ++i) | 232 | for (i = 0; i < numfriends; ++i) |
225 | friendlist[i].name_sent = 0; | 233 | friendlist[i].name_sent = 0; |
@@ -388,7 +396,7 @@ static void doFriends() | |||
388 | } | 396 | } |
389 | while (friendlist[i].status == 4) { /* friend is online */ | 397 | while (friendlist[i].status == 4) { /* friend is online */ |
390 | if (friendlist[i].name_sent == 0) { | 398 | if (friendlist[i].name_sent == 0) { |
391 | if (m_sendname(i, self_name)) | 399 | if (m_sendname(i, self_name, self_name_length)) |
392 | friendlist[i].name_sent = 1; | 400 | friendlist[i].name_sent = 1; |
393 | } | 401 | } |
394 | if (friendlist[i].userstatus_sent == 0) { | 402 | if (friendlist[i].userstatus_sent == 0) { |
@@ -399,12 +407,12 @@ static void doFriends() | |||
399 | if (len > 0) { | 407 | if (len > 0) { |
400 | switch (temp[0]) { | 408 | switch (temp[0]) { |
401 | case PACKET_ID_NICKNAME: { | 409 | case PACKET_ID_NICKNAME: { |
402 | if (len != MAX_NAME_LENGTH + 1) | 410 | if (len >= MAX_NAME_LENGTH + 1 || len == 1) |
403 | break; | 411 | break; |
404 | if(friend_namechange_isset) | 412 | if(friend_namechange_isset) |
405 | friend_namechange(i, temp + 1, MAX_NAME_LENGTH); /* TODO: use the actual length */ | 413 | friend_namechange(i, temp + 1, len - 1); |
406 | memcpy(friendlist[i].name, temp + 1, MAX_NAME_LENGTH); | 414 | memcpy(friendlist[i].name, temp + 1, len - 1); |
407 | friendlist[i].name[MAX_NAME_LENGTH - 1] = 0; /* make sure the NULL terminator is present. */ | 415 | friendlist[i].name[len - 2] = 0; /* make sure the NULL terminator is present. */ |
408 | break; | 416 | break; |
409 | } | 417 | } |
410 | case PACKET_ID_USERSTATUS: { | 418 | case PACKET_ID_USERSTATUS: { |
diff --git a/core/Messenger.h b/core/Messenger.h index 564e29f2..7263901c 100644 --- a/core/Messenger.h +++ b/core/Messenger.h | |||
@@ -85,9 +85,11 @@ int m_friendstatus(int friendnumber); | |||
85 | int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length); | 85 | int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length); |
86 | 86 | ||
87 | /* Set our nickname | 87 | /* Set our nickname |
88 | name must be a string of maximum MAX_NAME_LENGTH length. | 88 | name must be a string of maximum MAX_NAME_LENGTH length. |
89 | return 0 if success | 89 | length must be at least 1 byte |
90 | return -1 if failure */ | 90 | length is the length of name with the NULL terminator |
91 | return 0 if success | ||
92 | return -1 if failure */ | ||
91 | int setname(uint8_t *name, uint16_t length); | 93 | int setname(uint8_t *name, uint16_t length); |
92 | 94 | ||
93 | /* get name of friendnumber | 95 | /* get name of friendnumber |