public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Clean up conditionals for declaration of gets().
@ 2016-11-21 17:44 Zack Weinberg
  2016-11-27  7:14 ` Mike Frysinger
  0 siblings, 1 reply; 7+ messages in thread
From: Zack Weinberg @ 2016-11-21 17:44 UTC (permalink / raw)
  To: GNU C Library

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 and 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.

Tested on x86_64-linux-gnu, OK?

(If you're wondering why this patch doesn't touch libio/iogets.c, it is
because that file defines _IO_gets, not gets.  Yes, this raises
troubling questions about inconsistencies between the internal
declarations in libioP.h and the public ones in stdio.h.)

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/tst-chk1.c        |  5 +++++
 include/features.h      |  9 +++++++++
 include/stdio.h         | 18 ------------------
 libio/bits/stdio2.h     |  3 +--
 libio/stdio.h           | 13 +++++--------
 stdio-common/Makefile   |  3 +++
 stdio-common/tst-gets.c |  6 +++++-
 7 files changed, 28 insertions(+), 29 deletions(-)

diff --git a/debug/tst-chk1.c b/debug/tst-chk1.c
index 478c2fb..3e9d3a5 100644
--- a/debug/tst-chk1.c
+++ b/debug/tst-chk1.c
@@ -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>
diff --git a/include/features.h b/include/features.h
index 650d4c5..94feb1d 100644
--- a/include/features.h
+++ b/include/features.h
@@ -370,6 +370,15 @@
 # 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, ISO C++14, and _GNU_SOURCE.  */
+#if !defined __USE_ISOC11 \
+    || (defined __cplusplus && __cplusplus <= 201103L)
+# define __GLIBC_USE_DEPRECATED_GETS 1
+#else
+# define __GLIBC_USE_DEPRECATED_GETS 0
+#endif
+
 /* Get definitions of __STDC_* predefined macros, if the compiler has
    not preincluded this header automatically.  */
 #include <stdc-predef.h>
diff --git a/include/stdio.h b/include/stdio.h
index 266c3bb..5965439 100644
--- a/include/stdio.h
+++ b/include/stdio.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)
 diff --git a/libio/bits/stdio2.h b/libio/bits/stdio2.h
index fa3be79..df9662c 100644
--- a/libio/bits/stdio2.h
+++ b/libio/bits/stdio2.h
@@ -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 "
diff --git a/libio/stdio.h b/libio/stdio.h
index e37f901..792604f 100644
--- a/libio/stdio.h
+++ b/libio/stdio.h
@@ -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.  */
diff --git a/stdio-common/Makefile b/stdio-common/Makefile
index 4c4834b..dcbf103 100644
--- a/stdio-common/Makefile
+++ b/stdio-common/Makefile
@@ -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
diff --git a/stdio-common/tst-gets.c b/stdio-common/tst-gets.c
index 073bc54..b12a7ed 100644
--- a/stdio-common/tst-gets.c
+++ b/stdio-common/tst-gets.c
@@ -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)
 {
-- 
2.10.2

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Clean up conditionals for declaration of gets().
  2016-11-21 17:44 [PATCH] Clean up conditionals for declaration of gets() Zack Weinberg
@ 2016-11-27  7:14 ` Mike Frysinger
  2016-11-27 17:52   ` Zack Weinberg
  0 siblings, 1 reply; 7+ messages in thread
From: Mike Frysinger @ 2016-11-27  7:14 UTC (permalink / raw)
  To: Zack Weinberg; +Cc: GNU C Library

[-- Attachment #1: Type: text/plain, Size: 2122 bytes --]

On 21 Nov 2016 12:44, 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 and 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.

for the tests, what if we forced an older standard ?  that'd provide
double test coverage by also making sure things build in that mode.

your patch appears to have been mangled in transit.  might want to
check that.

> --- a/debug/tst-chk1.c
> +++ b/debug/tst-chk1.c
> @@ -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.  */

needs a newline above the comment

> --- a/include/features.h
> +++ b/include/features.h
> @@ -370,6 +370,15 @@
>  # 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, ISO C++14, and _GNU_SOURCE.  */

needs a newline above the comment

> --- a/stdio-common/tst-gets.c
> +++ b/stdio-common/tst-gets.c
> @@ -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.  */

here too
-mike

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Clean up conditionals for declaration of gets().
  2016-11-27  7:14 ` Mike Frysinger
@ 2016-11-27 17:52   ` Zack Weinberg
  0 siblings, 0 replies; 7+ messages in thread
From: Zack Weinberg @ 2016-11-27 17:52 UTC (permalink / raw)
  To: Zack Weinberg, GNU C Library

On Sun, Nov 27, 2016 at 2:13 AM, Mike Frysinger <vapier@gentoo.org> wrote:
> On 21 Nov 2016 12:44, 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 and 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.
>
> for the tests, what if we forced an older standard ?  that'd provide
> double test coverage by also making sure things build in that mode.

That's a good idea but effectively impossible right now; there's no
way to turn off -include libc-symbols.h even for just one test.
Please don't ask me to add one.

> your patch appears to have been mangled in transit.  might want to
> check that.

Yeah, turns out opening "git format-patch" files in Thunderbird
doesn't do what I wanted it to.  I'll resend these patches properly in
a couple days.

Style errors have been corrected in my tree.

zw

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Clean up conditionals for declaration of gets().
  2017-02-20 13:47 ` Carlos O'Donell
