fixincludes: Declare memmem if it's not declared in system headers [PR109293]

Message ID 20230327175908.424052-1-xry111@xry111.site
State New
Headers
Series fixincludes: Declare memmem if it's not declared in system headers [PR109293] |

Commit Message

Xi Ruoyao March 27, 2023, 5:59 p.m. UTC
  memmem is not POSIX so the system may lack it.  Then libiberty will
provide an implementation, but it's a "supplemental function" and not
declared in libiberty.h.  We need to declare the prototype to use it
then.

See libiberty doc at
https://gcc.gnu.org/onlinedocs/libiberty/Supplemental-Functions.html.

Tested by bootstrapping GCC in the following container environments on
x86_64-linux-gnu:

1. "Vanilla" system with memmem in Glibc.
2. memmem removed from string.h.
3. memmem removed from both string.h and libc.so.

For 3, also verified that memmem from libiberty is linked into fixincl
executable.

Ok for trunk?

fixincludes/ChangeLog:

	PR other/109293
	* configure.ac (AC_CHECK_DECLS): Add memmem.
	* configure: Regenerate.
	* config.h.in: Regenerate.
	* system.h (memmem): Declare if HAVE_DECL_MEMMEM is zero.
---
 fixincludes/config.h.in  |  4 ++++
 fixincludes/configure    | 10 ++++++++++
 fixincludes/configure.ac |  2 +-
 fixincludes/system.h     |  4 ++++
 4 files changed, 19 insertions(+), 1 deletion(-)
  

Comments

Richard Biener March 28, 2023, 6:35 a.m. UTC | #1
On Mon, Mar 27, 2023 at 8:00 PM Xi Ruoyao via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> memmem is not POSIX so the system may lack it.  Then libiberty will
> provide an implementation, but it's a "supplemental function" and not
> declared in libiberty.h.  We need to declare the prototype to use it
> then.

Was there a reason to use memmem over strstr in the first place?  Otherwise
looks reasonable.

Richard.

> See libiberty doc at
> https://gcc.gnu.org/onlinedocs/libiberty/Supplemental-Functions.html.
>
> Tested by bootstrapping GCC in the following container environments on
> x86_64-linux-gnu:
>
> 1. "Vanilla" system with memmem in Glibc.
> 2. memmem removed from string.h.
> 3. memmem removed from both string.h and libc.so.
>
> For 3, also verified that memmem from libiberty is linked into fixincl
> executable.
>
> Ok for trunk?
>
> fixincludes/ChangeLog:
>
>         PR other/109293
>         * configure.ac (AC_CHECK_DECLS): Add memmem.
>         * configure: Regenerate.
>         * config.h.in: Regenerate.
>         * system.h (memmem): Declare if HAVE_DECL_MEMMEM is zero.
> ---
>  fixincludes/config.h.in  |  4 ++++
>  fixincludes/configure    | 10 ++++++++++
>  fixincludes/configure.ac |  2 +-
>  fixincludes/system.h     |  4 ++++
>  4 files changed, 19 insertions(+), 1 deletion(-)
>
> diff --git a/fixincludes/config.h.in b/fixincludes/config.h.in
> index 69a67f5f116..0fd21b721b9 100644
> --- a/fixincludes/config.h.in
> +++ b/fixincludes/config.h.in
> @@ -78,6 +78,10 @@
>     don't. */
>  #undef HAVE_DECL_GETC_UNLOCKED
>
> +/* Define to 1 if you have the declaration of `memmem', and to 0 if you don't.
> +   */
> +#undef HAVE_DECL_MEMMEM
> +
>  /* Define to 1 if you have the declaration of `putchar_unlocked', and to 0 if
>     you don't. */
>  #undef HAVE_DECL_PUTCHAR_UNLOCKED
> diff --git a/fixincludes/configure b/fixincludes/configure
> index b3bca666a4d..bdcc41f6ddc 100755
> --- a/fixincludes/configure
> +++ b/fixincludes/configure
> @@ -5043,6 +5043,16 @@ fi
>  cat >>confdefs.h <<_ACEOF
>  #define HAVE_DECL_VASPRINTF $ac_have_decl
>  _ACEOF
> +ac_fn_c_check_decl "$LINENO" "memmem" "ac_cv_have_decl_memmem" "$ac_includes_default"
> +if test "x$ac_cv_have_decl_memmem" = xyes; then :
> +  ac_have_decl=1
> +else
> +  ac_have_decl=0
> +fi
> +
> +cat >>confdefs.h <<_ACEOF
> +#define HAVE_DECL_MEMMEM $ac_have_decl
> +_ACEOF
>
>  ac_fn_c_check_decl "$LINENO" "clearerr_unlocked" "ac_cv_have_decl_clearerr_unlocked" "$ac_includes_default"
>  if test "x$ac_cv_have_decl_clearerr_unlocked" = xyes; then :
> diff --git a/fixincludes/configure.ac b/fixincludes/configure.ac
> index 14813b910f1..ef2227e3c93 100644
> --- a/fixincludes/configure.ac
> +++ b/fixincludes/configure.ac
> @@ -88,7 +88,7 @@ define(fixincludes_UNLOCKED_FUNCS, clearerr_unlocked feof_unlocked dnl
>    fread_unlocked fwrite_unlocked getchar_unlocked getc_unlocked dnl
>    putchar_unlocked putc_unlocked)
>  AC_CHECK_FUNCS(fixincludes_UNLOCKED_FUNCS)
> -AC_CHECK_DECLS([abort, asprintf, basename(char *), errno, vasprintf])
> +AC_CHECK_DECLS([abort, asprintf, basename(char *), errno, vasprintf, memmem])
>  AC_CHECK_DECLS(m4_split(m4_normalize(fixincludes_UNLOCKED_FUNCS)))
>
>  # Checks for typedefs, structures, and compiler characteristics.
> diff --git a/fixincludes/system.h b/fixincludes/system.h
> index dca5d57b2e3..687fb2e2025 100644
> --- a/fixincludes/system.h
> +++ b/fixincludes/system.h
> @@ -209,6 +209,10 @@ extern int errno;
>  extern void abort (void);
>  #endif
>
> +#if defined (HAVE_DECL_MEMMEM) && !HAVE_DECL_MEMMEM
> +extern void *memmem (const void *, size_t, const void *, size_t);
> +#endif
> +
>  #if HAVE_SYS_STAT_H
>  # include <sys/stat.h>
>  #endif
> --
> 2.40.0
>
  
