public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH][libgfortran] Fix uninitialized variable use in fallback_access
@ 2018-09-14  8:06 Kyrill Tkachov
  2018-09-14  9:11 ` Paul Richard Thomas
  0 siblings, 1 reply; 7+ messages in thread
From: Kyrill Tkachov @ 2018-09-14  8:06 UTC (permalink / raw)
  To: gcc-patches, fortran

[-- Attachment #1: Type: text/plain, Size: 872 bytes --]

Hi all,

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.

Ok for trunk and the branches?

Thanks,
Kyrill

2018-09-14  Kyrylo Tkachov  <kyrylo.tkachov@arm.com>

     * io/unix.c (fallback_access): Avoid calling close on
     uninitialized file descriptor.

[-- Attachment #2: fort-access.patch --]
[-- Type: text/x-patch, Size: 740 bytes --]

diff --git a/libgfortran/io/unix.c b/libgfortran/io/unix.c
index 61e9f7997b25819514af546b50aa1d00b1d116f9..8081d6f96b2f6cd2489e876c8921cfb3510287ca 100644
--- a/libgfortran/io/unix.c
+++ b/libgfortran/io/unix.c
@@ -149,13 +149,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)
     {

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2018-09-15 18:10 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-09-14  8:06 [PATCH][libgfortran] Fix uninitialized variable use in fallback_access Kyrill Tkachov
2018-09-14  9:11 ` Paul Richard Thomas
2018-09-15 12:02   ` Teaching and debugging OpenMP in gfortran (or otherwise!) N.M. Maclaren
2018-09-15 15:30     ` Paul Richard Thomas
2018-09-15 15:52       ` N.M. Maclaren
2018-09-15 16:22         ` Thomas Koenig
2018-09-15 18:10       ` N.M. Maclaren

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).