fix(file): double free issue
This commit is contained in:
parent
1e3934cf20
commit
f27b523fbf
1 changed files with 22 additions and 8 deletions
26
src/file.c
26
src/file.c
|
|
@ -197,7 +197,10 @@ int snapshot_file(char* path, char* root, size_t basefile_id, char* hash) {
|
||||||
total_len += 1;
|
total_len += 1;
|
||||||
|
|
||||||
char* concat_file = calloc(total_len, sizeof(char));
|
char* concat_file = calloc(total_len, sizeof(char));
|
||||||
if (!concat_file) return 1;
|
if (!concat_file) {
|
||||||
|
free_file(file);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
strcpy(concat_file, content[0]);
|
strcpy(concat_file, content[0]);
|
||||||
for (size_t i = 1; i < lines; i++) {
|
for (size_t i = 1; i < lines; i++) {
|
||||||
|
|
@ -212,6 +215,8 @@ int snapshot_file(char* path, char* root, size_t basefile_id, char* hash) {
|
||||||
char* final_content = calloc(content_len, sizeof(char));
|
char* final_content = calloc(content_len, sizeof(char));
|
||||||
if (!final_content) {
|
if (!final_content) {
|
||||||
free(concat_file);
|
free(concat_file);
|
||||||
|
concat_file = NULL;
|
||||||
|
free_file(file);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -219,6 +224,7 @@ int snapshot_file(char* path, char* root, size_t basefile_id, char* hash) {
|
||||||
strcat(final_content, concat_file);
|
strcat(final_content, concat_file);
|
||||||
|
|
||||||
free(concat_file);
|
free(concat_file);
|
||||||
|
concat_file = NULL;
|
||||||
|
|
||||||
char id[2 + snprintf(NULL, 0, "%d", basefile_id) + strlen(path)];
|
char id[2 + snprintf(NULL, 0, "%d", basefile_id) + strlen(path)];
|
||||||
snprintf(id, sizeof(id), "%s %d", path, basefile_id);
|
snprintf(id, sizeof(id), "%s %d", path, basefile_id);
|
||||||
|
|
@ -233,7 +239,9 @@ int snapshot_file(char* path, char* root, size_t basefile_id, char* hash) {
|
||||||
FILE* fp = fopen(file_path, "wb");
|
FILE* fp = fopen(file_path, "wb");
|
||||||
if (!fp) {
|
if (!fp) {
|
||||||
perror("ERROR: cannot open path in snapshot_file!\n");
|
perror("ERROR: cannot open path in snapshot_file!\n");
|
||||||
free(concat_file);
|
free(final_content);
|
||||||
|
final_content = NULL;
|
||||||
|
free_file(file);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -242,20 +250,23 @@ int snapshot_file(char* path, char* root, size_t basefile_id, char* hash) {
|
||||||
Bytef* compressed = malloc(compressedLen);
|
Bytef* compressed = malloc(compressedLen);
|
||||||
if (!compressed) {
|
if (!compressed) {
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
free(concat_file);
|
free(final_content);
|
||||||
|
final_content = NULL;
|
||||||
|
free_file(file);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (compress(compressed, &compressedLen, (const Bytef*)final_content, originalLen) != Z_OK) {
|
if (compress(compressed, &compressedLen, (const Bytef*)final_content, originalLen) != Z_OK) {
|
||||||
perror("ERROR: compression failed in snapshot_file!");
|
perror("ERROR: compression failed in snapshot_file!");
|
||||||
free(compressed);
|
free(compressed);
|
||||||
free(concat_file);
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
free(final_content);
|
||||||
|
final_content = NULL;
|
||||||
|
free_file(file);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(fp, "%lu ", (unsigned long)originalLen);
|
fprintf(fp, "%lu ", (unsigned long)originalLen);
|
||||||
|
|
||||||
fwrite(compressed, 1, compressedLen, fp);
|
fwrite(compressed, 1, compressedLen, fp);
|
||||||
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
@ -263,7 +274,10 @@ int snapshot_file(char* path, char* root, size_t basefile_id, char* hash) {
|
||||||
chmod(file_path, S_IRUSR | S_IRGRP | S_IROTH);
|
chmod(file_path, S_IRUSR | S_IRGRP | S_IROTH);
|
||||||
|
|
||||||
free(compressed);
|
free(compressed);
|
||||||
free(concat_file);
|
free(final_content);
|
||||||
|
final_content = NULL; // Prevent double free
|
||||||
|
free_file(file);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue