From nobody@FreeBSD.org  Thu Jan 12 22:35:02 2012
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id DBC5F106566C
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 12 Jan 2012 22:35:02 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from red.freebsd.org (red.freebsd.org [IPv6:2001:4f8:fff6::22])
	by mx1.freebsd.org (Postfix) with ESMTP id CA4D88FC0A
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 12 Jan 2012 22:35:02 +0000 (UTC)
Received: from red.freebsd.org (localhost [127.0.0.1])
	by red.freebsd.org (8.14.4/8.14.4) with ESMTP id q0CMZ2cY004974
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 12 Jan 2012 22:35:02 GMT
	(envelope-from nobody@red.freebsd.org)
Received: (from nobody@localhost)
	by red.freebsd.org (8.14.4/8.14.4/Submit) id q0CMZ2F2004965;
	Thu, 12 Jan 2012 22:35:02 GMT
	(envelope-from nobody)
Message-Id: <201201122235.q0CMZ2F2004965@red.freebsd.org>
Date: Thu, 12 Jan 2012 22:35:02 GMT
From: Garrett Cooper <yaneurabeya@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: cd built-in in /bin/sh doesn't report cd(1) errors in an intuitive/correct manner
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         164070
>Category:       bin
>Synopsis:       sh(1): cd built-in in /bin/sh doesn't report cd(1) errors in an intuitive/correct manner
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    jilles
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Jan 12 22:40:03 UTC 2012
>Closed-Date:    Fri Jan 27 21:09:54 UTC 2012
>Last-Modified:  Sun Feb 03 22:30:12 UTC 2013
>Originator:     Garrett Cooper
>Release:        10-CURRENT
>Organization:
iXsystems, Inc.
>Environment:
FreeBSD streetfighter.ixsystems.com 10.0-CURRENT FreeBSD 10.0-CURRENT #1 r229730M: Fri Jan  6 14:03:26 PST 2012     gcooper@streetfighter.ixsystems.com:/usr/obj/usr/src/sys/STREETFIGHTER  amd64
>Description:
/bin/sh doesn't report proper errors when cd'ing to an invalid path (in this case a file, not a directory):

$ sh -c 'cd /usr/src/Makefile'
cd: /usr/src/Makefile: No such file or directory

bash does the right thing:

$ bash -c 'cd /usr/src/Makefile'
bash: line 0: cd: /usr/src/Makefile: Not a directory

$ svn status /usr/src/bin/sh/
$

The bash reported error matches fchdir(2) semantics, whereas it appears that the sh reported error matches the chdir(2) semantics:

     [ENOENT]           The named directory does not exist.

..

     [ENOTDIR]          The file descriptor does not reference a directory.

If it is as I suspect and bash is using fchdir whereas sh is using chdir, bash is also using less error prone/security conscious logic.
>How-To-Repeat:

>Fix:


>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->patched 
State-Changed-By: jilles 
State-Changed-When: Fri Jan 13 23:33:23 UTC 2012 
State-Changed-Why:  
Fixed in 10-current. 


