summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTimothy Pearson <kb9vqf@pearsoncomputing.net>2015-01-08 19:51:00 -0600
committerTimothy Pearson <kb9vqf@pearsoncomputing.net>2015-01-08 19:51:00 -0600
commit7690d56228fdf1f634046f53a489ab2b93099a83 (patch)
tree73031695dbc1c5bd57e5f2721bd9ad941c5feed9 /src
parent4c2accb349bc5b0abcb7af97e8e813898bf4d5d9 (diff)
downloadlibr-7690d56228fdf1f634046f53a489ab2b93099a83.tar.gz
libr-7690d56228fdf1f634046f53a489ab2b93099a83.zip
Fix cross-device editing failures
This resolves Bug 2215
Diffstat (limited to 'src')
-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;
}