Skip to content

Commit

Permalink
fix: out-of-memory condition by corrupted save file
Browse files Browse the repository at this point in the history
Don't allocate the memory trusting the values in a toxsave file.
  • Loading branch information
sudden6 committed Feb 21, 2022
1 parent 12dbafb commit a8ccdb1
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions toxcore/group.c
Original file line number Diff line number Diff line change
Expand Up @@ -3426,15 +3426,6 @@ static uint32_t load_group(Group_c *g, const Group_Chats *g_c, const uint8_t *da
lendian_bytes_to_host32(&g->numfrozen, data);
data += sizeof(uint32_t);

if (g->numfrozen > 0) {
g->frozen = (Group_Peer *)calloc(g->numfrozen, sizeof(Group_Peer));

if (g->frozen == nullptr) {
// Memory allocation failure
return 0;
}
}

g->title_len = *data;

if (g->title_len > MAX_NAME_LENGTH) {
Expand All @@ -3460,6 +3451,14 @@ static uint32_t load_group(Group_c *g, const Group_Chats *g_c, const uint8_t *da
return 0;
}

// This is inefficient, but allows us to check data consistency before allocating memory
g->frozen = (Group_Peer *)realloc(g->frozen, (j + 1) * sizeof(Group_Peer));

if (g->frozen == nullptr) {
// Memory allocation failure
return 0;
}

Group_Peer *peer = &g->frozen[j];
memset(peer, 0, sizeof(Group_Peer));

Expand Down

0 comments on commit a8ccdb1

Please sign in to comment.