diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index fa1f310d694772e7b7ae97f39d9f6ef57bd0d9bb..947e84cbc6fbe8d6dc2a5575c6840aa403354660 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,11 @@
+2016-10-05  Jerry DeLisle  <jvdelisle@gcc.gnu.org>
+
+	* gfortran.dg/dtio_15.f90: Fix spaces in dg-do.
+	* gfortran.dg/class_array_20.f03: Likewise.
+	* gfortran.dg/class_array_21.f03: Likewise.
+	* gfortran.dg/finalize_29.f08: Likewise.
+	* gfortran.dg/unlimited_polymorphic_23.f90: Likewise.
+
 2016-10-05  Richard Biener  <rguenther@suse.de>
 
 	PR middle-end/77826
diff --git a/gcc/testsuite/gfortran.dg/class_array_20.f03 b/gcc/testsuite/gfortran.dg/class_array_20.f03
index c49f7d2406fe514cd6cf443345ea4ba95ad6fd7d..b4be04579666ddf5173dfbedc703669dfafac6cd 100644
--- a/gcc/testsuite/gfortran.dg/class_array_20.f03
+++ b/gcc/testsuite/gfortran.dg/class_array_20.f03
@@ -1,4 +1,4 @@
-! {dg-do run}
+! { dg-do run }
 !
 ! Test contributed by Thomas L. Clune via pr60322
 !                  and Antony Lewis via pr64692
diff --git a/gcc/testsuite/gfortran.dg/class_array_21.f03 b/gcc/testsuite/gfortran.dg/class_array_21.f03
index 1e89d384385ab28c7958b3208675bb31fc6f4b71..ed03ed8d3f4d00efbe915d3414154e1d3ff54c5e 100644
--- a/gcc/testsuite/gfortran.dg/class_array_21.f03
+++ b/gcc/testsuite/gfortran.dg/class_array_21.f03
@@ -1,4 +1,4 @@
-! {dg-do run}
+! { dg-do run }
 !
 ! Contributed by Andre Vehreschild
 ! Check more elaborate class array addressing.
diff --git a/gcc/testsuite/gfortran.dg/dtio_15.f90 b/gcc/testsuite/gfortran.dg/dtio_15.f90
index 040bb3ebe1c259ee53a5ea4b87056a02abb1472f..776f442a4659eabab0514b678161e4db475218eb 100644
--- a/gcc/testsuite/gfortran.dg/dtio_15.f90
+++ b/gcc/testsuite/gfortran.dg/dtio_15.f90
@@ -1,4 +1,4 @@
-! {dg-do run }
+! { dg-do run }
 ! Test that inquire of string internal unit in child process errors.
 module string_m
   implicit none
diff --git a/gcc/testsuite/gfortran.dg/finalize_29.f08 b/gcc/testsuite/gfortran.dg/finalize_29.f08
index 1f5f742403bfa152391165136e022d6fc9a7dfb2..9640f4f7f9cd92736153f2f5e344207355ca3ca6 100644
--- a/gcc/testsuite/gfortran.dg/finalize_29.f08
+++ b/gcc/testsuite/gfortran.dg/finalize_29.f08
@@ -1,4 +1,4 @@
-! {dg-do run}
+! { dg-do run }
 !
 ! Testcase contributed by Andre Vehreschild  <vehre@gcc.gnu.org>
 
diff --git a/gcc/testsuite/gfortran.dg/unlimited_polymorphic_23.f90 b/gcc/testsuite/gfortran.dg/unlimited_polymorphic_23.f90
index 27eff31053273f9e9730989acb3ebd45c6092186..99b5f6b696257a779062a60c88e8d7594a554abb 100644
--- a/gcc/testsuite/gfortran.dg/unlimited_polymorphic_23.f90
+++ b/gcc/testsuite/gfortran.dg/unlimited_polymorphic_23.f90
@@ -1,4 +1,4 @@
-! {dg-do run }
+! { dg-do run }
 !
 ! Test the fix for PR65024, in which the structure for the 'info'
 ! component of type 'T' was not being converted into TREE_SSA and
diff --git a/libgfortran/ChangeLog b/libgfortran/ChangeLog
index 0e5c4d2c003034f471fce294f4edd52827124132..d38646ac8b7f2d63cc0b5e9addb3cde974a2e9b7 100644
--- a/libgfortran/ChangeLog
+++ b/libgfortran/ChangeLog
@@ -1,6 +1,12 @@
+2016-10-05  Jerry DeLisle  <jvdelisle@gcc.gnu.org>
+
+	PR fortran/77868
+	* io/inquire.c (inquire_via_unit): NULL guard the check for
+	internal unit passed into child IO procedure.
+
 2016-10-04  Jerry DeLisle  <jvdelisle@gcc.gnu.org>
 
-	io/inquire.c (inquire_via_unit): Add check for internal unit
+	* io/inquire.c (inquire_via_unit): Add check for internal unit
 	passed into child IO procedure.
 
 2016-10-01  Andre Vehreschild  <vehre@gcc.gnu.org>
diff --git a/libgfortran/io/inquire.c b/libgfortran/io/inquire.c
index 7751b8df4db3228bce46fc442f3da463d16eeba2..7e663130e566e387904c79aca79a53ec69ad3438 100644
--- a/libgfortran/io/inquire.c
+++ b/libgfortran/io/inquire.c
@@ -43,7 +43,7 @@ inquire_via_unit (st_parameter_inquire *iqp, gfc_unit * u)
 
   if (iqp->common.unit == GFC_INTERNAL_UNIT ||
 	iqp->common.unit == GFC_INTERNAL_UNIT4 ||
-	u->internal_unit_kind != 0)
+	(u != NULL && u->internal_unit_kind != 0))
     generate_error (&iqp->common, LIBERROR_INQUIRE_INTERNAL_UNIT, NULL);
 
   if ((cf & IOPARM_INQUIRE_HAS_EXIST) != 0)