From patchwork Sat Sep 19 22:40:41 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Doug Evans X-Patchwork-Id: 8806 Received: (qmail 74206 invoked by alias); 19 Sep 2015 22:41:59 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 74134 invoked by uid 89); 19 Sep 2015 22:41:58 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL, BAYES_00, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-pa0-f53.google.com Received: from mail-pa0-f53.google.com (HELO mail-pa0-f53.google.com) (209.85.220.53) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Sat, 19 Sep 2015 22:41:54 +0000 Received: by padhy16 with SMTP id hy16so81521837pad.1 for ; Sat, 19 Sep 2015 15:41:51 -0700 (PDT) X-Received: by 10.66.165.5 with SMTP id yu5mr15166090pab.109.1442702511604; Sat, 19 Sep 2015 15:41:51 -0700 (PDT) Received: from sspiff.org (173-13-178-53-sfba.hfc.comcastbusiness.net. [173.13.178.53]) by smtp.gmail.com with ESMTPSA id jw6sm3355292pbb.86.2015.09.19.15.41.50 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 19 Sep 2015 15:41:51 -0700 (PDT) Received: by sspiff.org (sSMTP sendmail emulation); Sat, 19 Sep 2015 15:40:41 -0700 From: Doug Evans To: gdb-patches@sourceware.org Subject: [PATCH] [PR symtab/17911] Recognize bad file types Date: Sat, 19 Sep 2015 15:40:41 -0700 Message-ID: MIME-Version: 1.0 X-IsSubscribed: yes Hi. I tripped over this this morning, and then found the bug report. We came up with basically the same answer, but I like EINVAL better than ENOEXEC if given neither a file nor a directory. Tested on amd64-linux. 2015-09-19 Doug Evans PR symtab/17911 * source.c (is_regular_file): Set errno. (openp): Init errno to ENOENT before iterating over search path. testsuite/ * gdb.base/bad-file.exp: New file. diff --git a/gdb/source.c b/gdb/source.c index fab974c..34384fa 100644 --- a/gdb/source.c +++ b/gdb/source.c @@ -684,7 +684,10 @@ source_info (char *ignore, int from_tty) } -/* Return True if the file NAME exists and is a regular file. */ +/* Return True if the file NAME exists and is a regular file. + If the result is false then errno is set to a useful value assuming we're + expecting a regular file. */ + static int is_regular_file (const char *name) { @@ -699,7 +702,14 @@ is_regular_file (const char *name) if (status != 0) return (errno != ENOENT); - return S_ISREG (st.st_mode); + if (S_ISREG (st.st_mode)) + return 1; + + if (S_ISDIR (st.st_mode)) + errno = EISDIR; + else + errno = EINVAL; + return 0; } /* Open a file named STRING, searching path PATH (dir names sep by some char) @@ -785,6 +795,7 @@ openp (const char *path, int opts, const char *string, { filename = NULL; fd = -1; + /* Note: is_regular_file will have set errno appropriately. */ } if (!(opts & OPF_SEARCH_IN_PATH)) @@ -808,6 +819,7 @@ openp (const char *path, int opts, const char *string, alloclen = strlen (path) + strlen (string) + 2; filename = alloca (alloclen); fd = -1; + errno = ENOENT; dir_vec = dirnames_to_char_ptr_vec (path); back_to = make_cleanup_free_char_ptr_vec (dir_vec); @@ -879,6 +891,10 @@ openp (const char *path, int opts, const char *string, if (fd >= 0) break; } + else + { + /* Note: is_regular_file will have set errno appropriately. */ + } } do_cleanups (back_to); diff --git a/gdb/testsuite/gdb.base/bad-file.exp b/gdb/testsuite/gdb.base/bad-file.exp new file mode 100644 index 0000000..09da32c --- /dev/null +++ b/gdb/testsuite/gdb.base/bad-file.exp @@ -0,0 +1,56 @@ +# Copyright 2015 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . */ + +# Test passing bad files to gdb. PR symtab/17911 + +# We watch for specific text for errno, so only test on systems we have +# confidence in the error text. + +if { ! [ishost "*-*-linux*"] } { + return 0 +} + +# There is no such file, but we still use the normal mechanism to pick +# its name and path. +standard_testfile + +gdb_exit +gdb_start + +set test "non-existent file" +set bad_file $testfile +remote_file host delete $bad_file +gdb_test_multiple "file $bad_file" "$test" { + -re "No such file or directory.\[\r\n\]+$gdb_prompt $" { + pass $test + } +} + +set test "directory" +set bad_file [standard_output_file {}] +remote_exec host "mkdir -p $bad_file" +gdb_test_multiple "file $bad_file" "$test" { + -re "Is a directory.\[\r\n\]+$gdb_prompt $" { + pass $test + } +} + +set test "neither file nor directory" +set bad_file "/dev/null" +gdb_test_multiple "file $bad_file" "$test" { + -re "Invalid argument.\[\r\n\]+$gdb_prompt $" { + pass $test + } +}