|Subject:||PATCH: File::Spec::UNIX->abs2rel() gets it wrong with ".." components|
|Date:||Sat, 3 Mar 2012 11:02:00 +0100 (CET)|
|To:||perlbug [...] perl.org|
|From:||Volker Schatz <perldoc [...] volkerschatz.com>|
Hello, this is a bugfix I submitted to CPAN a while ago (#61451). I have verified that the issue persists in PathTools 3.33 and rerun the test suite with the patched version. In the light of my experience with CPAN, I have added a line to the POD that asks bug reports to go to email@example.com (last hunk of the patch: @@ -469,6 +494,8 @@). If you have follow-up questions, it would probably be best to CC me, though I will try to watch the perl5-porters list archives. Kind regards, Volker Here is the detailed bug description: File::Spec::UNIX->abs2rel() returns wrong results in a few cases, most of which involve ".." path components. To reproduce, paste the following test cases into: perl -MFile::Spec::Unix -n -e 'print File::Spec::Unix->abs2rel(split),"\n";' ../foo bar/bat bar/bat ../foo foo bar/../bat . . / / Correct results when run at /home/me and no symlinks in base path: ../../../foo ../me/bar/bat ../foo . . Results for File::Spec::Unix from PathTols 3.33: ../../foo ../bar/bat ../../../foo / / The error in the first test case is due to an optimisation applied when both arguments are relative paths, which prepends "/" instead of the current directory. "/../" is then converted to "/" by canonpath(). I have replaced this optimisation by a single call to _cwd() in the following patch. This also fixes the fourth test case. Besides, I have moved checks which make sense only for absolute path arguments to the first branch of the if. (hunk @@ -362,28 +363,32 @@) The error in the last test case arises because a root dir $base is treated specially, and catdir() does not work well for fewer than two path components. The first added line in the following patch catches that. As regards the second and third test case, they can be solved without consulting the filesystem only if no symlinks are involved. Whereever $path contains .. components, the corresponding directory has to be descended into. The following patch does this. (hunk @@ -391,19 +396,39 @@) It can be impossible for abs2rel() to work correctly without looking at the filesystem if $base contains symlinks. I understand from the documentation that the File::Spec modules are not meant to consult the filesystem. Even though the docs state that abs2rel() does not consult the filesystem, the implications could perhaps be made clearer, for example like this: (hunk @@ -348,9 +348,10 @@)
Message body is not shown because sender requested not to inline it.