1
0
mirror of https://github.com/Tha14/toxic.git synced 2025-07-03 06:16:45 +02:00

Dynamically allocate memory for save file instead of using stack

This commit is contained in:
Jfreegman
2016-05-03 14:13:16 -04:00
parent 32442b6286
commit cf16849b37
2 changed files with 44 additions and 11 deletions

View File

@ -133,17 +133,24 @@ void kill_friendlist(void)
#define TEMP_BLOCKLIST_EXT ".tmp"
static int save_blocklist(char *path)
{
if (path == NULL)
if (path == NULL) {
return -1;
}
int len = sizeof(BlockedFriend) * Blocked.num_blocked;
char data[len];
char *data = malloc(len * sizeof(char));
if (data == NULL) {
return -1;
}
int i, count = 0;
for (i = 0; i < Blocked.max_idx; ++i) {
if (count > Blocked.num_blocked)
if (count > Blocked.num_blocked) {
free(data);
return -1;
}
if (Blocked.list[i].active) {
BlockedFriend tmp;
@ -164,8 +171,11 @@ static int save_blocklist(char *path)
/* Blocklist is empty, we can remove the empty file */
if (count == 0) {
if (remove(path) != 0)
free(data);
if (remove(path) != 0) {
return -1;
}
return 0;
}
@ -175,19 +185,24 @@ static int save_blocklist(char *path)
FILE *fp = fopen(temp_path, "wb");
if (fp == NULL)
if (fp == NULL) {
free(data);
return -1;
}
if (fwrite(data, len, 1, fp) != 1) {
fprintf(stderr, "Failed to write blocklist data.\n");
fclose(fp);
free(data);
return -1;
}
fclose(fp);
free(data);
if (rename(temp_path, path) != 0)
if (rename(temp_path, path) != 0) {
return -1;
}
return 0;
}