fix(file): double free issue

This commit is contained in:
lisk77 2025-09-08 23:59:00 +02:00
parent 1e3934cf20
commit f27b523fbf

View file

@ -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,8 +224,9 @@ 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);
object_hash(BaseFileObject, id, hash); object_hash(BaseFileObject, id, hash);
@ -228,12 +234,14 @@ int snapshot_file(char* path, char* root, size_t basefile_id, char* hash) {
char file_path[PATH_MAX]; char file_path[PATH_MAX];
snprintf(dir_path, sizeof(dir_path), "%s/.merk/objects/%.2s", root, hash); snprintf(dir_path, sizeof(dir_path), "%s/.merk/objects/%.2s", root, hash);
mkdir(dir_path, 0755); mkdir(dir_path, 0755);
snprintf(file_path, sizeof(file_path), "%s/%s", dir_path, hash+2); snprintf(file_path, sizeof(file_path), "%s/%s", dir_path, hash + 2);
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;
} }