Merge pull request #84 from ryancaicse/master

Fix resource leaks in different places
This commit is contained in:
Francisco Javier Trujillo Mata
2022-02-07 15:38:27 +01:00
committed by GitHub
4 changed files with 9 additions and 0 deletions

View File

@@ -362,17 +362,20 @@ int main(int argc, char *argv[])
buffer = malloc(size); buffer = malloc(size);
if (buffer == NULL) { if (buffer == NULL) {
printf("Failed to allocate memory.\n"); printf("Failed to allocate memory.\n");
fclose(dest);
return 1; return 1;
} }
if (fread(buffer, 1, size, source) != size) { if (fread(buffer, 1, size, source) != size) {
printf("Failed to read file.\n"); printf("Failed to read file.\n");
fclose(dest);
return 1; return 1;
} }
fclose(source); fclose(source);
if (!(dest = fopen(f_dest, "wb+"))) { if (!(dest = fopen(f_dest, "wb+"))) {
printf("Failed to open/create %s.\n", f_dest); printf("Failed to open/create %s.\n", f_dest);
fclose(dest);
return 1; return 1;
} }

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

@@ -183,6 +183,7 @@ unsigned char *load_file(const char *file, unsigned int *size)
if(*size < sizeof(Elf32_Ehdr)) if(*size < sizeof(Elf32_Ehdr))
{ {
fprintf(stderr, "Error, invalid file size\n"); fprintf(stderr, "Error, invalid file size\n");
fclose(fp);
break; break;
} }
@@ -190,6 +191,7 @@ unsigned char *load_file(const char *file, unsigned int *size)
if(data == NULL) if(data == NULL)
{ {
fprintf(stderr, "Error, could not allocate memory for ELF\n"); fprintf(stderr, "Error, could not allocate memory for ELF\n");
fclose(fp);
break; break;
} }

View File

@@ -132,6 +132,7 @@ unsigned char *load_file(const char *file)
if(size < sizeof(Elf32_Ehdr)) if(size < sizeof(Elf32_Ehdr))
{ {
fprintf(stderr, "Error, invalid file size\n"); fprintf(stderr, "Error, invalid file size\n");
fclose(fp);
break; break;
} }
@@ -139,6 +140,7 @@ unsigned char *load_file(const char *file)
if(data == NULL) if(data == NULL)
{ {
fprintf(stderr, "Error, could not allocate memory for ELF\n"); fprintf(stderr, "Error, could not allocate memory for ELF\n");
fclose(fp);
break; break;
} }