fix(file): double free issue
This commit is contained in:
parent
1e3934cf20
commit
f27b523fbf
1 changed files with 22 additions and 8 deletions
30
src/file.c
30
src/file.c
|
|
@ -197,7 +197,10 @@ int snapshot_file(char* path, char* root, size_t basefile_id, char* hash) {
|
|||
total_len += 1;
|
||||
|
||||
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]);
|
||||
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));
|
||||
if (!final_content) {
|
||||
free(concat_file);
|
||||
concat_file = NULL;
|
||||
free_file(file);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
@ -219,8 +224,9 @@ int snapshot_file(char* path, char* root, size_t basefile_id, char* hash) {
|
|||
strcat(final_content, 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);
|
||||
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];
|
||||
snprintf(dir_path, sizeof(dir_path), "%s/.merk/objects/%.2s", root, hash);
|
||||
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");
|
||||
if (!fp) {
|
||||
perror("ERROR: cannot open path in snapshot_file!\n");
|
||||
free(concat_file);
|
||||
free(final_content);
|
||||
final_content = NULL;
|
||||
free_file(file);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -242,20 +250,23 @@ int snapshot_file(char* path, char* root, size_t basefile_id, char* hash) {
|
|||
Bytef* compressed = malloc(compressedLen);
|
||||
if (!compressed) {
|
||||
fclose(fp);
|
||||
free(concat_file);
|
||||
free(final_content);
|
||||
final_content = NULL;
|
||||
free_file(file);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (compress(compressed, &compressedLen, (const Bytef*)final_content, originalLen) != Z_OK) {
|
||||
perror("ERROR: compression failed in snapshot_file!");
|
||||
free(compressed);
|
||||
free(concat_file);
|
||||
fclose(fp);
|
||||
free(final_content);
|
||||
final_content = NULL;
|
||||
free_file(file);
|
||||
return 0;
|
||||
}
|
||||
|
||||
fprintf(fp, "%lu ", (unsigned long)originalLen);
|
||||
|
||||
fwrite(compressed, 1, compressedLen, 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);
|
||||
|
||||
free(compressed);
|
||||
free(concat_file);
|
||||
free(final_content);
|
||||
final_content = NULL; // Prevent double free
|
||||
free_file(file);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue