2012-02-09 Jakub Jelinek Backported from mainline 2012-01-26 Jakub Jelinek * make-relative-prefix.c (make_relative_prefix_1): Avoid warning about using preprocessor directives inside of macro arguments. 2012-01-02 Jakub Jelinek * make-relative-prefix.c (make_relative_prefix_1): Avoid stack overflow if PATH contains just a single entry and HOST_EXECUTABLE_SUFFIX needs to be used. PR driver/48306 * make-relative-prefix.c: Include sys/stat.h. (make_relative_prefix_1): If access succeeds, check also stat if nstore is a regular file. --- libiberty/make-relative-prefix.c (revision 182819) +++ libiberty/make-relative-prefix.c (revision 183561) @@ -58,6 +58,9 @@ #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_SYS_STAT_H +#include +#endif #include @@ -245,10 +248,15 @@ { char *startp, *endp, *nstore; size_t prefixlen = strlen (temp) + 1; + size_t len; if (prefixlen < 2) prefixlen = 2; - nstore = (char *) alloca (prefixlen + strlen (progname) + 1); + len = prefixlen + strlen (progname) + 1; +#ifdef HAVE_HOST_EXECUTABLE_SUFFIX + len += strlen (HOST_EXECUTABLE_SUFFIX); +#endif + nstore = (char *) alloca (len); startp = endp = temp; while (1) @@ -263,7 +271,7 @@ } else { - strncpy (nstore, startp, endp - startp); + memcpy (nstore, startp, endp - startp); if (! IS_DIR_SEPARATOR (endp[-1])) { nstore[endp - startp] = DIR_SEPARATOR; @@ -279,8 +287,14 @@ #endif ) { - progname = nstore; - break; +#if defined (HAVE_SYS_STAT_H) && defined (S_ISREG) + struct stat st; + if (stat (nstore, &st) >= 0 && S_ISREG (st.st_mode)) +#endif + { + progname = nstore; + break; + } } if (*endp == 0)