@ 2017-02-25 14:36   ` Zack Weinberg
  0 siblings, 0 replies; 7+ messages in thread
From: Zack Weinberg @ 2017-02-25 14:36 UTC (permalink / raw)
  To: Carlos O'Donell, libc-alpha

On 02/20/2017 08:47 AM, Carlos O'Donell wrote:
> On 02/16/2017 10:01 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 and 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.
>>
>> Tested on x86-64-linux, OK to commit?
> 
> This cleanup looks good to me as long as you tested compiling with
> installed headers and nothing obvious broke (it doesn't look like it would).

I have now tested compiling with installed headers, in both C and C++.
gets is declared only when it's supposed to be.  I will commit the patch
shortly.

zw

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Clean up conditionals for declaration of gets().
  2017-02-16 15:12 Zack Weinberg
  2017-02-20 12:17 ` Zack Weinberg
@ 2017-02-20 13:47 ` Carlos O'Donell
  2017-02-25 14:36   ` Zack Weinberg
  1 sibling, 1 reply; 7+ messages in thread
From: Carlos O'Donell @ 2017-02-20 13:47 UTC (permalink / raw)
  To: Zack Weinberg, libc-alpha
  Cc: Joseph Myers, Wilco Dijkstra, Adhemerval Zanella

On 02/16/2017 10:01 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 and 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.
> 
> Tested on x86-64-linux, OK to commit?

This cleanup looks good to me as long as you tested compiling with
installed headers and nothing obvious broke (it doesn't look like it would).

-- 
Cheers,
Carlos.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Clean up conditionals for declaration of gets().
  2017-02-16 15:12 Zack Weinberg
@ 2017-02-20 12:17 ` Zack Weinberg
  2017-02-20 13:47 ` Carlos O'Donell
  1 sibling, 0 replies; 7+ messages in thread
From: Zack Weinberg @ 2017-02-20 12:17 UTC (permalink / raw)
  To: GNU C Library; +Cc: Joseph Myers, Wilco Dijkstra, Adhemerval Zanella

On Thu, Feb 16, 2017 at 10:01 AM, Zack Weinberg <zackw@panix.com> wrote:
>
> 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? It's only been five days, but this patch is quite
straightforward and it's a dependency for the testsuite patch series
I'm about to post.

zw

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH] Clean up conditionals for declaration of gets().
@ 2017-02-16 15:12 Zack Weinberg
  2017-02-20 12:17 ` Zack Weinberg
  2017-02-20 13:47 ` Carlos O'Donell
  0 siblings, 2 replies; 7+ messages in thread
From: Zack Weinberg @ 2017-02-16 15:12 UTC (permalink / raw)
  To: libc-alpha; +Cc: Joseph Myers, Wilco Dijkstra, Adhemerval Zanella

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 and 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.

Tested on x86-64-linux, OK to commit?

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: Remove redundant declaration of gets.
        * 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 |  5 +++++
 8 files changed, 46 insertions(+), 43 deletions(-)

diff --git a/debug/Makefile b/debug/Makefile
index 3d268f0422..cd4975c35b 100644
--- a/debug/Makefile
+++ b/debug/Makefile
@@ -103,21 +103,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++
diff --git a/debug/tst-chk1.c b/debug/tst-chk1.c
index 164d0ae63e..60c8e1e1d0 100644
--- a/debug/tst-chk1.c
+++ b/debug/tst-chk1.c
@@ -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>
diff --git a/include/features.h b/include/features.h
index dc8ae794a5..7de4089ed3 100644
--- a/include/features.h
+++ b/include/features.h
@@ -136,6 +136,7 @@
 #undef	__USE_GNU
 #undef	__USE_FORTIFY_LEVEL
 #undef	__KERNEL_STRICT_NAMES
+#undef	__GLIBC_USE_DEPRECATED_GETS
 
 /* Suppress kernel-name space pollution unless user expressedly asks
    for it.  */
@@ -383,6 +384,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 test
+   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>
diff --git a/include/stdio.h b/include/stdio.h
index 30e737efc2..17b5a05076 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -181,24 +181,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)
 
diff --git a/libio/bits/stdio2.h b/libio/bits/stdio2.h
index e31386dbd7..e9f9d6952b 100644
--- a/libio/bits/stdio2.h
+++ b/libio/bits/stdio2.h
@@ -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 "
diff --git a/libio/stdio.h b/libio/stdio.h
index a589e367a2..c4f734cb3c 100644
--- a/libio/stdio.h
+++ b/libio/stdio.h
@@ -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.  */
diff --git a/stdio-common/Makefile b/stdio-common/Makefile
index f693fa001f..01a6dd0eda 100644
--- a/stdio-common/Makefile
+++ b/stdio-common/Makefile
@@ -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
diff --git a/stdio-common/tst-gets.c b/stdio-common/tst-gets.c
index 102f235e53..9340c76f8a 100644
--- a/stdio-common/tst-gets.c
+++ b/stdio-common/tst-gets.c
@@ -17,6 +17,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 <stdio.h>
 #include <string.h>
 
-- 
2.11.0

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2017-02-25 14:36 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-11-21 17:44 [PATCH] Clean up conditionals for declaration of gets() Zack Weinberg
2016-11-27  7:14 ` Mike Frysinger
2016-11-27 17:52   ` Zack Weinberg
2017-02-16 15:12 Zack Weinberg
2017-02-20 12:17 ` Zack Weinberg
2017-02-20 13:47 ` Carlos O'Donell
2017-02-25 14:36   ` Zack Weinberg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).