From patchwork Tue Nov 28 21:08:57 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Goncharov X-Patchwork-Id: 24581 Received: (qmail 114036 invoked by alias); 28 Nov 2017 21:09:02 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 114026 invoked by uid 89); 28 Nov 2017 21:09:02 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.7 required=5.0 tests=BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KB_WAM_FROM_NAME_SINGLEWORD, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=demonstrate X-HELO: mail-qt0-f175.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:from:to:subject:message-id :mime-version:content-disposition:user-agent; bh=HYyYMJckFTfn9Q3Iin7tMra6Al1szwFxtAdyzA/SuH0=; b=GL13o20VTfRceb8nM0pesYYhYqQxSbYpYdrNn42bC7SN/s+kDIi40eZu33NJNmy/bY 55sBLGzDDq5QLExoFj7y6elflqESaNAl6yIy5i/c+S3lsThzsxHUBGs42+ycf+ckMEbQ jm9484kl1l+h7D78paBV7WBASQnXsECi45lXXxG6P7luuJb85D9Uv2u9wjAffTUrR1Un dbIl8vqyiTPTS5i/GO7FqLrJTxk3LBk6FqGrbewJGFVhIa/EyVe3gGJRU2z/Ffm1cHoj NK+7mre15TUt2xaO/Hzi3vwU2yAfnGyQ0uAwHGmA1G5bx6PUZtQutMi07dKrBwOpX6pv ADWQ== X-Gm-Message-State: AJaThX74q+wFHNvMBeTg9fZQq33Whk2ms921aUM7Vvea1jZTtQ6mw68z ZdL1ftW9NyRWp0iKH6isAmVNnw== X-Google-Smtp-Source: AGs4zMZ/qjMQN6MfWOp4KYcGy8dwGqs/B6k6Ic0w+ofxhNpKlkB8bZplNtqqQ46pI6XJXcHOeruPQw== X-Received: by 10.200.41.145 with SMTP id 17mr905702qts.239.1511903338937; Tue, 28 Nov 2017 13:08:58 -0800 (PST) Date: Tue, 28 Nov 2017 16:08:57 -0500 From: Dmitry Goncharov To: libc-alpha@sourceware.org Subject: [PATCH] posix: if glob has a trailing slash match directories only. Message-ID: <20171128210857.GC2745@madrid> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) If pattern has a trailing slash match directories only. This patch fixes [BZ #22513]. +2017-11-28 Dmitry Goncharov + + [BZ #22513] + * posix/glob.c (glob_in_dir): Make glob with a trailing slash match + directores only. + * posix/globtest.sh: Add tests. Tested on x86-64 linux. diff --git a/posix/glob.c b/posix/glob.c index cb39779d07..78873d83c6 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -1342,7 +1342,33 @@ glob_in_dir (const char *pattern, const char *directory, int flags, if (flags & GLOB_ONLYDIR) switch (readdir_result_type (d)) { - case DT_DIR: case DT_LNK: case DT_UNKNOWN: break; + case DT_DIR: case DT_LNK: break; + case DT_UNKNOWN: + { + int dir; + size_t namlen = strlen (d.name); + size_t fullsize; + bool alloca_fullname + = (! size_add_wrapv (dirlen + 1, namlen + 1, &fullsize) + && glob_use_alloca (alloca_used, fullsize)); + char *fullname; + if (alloca_fullname) + fullname = alloca_account (fullsize, alloca_used); + else + { + fullname = malloc (fullsize); + if (fullname == NULL) + return GLOB_NOSPACE; + } + mempcpy (mempcpy (mempcpy (fullname, directory, dirlen), + "/", 1), + d.name, namlen + 1); + dir = is_dir (fullname, flags, pglob); + if (__glibc_unlikely (!alloca_fullname)) + free (fullname); + if (dir) + break; + } default: continue; } diff --git a/posix/globtest.sh b/posix/globtest.sh index 73f7ae31cc..4f0c03715a 100755 --- a/posix/globtest.sh +++ b/posix/globtest.sh @@ -43,13 +43,19 @@ export LC_ALL # Create the arena testdir=${common_objpfx}posix/globtest-dir +testdir2=${common_objpfx}posix/globtest-dir2 testout=${common_objpfx}posix/globtest-out rm -rf $testdir $testout mkdir $testdir +mkdir $testdir2 +mkdir $testdir2/hello1d +mkdir $testdir2/hello2d +echo 1 > $testdir2/hello1f +echo 2 > $testdir2/hello2f cleanup() { chmod 777 $testdir/noread - rm -fr $testdir $testout + rm -fr $testdir $testdir2 $testout } trap cleanup 0 HUP INT QUIT TERM @@ -815,6 +821,41 @@ if test $failed -ne 0; then result=1 fi +# Test that if the specified glob ends with a slash then only directories are +# matched. +# First run with the glob that has no slash to demonstrate presence of a slash +# makes a difference. +failed=0 +${test_program_prefix} \ +${common_objpfx}posix/globtest "$testdir2" "hello*" | +sort > $testout +cat <<"EOF" | $CMP - $testout >> $logfile || failed=1 +`hello1d' +`hello1f' +`hello2d' +`hello2f' +EOF + +if test $failed -ne 0; then + echo "pattern+meta test failed" >> $logfile + result=1 +fi + +# The same pattern+meta test with a slash this time. +failed=0 +${test_program_prefix} \ +${common_objpfx}posix/globtest "$testdir2" "hello*/" | +sort > $testout +cat <<"EOF" | $CMP - $testout >> $logfile || failed=1 +`hello1d/' +`hello2d/' +EOF + +if test $failed -ne 0; then + echo "pattern+meta+slash test failed" >> $logfile + result=1 +fi + if test $result -eq 0; then echo "All OK." > $logfile fi