Bug 78616 - Poison strndup in system.h
Summary: Poison strndup in system.h
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: bootstrap (show other bugs)
Version: 7.0
: P3 normal
Target Milestone: 7.0
Assignee: David Malcolm
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-11-30 16:45 UTC by Iain Sandoe
Modified: 2017-01-16 15:06 UTC (History)
2 users (show)

See Also:
Host: x86_64-apple-darwin14
Target: x86_64-apple-darwin14
Build: x86_64-apple-darwin14
Known to work:
Known to fail:
Last reconfirmed: 2016-11-30 00:00:00


Attachments
test for strndup and publish it from libiberty if not available from the host. (559 bytes, patch)
2016-11-30 19:27 UTC, Iain Sandoe
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Iain Sandoe 2016-11-30 16:45:14 UTC
longest backtrack:       17 (code:    156)
Shared 29773 out of 59469 states by creating 7857 new states, saving 21916
/src/gcc-trunk/gcc/selftest.c: In function 'void selftest::assert_strndup_eq(const char*, const char*, size_t)':
/src/gcc-trunk/gcc/selftest.c:209:30: error: 'strndup' was not declared in this scope
   char *buf = strndup (src, n);
                              ^
make[3]: *** [selftest.o] Error 1
make[3]: *** Waiting for unfinished jobs....
make[2]: *** [all-stage1-gcc] Error 2
make[1]: *** [stage1-bubble] Error 2

