patchThe GNU Bourne-Again SHell - Patches: patch #9836, Fix cd to . or .. in removed...

 
 

You are not allowed to post comments on this tracker with your current authentication level.

patch #9836: Fix cd to . or .. in removed directories

Submitter:  Tomasz Gorochowik <tkgk>
Submitted:  Thu 25 Jul 2019 12:46:36 PM UTC
   
 
Category:  None Priority:  5 - Normal
Status:  Need Info Privacy:  Public
Assigned to:  None Open/Closed:  Open

Thu 25 Jul 2019 06:27:18 PM UTC, comment #4: 

If the shell finds itself in an orphaned directory like that, no answer is correct. PWD will be set to something bogus (or unset) -- behavior varies between shells.

A straight pwd is basically equivalent to `echo "$PWD"'; `pwd -P' will attempt to use getcwd() to walk the file hierarchy and will produce an error.

I like the current behavior better than unsetting PWD, and certainly more than leaving it set to something really wrong.

Chet Ramey <chet>
Group administrator
Thu 25 Jul 2019 04:08:25 PM UTC, comment #3: 

You are right that this patch doesn't solve this properly yet, it needs some more work.

Let's first discuss it though, to figure out if it needs fixing at all.

So what looks like the error to me, is that when you are in the removed directory, and try to cd to . or .. (assuming a path up doesn't exist either) you actually do get an error message, BUT the current path still changes (to something that looks bogus).

> Having `.' or `..' in PWD is at least a hint that something is wrong with the current directory.


That is indeed a valid point, so if this is a design choice I guess it is something we can live with, however before my attempt to fix it I compared it with other shells (zsh, fish, dash) and neither seemed to do that (error message is all there was). I know that it doesn't mean that bash should behave the same, but this behavior surely seems bogus.

If you don't agree with that, feel free to reject this completely.

Tomasz Gorochowik <tkgk>
Thu 25 Jul 2019 03:57:42 PM UTC, comment #2: 

How does this help things? If you're in a removed directory, where `..' doesn't exist, trying to cd into another removed directory (so pathname canonicalization doesn't help you), PWD
is going to be wrong no matter what. getcwd(3) won't help.

Having `.' or `..' in PWD is at least a hint that something is wrong with the current directory.

Chet Ramey <chet>
Group administrator
Thu 25 Jul 2019 02:52:00 PM UTC, comment #1: 

I am sorry for the noise here, be I just realized there was another issue with the patch.

I added newdir canocalization before doing the comparison to make sure stuff like "cd ./././.././." also works.

See the v3 patch

(file #47259)

Tomasz Gorochowik <tkgk>
Thu 25 Jul 2019 12:46:36 PM UTC, original submission:  

Without this patch it is possible to create a path with lots of "." or ".." in CWD.

For example:

   $ mkdir -p /tmp/test/test/test && cd /tmp/test/test/test
   $ rm -r /tmp/test && cd . ; pwd
   cd: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
   /tmp/test/test/test/.
   $ cd ..
   cd: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
   $ cd .
   cd: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
   $ cd ..
   cd: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
   $ pwd
   /tmp/test/test/test/./.././..

This patch fixes it while making sure that when possible a regular "cd .." should still work, e.g:

   $ mkdir /tmp/test && cd /tmp/test && rm -r /tmp/test
   & cd .. && pwd
   /tmp

Tomasz Gorochowik <tkgk>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attached Files
file #47259:  0001-Fix-cd-to-.-or-.-in-removed-directories.patch added by tkgk (2KiB - text/x-patch - v3, canonize newdir before comparing)
file #47257:  0001-Fix-cd-to-.-or-.-in-removed-directories.patch added by tkgk (2KiB - text/x-patch - A simpler version and without a mem leak present in v1)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by chet (Posted a comment)
  • -email is unavailable- added by tkgk (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

     

    Follow 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2019-07-25 chet StatusNone Need Info
    2019-07-25 tkgk Attached File- Added 0001-Fix-cd-to-.-or-.-in-removed-directories.patch, #47259
    2019-07-25 tkgk Attached File- Added 0001-Fix-cd-to-.-or-.-in-removed-directories.patch, #47257
    2019-07-25 tkgk Attached File- Added 0001-Fix-cd-to-.-or-.-in-removed-directories.patch, #47256

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code