Skip to content

Commit fff2b1c

Browse files
committed
fix: out-of-memory condition by corrupted save file
Don't allocate the memory trusting the values in a toxsave file.
1 parent 12dbafb commit fff2b1c

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

toxcore/group.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3426,15 +3426,6 @@ static uint32_t load_group(Group_c *g, const Group_Chats *g_c, const uint8_t *da
34263426
lendian_bytes_to_host32(&g->numfrozen, data);
34273427
data += sizeof(uint32_t);
34283428

3429-
if (g->numfrozen > 0) {
3430-
g->frozen = (Group_Peer *)calloc(g->numfrozen, sizeof(Group_Peer));
3431-
3432-
if (g->frozen == nullptr) {
3433-
// Memory allocation failure
3434-
return 0;
3435-
}
3436-
}
3437-
34383429
g->title_len = *data;
34393430

34403431
if (g->title_len > MAX_NAME_LENGTH) {
@@ -3460,6 +3451,16 @@ static uint32_t load_group(Group_c *g, const Group_Chats *g_c, const uint8_t *da
34603451
return 0;
34613452
}
34623453

3454+
// This is inefficient, but allows us to check data consistency before allocating memory
3455+
Group_Peer *tmp_frozen = (Group_Peer *)realloc(g->frozen, (j + 1) * sizeof(Group_Peer));
3456+
3457+
if (tmp_frozen == nullptr) {
3458+
// Memory allocation failure
3459+
return 0;
3460+
}
3461+
3462+
g->frozen = tmp_frozen;
3463+
34633464
Group_Peer *peer = &g->frozen[j];
34643465
memset(peer, 0, sizeof(Group_Peer));
34653466

0 commit comments

Comments
 (0)