That doesn't fix the issue entirely, you also need to check if i < chat->numpeers.

I would put: if (chat->group == NULL || i >= chat->numpeers) break;
before that statement to solve it.