summaryrefslogtreecommitdiffstats
path: root/src/libr-bfd.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libr-bfd.c')
-rw-r--r--src/libr-bfd.c30
1 files changed, 22 insertions, 8 deletions
diff --git a/src/libr-bfd.c b/src/libr-bfd.c
index c4bc8b1..629a466 100644
--- a/src/libr-bfd.c
+++ b/src/libr-bfd.c
@@ -1,6 +1,7 @@
/*
*
* Copyright (c) 2008-2011 Erich Hoover
+ * Copyright (c) 2015 Timothy Pearson <kb9vqf@pearsoncomputing.net>
*
* libr libbfd Backend - Add resources into ELF binaries using libbfd
*
@@ -320,23 +321,36 @@ int safe_rename(const char *old, const char *new)
{
char buffer[1024];
FILE *in, *out;
- int read;
+ size_t read;
+ int status_in;
+ int status_out;
in = fopen(old, "r");
- if(!in)
+ if(!in) {
return -1;
+ }
out = fopen(new, "w");
- if(!out)
+ if(!out) {
+ fclose(in);
return -1;
- while(!feof(in) && !ferror(in))
- {
+ }
+ while(!feof(in) && !ferror(in)) {
read = fread(buffer, 1, sizeof(buffer), in);
- fwrite(buffer, read, 1, out);
+ if (read > 0) {
+ fwrite(buffer, 1, read, out);
+ }
+ if (ferror(in) || ferror(out)) {
+ fclose(in);
+ fclose(out);
+ remove(new);
+ return -1;
+ }
}
+ status_in = ferror(in);
+ status_out = ferror(out);
fclose(in);
fclose(out);
- if(ferror(in))
- {
+ if(status_in || status_out) {
remove(new);
return -1;
}