fix resource leaks

This commit is contained in:
diamant3
2023-09-26 13:01:09 +08:00
parent fc94f99fc6
commit 1e1fe31165
2 changed files with 4 additions and 0 deletions

View File

@@ -40,11 +40,13 @@ int main(int argc, char *argv[])
buffer = malloc(fd_size); buffer = malloc(fd_size);
if(buffer == NULL) { if(buffer == NULL) {
printf("Failed to allocate memory.\n"); printf("Failed to allocate memory.\n");
fclose(source);
return 1; return 1;
} }
if(fread(buffer,1,fd_size,source) != fd_size) { if(fread(buffer,1,fd_size,source) != fd_size) {
printf("Failed to read file.\n"); printf("Failed to read file.\n");
fclose(source);
return 1; return 1;
} }
fclose(source); fclose(source);

View File

@@ -92,6 +92,7 @@ int main(int argc, char *argv[]) {
fseek(infile, 0, SEEK_SET); fseek(infile, 0, SEEK_SET);
if (total_size < 0) { if (total_size < 0) {
printf("ERROR: Could not get the input file size.\n"); printf("ERROR: Could not get the input file size.\n");
fclose(infile);
return -1; return -1;
} }
@@ -99,6 +100,7 @@ int main(int argc, char *argv[]) {
size_t result = fread(&header, sizeof(HEADER), 1, infile); size_t result = fread(&header, sizeof(HEADER), 1, infile);
if (result < 0) { if (result < 0) {
printf("ERROR: Could not read the input file header.\n"); printf("ERROR: Could not read the input file header.\n");
fclose(infile);
return -1; return -1;
} }