The bootstrap compiler has a sysroot suitable for Darwin10 (which doesn't have strndup, strnlen etc.)
Comment 1 David Malcolm 2016-11-30 16:51:33 UTC
If I'm reading things right, it looks like libiberty provides an implementation of strndup if it's not available, but it doesn't provide a decl for it.
Comment 2 Iain Sandoe 2016-11-30 16:58:00 UTC
(In reply to David Malcolm from comment #1)
> If I'm reading things right, it looks like libiberty provides an
> implementation of strndup if it's not available, but it doesn't provide a
> decl for it.

indeed
$ grep strndup include/libiberty.h 
extern char *xstrndup (const char *, size_t) ATTRIBUTE_MALLOC ATTRIBUTE_RETURNS_NONNULL;

not sure exactly what the phasing is supposed to be.
Comment 3 Iain Sandoe 2016-11-30 17:09:53 UTC
trying this (after regnerating gcc/configure and gcc/config.in)

$ git diff gcc/configure.ac include/libiberty.h 
diff --git a/gcc/configure.ac b/gcc/configure.ac
index 703250f..ece1ffa 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -1306,7 +1306,7 @@ AC_CHECK_DECLS([basename(const char*), strstr(const char*,const char*)], , ,[
 #include "system.h"])
 
 gcc_AC_CHECK_DECLS(getenv atol atoll asprintf sbrk abort atof getcwd getwd \
-       madvise stpcpy strnlen strsignal strverscmp \
+       madvise stpcpy strnlen strndup strsignal strverscmp \
        strtol strtoul strtoll strtoull setenv unsetenv \
        errno snprintf vsnprintf vasprintf malloc realloc calloc \
        free getopt clock getpagesize ffs gcc_UNLOCKED_FUNCS, , ,[
diff --git a/include/libiberty.h b/include/libiberty.h
index 605ff56..0401dac 100644
--- a/include/libiberty.h
+++ b/include/libiberty.h
@@ -670,6 +670,10 @@ extern int vsnprintf (char *, size_t, const char *, va_list) ATTRIBUTE_PRINTF(3,
 extern size_t strnlen (const char *, size_t);
 #endif
 
+#if defined (HAVE_DECL_STRNDUP) && !HAVE_DECL_STRNDUP
+extern size_t strndup (const char *, size_t);
+#endif
+
 #if defined(HAVE_DECL_STRVERSCMP) && !HAVE_DECL_STRVERSCMP
 /* Compare version strings.  */
 extern int strverscmp (const char *, const char *);
Comment 4 Iain Sandoe 2016-11-30 17:13:27 UTC
(In reply to Iain Sandoe from comment #3)
> trying this (after regnerating gcc/configure and gcc/config.in)
> 
> $ git diff gcc/configure.ac include/libiberty.h 
> diff --git a/gcc/configure.ac b/gcc/configure.ac
> index 703250f..ece1ffa 100644
> --- a/gcc/configure.ac
> +++ b/gcc/configure.ac
> @@ -1306,7 +1306,7 @@ AC_CHECK_DECLS([basename(const char*), strstr(const
> char*,const char*)], , ,[
>  #include "system.h"])
>  
>  gcc_AC_CHECK_DECLS(getenv atol atoll asprintf sbrk abort atof getcwd getwd \
> -       madvise stpcpy strnlen strsignal strverscmp \
> +       madvise stpcpy strnlen strndup strsignal strverscmp \
>         strtol strtoul strtoll strtoull setenv unsetenv \
>         errno snprintf vsnprintf vasprintf malloc realloc calloc \
>         free getopt clock getpagesize ffs gcc_UNLOCKED_FUNCS, , ,[
> diff --git a/include/libiberty.h b/include/libiberty.h
> index 605ff56..0401dac 100644
> --- a/include/libiberty.h
> +++ b/include/libiberty.h
> @@ -670,6 +670,10 @@ extern int vsnprintf (char *, size_t, const char *,
> va_list) ATTRIBUTE_PRINTF(3,
>  extern size_t strnlen (const char *, size_t);
>  #endif
>  

oops pasto on strndup signature; meant char * of course


> +#if defined (HAVE_DECL_STRNDUP) && !HAVE_DECL_STRNDUP
> +extern size_t strndup (const char *, size_t);
> +#endif
> +
>  #if defined(HAVE_DECL_STRVERSCMP) && !HAVE_DECL_STRVERSCMP
>  /* Compare version strings.  */
>  extern int strverscmp (const char *, const char *);
Comment 5 Iain Sandoe 2016-11-30 19:27:43 UTC
Created attachment 40210 [details]
test for strndup and publish it from libiberty if not available from the host.


bootstrap (all langs, checking=yes,rtl,tree) completed at 243033 with this patch.
I guess the question is why wasn't the function exported from libiberty before?
Comment 6 Richard Biener 2016-12-01 08:36:22 UTC
Or just use xstrndup like we do in gcc/?
Comment 7 Jakub Jelinek 2016-12-01 10:13:27 UTC
Yeah, I'm susprised strndup isn't poisoned in system.h, strdup is.
selftest.c shouldn't be using strndup, just xstrndup.
Comment 8 David Malcolm 2016-12-01 15:02:20 UTC
Thanks.  I'm working on a patch to remove the use of strndup.  Sorry for the breakage.
Comment 9 David Malcolm 2016-12-02 22:40:14 UTC
Author: dmalcolm
Date: Fri Dec  2 22:39:43 2016
New Revision: 243207

URL: https://gcc.gnu.org/viewcvs?rev=243207&root=gcc&view=rev
Log:
selftest.c: remove calls to strndup (PR bootstrap/78616)

gcc/ChangeLog:
	PR bootstrap/78616
	* selftest.c (selftest::assert_strndup_eq): Rename to...
	(selftest::assert_xstrndup_eq): ...this, and remove call to
	strndup.
	(selftest::test_strndup): Rename to...
	(selftest::test_xstrndup): ...this, updating for above renaming.
	(selftest::test_libiberty): Update for renaming.


Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/selftest.c
Comment 10 David Malcolm 2016-12-02 22:47:39 UTC
Build breakage should have been fixed as of r243207 (sorry again).

Should we poision strndup in system.h?
Comment 11 Eric Gallager 2016-12-03 13:54:49 UTC
(In reply to David Malcolm from comment #10)
> Build breakage should have been fixed as of r243207 (sorry again).
> 
> Should we poision strndup in system.h?

I think so, I meant to ask that last time this happened in bug 67363 but never got around to it.
Comment 12 Jeffrey A. Law 2017-01-05 08:38:43 UTC
Regression fixed (regression marker removed).  All that's left is to poison strndup in system.h
Comment 13 David Malcolm 2017-01-13 20:43:33 UTC
(In reply to Jeffrey A. Law from comment #12)
> Regression fixed (regression marker removed).  All that's left is to poison
> strndup in system.h

Candidate patch:
  https://gcc.gnu.org/ml/gcc-patches/2017-01/msg00998.html
Comment 14 David Malcolm 2017-01-16 15:04:13 UTC
Author: dmalcolm
Date: Mon Jan 16 15:03:41 2017
New Revision: 244494

URL: https://gcc.gnu.org/viewcvs?rev=244494&root=gcc&view=rev
Log:
system.h: Poison strndup (PR bootstrap/78616)

gcc/ChangeLog:
	PR bootstrap/78616
	* system.h: Poison strndup.


Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/system.h
Comment 15 David Malcolm 2017-01-16 15:06:29 UTC
(In reply to Jeffrey A. Law from comment #12)
> Regression fixed (regression marker removed).  All that's left is to poison
> strndup in system.h

Poisoned in r244494; marking this PR as resolved.