Commit ef5057c8 by Kyrylo Tkachov Committed by Kyrylo Tkachov

[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
parent 7efd5ff3
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.
......
......@@ -150,13 +150,21 @@ fallback_access (const char *path, int mode)
{
int fd;
if ((mode & R_OK) && (fd = open (path, O_RDONLY)) < 0)
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)
if (mode & W_OK)
{
if ((fd = open (path, O_WRONLY)) < 0)
return -1;
else
close (fd);
}
if (mode == F_OK)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment