1feee8
commit 2a44960cbc78713c6a2721683a4319d50e71a01f
1feee8
Author: Tulio Magno Quites Machado Filho <tuliom@linux.ibm.com>
1feee8
Date:   Thu Jul 7 18:12:58 2022 -0300
1feee8
1feee8
    Apply asm redirections in stdio.h before first use [BZ #27087]
1feee8
    
1feee8
    Compilers may not be able to apply asm redirections to functions after
1feee8
    these functions are used for the first time, e.g. clang 13.
1feee8
    Fix [BZ #27087] by applying all long double-related asm redirections
1feee8
    before using functions in bits/stdio.h.
1feee8
    However, as these asm redirections depend on the declarations provided
1feee8
    by libio/bits/stdio2.h, this header was split in 2:
1feee8
    
1feee8
     - libio/bits/stdio2-decl.h contains all function declarations;
1feee8
     - libio/bits/stdio2.h remains with the remaining contents, including
1feee8
       redirections.
1feee8
    
1feee8
    This also adds the access attribute to __vsnprintf_chk that was missing.
1feee8
    
1feee8
    Tested with build-many-glibcs.py.
1feee8
    
1feee8
    Reviewed-by: Paul E. Murphy <murphyp@linux.ibm.com>
1feee8
    (cherry picked from commit d0fa09a7701956036ff36f8ca188e9fff81553d8)
1feee8
1feee8
diff --git a/include/bits/stdio2-decl.h b/include/bits/stdio2-decl.h
1feee8
new file mode 100644
1feee8
index 0000000000000000..bbb052f192218219
1feee8
--- /dev/null
1feee8
+++ b/include/bits/stdio2-decl.h
1feee8
@@ -0,0 +1 @@
1feee8
+#include <libio/bits/stdio2-decl.h>
1feee8
diff --git a/libio/Makefile b/libio/Makefile
1feee8
index 5336b7d59584927f..981c876940f67fbf 100644
1feee8
--- a/libio/Makefile
1feee8
+++ b/libio/Makefile
1feee8
@@ -23,7 +23,7 @@ subdir	:= libio
1feee8
 include ../Makeconfig
1feee8
 
1feee8
 headers	:= stdio.h \
1feee8
-	   bits/stdio.h bits/stdio2.h bits/stdio-ldbl.h \
1feee8
+	   bits/stdio.h bits/stdio2.h bits/stdio2-decl.h bits/stdio-ldbl.h \
1feee8
 	   bits/types/FILE.h bits/types/__FILE.h bits/types/struct_FILE.h \
1feee8
 	   bits/types/__fpos_t.h bits/types/__fpos64_t.h \
1feee8
 	   bits/types/cookie_io_functions_t.h
1feee8
diff --git a/libio/bits/stdio2-decl.h b/libio/bits/stdio2-decl.h
1feee8
new file mode 100644
1feee8
index 0000000000000000..e398f7182b98e4d7
1feee8
--- /dev/null
1feee8
+++ b/libio/bits/stdio2-decl.h
1feee8
@@ -0,0 +1,111 @@
1feee8
+/* Checking macros for stdio functions. Declarations only.
1feee8
+   Copyright (C) 2004-2022 Free Software Foundation, Inc.
1feee8
+   This file is part of the GNU C Library.
1feee8
+
1feee8
+   The GNU C Library is free software; you can redistribute it and/or
1feee8
+   modify it under the terms of the GNU Lesser General Public
1feee8
+   License as published by the Free Software Foundation; either
1feee8
+   version 2.1 of the License, or (at your option) any later version.
1feee8
+
1feee8
+   The GNU C Library is distributed in the hope that it will be useful,
1feee8
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
1feee8
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1feee8
+   Lesser General Public License for more details.
1feee8
+
1feee8
+   You should have received a copy of the GNU Lesser General Public
1feee8
+   License along with the GNU C Library; if not, see
1feee8
+   <https://www.gnu.org/licenses/>.  */
1feee8
+
1feee8
+#ifndef _BITS_STDIO2_DEC_H
1feee8
+#define _BITS_STDIO2_DEC_H 1
1feee8
+
1feee8
+#ifndef _STDIO_H
1feee8
+# error "Never include <bits/stdio2-decl.h> directly; use <stdio.h> instead."
1feee8
+#endif
1feee8
+
1feee8
+extern int __sprintf_chk (char *__restrict __s, int __flag, size_t __slen,
1feee8
+			  const char *__restrict __format, ...) __THROW
1feee8
+    __attr_access ((__write_only__, 1, 3));
1feee8
+extern int __vsprintf_chk (char *__restrict __s, int __flag, size_t __slen,
1feee8
+			   const char *__restrict __format,
1feee8
+			   __gnuc_va_list __ap) __THROW
1feee8
+    __attr_access ((__write_only__, 1, 3));
1feee8
+
1feee8
+#if defined __USE_ISOC99 || defined __USE_UNIX98
1feee8
+
1feee8
+extern int __snprintf_chk (char *__restrict __s, size_t __n, int __flag,
1feee8
+			   size_t __slen, const char *__restrict __format,
1feee8
+			   ...) __THROW
1feee8
+    __attr_access ((__write_only__, 1, 2));
1feee8
+extern int __vsnprintf_chk (char *__restrict __s, size_t __n, int __flag,
1feee8
+			    size_t __slen, const char *__restrict __format,
1feee8
+			    __gnuc_va_list __ap) __THROW
1feee8
+    __attr_access ((__write_only__, 1, 2));
1feee8
+
1feee8
+#endif
1feee8
+
1feee8
+#if __USE_FORTIFY_LEVEL > 1
1feee8
+
1feee8
+extern int __fprintf_chk (FILE *__restrict __stream, int __flag,
1feee8
+			  const char *__restrict __format, ...);
1feee8
+extern int __printf_chk (int __flag, const char *__restrict __format, ...);
1feee8
+extern int __vfprintf_chk (FILE *__restrict __stream, int __flag,
1feee8
+			   const char *__restrict __format, __gnuc_va_list __ap);
1feee8
+extern int __vprintf_chk (int __flag, const char *__restrict __format,
1feee8
+			  __gnuc_va_list __ap);
1feee8
+
1feee8
+# ifdef __USE_XOPEN2K8
1feee8
+extern int __dprintf_chk (int __fd, int __flag, const char *__restrict __fmt,
1feee8
+			  ...) __attribute__ ((__format__ (__printf__, 3, 4)));
1feee8
+extern int __vdprintf_chk (int __fd, int __flag,
1feee8
+			   const char *__restrict __fmt, __gnuc_va_list __arg)
1feee8
+     __attribute__ ((__format__ (__printf__, 3, 0)));
1feee8
+# endif
1feee8
+
1feee8
+# ifdef __USE_GNU
1feee8
+
1feee8
+extern int __asprintf_chk (char **__restrict __ptr, int __flag,
1feee8
+			   const char *__restrict __fmt, ...)
1feee8
+     __THROW __attribute__ ((__format__ (__printf__, 3, 4))) __wur;
1feee8
+extern int __vasprintf_chk (char **__restrict __ptr, int __flag,
1feee8
+			    const char *__restrict __fmt, __gnuc_va_list __arg)
1feee8
+     __THROW __attribute__ ((__format__ (__printf__, 3, 0))) __wur;
1feee8
+extern int __obstack_printf_chk (struct obstack *__restrict __obstack,
1feee8
+				 int __flag, const char *__restrict __format,
1feee8
+				 ...)
1feee8
+     __THROW __attribute__ ((__format__ (__printf__, 3, 4)));
1feee8
+extern int __obstack_vprintf_chk (struct obstack *__restrict __obstack,
1feee8
+				  int __flag,
1feee8
+				  const char *__restrict __format,
1feee8
+				  __gnuc_va_list __args)
1feee8
+     __THROW __attribute__ ((__format__ (__printf__, 3, 0)));
1feee8
+
1feee8
+# endif
1feee8
+#endif
1feee8
+
1feee8
+#if __GLIBC_USE (DEPRECATED_GETS)
1feee8
+extern char *__gets_chk (char *__str, size_t) __wur;
1feee8
+#endif
1feee8
+
1feee8
+extern char *__fgets_chk (char *__restrict __s, size_t __size, int __n,
1feee8
+			  FILE *__restrict __stream)
1feee8
+    __wur __attr_access ((__write_only__, 1, 3));
1feee8
+
1feee8
+extern size_t __fread_chk (void *__restrict __ptr, size_t __ptrlen,
1feee8
+			   size_t __size, size_t __n,
1feee8
+			   FILE *__restrict __stream) __wur;
1feee8
+
1feee8
+#ifdef __USE_GNU
1feee8
+extern char *__fgets_unlocked_chk (char *__restrict __s, size_t __size,
1feee8
+				   int __n, FILE *__restrict __stream)
1feee8
+    __wur __attr_access ((__write_only__, 1, 3));
1feee8
+#endif
1feee8
+
1feee8
+#ifdef __USE_MISC
1feee8
+# undef fread_unlocked
1feee8
+extern size_t __fread_unlocked_chk (void *__restrict __ptr, size_t __ptrlen,
1feee8
+				    size_t __size, size_t __n,
1feee8
+				    FILE *__restrict __stream) __wur;
1feee8
+#endif
1feee8
+
1feee8
+#endif /* bits/stdio2-decl.h.  */
1feee8
diff --git a/libio/bits/stdio2.h b/libio/bits/stdio2.h
1feee8
index 40ff16b01b4f4876..4570f86a4496c1ee 100644
1feee8
--- a/libio/bits/stdio2.h
1feee8
+++ b/libio/bits/stdio2.h
1feee8
@@ -23,14 +23,6 @@
1feee8
 # error "Never include <bits/stdio2.h> directly; use <stdio.h> instead."
1feee8
 #endif
1feee8
 
1feee8
-extern int __sprintf_chk (char *__restrict __s, int __flag, size_t __slen,
1feee8
-			  const char *__restrict __format, ...) __THROW
1feee8
-    __attr_access ((__write_only__, 1, 3));
1feee8
-extern int __vsprintf_chk (char *__restrict __s, int __flag, size_t __slen,
1feee8
-			   const char *__restrict __format,
1feee8
-			   __gnuc_va_list __ap) __THROW
1feee8
-    __attr_access ((__write_only__, 1, 3));
1feee8
-
1feee8
 #ifdef __va_arg_pack
1feee8
 __fortify_function int
1feee8
 __NTH (sprintf (char *__restrict __s, const char *__restrict __fmt, ...))
1feee8
@@ -54,15 +46,6 @@ __NTH (vsprintf (char *__restrict __s, const char *__restrict __fmt,
1feee8
 }
1feee8
 
1feee8
 #if defined __USE_ISOC99 || defined __USE_UNIX98
1feee8
-
1feee8
-extern int __snprintf_chk (char *__restrict __s, size_t __n, int __flag,
1feee8
-			   size_t __slen, const char *__restrict __format,
1feee8
-			   ...) __THROW
1feee8
-    __attr_access ((__write_only__, 1, 2));
1feee8
-extern int __vsnprintf_chk (char *__restrict __s, size_t __n, int __flag,
1feee8
-			    size_t __slen, const char *__restrict __format,
1feee8
-			    __gnuc_va_list __ap) __THROW;
1feee8
-
1feee8
 # ifdef __va_arg_pack
1feee8
 __fortify_function int
1feee8
 __NTH (snprintf (char *__restrict __s, size_t __n,
1feee8
@@ -89,15 +72,6 @@ __NTH (vsnprintf (char *__restrict __s, size_t __n,
1feee8
 #endif
1feee8
 
1feee8
 #if __USE_FORTIFY_LEVEL > 1
1feee8
-
1feee8
-extern int __fprintf_chk (FILE *__restrict __stream, int __flag,
1feee8
-			  const char *__restrict __format, ...);
1feee8
-extern int __printf_chk (int __flag, const char *__restrict __format, ...);
1feee8
-extern int __vfprintf_chk (FILE *__restrict __stream, int __flag,
1feee8
-			   const char *__restrict __format, __gnuc_va_list __ap);
1feee8
-extern int __vprintf_chk (int __flag, const char *__restrict __format,
1feee8
-			  __gnuc_va_list __ap);
1feee8
-
1feee8
 # ifdef __va_arg_pack
1feee8
 __fortify_function int
1feee8
 fprintf (FILE *__restrict __stream, const char *__restrict __fmt, ...)
1feee8
@@ -136,12 +110,6 @@ vfprintf (FILE *__restrict __stream,
1feee8
 }
1feee8
 
1feee8
 # ifdef __USE_XOPEN2K8
1feee8
-extern int __dprintf_chk (int __fd, int __flag, const char *__restrict __fmt,
1feee8
-			  ...) __attribute__ ((__format__ (__printf__, 3, 4)));
1feee8
-extern int __vdprintf_chk (int __fd, int __flag,
1feee8
-			   const char *__restrict __fmt, __gnuc_va_list __arg)
1feee8
-     __attribute__ ((__format__ (__printf__, 3, 0)));
1feee8
-
1feee8
 #  ifdef __va_arg_pack
1feee8
 __fortify_function int
1feee8
 dprintf (int __fd, const char *__restrict __fmt, ...)
1feee8
@@ -162,23 +130,6 @@ vdprintf (int __fd, const char *__restrict __fmt, __gnuc_va_list __ap)
1feee8
 # endif
1feee8
 
1feee8
 # ifdef __USE_GNU
1feee8
-
1feee8
-extern int __asprintf_chk (char **__restrict __ptr, int __flag,
1feee8
-			   const char *__restrict __fmt, ...)
1feee8
-     __THROW __attribute__ ((__format__ (__printf__, 3, 4))) __wur;
1feee8
-extern int __vasprintf_chk (char **__restrict __ptr, int __flag,
1feee8
-			    const char *__restrict __fmt, __gnuc_va_list __arg)
1feee8
-     __THROW __attribute__ ((__format__ (__printf__, 3, 0))) __wur;
1feee8
-extern int __obstack_printf_chk (struct obstack *__restrict __obstack,
1feee8
-				 int __flag, const char *__restrict __format,
1feee8
-				 ...)
1feee8
-     __THROW __attribute__ ((__format__ (__printf__, 3, 4)));
1feee8
-extern int __obstack_vprintf_chk (struct obstack *__restrict __obstack,
1feee8
-				  int __flag,
1feee8
-				  const char *__restrict __format,
1feee8
-				  __gnuc_va_list __args)
1feee8
-     __THROW __attribute__ ((__format__ (__printf__, 3, 0)));
1feee8
-
1feee8
 #  ifdef __va_arg_pack
1feee8
 __fortify_function int
1feee8
 __NTH (asprintf (char **__restrict __ptr, const char *__restrict __fmt, ...))
1feee8
@@ -231,7 +182,6 @@ __NTH (obstack_vprintf (struct obstack *__restrict __obstack,
1feee8
 #endif
1feee8
 
1feee8
 #if __GLIBC_USE (DEPRECATED_GETS)
1feee8
-extern char *__gets_chk (char *__str, size_t) __wur;
1feee8
 extern char *__REDIRECT (__gets_warn, (char *__str), gets)
1feee8
      __wur __warnattr ("please use fgets or getline instead, gets can't "
1feee8
 		       "specify buffer size");
1feee8
@@ -245,9 +195,6 @@ gets (char *__str)
1feee8
 }
1feee8
 #endif
1feee8
 
1feee8
-extern char *__fgets_chk (char *__restrict __s, size_t __size, int __n,
1feee8
-			  FILE *__restrict __stream)
1feee8
-    __wur __attr_access ((__write_only__, 1, 3));
1feee8
 extern char *__REDIRECT (__fgets_alias,
1feee8
 			 (char *__restrict __s, int __n,
1feee8
 			  FILE *__restrict __stream), fgets)
1feee8
@@ -269,9 +216,6 @@ fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
1feee8
   return __fgets_chk (__s, sz, __n, __stream);
1feee8
 }
1feee8
 
1feee8
-extern size_t __fread_chk (void *__restrict __ptr, size_t __ptrlen,
1feee8
-			   size_t __size, size_t __n,
1feee8
-			   FILE *__restrict __stream) __wur;
1feee8
 extern size_t __REDIRECT (__fread_alias,
1feee8
 			  (void *__restrict __ptr, size_t __size,
1feee8
 			   size_t __n, FILE *__restrict __stream),
1feee8
@@ -297,9 +241,6 @@ fread (void *__restrict __ptr, size_t __size, size_t __n,
1feee8
 }
1feee8
 
1feee8
 #ifdef __USE_GNU
1feee8
-extern char *__fgets_unlocked_chk (char *__restrict __s, size_t __size,
1feee8
-				   int __n, FILE *__restrict __stream)
1feee8
-    __wur __attr_access ((__write_only__, 1, 3));
1feee8
 extern char *__REDIRECT (__fgets_unlocked_alias,
1feee8
 			 (char *__restrict __s, int __n,
1feee8
 			  FILE *__restrict __stream), fgets_unlocked)
1feee8
@@ -324,9 +265,6 @@ fgets_unlocked (char *__restrict __s, int __n, FILE *__restrict __stream)
1feee8
 
1feee8
 #ifdef __USE_MISC
1feee8
 # undef fread_unlocked
1feee8
-extern size_t __fread_unlocked_chk (void *__restrict __ptr, size_t __ptrlen,
1feee8
-				    size_t __size, size_t __n,
1feee8
-				    FILE *__restrict __stream) __wur;
1feee8
 extern size_t __REDIRECT (__fread_unlocked_alias,
1feee8
 			  (void *__restrict __ptr, size_t __size,
1feee8
 			   size_t __n, FILE *__restrict __stream),
1feee8
diff --git a/libio/stdio.h b/libio/stdio.h
1feee8
index abefe640e52d18d5..d36e61c56bbb3117 100644
1feee8
--- a/libio/stdio.h
1feee8
+++ b/libio/stdio.h
1feee8
@@ -879,20 +879,27 @@ extern void funlockfile (FILE *__stream) __THROW;
1feee8
 extern int __uflow (FILE *);
1feee8
 extern int __overflow (FILE *, int);
1feee8
 
1feee8
+#if __USE_FORTIFY_LEVEL > 0 && defined __fortify_function
1feee8
+/* Declare all functions from bits/stdio2-decl.h first.  */
1feee8
+# include <bits/stdio2-decl.h>
1feee8
+#endif
1feee8
+
1feee8
+/* The following headers provide asm redirections.  These redirections must
1feee8
+   appear before the first usage of these functions, e.g. in bits/stdio.h.  */
1feee8
+#if defined __LDBL_COMPAT || __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI == 1
1feee8
+# include <bits/stdio-ldbl.h>
1feee8
+#endif
1feee8
+
1feee8
 /* If we are compiling with optimizing read this file.  It contains
1feee8
    several optimizing inline functions and macros.  */
1feee8
 #ifdef __USE_EXTERN_INLINES
1feee8
 # include <bits/stdio.h>
1feee8
 #endif
1feee8
 #if __USE_FORTIFY_LEVEL > 0 && defined __fortify_function
1feee8
+/* Now include the function definitions and redirects too.  */
1feee8
 # include <bits/stdio2.h>
1feee8
 #endif
1feee8
 
1feee8
-#include <bits/floatn.h>
1feee8
-#if defined __LDBL_COMPAT || __LDOUBLE_REDIRECTS_TO_FLOAT128_ABI == 1
1feee8
-# include <bits/stdio-ldbl.h>
1feee8
-#endif
1feee8
-
1feee8
 __END_DECLS
1feee8
 
1feee8
 #endif /* <stdio.h> included.  */