From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 10101 invoked by alias); 18 Apr 2002 10:25:46 -0000 Mailing-List: contact gdb-patches-help@sources.redhat.com; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sources.redhat.com Received: (qmail 10077 invoked from network); 18 Apr 2002 10:25:40 -0000 Received: from unknown (HELO dublin.ACT-Europe.FR) (212.157.227.154) by sources.redhat.com with SMTP; 18 Apr 2002 10:25:40 -0000 Received: from berlin.ACT-Europe.FR (berlin.int.act-europe.fr [10.10.0.169]) by dublin.ACT-Europe.FR (Postfix) with ESMTP id CBDE2229E31 for ; Thu, 18 Apr 2002 12:25:38 +0200 (MET DST) Received: by berlin.ACT-Europe.FR (Postfix, from userid 507) id 49DEEA5C; Thu, 18 Apr 2002 12:25:36 +0200 (CEST) Date: Thu, 18 Apr 2002 03:25:00 -0000 From: Joel Brobecker To: gdb-patches@sources.redhat.com Subject: [RFA] Should openp open directories? Message-ID: <20020418122535.B11802@act-europe.fr> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="5mCyUwZo2JvN/JJP" Content-Disposition: inline User-Agent: Mutt/1.2.5i X-SW-Source: 2002-04/txt/msg00585.txt.bz2 --5mCyUwZo2JvN/JJP Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-length: 1576 Hello, Suppose you have a directory called "root", inside which you have a directory called "foo". You also have an executable called "foo" in your PATH that you would like to debug. Suppose now that you launch gdb from "root" using the following command: % gdb foo Here is what is going to happen: << GNU gdb 2002-04-17-cvs [...] This GDB was configured as "i686-pc-linux-gnu"..."/bonn.a/brobecke/gdb-public-ssh/gdb": not in executable format: Is a directory >> As you see, GDB found directory "foo" in the current directory, and thought it was the executable it was asked to debug. More generally, this problem can also happen if GDB finds in the PATH a directory sharing the same name before the executable. For instance, say your PATH is ~/bin:/usr/local/bin:/usr/bin, and that the directory ~/bin/sh exists, invoking GDB using the following command from your home directory will also demonstrate the problem. % gdb sh This is because openp does not verify that the file it opens is a regular file (ie not a directory, or a device, etc). From the description of this function, and the name of the parameters, it seems that this was the intent, and the attached patch fixes this. 2002-04-18 Joel Brobecker * source.c (is_regular_file): New function. (openp): Check wether file to open is a regular file to avoid opening directories. I verified that this patch does not introduce any regression: Summary 1 2 FAIL 60 60 PASS 8242 8242 XFAIL 170 170 XPASS 11 11 Differences: 0 -- Joel --5mCyUwZo2JvN/JJP Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="source.c.diff" Content-length: 1719 Index: source.c =================================================================== RCS file: /cvs/src/src/gdb/source.c,v retrieving revision 1.27 diff -c -3 -p -r1.27 source.c *** source.c 12 Apr 2002 19:46:29 -0000 1.27 --- source.c 18 Apr 2002 10:23:55 -0000 *************** source_info (char *ignore, int from_tty) *** 503,508 **** --- 503,520 ---- } + /* Return True if the file NAME exists and is a regular file */ + static int + is_regular_file (const char *name) + { + struct stat st; + const int status = stat (name, &st); + + if (status != 0) + return 0; + + return S_ISREG (st.st_mode); + } /* Open a file named STRING, searching path PATH (dir names sep by some char) using mode MODE and protection bits PROT in the calls to open. *************** openp (const char *path, int try_cwd_fir *** 543,549 **** mode |= O_BINARY; #endif ! if (try_cwd_first || IS_ABSOLUTE_PATH (string)) { int i; filename = alloca (strlen (string) + 1); --- 555,561 ---- mode |= O_BINARY; #endif ! if ((try_cwd_first || IS_ABSOLUTE_PATH (string)) && is_regular_file (string)) { int i; filename = alloca (strlen (string) + 1); *************** openp (const char *path, int try_cwd_fir *** 601,609 **** strcat (filename + len, SLASH_STRING); strcat (filename, string); ! fd = open (filename, mode); ! if (fd >= 0) ! break; } done: --- 613,624 ---- strcat (filename + len, SLASH_STRING); strcat (filename, string); ! if (is_regular_file (filename)) ! { ! fd = open (filename, mode); ! if (fd >= 0) ! break; ! } } done: --5mCyUwZo2JvN/JJP--