Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup: remove kicked peers from saved peers list #2738

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions toxcore/group_chats.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,6 @@
non_null() static void group_cleanup(const GC_Session *c, GC_Chat *chat);
non_null() static bool group_exists(const GC_Session *c, const uint8_t *chat_id);
non_null() static void add_tcp_relays_to_chat(const GC_Session *c, GC_Chat *chat);
non_null(1, 2) nullable(4)
static bool peer_delete(const GC_Session *c, GC_Chat *chat, uint32_t peer_number, void *userdata);
non_null() static void create_gc_session_keypair(const Logger *log, const Random *rng, uint8_t *public_key,
uint8_t *secret_key);
non_null() static size_t load_gc_peers(GC_Chat *chat, const GC_SavedPeerInfo *addrs, uint16_t num_addrs);
Expand Down Expand Up @@ -837,6 +835,21 @@
return -1;
}

/** @brief Removes entry containing `public_key` from the saved peers list. */
non_null()
static void saved_peers_remove_entry(GC_Chat *chat, const uint8_t *public_key)
{
const int idx = saved_peer_index(chat, public_key);

if (idx < 0) {
return;

Check warning on line 845 in toxcore/group_chats.c

View check run for this annotation

Codecov / codecov/patch

toxcore/group_chats.c#L845

Added line #L845 was not covered by tests
}

chat->saved_peers[idx] = (GC_SavedPeerInfo) {
0
};
}

/** @brief Returns the index of the first vacant entry in saved peers list.
*
* If `public_key` is non-null and already exists in the list, its index will be returned.
Expand Down Expand Up @@ -6702,6 +6715,7 @@
*
* Return true on success.
*/
non_null(1, 2) nullable(4)
static bool peer_delete(const GC_Session *c, GC_Chat *chat, uint32_t peer_number, void *userdata)
{
GC_Peer *peer = get_gc_peer(chat, peer_number);
Expand All @@ -6710,17 +6724,23 @@
return false;
}

GC_Connection *gconn = &peer->gconn;
iphydf marked this conversation as resolved.
Show resolved Hide resolved

// We need to save some peer info for the callback before deleting it
const bool peer_confirmed = peer->gconn.confirmed;
const bool peer_confirmed = gconn->confirmed;
const GC_Peer_Id peer_id = peer->peer_id;
uint8_t nick[MAX_GC_NICK_SIZE];
const uint16_t nick_length = peer->nick_length;
const GC_Exit_Info exit_info = peer->gconn.exit_info;
const GC_Exit_Info exit_info = gconn->exit_info;

assert(nick_length <= MAX_GC_NICK_SIZE);
memcpy(nick, peer->nick, nick_length);

gcc_peer_cleanup(&peer->gconn);
if (exit_info.exit_type == GC_EXIT_TYPE_KICKED) {
saved_peers_remove_entry(chat, gconn->addr.public_key.enc);
}

gcc_peer_cleanup(gconn);

--chat->numpeers;

Expand Down
Loading