diff --git a/libio/Makefile b/libio/Makefile
index 8720381fdc..5f30fe99e2 100644
--- a/libio/Makefile
+++ b/libio/Makefile
@@ -98,6 +98,7 @@ tests = \
   tst-fgetc-after-eof \
   tst-fgetwc \
   tst-fgetws \
+  tst-fopen1 \
   tst-fopenloc2 \
   tst-fputws \
   tst-freopen \
diff --git a/libio/genops.c b/libio/genops.c
index 994ee9c0b1..99f5e80f20 100644
--- a/libio/genops.c
+++ b/libio/genops.c
@@ -119,7 +119,8 @@ _IO_link_in (struct _IO_FILE_plus *fp)
       if (_IO_vtable_offset ((FILE *) fp) == 0)
 	{
 	  fp->file._prevchain = (FILE **) &_IO_list_all;
-	  _IO_list_all->file._prevchain = &fp->file._chain;
+	  if (_IO_list_all != NULL)
+	    _IO_list_all->file._prevchain = &fp->file._chain;
 	}
       _IO_list_all = fp;
 #ifdef _IO_MTSAFE_IO
diff --git a/libio/tst-fopen1.c b/libio/tst-fopen1.c
new file mode 100644
index 0000000000..5a06cb8b1d
--- /dev/null
+++ b/libio/tst-fopen1.c
@@ -0,0 +1,45 @@
+/* Test fopen after closing all FILEs.
+   Copyright (C) 2024 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <stdio.h>
+
+static void
+close_stdio (void)
+{
+  fclose (stdin);
+  fclose (stdout);
+  fclose (stderr);
+}
+
+static void
+final (void)
+{
+  FILE *f = fopen ("/dev/null", "w");
+  fprintf (f, "final\n");
+  fclose (f);
+}
+
+static int
+do_test (void)
+{
+  close_stdio ();
+  final ();
+  return 0;
+}
+
+#include <support/test-driver.c>