Responsible-Changed-From-To: freebsd-bugs->jilles 
Responsible-Changed-By: jilles 
Responsible-Changed-When: Fri Jan 13 23:33:23 UTC 2012 
Responsible-Changed-Why:  
Take. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=164070 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/164070: commit references a PR
Date: Fri, 13 Jan 2012 23:32:43 +0000 (UTC)

 Author: jilles
 Date: Fri Jan 13 23:32:27 2012
 New Revision: 230095
 URL: http://svn.freebsd.org/changeset/base/230095
 
 Log:
   sh: Properly show "Not a directory" error in cd builtin.
   
   The errno message display added in r222292 did not take attempting to
   cd to a non-directory or something that cannot be stat()ed into account.
   
   PR:		bin/164070
   MFC after:	10 days
 
 Added:
   head/tools/regression/bin/sh/builtins/cd8.0   (contents, props changed)
 Modified:
   head/bin/sh/cd.c
 
 Modified: head/bin/sh/cd.c
 ==============================================================================
 --- head/bin/sh/cd.c	Fri Jan 13 23:31:36 2012	(r230094)
 +++ head/bin/sh/cd.c	Fri Jan 13 23:32:27 2012	(r230095)
 @@ -130,7 +130,12 @@ cdcmd(int argc, char **argv)
  	    (path = bltinlookup("CDPATH", 1)) == NULL)
  		path = nullstr;
  	while ((p = padvance(&path, dest)) != NULL) {
 -		if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
 +		if (stat(p, &statb) < 0) {
 +			if (errno != ENOENT)
 +				errno1 = errno;
 +		} else if (!S_ISDIR(statb.st_mode))
 +			errno1 = ENOTDIR;
 +		else {
  			if (!print) {
  				/*
  				 * XXX - rethink
 
 Added: head/tools/regression/bin/sh/builtins/cd8.0
 ==============================================================================
 --- /dev/null	00:00:00 1970	(empty, because file is newly added)
 +++ head/tools/regression/bin/sh/builtins/cd8.0	Fri Jan 13 23:32:27 2012	(r230095)
 @@ -0,0 +1,26 @@
 +# $FreeBSD$
 +
 +# The exact wording of the error message is not standardized, but giving
 +# a description of the errno is useful.
 +
 +LC_ALL=C
 +export LC_ALL
 +r=0
 +
 +t() {
 +	exec 3>&1
 +	errmsg=`cd "$1" 2>&1 >&3 3>&-`
 +	exec 3>&-
 +	case $errmsg in
 +	*[Nn]ot\ a\ directory*)
 +		;;
 +	*)
 +		printf "Wrong error message for %s: %s\n" "$1" "$errmsg"
 +		r=3
 +		;;
 +	esac
 +}
 +
 +t /dev/tty
 +t /dev/tty/x
 +exit $r
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/164070: commit references a PR
Date: Fri, 27 Jan 2012 20:53:53 +0000 (UTC)

 Author: jilles
 Date: Fri Jan 27 20:53:37 2012
 New Revision: 230624
 URL: http://svn.freebsd.org/changeset/base/230624
 
 Log:
   MFC r230095: sh: Properly show "Not a directory" error in cd builtin.
   
   The errno message display added in r222292 did not take attempting to
   cd to a non-directory or something that cannot be stat()ed into account.
   
   PR:		bin/164070
 
 Added:
   stable/9/tools/regression/bin/sh/builtins/cd8.0
      - copied unchanged from r230095, head/tools/regression/bin/sh/builtins/cd8.0
 Modified:
   stable/9/bin/sh/cd.c
 Directory Properties:
   stable/9/bin/sh/   (props changed)
   stable/9/tools/regression/bin/sh/   (props changed)
 
 Modified: stable/9/bin/sh/cd.c
 ==============================================================================
 --- stable/9/bin/sh/cd.c	Fri Jan 27 20:18:31 2012	(r230623)
 +++ stable/9/bin/sh/cd.c	Fri Jan 27 20:53:37 2012	(r230624)
 @@ -130,7 +130,12 @@ cdcmd(int argc, char **argv)
  	    (path = bltinlookup("CDPATH", 1)) == NULL)
  		path = nullstr;
  	while ((p = padvance(&path, dest)) != NULL) {
 -		if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
 +		if (stat(p, &statb) < 0) {
 +			if (errno != ENOENT)
 +				errno1 = errno;
 +		} else if (!S_ISDIR(statb.st_mode))
 +			errno1 = ENOTDIR;
 +		else {
  			if (!print) {
  				/*
  				 * XXX - rethink
 
 Copied: stable/9/tools/regression/bin/sh/builtins/cd8.0 (from r230095, head/tools/regression/bin/sh/builtins/cd8.0)
 ==============================================================================
 --- /dev/null	00:00:00 1970	(empty, because file is newly added)
 +++ stable/9/tools/regression/bin/sh/builtins/cd8.0	Fri Jan 27 20:53:37 2012	(r230624, copy of r230095, head/tools/regression/bin/sh/builtins/cd8.0)
 @@ -0,0 +1,26 @@
 +# $FreeBSD$
 +
 +# The exact wording of the error message is not standardized, but giving
 +# a description of the errno is useful.
 +
 +LC_ALL=C
 +export LC_ALL
 +r=0
 +
 +t() {
 +	exec 3>&1
 +	errmsg=`cd "$1" 2>&1 >&3 3>&-`
 +	exec 3>&-
 +	case $errmsg in
 +	*[Nn]ot\ a\ directory*)
 +		;;
 +	*)
 +		printf "Wrong error message for %s: %s\n" "$1" "$errmsg"
 +		r=3
 +		;;
 +	esac
 +}
 +
 +t /dev/tty
 +t /dev/tty/x
 +exit $r
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: patched->closed 
State-Changed-By: jilles 
State-Changed-When: Fri Jan 27 21:09:17 UTC 2012 
State-Changed-Why:  
Fixed in head and stable/9. This code is not in any older branches. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=164070 
>Unformatted:
