aboutsummaryrefslogtreecommitdiff
path: root/libgfortran
diff options
context:
space:
mode:
authorKyrylo Tkachov <kyrylo.tkachov@arm.com>2018-09-14 09:22:01 +0000
committerKyrylo Tkachov <ktkachov@gcc.gnu.org>2018-09-14 09:22:01 +0000
commitef5057c89e636d7d566d52c06609a4615430e5b0 (patch)
treeb69851d448758d64451837fb961a0bbf073c1922 /libgfortran
parent7efd5ff31df5ac69a23b6285474f0b50154801f7 (diff)
downloadgcc-ef5057c89e636d7d566d52c06609a4615430e5b0.zip
gcc-ef5057c89e636d7d566d52c06609a4615430e5b0.tar.gz
gcc-ef5057c89e636d7d566d52c06609a4615430e5b0.tar.bz2
[libgfortran] Fix uninitialized variable use in fallback_access
I've been tracking down a bug in a Fortran program on a newlib target and it boils down to fallback_access doing something bad. The unconditional calls to close cause havoc when open doesn't get called due to the short-circuiting in the if-statement above because the fd is uninitialised. In my environment GCC ends up calling close on file descriptor 0, thus trying to close stdin. This patch tightens up the calling so that close is called only when the corresponding open call succeeded. With this my runtime failure disappears. Bootstrapped and tested on aarch64-none-linux-gnu. Though that doesn't exercise this call I hope it's an obviously correct change. * io/unix.c (fallback_access): Avoid calling close on uninitialized file descriptor. From-SVN: r264305
Diffstat (limited to 'libgfortran')
-rw-r--r--libgfortran/ChangeLog5
-rw-r--r--libgfortran/io/unix.c20
2 files changed, 19 insertions, 6 deletions
diff --git a/libgfortran/ChangeLog b/libgfortran/ChangeLog
index 1120d30..56828b1 100644
--- a/libgfortran/ChangeLog
+++ b/libgfortran/ChangeLog
@@ -1,3 +1,8 @@
+2018-09-14 Kyrylo Tkachov <kyrylo.tkachov@arm.com>
+
+ * io/unix.c (fallback_access): Avoid calling close on
+ uninitialized file descriptor.
+
2018-09-12 Kwok Cheung Yeung <kcy@codesourcery.com>
* runtime/minimal.c (estr_write): Define in terms of write.
diff --git a/libgfortran/io/unix.c b/libgfortran/io/unix.c
index 4a133fd..ad2577c 100644
--- a/libgfortran/io/unix.c
+++ b/libgfortran/io/unix.c
@@ -150,13 +150,21 @@ fallback_access (const char *path, int mode)
{
int fd;
- if ((mode & R_OK) && (fd = open (path, O_RDONLY)) < 0)
- return -1;
- close (fd);
+ if (mode & R_OK)
+ {
+ if ((fd = open (path, O_RDONLY)) < 0)
+ return -1;
+ else
+ close (fd);
+ }
- if ((mode & W_OK) && (fd = open (path, O_WRONLY)) < 0)
- return -1;
- close (fd);
+ if (mode & W_OK)
+ {
+ if ((fd = open (path, O_WRONLY)) < 0)
+ return -1;
+ else
+ close (fd);
+ }
if (mode == F_OK)
{