diff mbox series

[libgfortran] Fix uninitialized variable use in fallback_access

Message ID 5B9B6BF0.5010108@foss.arm.com
State New
Headers show
Series [libgfortran] Fix uninitialized variable use in fallback_access | expand

Commit Message

Kyrill Tkachov Sept. 14, 2018, 8:06 a.m. UTC
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.

Comments

Paul Richard Thomas Sept. 14, 2018, 9:10 a.m. UTC | #1
Hi Kyrill,

This indeed is an obvious change. OK for trunk and 7- and 8-branches.

Thanks for the patch

Paul

On 14 September 2018 at 09:06, Kyrill  Tkachov
<kyrylo.tkachov@foss.arm.com> wrote:
> 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.
diff mbox series

Patch

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)
     {