From patchwork Wed Dec 21 14:43:14 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 132669 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 8C001B712E for ; Thu, 22 Dec 2011 01:43:36 +1100 (EST) Received: (qmail 7329 invoked by alias); 21 Dec 2011 14:43:33 -0000 Received: (qmail 7320 invoked by uid 22791); 21 Dec 2011 14:43:32 -0000 X-SWARE-Spam-Status: No, hits=-7.9 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, SPF_HELO_PASS, TW_CP X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 21 Dec 2011 14:43:16 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id pBLEhF8k029337 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 21 Dec 2011 09:43:15 -0500 Received: from tyan-ft48-01.lab.bos.redhat.com (tyan-ft48-01.lab.bos.redhat.com [10.16.42.4]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id pBLEhEAZ011280 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Wed, 21 Dec 2011 09:43:15 -0500 Received: from tyan-ft48-01.lab.bos.redhat.com (tyan-ft48-01.lab.bos.redhat.com [127.0.0.1]) by tyan-ft48-01.lab.bos.redhat.com (8.14.4/8.14.4) with ESMTP id pBLEhE4T017220 for ; Wed, 21 Dec 2011 15:43:14 +0100 Received: (from jakub@localhost) by tyan-ft48-01.lab.bos.redhat.com (8.14.4/8.14.4/Submit) id pBLEhEtk017218 for gcc-patches@gcc.gnu.org; Wed, 21 Dec 2011 15:43:14 +0100 Date: Wed, 21 Dec 2011 15:43:14 +0100 From: Jakub Jelinek To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix make_relative_prefix_1 (PR driver/48306) Message-ID: <20111221144313.GZ1957@tyan-ft48-01.lab.bos.redhat.com> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Hi! As reported in the PR, when the gcc (or g++, ...) binary is not started with full path and somewhere in $PATH earlier then were gcc binary is is a directory with the same name (gcc, g++, ...), then execvp will ignore it, but make_relative_prefix will think it is the path to the driver and derive paths from that, thus likely won't be able to find cc1 etc. The following patch fixes it by skipping pathnames that aren't regular files. Unfortunately it adds an extra syscall for each path element successfully statted. For Linux we could perhaps do better, if we #ifdef __linux__ ssize_t len = readlink ("/proc/self/exe", buf, sizeof buf); if (len > 0 && len < sizeof buf) { buf[len] = '\0'; /* Executable filename is in buf, goto somewhere and skip the $PATH loop. */ } #endif but would need to do some portability hacks for that. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2011-12-21 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. Jakub --- libiberty/make-relative-prefix.c.jj 2011-02-15 15:40:07.000000000 +0100 +++ libiberty/make-relative-prefix.c 2011-12-19 20:47:02.613081452 +0100 @@ -1,6 +1,6 @@ /* Relative (relocatable) prefix support. Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, - 1999, 2000, 2001, 2002, 2006 Free Software Foundation, Inc. + 1999, 2000, 2001, 2002, 2006, 2011 Free Software Foundation, Inc. This file is part of libiberty. @@ -58,6 +58,9 @@ relative prefix can be found, return @co #ifdef HAVE_UNISTD_H #include #endif +#ifdef HAVE_SYS_STAT_H +#include +#endif #include @@ -248,7 +251,11 @@ make_relative_prefix_1 (const char *prog if (prefixlen < 2) prefixlen = 2; - nstore = (char *) alloca (prefixlen + strlen (progname) + 1); + nstore = (char *) alloca (prefixlen + strlen (progname) + 1 +#ifdef HAVE_HOST_EXECUTABLE_SUFFIX + + strlen (HOST_EXECUTABLE_SUFFIX) +#endif + ); startp = endp = temp; while (1) @@ -263,7 +270,7 @@ make_relative_prefix_1 (const char *prog } else { - strncpy (nstore, startp, endp - startp); + memcpy (nstore, startp, endp - startp); if (! IS_DIR_SEPARATOR (endp[-1])) { nstore[endp - startp] = DIR_SEPARATOR; @@ -279,8 +286,14 @@ make_relative_prefix_1 (const char *prog #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)