summaryrefslogtreecommitdiff
path: root/io.c
diff options
context:
space:
mode:
Diffstat (limited to 'io.c')
-rw-r--r--io.c12
1 files changed, 8 insertions, 4 deletions
diff --git a/io.c b/io.c
index 47b86ed240..9ad7bff972 100644
--- a/io.c
+++ b/io.c
@@ -6665,10 +6665,14 @@ rb_io_reopen(int argc, VALUE *argv, VALUE file)
}
}
else {
- if (close(fptr->fd) < 0)
- rb_sys_fail_path(fptr->pathv);
- fptr->fd = -1;
- fptr->fd = rb_sysopen(fptr->pathv, oflags, 0666);
+ int tmpfd = rb_sysopen(fptr->pathv, oflags, 0666);
+ int err = 0;
+ if (rb_cloexec_dup2(tmpfd, fptr->fd) < 0)
+ err = errno;
+ (void)close(tmpfd);
+ if (err) {
+ rb_syserr_fail_path(err, fptr->pathv);
+ }
}
return file;