Xi Ruoyao March 28, 2023, 7:27 a.m. UTC | #2
On Tue, 2023-03-28 at 08:35 +0200, Richard Biener wrote:
> On Mon, Mar 27, 2023 at 8:00 PM Xi Ruoyao via Gcc-patches
> <gcc-patches@gcc.gnu.org> wrote:
> > 
> > memmem is not POSIX so the system may lack it.  Then libiberty will
> > provide an implementation, but it's a "supplemental function" and not
> > declared in libiberty.h.  We need to declare the prototype to use it
> > then.
> 
> Was there a reason to use memmem over strstr in the first place?  Otherwise
> looks reasonable.

Because we need to limit the range for matching in [begin, pos).  If
using strstr, we have to set *pos = 0, then change it back after the
matching.  I remember I tried this in the first place but the code did
not look very pretty.

Pushed r13-6888.  Will make a backport into gcc-12 too after a
bootstrapping for verification.
>
  

Patch

diff --git a/fixincludes/config.h.in b/fixincludes/config.h.in
index 69a67f5f116..0fd21b721b9 100644
--- a/fixincludes/config.h.in
+++ b/fixincludes/config.h.in
@@ -78,6 +78,10 @@ 
    don't. */
 #undef HAVE_DECL_GETC_UNLOCKED
 
+/* Define to 1 if you have the declaration of `memmem', and to 0 if you don't.
+   */
+#undef HAVE_DECL_MEMMEM
+
 /* Define to 1 if you have the declaration of `putchar_unlocked', and to 0 if
    you don't. */
 #undef HAVE_DECL_PUTCHAR_UNLOCKED
diff --git a/fixincludes/configure b/fixincludes/configure
index b3bca666a4d..bdcc41f6ddc 100755
--- a/fixincludes/configure
+++ b/fixincludes/configure
@@ -5043,6 +5043,16 @@  fi
 cat >>confdefs.h <<_ACEOF
 #define HAVE_DECL_VASPRINTF $ac_have_decl
 _ACEOF
+ac_fn_c_check_decl "$LINENO" "memmem" "ac_cv_have_decl_memmem" "$ac_includes_default"
+if test "x$ac_cv_have_decl_memmem" = xyes; then :
+  ac_have_decl=1
+else
+  ac_have_decl=0
+fi
+
+cat >>confdefs.h <<_ACEOF
+#define HAVE_DECL_MEMMEM $ac_have_decl
+_ACEOF
 
 ac_fn_c_check_decl "$LINENO" "clearerr_unlocked" "ac_cv_have_decl_clearerr_unlocked" "$ac_includes_default"
 if test "x$ac_cv_have_decl_clearerr_unlocked" = xyes; then :
diff --git a/fixincludes/configure.ac b/fixincludes/configure.ac
index 14813b910f1..ef2227e3c93 100644
--- a/fixincludes/configure.ac
+++ b/fixincludes/configure.ac
@@ -88,7 +88,7 @@  define(fixincludes_UNLOCKED_FUNCS, clearerr_unlocked feof_unlocked dnl
   fread_unlocked fwrite_unlocked getchar_unlocked getc_unlocked dnl
   putchar_unlocked putc_unlocked)
 AC_CHECK_FUNCS(fixincludes_UNLOCKED_FUNCS)
-AC_CHECK_DECLS([abort, asprintf, basename(char *), errno, vasprintf])
+AC_CHECK_DECLS([abort, asprintf, basename(char *), errno, vasprintf, memmem])
 AC_CHECK_DECLS(m4_split(m4_normalize(fixincludes_UNLOCKED_FUNCS)))
 
 # Checks for typedefs, structures, and compiler characteristics.
diff --git a/fixincludes/system.h b/fixincludes/system.h
index dca5d57b2e3..687fb2e2025 100644
--- a/fixincludes/system.h
+++ b/fixincludes/system.h
@@ -209,6 +209,10 @@  extern int errno;
 extern void abort (void);
 #endif
 
+#if defined (HAVE_DECL_MEMMEM) && !HAVE_DECL_MEMMEM
+extern void *memmem (const void *, size_t, const void *, size_t);
+#endif
+
 #if HAVE_SYS_STAT_H
 # include <sys/stat.h>
 #endif