]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
PR 61187 Avoid reading uninitialized memory.
authorJanne Blomqvist <jb@gcc.gnu.org>
Sun, 25 May 2014 19:29:00 +0000 (22:29 +0300)
committerJanne Blomqvist <jb@gcc.gnu.org>
Sun, 25 May 2014 19:29:00 +0000 (22:29 +0300)
2014-05-25  Janne Blomqvist  <jb@gcc.gnu.org>

Backport from trunk.
PR libfortran/61187
* io/unix.c (raw_close): Check if s->fd is -1.
(fd_to_stream): Check return value of fstat(), handle error.

From-SVN: r210914

libgfortran/ChangeLog
libgfortran/io/unix.c

index 1aaf6845cd79784770b4f6ada1f272a90005b9eb..6f70effe77be6bfc247b533a34b1bd276f1c278a 100644 (file)
@@ -1,3 +1,10 @@
+2014-05-25  Janne Blomqvist  <jb@gcc.gnu.org>
+
+       Backport from trunk.
+       PR libfortran/61187
+       * io/unix.c (raw_close): Check if s->fd is -1.
+       (fd_to_stream): Check return value of fstat(), handle error.
+
 2014-05-22  Release Manager
 
        * GCC 4.8.3 released.
index 8b9d7a773425224106446105f6d2bb43be4fa5f6..185d0dca157bde4ae5359f79d6460f1562fc65f5 100644 (file)
@@ -407,7 +407,9 @@ raw_close (unix_stream * s)
 {
   int retval;
   
-  if (s->fd != STDOUT_FILENO
+  if (s->fd == -1)
+    retval = -1;
+  else if (s->fd != STDOUT_FILENO
       && s->fd != STDERR_FILENO
       && s->fd != STDIN_FILENO)
     retval = close (s->fd);
@@ -983,7 +985,15 @@ fd_to_stream (int fd)
 
   /* Get the current length of the file. */
 
-  fstat (fd, &statbuf);
+  if (fstat (fd, &statbuf) == -1)
+    {
+      s->st_dev = s->st_ino = -1;
+      s->file_length = 0;
+      if (errno == EBADF)
+       s->fd = -1;
+      raw_init (s);
+      return (stream *) s;
+    }
 
   s->st_dev = statbuf.st_dev;
   s->st_ino = statbuf.st_ino;