[3/5] Clean up conditionals for declaration of gets().
Commit Message
gets() has the dubious honor of being the only C89 library feature
that has been completely removed from the current C and C++ standards.
glibc follows suit by not declaring it in _GNU_SOURCE mode either, but
it remains present in older compatibility modes. Internally, two test
cases need to see stdio.h make the declaration, but all our internal
code is of course compiled under _GNU_SOURCE. This is currently
kludged by duplicating the gets declaration, fortify wrapper and all,
in include/stdio.h. Also, the conditional in the public headers for
deciding when to declare gets is complicated, contains a non-obvious
dependence on short-circuit evaluation, and is repeated in two places.
This patch adds a new macro to features.h that encapsulates the
complicated rule for when to declare gets. stdio.h and bits/stdio2.h
can simply test __GLIBC_USE (DEPRECATED_GETS). Then, instead of
having a duplicate gets declaration in include/stdio.h,
debug/tst-chk1.c and stdio-common/tst-gets.c force that flag on.
zw
* include/features.h (__GLIBC_USE_DEPRECATED_GETS): New macro.
* libio/stdio.h, libio/bits/stdio2.h: Condition gets on
__GLIBC_USE (DEPRECATED_GETS). Update comments to indicate
gets was removed from C++ in C++14.
* include/stdio.h [!_ISOMAC]: Force gets to be declared by
libio/stdio.h, instead of repeating its declaration.
* debug/tst-chk1.c, stdio-common/tst-gets.c: Force gets to
be declared, since we are testing it.
* stdio-common/Makefile (tst-gets.c): Compile with
-Wno-deprecated-declarations.
* debug/Makefile (tst-chk1.c, tst-chk2.c, tst-chk3.c, tst-chk4.cc)
(tst-chk5.cc, tst-chk6.cc, tst-lfschk1.c, tst-lfschk2.c)
(tst-lfschk3.c, tst-lfschk4.cc, tst-lfschk5.cc, tst-lfschk6.cc):
Compile with -Wno-deprecated-declarations.
---
debug/Makefile | 31 ++++++++++++++++---------------
debug/tst-chk1.c | 5 +++++
include/features.h | 11 +++++++++++
include/stdio.h | 18 ------------------
libio/bits/stdio2.h | 3 +--
libio/stdio.h | 13 +++++--------
stdio-common/Makefile | 3 +++
stdio-common/tst-gets.c | 6 +++++-
8 files changed, 46 insertions(+), 44 deletions(-)
Comments
On 11/28/2016 11:00 AM, Zack Weinberg wrote:
> gets() has the dubious honor of being the only C89 library feature
> that has been completely removed from the current C and C++ standards.
> glibc follows suit by not declaring it in _GNU_SOURCE mode either, but
> it remains present in older compatibility modes. Internally, two test
> cases need to see stdio.h make the declaration, but all our internal
> code is of course compiled under _GNU_SOURCE. This is currently
> kludged by duplicating the gets declaration, fortify wrapper and all,
> in include/stdio.h. Also, the conditional in the public headers for
> deciding when to declare gets is complicated, contains a non-obvious
> dependence on short-circuit evaluation, and is repeated in two places.
>
> This patch adds a new macro to features.h that encapsulates the
> complicated rule for when to declare gets. stdio.h and bits/stdio2.h
> can simply test __GLIBC_USE (DEPRECATED_GETS). Then, instead of
> having a duplicate gets declaration in include/stdio.h,
> debug/tst-chk1.c and stdio-common/tst-gets.c force that flag on.
Ping.
@@ -96,21 +96,22 @@ CFLAGS-tst-longjmp_chk3.c = -fexceptions -fasynchronous-unwind-tables
CPPFLAGS-tst-longjmp_chk3.c = -D_FORTIFY_SOURCE=1
# We know these tests have problems with format strings, this is what
-# we are testing. Disable that warning. They also generate warnings
-# from warning attributes, which cannot be disabled via pragmas, so
-# require -Wno-error to be used.
-CFLAGS-tst-chk1.c = -Wno-format -Wno-error
-CFLAGS-tst-chk2.c = -Wno-format -Wno-error
-CFLAGS-tst-chk3.c = -Wno-format -Wno-error
-CFLAGS-tst-chk4.cc = -Wno-format -Wno-error
-CFLAGS-tst-chk5.cc = -Wno-format -Wno-error
-CFLAGS-tst-chk6.cc = -Wno-format -Wno-error
-CFLAGS-tst-lfschk1.c = -Wno-format -Wno-error
-CFLAGS-tst-lfschk2.c = -Wno-format -Wno-error
-CFLAGS-tst-lfschk3.c = -Wno-format -Wno-error
-CFLAGS-tst-lfschk4.cc = -Wno-format -Wno-error
-CFLAGS-tst-lfschk5.cc = -Wno-format -Wno-error
-CFLAGS-tst-lfschk6.cc = -Wno-format -Wno-error
+# we are testing. Disable that warning. They are also testing
+# deprecated functions (notably gets) so disable that warning as well.
+# And they also generate warnings from warning attributes, which
+# cannot be disabled via pragmas, so require -Wno-error to be used.
+CFLAGS-tst-chk1.c = -Wno-format -Wno-deprecated-declarations -Wno-error
+CFLAGS-tst-chk2.c = -Wno-format -Wno-deprecated-declarations -Wno-error
+CFLAGS-tst-chk3.c = -Wno-format -Wno-deprecated-declarations -Wno-error
+CFLAGS-tst-chk4.cc = -Wno-format -Wno-deprecated-declarations -Wno-error
+CFLAGS-tst-chk5.cc = -Wno-format -Wno-deprecated-declarations -Wno-error
+CFLAGS-tst-chk6.cc = -Wno-format -Wno-deprecated-declarations -Wno-error
+CFLAGS-tst-lfschk1.c = -Wno-format -Wno-deprecated-declarations -Wno-error
+CFLAGS-tst-lfschk2.c = -Wno-format -Wno-deprecated-declarations -Wno-error
+CFLAGS-tst-lfschk3.c = -Wno-format -Wno-deprecated-declarations -Wno-error
+CFLAGS-tst-lfschk4.cc = -Wno-format -Wno-deprecated-declarations -Wno-error
+CFLAGS-tst-lfschk5.cc = -Wno-format -Wno-deprecated-declarations -Wno-error
+CFLAGS-tst-lfschk6.cc = -Wno-format -Wno-deprecated-declarations -Wno-error
LDLIBS-tst-chk4 = -lstdc++
LDLIBS-tst-chk5 = -lstdc++
LDLIBS-tst-chk6 = -lstdc++
@@ -16,6 +16,11 @@
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
+/* This file tests gets(). Force it to be declared. */
+#include <features.h>
+#undef __GLIBC_USE_DEPRECATED_GETS
+#define __GLIBC_USE_DEPRECATED_GETS 1
+
#include <assert.h>
#include <fcntl.h>
#include <locale.h>
@@ -133,6 +133,7 @@
#undef __USE_REENTRANT
#undef __USE_FORTIFY_LEVEL
#undef __KERNEL_STRICT_NAMES
+#undef __GLIBC_USE_DEPRECATED_GETS
/* Suppress kernel-name space pollution unless user expressedly asks
for it. */
@@ -370,6 +371,16 @@
# define __USE_FORTIFY_LEVEL 0
#endif
+/* The function 'gets' existed in C89, but is impossible to use
+ safely. It has been removed from ISO C11 and ISO C++14. Note: for
+ compatibility with various implementations of <cstdio>, this condition
+ must consider only the value of __cplusplus when compiling C++. */
+#if defined __cplusplus ? __cplusplus >= 201402L : defined __USE_ISOC11
+# define __GLIBC_USE_DEPRECATED_GETS 0
+#else
+# define __GLIBC_USE_DEPRECATED_GETS 1
+#endif
+
/* Get definitions of __STDC_* predefined macros, if the compiler has
not preincluded this header automatically. */
#include <stdc-predef.h>
@@ -178,24 +178,6 @@ libc_hidden_proto (__vasprintf_chk)
libc_hidden_proto (__vdprintf_chk)
libc_hidden_proto (__obstack_vprintf_chk)
-/* The <stdio.h> header does not include the declaration for gets
- anymore when compiling with _GNU_SOURCE. Provide a copy here. */
-extern char *gets (char *__s);
-# if __USE_FORTIFY_LEVEL > 0
-extern char *__gets_chk (char *__str, size_t) __wur;
-extern char *__REDIRECT (__gets_warn, (char *__str), gets)
- __wur __warnattr ("please use fgets or getline instead, gets can't "
- "specify buffer size");
-
-__fortify_function __wur char *
-gets (char *__str)
-{
- if (__bos (__str) != (size_t) -1)
- return __gets_chk (__str, __bos (__str));
- return __gets_warn (__str);
-}
-# endif
-
extern FILE * __fmemopen (void *buf, size_t len, const char *mode);
libc_hidden_proto (__fmemopen)
@@ -222,8 +222,7 @@ __NTH (obstack_vprintf (struct obstack *__restrict __obstack,
#endif
-#if !defined __USE_ISOC11 \
- || (defined __cplusplus && __cplusplus <= 201103L && !defined __USE_GNU)
+#if __GLIBC_USE (DEPRECATED_GETS)
extern char *__gets_chk (char *__str, size_t) __wur;
extern char *__REDIRECT (__gets_warn, (char *__str), gets)
__wur __warnattr ("please use fgets or getline instead, gets can't "
@@ -625,16 +625,13 @@ __BEGIN_NAMESPACE_STD
extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
__wur;
-#if !defined __USE_ISOC11 \
- || (defined __cplusplus && __cplusplus <= 201103L)
+#if __GLIBC_USE (DEPRECATED_GETS)
/* Get a newline-terminated string from stdin, removing the newline.
- DO NOT USE THIS FUNCTION!! There is no limit on how much it will read.
- The function has been officially removed in ISO C11. This opportunity
- is used to also remove it from the GNU feature list. It is now only
- available when explicitly using an old ISO C, Unix, or POSIX standard.
- GCC defines _GNU_SOURCE when building C++ code and the function is still
- in C++11, so it is also available for C++.
+ This function is impossible to use safely. It has been officially
+ removed from ISO C11 and ISO C++14, and we have also removed it
+ from the _GNU_SOURCE feature list. It remains available when
+ explicitly using an old ISO C, Unix, or POSIX standard.
This function is a possible cancellation point and therefore not
marked with __THROW. */
@@ -141,6 +141,9 @@ CFLAGS-scanf15.c = -I../libio -I../stdlib -I../wcsmbs -I../time -I../string \
CFLAGS-scanf17.c = -I../libio -I../stdlib -I../wcsmbs -I../time -I../string \
-I../wctype
+# tst-gets.c tests a deprecated function.
+CFLAGS-tst-gets.c += -Wno-deprecated-declarations
+
CPPFLAGS += $(libio-mtsafe)
$(objpfx)tst-setvbuf1.out: /dev/null $(objpfx)tst-setvbuf1
@@ -17,10 +17,14 @@
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
+/* This file tests gets(). Force it to be declared. */
+#include <features.h>
+#undef __GLIBC_USE_DEPRECATED_GETS
+#define __GLIBC_USE_DEPRECATED_GETS 1
+
#include <stdio.h>
#include <string.h>
-
static int
do_test (void)
{