diff --git a/toxcore/group.c b/toxcore/group.c index 826572ae..8c3c7431 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -1182,12 +1182,12 @@ int group_title_send(const Group_Chats *g_c, int groupnumber, const uint8_t *tit } /* Get group title from groupnumber and put it in title. - * title needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. + * title needs to be a valid memory location with a max_length size of at least MAX_NAME_LENGTH (128) bytes. * - * return length of title if success. + * return length of copied title if success. * return -1 if failure. */ -int group_title_get(const Group_Chats *g_c, int groupnumber, uint8_t *title) +int group_title_get(const Group_Chats *g_c, int groupnumber, uint8_t *title, uint32_t max_length) { Group_c *g = get_group_c(g_c, groupnumber); @@ -1197,8 +1197,11 @@ int group_title_get(const Group_Chats *g_c, int groupnumber, uint8_t *title) if (g->title_len == 0 || g->title_len > MAX_NAME_LENGTH) return -1; - memcpy(title, g->title, g->title_len); - return g->title_len; + if (max_length > g->title_len) + max_length = g->title_len; + + memcpy(title, g->title, max_length); + return max_length; } static void handle_friend_invite_packet(Messenger *m, int32_t friendnumber, const uint8_t *data, uint16_t length) diff --git a/toxcore/group.h b/toxcore/group.h index c4a513b7..f4d93c9a 100644 --- a/toxcore/group.h +++ b/toxcore/group.h @@ -235,12 +235,12 @@ int group_title_send(const Group_Chats *g_c, int groupnumber, const uint8_t *tit /* Get group title from groupnumber and put it in title. - * title needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. + * title needs to be a valid memory location with a max_length size of at least MAX_NAME_LENGTH (128) bytes. * - * return length of title if success. + * return length of copied title if success. * return -1 if failure. */ -int group_title_get(const Group_Chats *g_c, int groupnumber, uint8_t *title); +int group_title_get(const Group_Chats *g_c, int groupnumber, uint8_t *title, uint32_t max_length); /* Return the number of peers in the group chat on success. * return -1 on failure diff --git a/toxcore/tox.c b/toxcore/tox.c index 45014e63..f73ac285 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -694,15 +694,15 @@ int tox_group_set_title(Tox *tox, int groupnumber, const uint8_t *title, uint8_t } /* Get group title from groupnumber and put it in title. - * title needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. + * title needs to be a valid memory location with a max_length size of at least MAX_NAME_LENGTH (128) bytes. * - * return length of title if success. + * return length of copied title if success. * return -1 if failure. */ -int tox_group_get_title(Tox *tox, int groupnumber, uint8_t *title) +int tox_group_get_title(Tox *tox, int groupnumber, uint8_t *title, uint32_t max_length) { Messenger *m = tox; - return group_title_get(m->group_chat_object, groupnumber, title); + return group_title_get(m->group_chat_object, groupnumber, title, max_length); } /* Check if the current peernumber corresponds to ours. diff --git a/toxcore/tox.h b/toxcore/tox.h index 0902e166..4caf4d26 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -529,12 +529,12 @@ int tox_group_action_send(Tox *tox, int groupnumber, const uint8_t *action, uint int tox_group_set_title(Tox *tox, int groupnumber, const uint8_t *title, uint8_t length); /* Get group title from groupnumber and put it in title. - * title needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. + * title needs to be a valid memory location with a max_length size of at least MAX_NAME_LENGTH (128) bytes. * - * return length of title if success. + * return length of copied title if success. * return -1 if failure. */ -int tox_group_get_title(Tox *tox, int groupnumber, uint8_t *title); +int tox_group_get_title(Tox *tox, int groupnumber, uint8_t *title, uint32_t max_length); /* Check if the current peernumber corresponds to ours. *