libio: Check _IO_list_all != NULL [BZ #31963]

Message ID 20240708220201.3252178-1-hjl.tools@gmail.com
State New
Headers
Series libio: Check _IO_list_all != NULL [BZ #31963] |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Build passed
redhat-pt-bot/TryBot-32bit success Build for i686
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Test passed

Commit Message

H.J. Lu July 8, 2024, 10:02 p.m. UTC
  When fopen is called after all FILEs have been closed, _IO_list_all is
NULL.  Check _IO_list_all != NULL in _IO_link_in.  This fixes BZ #31963.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 libio/Makefile     |  1 +
 libio/genops.c     |  3 ++-
 libio/tst-fopen1.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 48 insertions(+), 1 deletion(-)
 create mode 100644 libio/tst-fopen1.c
  

Comments

Andreas K. Huettel July 8, 2024, 10:30 p.m. UTC | #1
Am Dienstag, 9. Juli 2024, 00:02:01 MESZ schrieb H.J. Lu:
> When fopen is called after all FILEs have been closed, _IO_list_all is
> NULL.  Check _IO_list_all != NULL in _IO_link_in.  This fixes BZ #31963.
> 

OK for 2.40 with someone's R-B

> Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
> ---
>  libio/Makefile     |  1 +
>  libio/genops.c     |  3 ++-
>  libio/tst-fopen1.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 48 insertions(+), 1 deletion(-)
>  create mode 100644 libio/tst-fopen1.c
> 
> 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>
>
  
Sam James July 9, 2024, 4:06 a.m. UTC | #2
"H.J. Lu" <hjl.tools@gmail.com> writes:

> When fopen is called after all FILEs have been closed, _IO_list_all is
> NULL.  Check _IO_list_all != NULL in _IO_link_in.  This fixes BZ #31963.

r-b for either patch

>
> Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
> ---
>  libio/Makefile     |  1 +
>  libio/genops.c     |  3 ++-
>  libio/tst-fopen1.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 48 insertions(+), 1 deletion(-)
>  create mode 100644 libio/tst-fopen1.c
>
> 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>
  

Patch

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>