Limit number of group chats to 65536.

By changing numchats from uint32_t to uint16_t. This is done in PGC. This
PR is making that change in master to reduce the diff in the PGC branch.

Also:
* Inverted groupnumber_not_valid and renamed to is_groupnumber_valid.
* Renamed realloc_groupchats to realloc_conferences and made it return bool.
* Added setup_conference function that currently just zeroes the
  conference structure but later will initialise more values.
* Made some `i` iterator variables local to the for-loop using
  for-init-decl. This is also done in PGC.
This commit is contained in:
iphydf 2018-07-12 23:58:24 +00:00
parent 2377cac94b
commit 3d5fd9c2d0
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
2 changed files with 45 additions and 62 deletions

View File

@ -33,72 +33,74 @@
#include "mono_time.h"
#include "util.h"
/* return 1 if the groupnumber is not valid.
* return 0 if the groupnumber is valid.
/* return false if the groupnumber is not valid.
* return true if the groupnumber is valid.
*/
static uint8_t groupnumber_not_valid(const Group_Chats *g_c, uint32_t groupnumber)
static bool is_groupnumber_valid(const Group_Chats *g_c, uint32_t groupnumber)
{
if (groupnumber >= g_c->num_chats) {
return 1;
return false;
}
if (g_c->chats == nullptr) {
return 1;
return false;
}
if (g_c->chats[groupnumber].status == GROUPCHAT_STATUS_NONE) {
return 1;
return false;
}
return 0;
return true;
}
/* Set the size of the groupchat list to num.
*
* return -1 if realloc fails.
* return 0 if it succeeds.
* return false if realloc fails.
* return true if it succeeds.
*/
static int realloc_groupchats(Group_Chats *g_c, uint32_t num)
static bool realloc_conferences(Group_Chats *g_c, uint16_t num)
{
if (num == 0) {
free(g_c->chats);
g_c->chats = nullptr;
return 0;
return true;
}
Group_c *newgroup_chats = (Group_c *)realloc(g_c->chats, num * sizeof(Group_c));
if (newgroup_chats == nullptr) {
return -1;
return false;
}
g_c->chats = newgroup_chats;
return 0;
return true;
}
static void setup_conference(Group_c *g)
{
memset(g, 0, sizeof(Group_c));
}
/* Create a new empty groupchat connection.
*
* return -1 on failure.
* return groupnumber on success.
*/
static int create_group_chat(Group_Chats *g_c)
static int32_t create_group_chat(Group_Chats *g_c)
{
uint32_t i;
for (i = 0; i < g_c->num_chats; ++i) {
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
if (g_c->chats[i].status == GROUPCHAT_STATUS_NONE) {
return i;
}
}
int id = -1;
int32_t id = -1;
if (realloc_groupchats(g_c, g_c->num_chats + 1) == 0) {
if (realloc_conferences(g_c, g_c->num_chats + 1)) {
id = g_c->num_chats;
++g_c->num_chats;
memset(&g_c->chats[id], 0, sizeof(Group_c));
setup_conference(&g_c->chats[id]);
}
return id;
@ -112,11 +114,11 @@ static int create_group_chat(Group_Chats *g_c)
*/
static int wipe_group_chat(Group_Chats *g_c, uint32_t groupnumber)
{
if (groupnumber_not_valid(g_c, groupnumber)) {
if (!is_groupnumber_valid(g_c, groupnumber)) {
return -1;
}
uint32_t i;
uint16_t i;
crypto_memzero(&g_c->chats[groupnumber], sizeof(Group_c));
for (i = g_c->num_chats; i != 0; --i) {
@ -127,7 +129,7 @@ static int wipe_group_chat(Group_Chats *g_c, uint32_t groupnumber)
if (g_c->num_chats != i) {
g_c->num_chats = i;
realloc_groupchats(g_c, g_c->num_chats);
realloc_conferences(g_c, g_c->num_chats);
}
return 0;
@ -135,7 +137,7 @@ static int wipe_group_chat(Group_Chats *g_c, uint32_t groupnumber)
static Group_c *get_group_c(const Group_Chats *g_c, uint32_t groupnumber)
{
if (groupnumber_not_valid(g_c, groupnumber)) {
if (!is_groupnumber_valid(g_c, groupnumber)) {
return nullptr;
}
@ -153,9 +155,7 @@ static Group_c *get_group_c(const Group_Chats *g_c, uint32_t groupnumber)
static int peer_in_chat(const Group_c *chat, const uint8_t *real_pk)
{
uint32_t i;
for (i = 0; i < chat->numpeers; ++i) {
for (uint32_t i = 0; i < chat->numpeers; ++i) {
if (id_equal(chat->group[i].real_pk, real_pk)) {
return i;
}
@ -172,11 +172,9 @@ static int peer_in_chat(const Group_c *chat, const uint8_t *real_pk)
*
* TODO(irungentoo): make this more efficient and maybe use constant time comparisons?
*/
static int get_group_num(const Group_Chats *g_c, const uint8_t *identifier)
static int32_t get_group_num(const Group_Chats *g_c, const uint8_t *identifier)
{
uint32_t i;
for (i = 0; i < g_c->num_chats; ++i) {
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
if (crypto_memcmp(g_c->chats[i].identifier, identifier, GROUP_IDENTIFIER_LENGTH) == 0) {
return i;
}
@ -195,9 +193,7 @@ static int get_group_num(const Group_Chats *g_c, const uint8_t *identifier)
*/
static int get_peer_index(Group_c *g, uint16_t peer_number)
{
uint32_t i;
for (i = 0; i < g->numpeers; ++i) {
for (uint32_t i = 0; i < g->numpeers; ++i) {
if (g->group[i].peer_number == peer_number) {
return i;
}
@ -345,15 +341,13 @@ static int connect_to_closest(Group_Chats *g_c, uint32_t groupnumber, void *user
return 0;
}
unsigned int i;
if (g->changed == GROUPCHAT_CLOSEST_REMOVED) {
for (i = 0; i < g->numpeers; ++i) {
for (uint32_t i = 0; i < g->numpeers; ++i) {
add_to_closest(g_c, groupnumber, g->group[i].real_pk, g->group[i].temp_pk);
}
}
for (i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
for (uint32_t i = 0; i < MAX_GROUP_CONNECTIONS; ++i) {
if (g->close[i].type == GROUPCHAT_CLOSE_NONE) {
continue;
}
@ -372,7 +366,7 @@ static int connect_to_closest(Group_Chats *g_c, uint32_t groupnumber, void *user
}
}
for (i = 0; i < DESIRED_CLOSE_CONNECTIONS; ++i) {
for (uint32_t i = 0; i < DESIRED_CLOSE_CONNECTIONS; ++i) {
if (!g->closest_peers[i].entry) {
continue;
}
@ -467,7 +461,7 @@ static int addpeer(Group_Chats *g_c, uint32_t groupnumber, const uint8_t *real_p
g->peer_on_join(g->object, groupnumber, g->numpeers - 1);
}
return (g->numpeers - 1);
return g->numpeers - 1;
}
static int remove_close_conn(Group_Chats *g_c, uint32_t groupnumber, int friendcon_id)
@ -657,9 +651,7 @@ static void set_conns_type_close(Group_Chats *g_c, uint32_t groupnumber, int fri
/* Set the type for all close connections with friendcon_id */
static void set_conns_status_groups(Group_Chats *g_c, int friendcon_id, uint8_t type)
{
uint32_t i;
for (i = 0; i < g_c->num_chats; ++i) {
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
set_conns_type_close(g_c, i, friendcon_id, type);
}
}
@ -736,7 +728,7 @@ static int add_conn_to_groupchat(Group_Chats *g_c, int friendcon_id, uint32_t gr
*/
int add_groupchat(Group_Chats *g_c, uint8_t type)
{
int groupnumber = create_group_chat(g_c);
int32_t groupnumber = create_group_chat(g_c);
if (groupnumber == -1) {
return -1;
@ -1629,7 +1621,7 @@ static unsigned int send_peers(Group_Chats *g_c, uint32_t groupnumber, int frien
uint8_t *p = response_packet + 1;
uint16_t sent = 0;
unsigned int i;
uint32_t i;
for (i = 0; i < g->numpeers; ++i) {
if ((p - response_packet) + sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE * 2 + 1 + g->group[i].nick_len > sizeof(
@ -2425,9 +2417,7 @@ static int groupchat_clear_timedout(Group_Chats *g_c, uint32_t groupnumber, void
return -1;
}
uint32_t i;
for (i = 0; i < g->numpeers; ++i) {
for (uint32_t i = 0; i < g->numpeers; ++i) {
if (g->peer_number != g->group[i].peer_number && is_timeout(g->group[i].last_recv, GROUP_PING_INTERVAL * 3)) {
delpeer(g_c, groupnumber, i, userdata);
}
@ -2444,9 +2434,7 @@ static int groupchat_clear_timedout(Group_Chats *g_c, uint32_t groupnumber, void
*/
void send_name_all_groups(Group_Chats *g_c)
{
unsigned int i;
for (i = 0; i < g_c->num_chats; ++i) {
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
Group_c *g = get_group_c(g_c, i);
if (!g) {
@ -2483,9 +2471,7 @@ Group_Chats *new_groupchats(Messenger *m)
/* main groupchats loop. */
void do_groupchats(Group_Chats *g_c, void *userdata)
{
unsigned int i;
for (i = 0; i < g_c->num_chats; ++i) {
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
Group_c *g = get_group_c(g_c, i);
if (!g) {
@ -2505,9 +2491,7 @@ void do_groupchats(Group_Chats *g_c, void *userdata)
/* Free everything related with group chats. */
void kill_groupchats(Group_Chats *g_c)
{
unsigned int i;
for (i = 0; i < g_c->num_chats; ++i) {
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
del_groupchat(g_c, i);
}
@ -2523,9 +2507,8 @@ void kill_groupchats(Group_Chats *g_c)
uint32_t count_chatlist(Group_Chats *g_c)
{
uint32_t ret = 0;
uint32_t i;
for (i = 0; i < g_c->num_chats; ++i) {
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
if (g_c->chats[i].status != GROUPCHAT_STATUS_NONE) {
++ret;
}
@ -2549,9 +2532,9 @@ uint32_t copy_chatlist(Group_Chats *g_c, uint32_t *out_list, uint32_t list_size)
return 0;
}
uint32_t i, ret = 0;
uint32_t ret = 0;
for (i = 0; i < g_c->num_chats; ++i) {
for (uint16_t i = 0; i < g_c->num_chats; ++i) {
if (ret >= list_size) {
break; /* Abandon ship */
}

View File

@ -138,7 +138,7 @@ typedef struct Group_Chats {
Friend_Connections *fr_c;
Group_c *chats;
uint32_t num_chats;
uint16_t num_chats;
g_conference_invite_cb *invite_callback;
g_conference_message_cb *message_callback;