pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
drop tablespace failed when location contains .. on win32
23+ messages / 6 participants
[nested] [flat]

Commits from this thread:
  2022-01-31 b426bd48ee  Simplify coding around path_contains_parent_reference().
  2022-01-31 c10f830c51  Make canonicalize_path() more canonical.
* drop tablespace failed when location contains .. on win32
@ 2021-09-08 10:16 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-08 12:54 ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  0 siblings, 1 reply; 23+ messages in thread

From: wangsh.fnst@fujitsu.com @ 2021-09-08 10:16 UTC (permalink / raw)
  To: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

Hi,

I find a problem related to tablespace on win32(server2019).

> postgres=# create tablespace tbs location 'C:\Users\postgres\postgres_install\aa\..\aa';
> CREATE TABLESPACE
> postgres=# create table tbl(col int) tablespace tbs;
> ERROR:  could not stat directory "pg_tblspc/16384/PG_15_202109061/12754": Invalid argument
> postgres=# drop tablespace tbs;
> WARNING:  could not open directory "pg_tblspc/16384/PG_15_202109061": No such file or directory
> ERROR:  could not stat file "pg_tblspc/16384": Invalid argument

I find that canonicalize_path() only remove the trailing '..',  in this case,  '..' is not removed , and 
pgsymlink succeed.

But, in fact, if I double click the dir (%PGDATA%\pg_tblspac\16387),  the error message is shown:
> The filename, directory name, or volume label syntax is incorrect.

Since the pgsymlink() seems right and I'm not sure I can change the action of canonicalize_path, 
so I want to add a error check(patch is attached).

Any comment ?

Regards,
Shenhao Wang

Attachments:

problem.diff (application/octet-stream, 1.2K) download diff --git a/src/backend/commands/tablespace.c b/src/backend/commands/tablespace.c index 932e7ae37b..c1b047b8e9 100644 --- a/src/backend/commands/tablespace.c +++ b/src/backend/commands/tablespace.c @@ -269,20 +269,27 @@ CreateTableSpace(CreateTableSpaceStmt *stmt) /* * Allowing relative paths seems risky * * this also helps us ensure that location is not empty or whitespace */ if (!is_absolute_path(location)) ereport(ERROR, (errcode(ERRCODE_INVALID_OBJECT_DEFINITION), errmsg("tablespace location must be an absolute path"))); +#if WIN32 + if (path_contains_parent_reference(location)) + ereport(ERROR, + (errcode(ERRCODE_INVALID_OBJECT_DEFINITION), + errmsg("tablespace location cannot contain \"..\""))); +#endif + /* * Check that location isn't too long. Remember that we're going to append * 'PG_XXX/<dboid>/<relid>_<fork>.<nnn>'. FYI, we never actually * reference the whole path here, but MakePGDirectory() uses the first two * parts. */ if (strlen(location) + 1 + strlen(TABLESPACE_VERSION_DIRECTORY) + 1 + OIDCHARS + 1 + OIDCHARS + 1 + FORKNAMECHARS + 1 + OIDCHARS > MAXPGPATH) ereport(ERROR, (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
^ permalink raw reply [nested|flat] 23+ messages in thread

* Re: drop tablespace failed when location contains .. on win32
  2021-09-08 10:16 drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
@ 2021-09-08 12:54 ` Andrew Dunstan <andrew@dunslane.net>
  2021-09-09 02:19   ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 23+ messages in thread

From: Andrew Dunstan @ 2021-09-08 12:54 UTC (permalink / raw)
  To: wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>


On 9/8/21 6:16 AM, wangsh.fnst@fujitsu.com wrote:
> Hi,
>
> I find a problem related to tablespace on win32(server2019).
>
>> postgres=# create tablespace tbs location 'C:\Users\postgres\postgres_install\aa\..\aa';
>> CREATE TABLESPACE
>> postgres=# create table tbl(col int) tablespace tbs;
>> ERROR:  could not stat directory "pg_tblspc/16384/PG_15_202109061/12754": Invalid argument
>> postgres=# drop tablespace tbs;
>> WARNING:  could not open directory "pg_tblspc/16384/PG_15_202109061": No such file or directory
>> ERROR:  could not stat file "pg_tblspc/16384": Invalid argument
> I find that canonicalize_path() only remove the trailing '..',  in this case,  '..' is not removed , and 
> pgsymlink succeed.


That seems like a bug. It's not very canonical :-)


cheers


andrew

-- 

Andrew Dunstan
EDB: https://www.enterprisedb.com






^ permalink  raw  reply  [nested|flat] 23+ messages in thread

* Re: drop tablespace failed when location contains .. on win32
  2021-09-08 10:16 drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-08 12:54 ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
@ 2021-09-09 02:19   ` Michael Paquier <michael@paquier.xyz>
  2021-09-09 02:35     ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  0 siblings, 1 reply; 23+ messages in thread

From: Michael Paquier @ 2021-09-09 02:19 UTC (permalink / raw)
  To: Andrew Dunstan <andrew@dunslane.net>; +Cc: wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Wed, Sep 08, 2021 at 08:54:25AM -0400, Andrew Dunstan wrote:
> That seems like a bug. It's not very canonical :-)

Yes, better to fix that.  I fear that more places are impacted than
just the tablespace code paths.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 23+ messages in thread

* RE: drop tablespace failed when location contains .. on win32
  2021-09-08 10:16 drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-08 12:54 ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-09 02:19   ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
@ 2021-09-09 02:35     ` wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-09 03:44       ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 23+ messages in thread

From: wangsh.fnst@fujitsu.com @ 2021-09-09 02:35 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; Andrew Dunstan <andrew@dunslane.net>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

Hi, 

> -----Original Message-----
> From: Michael Paquier <michael@paquier.xyz>
> 
> On Wed, Sep 08, 2021 at 08:54:25AM -0400, Andrew Dunstan wrote:
> > That seems like a bug. It's not very canonical :-)
> 
> Yes, better to fix that.  I fear that more places are impacted than
> just the tablespace code paths.
> --
> Michael

Do you mean changing the action of canonicalize_path(), like remove all the (..) ?

I'm willing to fix this problem.

Regards
Shenhao Wang





^ permalink  raw  reply  [nested|flat] 23+ messages in thread

* Re: drop tablespace failed when location contains .. on win32
  2021-09-08 10:16 drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-08 12:54 ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-09 02:19   ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 02:35     ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
@ 2021-09-09 03:44       ` Michael Paquier <michael@paquier.xyz>
  2021-09-09 04:34         ` Re: drop tablespace failed when location contains .. on win32 Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2021-09-09 12:30         ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  0 siblings, 2 replies; 23+ messages in thread

From: Michael Paquier @ 2021-09-09 03:44 UTC (permalink / raw)
  To: wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>; +Cc: Andrew Dunstan <andrew@dunslane.net>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Thu, Sep 09, 2021 at 02:35:52AM +0000, wangsh.fnst@fujitsu.com wrote:
> Do you mean changing the action of canonicalize_path(), like remove all the (..) ?
> 
> I'm willing to fix this problem.

Looking at canonicalize_path(), we have already some logic around
pending_strips to remove paths when we find a "/.." in the path, so
that's a matter of adjusting this area to trim properly the previous
directory.

On *nix platforms, we don't apply this much caution either, say a
simple /tmp/path/../path/ results in this same path used in the link
from pg_tblspc.  But we are speaking about Windows here, and junction
points.

Based on the lack of complains over the years, that does not seem
really worth backpatching.  Just my 2c on this point.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 23+ messages in thread

* Re: drop tablespace failed when location contains .. on win32
  2021-09-08 10:16 drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-08 12:54 ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-09 02:19   ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 02:35     ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-09 03:44       ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
@ 2021-09-09 04:34         ` Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  1 sibling, 0 replies; 23+ messages in thread

From: Kyotaro Horiguchi @ 2021-09-09 04:34 UTC (permalink / raw)
  To: michael@paquier.xyz; +Cc: wangsh.fnst@fujitsu.com; andrew@dunslane.net; pgsql-hackers@lists.postgresql.org

At Thu, 9 Sep 2021 12:44:45 +0900, Michael Paquier <michael@paquier.xyz> wrote in 
> On Thu, Sep 09, 2021 at 02:35:52AM +0000, wangsh.fnst@fujitsu.com wrote:
> > Do you mean changing the action of canonicalize_path(), like remove all the (..) ?
> > 
> > I'm willing to fix this problem.
> 
> Looking at canonicalize_path(), we have already some logic around
> pending_strips to remove paths when we find a "/.." in the path, so
> that's a matter of adjusting this area to trim properly the previous
> directory.
> 
> On *nix platforms, we don't apply this much caution either, say a
> simple /tmp/path/../path/ results in this same path used in the link
> from pg_tblspc.  But we are speaking about Windows here, and junction
> points.
> 
> Based on the lack of complains over the years, that does not seem
> really worth backpatching.  Just my 2c on this point.

Reading the first complaint, I remember I proposed that as a part of a
larger patch.

https://www.postgresql.org/message-id/20190425.170855.39056106.horiguchi.kyotaro%40lab.ntt.co.jp

regards.

-- 
Kyotaro Horiguchi
NTT Open Source Software Center





^ permalink  raw  reply  [nested|flat] 23+ messages in thread

* Re: drop tablespace failed when location contains .. on win32
  2021-09-08 10:16 drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-08 12:54 ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-09 02:19   ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 02:35     ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-09 03:44       ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
@ 2021-09-09 12:30         ` Andrew Dunstan <andrew@dunslane.net>
  2021-09-12 07:33           ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  1 sibling, 1 reply; 23+ messages in thread

From: Andrew Dunstan @ 2021-09-09 12:30 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>


On 9/8/21 11:44 PM, Michael Paquier wrote:
> On Thu, Sep 09, 2021 at 02:35:52AM +0000, wangsh.fnst@fujitsu.com wrote:
>> Do you mean changing the action of canonicalize_path(), like remove all the (..) ?
>>
>> I'm willing to fix this problem.
> Looking at canonicalize_path(), we have already some logic around
> pending_strips to remove paths when we find a "/.." in the path, so
> that's a matter of adjusting this area to trim properly the previous
> directory.
>
> On *nix platforms, we don't apply this much caution either, say a
> simple /tmp/path/../path/ results in this same path used in the link
> from pg_tblspc.  But we are speaking about Windows here, and junction
> points.
>
> Based on the lack of complains over the years, that does not seem
> really worth backpatching.  Just my 2c on this point.



Maybe, although it's arguably a bug.


I think I would say that we should remove any "." or ".." element in the
path except at the beginning, and in the case of ".." also remove the
preceding element, unless someone can convince me that there's a problem
with that.


cheers


andrew

--
Andrew Dunstan
EDB: https://www.enterprisedb.com






^ permalink  raw  reply  [nested|flat] 23+ messages in thread

* RE: drop tablespace failed when location contains .. on win32
  2021-09-08 10:16 drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-08 12:54 ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-09 02:19   ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 02:35     ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-09 03:44       ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 12:30         ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
@ 2021-09-12 07:33           ` wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-13 07:06             ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 23+ messages in thread

From: wangsh.fnst@fujitsu.com @ 2021-09-12 07:33 UTC (permalink / raw)
  To: Andrew Dunstan <andrew@dunslane.net>; Michael Paquier <michael@paquier.xyz>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>

Hi,

> -----Original Message-----
> From: Andrew Dunstan <andrew@dunslane.net>
> Sent: Thursday, September 9, 2021 8:30 PM

> I think I would say that we should remove any "." or ".." element in the
> path except at the beginning, and in the case of ".." also remove the
> preceding element, unless someone can convince me that there's a problem
> with that.

These WIP patches try to remove all the '.' or '..' in the path except at
the beginning.

0001 is a small fix, because I find that is_absolute_path is not appropriate, 
see comment in skip_drive:
> * On Windows, a path may begin with "C:" or "//network/".

But this modification will lead to a regress test failure on Windows:
> -- Will fail with bad path
> CREATE TABLESPACE regress_badspace LOCATION '/no/such/location';
> -ERROR:  directory "/no/such/location" does not exist
> +ERROR:  tablespace location must be an absolute path

Do you think this modification is necessary ?

Rest of the modification is in 0002. I think this patch need more test and review.

0003 is a test extension for me to check the action of canonicalize_path.
Do you think is necessary to add this test extension(and some test scripts) to master ?
If necessary,  maybe I can use the taptest to test the action of canonicalize_path
in Linux and Windows.


I will add this to next commitfest after further test .

Regards.
Shenhao Wang

Attachments: download all (3 files, .tar.gz)

0001-WIP-change-is_absolute_path-s-action.patch (application/octet-stream, 828B) download From 6a8dcb9e871820dba6689830ba8e4b926a7b0b55 Mon Sep 17 00:00:00 2001 From: Shenhao Wang <wangsh.fnst@fujitsu.com> Date: Sun, 12 Sep 2021 14:51:05 +0800 Subject: [PATCH 1/3] WIP:change is_absolute_path's action --- src/include/port.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/include/port.h b/src/include/port.h index 82f63de325..2c18a27f77 100644 --- a/src/include/port.h +++ b/src/include/port.h @@ -93,7 +93,7 @@ extern void pgfnames_cleanup(char **filenames); /* See path_is_relative_and_below_cwd() for how we handle 'E:abc'. */ #define is_absolute_path(filename) \ ( \ - IS_DIR_SEP((filename)[0]) || \ + (IS_DIR_SEP((filename)[0]) && IS_DIR_SEP((filename)[1])) || \ (isalpha((unsigned char) ((filename)[0])) && (filename)[1] == ':' && \ IS_DIR_SEP((filename)[2])) \ ) -- 2.26.2
0002-WIP-make-canonicalize_path-remove-all-.-in-path.patch (application/octet-stream, 5.4K) download From 368e798adb37c055aac76a0841a3d37285dae106 Mon Sep 17 00:00:00 2001 From: Shenhao Wang <wangsh.fnst@fujitsu.com> Date: Sun, 12 Sep 2021 14:51:44 +0800 Subject: [PATCH 2/3] WIP:make canonicalize_path remove all '..' in path --- src/port/path.c | 178 ++++++++++++++++++++++++++---------------------- 1 file changed, 98 insertions(+), 80 deletions(-) diff --git a/src/port/path.c b/src/port/path.c index c39d4688cd..5fa2669801 100644 --- a/src/port/path.c +++ b/src/port/path.c @@ -240,6 +240,61 @@ join_path_components(char *ret_path, } } +static inline void +canonicalize_path_sub(char *path, bool isabs, char *sub, int *nstrips) +{ + if (*sub == '\0') + { + /* remove multi slashes, like "/a///b" */ + } + else if (strcmp(sub, ".") == 0) + { + /* if this is a relative path, don't remove the leading '.' */ + if (!isabs && *nstrips == 0 && strcmp(path, "./") != 0) + strcpy(path, "./"); + } + else if (strcmp(sub, "..") == 0 ) + { + /* handle leading '..', like '../../a/b' */ + if (!isabs && *nstrips == 0) + { + /* handle leading './..', this should be '..' */ + if (strcmp(path, "./") == 0) + strcpy(path, "../"); + + /* handle leading '../..' */ + else + strcat(path, "../"); + } + else + { + *nstrips = *nstrips - 1 >= 0 ? *nstrips - 1 : 0; + trim_directory(path); + + /* foo/.. should become ".", not empty */ + if (path[0] == '\0') + strcpy(path, "./"); + + /* trim_directory never remove a leading slash. */ + else if (strcmp(skip_drive(path), "/") != 0) + { + strcat(path, "/"); + } + } + } + else + { + /* handle leading './dir', this should be 'dir' */ + if (!isabs && strcmp(path, "./") == 0) + strcpy(path, sub); + else + strcat(path, sub); + + strcat(path, "/"); + + *nstrips = *nstrips + 1; + } +} /* * Clean up path by: @@ -247,17 +302,17 @@ join_path_components(char *ret_path, * o remove trailing quote on Win32 * o remove trailing slash * o remove duplicate adjacent separators - * o remove trailing '.' - * o process trailing '..' ourselves + * o remove '.' (absolute path or relative path excpet leading .) + * o remove '..' (absolute path or relative path excpet leading ..) */ void canonicalize_path(char *path) { - char *p, - *to_p; + char *p; char *spath; - bool was_sep = false; - int pending_strips; + char *tmppath; + bool isabs; + int nstrips = 0; #ifdef WIN32 @@ -279,89 +334,52 @@ canonicalize_path(char *path) *(p - 1) = '/'; #endif - /* - * Removing the trailing slash on a path means we never get ugly double - * trailing slashes. Also, Win32 can't stat() a directory with a trailing - * slash. Don't remove a leading slash, though. - */ - trim_trailing_separator(path); - - /* - * Remove duplicate adjacent separators - */ - p = path; -#ifdef WIN32 - /* Don't remove leading double-slash on Win32 */ - if (*p) - p++; -#endif - to_p = p; - for (; *p; p++, to_p++) + isabs = is_absolute_path(path); + tmppath = strdup(path); + if (!tmppath) { - /* Handle many adjacent slashes, like "/a///b" */ - while (*p == '/' && was_sep) - p++; - if (to_p != p) - *to_p = *p; - was_sep = (*p == '/'); +#ifndef FRONTEND + ereport(ERROR, + (errcode(ERRCODE_OUT_OF_MEMORY), + errmsg("out of memory"))); +#else + fprintf(stderr, _("out of memory\n")); + return; +#endif } - *to_p = '\0'; - /* - * Remove any trailing uses of "." and process ".." ourselves - * - * Note that "/../.." should reduce to just "/", while "../.." has to be - * kept as-is. In the latter case we put back mistakenly trimmed ".." - * components below. Also note that we want a Windows drive spec to be - * visible to trim_directory(), but it's not part of the logic that's - * looking at the name components; hence distinction between path and - * spath. - */ spath = skip_drive(path); - pending_strips = 0; - for (;;) - { - int len = strlen(spath); + if (isabs && spath[0] != '\0') + spath[1] = '\0'; + else + spath[0] = '\0'; - if (len >= 2 && strcmp(spath + len - 2, "/.") == 0) - trim_directory(path); - else if (strcmp(spath, ".") == 0) - { - /* Want to leave "." alone, but "./.." has to become ".." */ - if (pending_strips > 0) - *spath = '\0'; - break; - } - else if ((len >= 3 && strcmp(spath + len - 3, "/..") == 0) || - strcmp(spath, "..") == 0) - { - trim_directory(path); - pending_strips++; - } - else if (pending_strips > 0 && *spath != '\0') + spath = skip_drive(tmppath); + if (isabs && spath[0] != '\0') + spath++; + + for (p = spath; *p; p++) + { + if (IS_DIR_SEP(*p)) { - /* trim a regular directory name canceled by ".." */ - trim_directory(path); - pending_strips--; - /* foo/.. should become ".", not empty */ - if (*spath == '\0') - strcpy(spath, "."); + *p = '\0'; + + canonicalize_path_sub(path, isabs, spath, &nstrips); + + spath = p + 1; } - else - break; } - if (pending_strips > 0) - { - /* - * We could only get here if path is now totally empty (other than a - * possible drive specifier on Windows). We have to put back one or - * more ".."'s that we took off. - */ - while (--pending_strips > 0) - strcat(path, "../"); - strcat(path, ".."); - } + canonicalize_path_sub(path, isabs, spath, &nstrips); + + /* + * Removing the trailing slash on a path means we never get ugly double + * trailing slashes. Also, Win32 can't stat() a directory with a trailing + * slash. Don't remove a leading slash, though. + */ + trim_trailing_separator(path); + + free(tmppath); } /* -- 2.26.2
0003-WIP-add-a-canonicalize_path-test.patch (application/octet-stream, 3.7K) download From d49f395e51dd0a80e54e56be751d0b92815df380 Mon Sep 17 00:00:00 2001 From: Shenhao Wang <wangsh.fnst@fujitsu.com> Date: Sun, 12 Sep 2021 14:52:34 +0800 Subject: [PATCH 3/3] WIP:add a canonicalize_path test --- src/test/modules/Makefile | 1 + src/test/modules/test_path/Makefile | 23 +++++++++++ src/test/modules/test_path/test_path--1.0.sql | 9 +++++ src/test/modules/test_path/test_path.c | 38 +++++++++++++++++++ src/test/modules/test_path/test_path.control | 5 +++ 5 files changed, 76 insertions(+) create mode 100644 src/test/modules/test_path/Makefile create mode 100644 src/test/modules/test_path/test_path--1.0.sql create mode 100644 src/test/modules/test_path/test_path.c create mode 100644 src/test/modules/test_path/test_path.control diff --git a/src/test/modules/Makefile b/src/test/modules/Makefile index dffc79b2d9..1317d97049 100644 --- a/src/test/modules/Makefile +++ b/src/test/modules/Makefile @@ -21,6 +21,7 @@ SUBDIRS = \ test_integerset \ test_misc \ test_parser \ + test_path \ test_pg_dump \ test_predtest \ test_rbtree \ diff --git a/src/test/modules/test_path/Makefile b/src/test/modules/test_path/Makefile new file mode 100644 index 0000000000..67b2f6a80a --- /dev/null +++ b/src/test/modules/test_path/Makefile @@ -0,0 +1,23 @@ +# src/test/modules/test_path/Makefile + +MODULE_big = test_path +OBJS = \ + $(WIN32RES) \ + test_path.o +PGFILEDESC = "test_path - test canonicalize_path" + +EXTENSION = test_path +DATA = test_path--1.0.sql + +REGRESS = test_path + +ifdef USE_PGXS +PG_CONFIG = pg_config +PGXS := $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) +else +subdir = src/test/modules/test_parser +top_builddir = ../../../.. +include $(top_builddir)/src/Makefile.global +include $(top_srcdir)/contrib/contrib-global.mk +endif diff --git a/src/test/modules/test_path/test_path--1.0.sql b/src/test/modules/test_path/test_path--1.0.sql new file mode 100644 index 0000000000..a6fd134439 --- /dev/null +++ b/src/test/modules/test_path/test_path--1.0.sql @@ -0,0 +1,9 @@ +/* src/test/modules/test_path/test_path--1.0.sql */ + +-- complain if script is sourced in psql, rather than via CREATE EXTENSION +\echo Use "CREATE EXTENSION test_path" to load this file. \quit + +CREATE FUNCTION test_canonicalize_path(text) +RETURNS text +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; diff --git a/src/test/modules/test_path/test_path.c b/src/test/modules/test_path/test_path.c new file mode 100644 index 0000000000..7922b1f837 --- /dev/null +++ b/src/test/modules/test_path/test_path.c @@ -0,0 +1,38 @@ +/*------------------------------------------------------------------------- + * + * test_parser.c + * test canonicalize_path + * + * Copyright (c) 2007-2021, PostgreSQL Global Development Group + * + * IDENTIFICATION + * src/test/modules/test_path/test_path.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "fmgr.h" +#include "utils/builtins.h" + +PG_MODULE_MAGIC; + +/* + * functions + */ +PG_FUNCTION_INFO_V1(test_canonicalize_path); + +Datum +test_canonicalize_path(PG_FUNCTION_ARGS) +{ + char *path = strdup(text_to_cstring(PG_GETARG_TEXT_PP(0))); + + if (!path) + ereport(ERROR, + (errcode(ERRCODE_OUT_OF_MEMORY), + errmsg("out of memory"))); + + canonicalize_path(path); + + PG_RETURN_TEXT_P(cstring_to_text(path)); +} diff --git a/src/test/modules/test_path/test_path.control b/src/test/modules/test_path/test_path.control new file mode 100644 index 0000000000..31ab323033 --- /dev/null +++ b/src/test/modules/test_path/test_path.control @@ -0,0 +1,5 @@ +# test_path extension +comment = 'test canonicalize_path' +default_version = '1.0' +module_pathname = '$libdir/test_path' +relocatable = true -- 2.26.2
^ permalink raw reply [nested|flat] 23+ messages in thread

* Re: drop tablespace failed when location contains .. on win32
  2021-09-08 10:16 drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-08 12:54 ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-09 02:19   ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 02:35     ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-09 03:44       ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 12:30         ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-12 07:33           ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
@ 2021-09-13 07:06             ` Michael Paquier <michael@paquier.xyz>
  2021-09-13 08:36               ` Re: drop tablespace failed when location contains .. on win32 Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  0 siblings, 1 reply; 23+ messages in thread

From: Michael Paquier @ 2021-09-13 07:06 UTC (permalink / raw)
  To: wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>; +Cc: Andrew Dunstan <andrew@dunslane.net>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>

On Sun, Sep 12, 2021 at 07:33:23AM +0000, wangsh.fnst@fujitsu.com wrote:
> 0001 is a small fix, because I find that is_absolute_path is not appropriate, 
> see comment in skip_drive:
> > * On Windows, a path may begin with "C:" or "//network/".

 #define is_absolute_path(filename) \
 ( \
-       IS_DIR_SEP((filename)[0]) || \
+       (IS_DIR_SEP((filename)[0]) && IS_DIR_SEP((filename)[1])) || \
        (isalpha((unsigned char) ((filename)[0])) && (filename)[1] == ':' && \
         IS_DIR_SEP((filename)[2])) \
With this change you would consider a path beginning with "/foo/.." as
not being an absolute path, but that's not correct.  Or am I missing
 something obvious?

> 0003 is a test extension for me to check the action of canonicalize_path.
> Do you think is necessary to add this test extension(and some test scripts) to master ?
> If necessary,  maybe I can use the taptest to test the action of canonicalize_path
> in Linux and Windows.

I am not sure that this is worth the cycles spent on, so I would
discard it.  This will help a lot in reviewing this patch, for sure.
And you could add some regression tests to show how much testing you
have done, for both WIN32 and non-WIN32.  I do that from time to time,
and was actually thinking to test this API this way.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 23+ messages in thread

* Re: drop tablespace failed when location contains .. on win32
  2021-09-08 10:16 drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-08 12:54 ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-09 02:19   ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 02:35     ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-09 03:44       ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 12:30         ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-12 07:33           ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-13 07:06             ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
@ 2021-09-13 08:36               ` Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2021-09-26 08:40                 ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  0 siblings, 1 reply; 23+ messages in thread

From: Kyotaro Horiguchi @ 2021-09-13 08:36 UTC (permalink / raw)
  To: michael@paquier.xyz; +Cc: wangsh.fnst@fujitsu.com; andrew@dunslane.net; pgsql-hackers@lists.postgresql.org

At Mon, 13 Sep 2021 16:06:52 +0900, Michael Paquier <michael@paquier.xyz> wrote in 
> On Sun, Sep 12, 2021 at 07:33:23AM +0000, wangsh.fnst@fujitsu.com wrote:
> > 0001 is a small fix, because I find that is_absolute_path is not appropriate, 
> > see comment in skip_drive:
> > > * On Windows, a path may begin with "C:" or "//network/".
> 
>  #define is_absolute_path(filename) \
>  ( \
> -       IS_DIR_SEP((filename)[0]) || \
> +       (IS_DIR_SEP((filename)[0]) && IS_DIR_SEP((filename)[1])) || \
>         (isalpha((unsigned char) ((filename)[0])) && (filename)[1] == ':' && \
>          IS_DIR_SEP((filename)[2])) \
> With this change you would consider a path beginning with "/foo/.." as
> not being an absolute path, but that's not correct.  Or am I missing
>  something obvious?

Mmm. I haven't thought that so seriously, but '/hoge/foo/bar' doesn't
seem to be an absolute path on Windows since it lacks
"<dirver-letter>:" or "//hostname" part.  If we're on drive D:,
"/Program\ Files" doesn't mean "C:\Program\ Files" but "D:\Program\
Files".

regards.

-- 
Kyotaro Horiguchi
NTT Open Source Software Center





^ permalink  raw  reply  [nested|flat] 23+ messages in thread

* RE: drop tablespace failed when location contains .. on win32
  2021-09-08 10:16 drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-08 12:54 ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-09 02:19   ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 02:35     ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-09 03:44       ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 12:30         ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-12 07:33           ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-13 07:06             ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-13 08:36               ` Re: drop tablespace failed when location contains .. on win32 Kyotaro Horiguchi <horikyota.ntt@gmail.com>
@ 2021-09-26 08:40                 ` wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-11-10 22:43                   ` Re: drop tablespace failed when location contains .. on win32 Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 23+ messages in thread

From: wangsh.fnst@fujitsu.com @ 2021-09-26 08:40 UTC (permalink / raw)
  To: Kyotaro Horiguchi <horikyota.ntt@gmail.com>; michael@paquier.xyz <michael@paquier.xyz>; +Cc: andrew@dunslane.net <andrew@dunslane.net>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>

Hi, 

> -----Original Message-----
> From: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
> Sent: Monday, September 13, 2021 4:36 PM
> To: michael@paquier.xyz

> Mmm. I haven't thought that so seriously, but '/hoge/foo/bar' doesn't
> seem to be an absolute path on Windows since it lacks
> "<dirver-letter>:" or "//hostname" part.  If we're on drive D:,
> "/Program\ Files" doesn't mean "C:\Program\ Files" but "D:\Program\
> Files".

I don't know this.  After some test, I think it's better to consider '/hoge/foo/bar'
as a absolute path.

0001 and 0002 are the are the bugfix patches.
0003 is the test patch what I have tested on Linux and Windows.

Waiting for some comment.
Add to the commitfest: https://commitfest.postgresql.org/35/3331/

Regards,
Shenhao Wang

Attachments: download all (3 files, .tar.gz)

0002-v1-remove-function-path_contains_parent_reference.patch (application/octet-stream, 5.6K) download From bb099a079d6cbd3fe5a0eab1170ec9e6c91f278b Mon Sep 17 00:00:00 2001 From: Shenhao Wang <wangsh.fnst@fujitsu.com> Date: Sun, 26 Sep 2021 15:44:42 +0800 Subject: [PATCH 2/3] v1 remove function path_contains_parent_reference --- contrib/adminpack/adminpack.c | 6 ---- contrib/adminpack/expected/adminpack.out | 4 +-- contrib/adminpack/sql/adminpack.sql | 2 +- src/backend/utils/adt/genfile.c | 6 ---- src/include/port.h | 1 - src/port/path.c | 36 ++---------------------- 6 files changed, 5 insertions(+), 50 deletions(-) diff --git a/contrib/adminpack/adminpack.c b/contrib/adminpack/adminpack.c index 48c1746910..d5660da669 100644 --- a/contrib/adminpack/adminpack.c +++ b/contrib/adminpack/adminpack.c @@ -88,12 +88,6 @@ convert_and_check_filename(text *arg) */ if (is_absolute_path(filename)) { - /* Disallow '/a/b/data/..' */ - if (path_contains_parent_reference(filename)) - ereport(ERROR, - (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), - errmsg("reference to parent directory (\"..\") not allowed"))); - /* Allow absolute paths if within DataDir */ if (!path_is_prefix_of_path(DataDir, filename)) ereport(ERROR, diff --git a/contrib/adminpack/expected/adminpack.out b/contrib/adminpack/expected/adminpack.out index edf3ebfcba..ad683ba964 100644 --- a/contrib/adminpack/expected/adminpack.out +++ b/contrib/adminpack/expected/adminpack.out @@ -50,8 +50,8 @@ SELECT pg_file_write(current_setting('data_directory') || '/test_file4', 'test4' 5 (1 row) -SELECT pg_file_write(current_setting('data_directory') || '/../test_file4', 'test4', false); -ERROR: reference to parent directory ("..") not allowed +SELECT pg_file_write('abc/../../test_file4', 'test4', false); +ERROR: path must be in or below the current directory RESET ROLE; REVOKE EXECUTE ON FUNCTION pg_file_write(text,text,bool) FROM regress_user1; REVOKE pg_read_all_settings FROM regress_user1; diff --git a/contrib/adminpack/sql/adminpack.sql b/contrib/adminpack/sql/adminpack.sql index 918d0bdc65..87ec827bef 100644 --- a/contrib/adminpack/sql/adminpack.sql +++ b/contrib/adminpack/sql/adminpack.sql @@ -23,7 +23,7 @@ SET ROLE regress_user1; SELECT pg_file_write('../test_file0', 'test0', false); SELECT pg_file_write('/tmp/test_file0', 'test0', false); SELECT pg_file_write(current_setting('data_directory') || '/test_file4', 'test4', false); -SELECT pg_file_write(current_setting('data_directory') || '/../test_file4', 'test4', false); +SELECT pg_file_write('abc/../../test_file4', 'test4', false); RESET ROLE; REVOKE EXECUTE ON FUNCTION pg_file_write(text,text,bool) FROM regress_user1; REVOKE pg_read_all_settings FROM regress_user1; diff --git a/src/backend/utils/adt/genfile.c b/src/backend/utils/adt/genfile.c index c436d9318b..1ad7fb644c 100644 --- a/src/backend/utils/adt/genfile.c +++ b/src/backend/utils/adt/genfile.c @@ -71,12 +71,6 @@ convert_and_check_filename(text *arg) */ if (is_absolute_path(filename)) { - /* Disallow '/a/b/data/..' */ - if (path_contains_parent_reference(filename)) - ereport(ERROR, - (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), - errmsg("reference to parent directory (\"..\") not allowed"))); - /* * Allow absolute paths if within DataDir or Log_directory, even * though Log_directory might be outside DataDir. diff --git a/src/include/port.h b/src/include/port.h index 82f63de325..024f2fb21b 100644 --- a/src/include/port.h +++ b/src/include/port.h @@ -52,7 +52,6 @@ extern void join_path_components(char *ret_path, extern void canonicalize_path(char *path); extern void make_native_path(char *path); extern void cleanup_path(char *path); -extern bool path_contains_parent_reference(const char *path); extern bool path_is_relative_and_below_cwd(const char *path); extern bool path_is_prefix_of_path(const char *path1, const char *path2); extern char *make_absolute_path(const char *path); diff --git a/src/port/path.c b/src/port/path.c index d6741181ba..b32eeb5fde 100644 --- a/src/port/path.c +++ b/src/port/path.c @@ -386,42 +386,10 @@ canonicalize_path(char *path) free(tmppath); } -/* - * Detect whether a path contains any parent-directory references ("..") - * - * The input *must* have been put through canonicalize_path previously. - * - * This is a bit tricky because we mustn't be fooled by "..a.." (legal) - * nor "C:.." (legal on Unix but not Windows). - */ -bool -path_contains_parent_reference(const char *path) -{ - int path_len; - - path = skip_drive(path); /* C: shouldn't affect our conclusion */ - - path_len = strlen(path); - - /* - * ".." could be the whole path; otherwise, if it's present it must be at - * the beginning, in the middle, or at the end. - */ - if (strcmp(path, "..") == 0 || - strncmp(path, "../", 3) == 0 || - strstr(path, "/../") != NULL || - (path_len >= 3 && strcmp(path + path_len - 3, "/..") == 0)) - return true; - - return false; -} - /* * Detect whether a path is only in or below the current working directory. * An absolute path that matches the current working directory should - * return false (we only want relative to the cwd). We don't allow - * "/../" even if that would keep us under the cwd (it is too hard to - * track that). + * return false (we only want relative to the cwd). */ bool path_is_relative_and_below_cwd(const char *path) @@ -429,7 +397,7 @@ path_is_relative_and_below_cwd(const char *path) if (is_absolute_path(path)) return false; /* don't allow anything above the cwd */ - else if (path_contains_parent_reference(path)) + else if (path_is_prefix_of_path("..", path)) return false; #ifdef WIN32 -- 2.26.2
0003-v1-some-canonicalize_path-tests.patch (application/octet-stream, 32.1K) download From 103453166a81967611df6c99b7906a635940927f Mon Sep 17 00:00:00 2001 From: Shenhao Wang <wangsh.fnst@fujitsu.com> Date: Sun, 26 Sep 2021 15:46:03 +0800 Subject: [PATCH 3/3] v1 some canonicalize_path tests --- src/test/modules/Makefile | 1 + src/test/modules/test_path/.gitignore | 4 + src/test/modules/test_path/Makefile | 23 + .../modules/test_path/expected/test_path.out | 485 ++++++++++++++++++ .../test_path/expected/test_path_1.out | 485 ++++++++++++++++++ src/test/modules/test_path/sql/test_path.sql | 101 ++++ src/test/modules/test_path/test_path--1.0.sql | 14 + src/test/modules/test_path/test_path.c | 53 ++ src/test/modules/test_path/test_path.control | 5 + 9 files changed, 1171 insertions(+) create mode 100644 src/test/modules/test_path/.gitignore create mode 100644 src/test/modules/test_path/Makefile create mode 100644 src/test/modules/test_path/expected/test_path.out create mode 100644 src/test/modules/test_path/expected/test_path_1.out create mode 100644 src/test/modules/test_path/sql/test_path.sql create mode 100644 src/test/modules/test_path/test_path--1.0.sql create mode 100644 src/test/modules/test_path/test_path.c create mode 100644 src/test/modules/test_path/test_path.control diff --git a/src/test/modules/Makefile b/src/test/modules/Makefile index dffc79b2d9..1317d97049 100644 --- a/src/test/modules/Makefile +++ b/src/test/modules/Makefile @@ -21,6 +21,7 @@ SUBDIRS = \ test_integerset \ test_misc \ test_parser \ + test_path \ test_pg_dump \ test_predtest \ test_rbtree \ diff --git a/src/test/modules/test_path/.gitignore b/src/test/modules/test_path/.gitignore new file mode 100644 index 0000000000..5dcb3ff972 --- /dev/null +++ b/src/test/modules/test_path/.gitignore @@ -0,0 +1,4 @@ +# Generated subdirectories +/log/ +/results/ +/tmp_check/ diff --git a/src/test/modules/test_path/Makefile b/src/test/modules/test_path/Makefile new file mode 100644 index 0000000000..3f2d4ff001 --- /dev/null +++ b/src/test/modules/test_path/Makefile @@ -0,0 +1,23 @@ +# src/test/modules/test_path/Makefile + +MODULE_big = test_path +OBJS = \ + $(WIN32RES) \ + test_path.o +PGFILEDESC = "test_path - test canonicalize_path" + +EXTENSION = test_path +DATA = test_path--1.0.sql + +REGRESS = test_path + +ifdef USE_PGXS +PG_CONFIG = pg_config +PGXS := $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) +else +subdir = src/test/modules/test_path +top_builddir = ../../../.. +include $(top_builddir)/src/Makefile.global +include $(top_srcdir)/contrib/contrib-global.mk +endif diff --git a/src/test/modules/test_path/expected/test_path.out b/src/test/modules/test_path/expected/test_path.out new file mode 100644 index 0000000000..c60eac452b --- /dev/null +++ b/src/test/modules/test_path/expected/test_path.out @@ -0,0 +1,485 @@ +CREATE EXTENSION test_path; +SELECT * FROM test_platform(); + test_platform +--------------- + Linux +(1 row) + +-- prefix is '/' +SELECT * FROM test_canonicalize_path('/'); + test_canonicalize_path +------------------------ + / +(1 row) + +SELECT * FROM test_canonicalize_path('//'); + test_canonicalize_path +------------------------ + / +(1 row) + +SELECT * FROM test_canonicalize_path('/./abc/def/'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/./../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/./../../abc/def/'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/../../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/./../abc/def//'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/../../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/def/../../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/def/../../../../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +-- prefix is '/abc' +SELECT * FROM test_canonicalize_path('/abc/./abc/def'); + test_canonicalize_path +------------------------ + /abc/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/abc/../abc/def'); + test_canonicalize_path +------------------------ + /abc/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/abc/./../abc/def'); + test_canonicalize_path +------------------------ + /abc/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/abc/def/../../abc/def'); + test_canonicalize_path +------------------------ + /abc/abc/def +(1 row) + +-- prefix is '.' +SELECT * FROM test_canonicalize_path('.'); + test_canonicalize_path +------------------------ + . +(1 row) + +SELECT * FROM test_canonicalize_path('./'); + test_canonicalize_path +------------------------ + . +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/..'); + test_canonicalize_path +------------------------ + . +(1 row) + +-- prefix is '..' +SELECT * FROM test_canonicalize_path('..'); + test_canonicalize_path +------------------------ + .. +(1 row) + +SELECT * FROM test_canonicalize_path('../'); + test_canonicalize_path +------------------------ + .. +(1 row) + +SELECT * FROM test_canonicalize_path('../abc/def'); + test_canonicalize_path +------------------------ + ../abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./../abc/def'); + test_canonicalize_path +------------------------ + ../abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/../../abc/def'); + test_canonicalize_path +------------------------ + ../abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/./def/.././ghi/../../../abc/def'); + test_canonicalize_path +------------------------ + ../abc/def +(1 row) + +-- prefix is 'abc' +SELECT * FROM test_canonicalize_path('abc'); + test_canonicalize_path +------------------------ + abc +(1 row) + +SELECT * FROM test_canonicalize_path('abc/'); + test_canonicalize_path +------------------------ + abc +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/../abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/./../abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/./def/.././../abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('abc/../abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('abc/./../abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('abc/./def/.././../abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +-- special +SELECT * FROM test_canonicalize_path('/abc/c:abc'); + test_canonicalize_path +------------------------ + /abc/c:abc +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/c:/abc'); + test_canonicalize_path +------------------------ + /abc/c:/abc +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/c:\abc'); + test_canonicalize_path +------------------------ + /abc/c:\abc +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/c:abc/def'); + test_canonicalize_path +------------------------ + /abc/c:abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/c:abc/./def'); + test_canonicalize_path +------------------------ + /abc/c:abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/c:abc/./../def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('c:abc'); + test_canonicalize_path +------------------------ + c:abc +(1 row) + +SELECT * FROM test_canonicalize_path('c:abc/def'); + test_canonicalize_path +------------------------ + c:abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./c:abc/./def'); + test_canonicalize_path +------------------------ + c:abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('../c:abc/./../def'); + test_canonicalize_path +------------------------ + ../def +(1 row) + +SELECT * FROM test_canonicalize_path('/..a/b..'); + test_canonicalize_path +------------------------ + /..a/b.. +(1 row) + +-- Windows +-- prefix is 'C:\' or '\\network' +SELECT * FROM test_canonicalize_path('C:'); + test_canonicalize_path +------------------------ + C: +(1 row) + +SELECT * FROM test_canonicalize_path('C:\'); + test_canonicalize_path +------------------------ + C:\ +(1 row) + +SELECT * FROM test_canonicalize_path('C:\\'); + test_canonicalize_path +------------------------ + C:\\ +(1 row) + +SELECT * FROM test_canonicalize_path('C:\\\'); + test_canonicalize_path +------------------------ + C:\\\ +(1 row) + +SELECT * FROM test_canonicalize_path('C:\\.\abc\def'); + test_canonicalize_path +------------------------ + C:\\.\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\\.\..\abc\def'); + test_canonicalize_path +------------------------ + C:\\.\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\.\..\..\abc\def'); + test_canonicalize_path +------------------------ + C:\.\..\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\..\abc\def'); + test_canonicalize_path +------------------------ + C:\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\..\..\abc\def'); + test_canonicalize_path +------------------------ + C:\..\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\abc\..\abc\def'); + test_canonicalize_path +------------------------ + C:\abc\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\abc\.\..\abc\def'); + test_canonicalize_path +------------------------ + C:\abc\.\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\abc\..\..\abc\def'); + test_canonicalize_path +------------------------ + C:\abc\..\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\abc\def\..\..\abc\def'); + test_canonicalize_path +-------------------------- + C:\abc\def\..\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\abc\def\..\..\..\..\abc\def'); + test_canonicalize_path +-------------------------------- + C:\abc\def\..\..\..\..\abc\def +(1 row) + +-- prefix is '\\network' +SELECT * FROM test_canonicalize_path('\\127.0.0.1'); + test_canonicalize_path +------------------------ + \\127.0.0.1 +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\'); + test_canonicalize_path +------------------------ + \\127.0.0.1\ +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\\'); + test_canonicalize_path +------------------------ + \\127.0.0.1\\ +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\\\'); + test_canonicalize_path +------------------------ + \\127.0.0.1\\\ +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\\.\abc\def'); + test_canonicalize_path +------------------------ + \\127.0.0.1\\.\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\\.\..\abc\def'); + test_canonicalize_path +--------------------------- + \\127.0.0.1\\.\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\.\..\..\abc\def'); + test_canonicalize_path +----------------------------- + \\127.0.0.1\.\..\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\..\abc\def'); + test_canonicalize_path +------------------------ + \\127.0.0.1\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\..\..\abc\def'); + test_canonicalize_path +--------------------------- + \\127.0.0.1\..\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\..\abc\def'); + test_canonicalize_path +---------------------------- + \\127.0.0.1\abc\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\.\..\abc\def'); + test_canonicalize_path +------------------------------ + \\127.0.0.1\abc\.\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\..\..\abc\def'); + test_canonicalize_path +------------------------------- + \\127.0.0.1\abc\..\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\def\..\..\abc\def'); + test_canonicalize_path +----------------------------------- + \\127.0.0.1\abc\def\..\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\def\..\..\..\..\abc\def'); + test_canonicalize_path +----------------------------------------- + \\127.0.0.1\abc\def\..\..\..\..\abc\def +(1 row) + +-- prefix is 'C:\abc' or '\\network\abc' +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\.\abc\def'); + test_canonicalize_path +--------------------------- + \\127.0.0.1\abc\.\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\abc\..\abc\def'); + test_canonicalize_path +-------------------------------- + \\127.0.0.1\abc\abc\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\abc\.\..\abc\def'); + test_canonicalize_path +---------------------------------- + \\127.0.0.1\abc\abc\.\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\abc\def\..\..\abc\def'); + test_canonicalize_path +--------------------------------------- + \\127.0.0.1\abc\abc\def\..\..\abc\def +(1 row) + diff --git a/src/test/modules/test_path/expected/test_path_1.out b/src/test/modules/test_path/expected/test_path_1.out new file mode 100644 index 0000000000..6a15af3780 --- /dev/null +++ b/src/test/modules/test_path/expected/test_path_1.out @@ -0,0 +1,485 @@ +CREATE EXTENSION test_path; +SELECT * FROM test_platform(); + test_platform +--------------- + Win32 +(1 row) + +-- prefix is '/' +SELECT * FROM test_canonicalize_path('/'); + test_canonicalize_path +------------------------ + / +(1 row) + +SELECT * FROM test_canonicalize_path('//'); + test_canonicalize_path +------------------------ + // +(1 row) + +SELECT * FROM test_canonicalize_path('/./abc/def/'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/./../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/./../../abc/def/'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/../../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/./../abc/def//'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/../../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/def/../../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/def/../../../../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +-- prefix is '/abc' +SELECT * FROM test_canonicalize_path('/abc/./abc/def'); + test_canonicalize_path +------------------------ + /abc/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/abc/../abc/def'); + test_canonicalize_path +------------------------ + /abc/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/abc/./../abc/def'); + test_canonicalize_path +------------------------ + /abc/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/abc/def/../../abc/def'); + test_canonicalize_path +------------------------ + /abc/abc/def +(1 row) + +-- prefix is '.' +SELECT * FROM test_canonicalize_path('.'); + test_canonicalize_path +------------------------ + . +(1 row) + +SELECT * FROM test_canonicalize_path('./'); + test_canonicalize_path +------------------------ + . +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/..'); + test_canonicalize_path +------------------------ + . +(1 row) + +-- prefix is '..' +SELECT * FROM test_canonicalize_path('..'); + test_canonicalize_path +------------------------ + .. +(1 row) + +SELECT * FROM test_canonicalize_path('../'); + test_canonicalize_path +------------------------ + .. +(1 row) + +SELECT * FROM test_canonicalize_path('../abc/def'); + test_canonicalize_path +------------------------ + ../abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./../abc/def'); + test_canonicalize_path +------------------------ + ../abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/../../abc/def'); + test_canonicalize_path +------------------------ + ../abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/./def/.././ghi/../../../abc/def'); + test_canonicalize_path +------------------------ + ../abc/def +(1 row) + +-- prefix is 'abc' +SELECT * FROM test_canonicalize_path('abc'); + test_canonicalize_path +------------------------ + abc +(1 row) + +SELECT * FROM test_canonicalize_path('abc/'); + test_canonicalize_path +------------------------ + abc +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/../abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/./../abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/./def/.././../abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('abc/../abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('abc/./../abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('abc/./def/.././../abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +-- special +SELECT * FROM test_canonicalize_path('/abc/c:abc'); + test_canonicalize_path +------------------------ + /abc/c:abc +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/c:/abc'); + test_canonicalize_path +------------------------ + /abc/c:/abc +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/c:\abc'); + test_canonicalize_path +------------------------ + /abc/c:/abc +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/c:abc/def'); + test_canonicalize_path +------------------------ + /abc/c:abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/c:abc/./def'); + test_canonicalize_path +------------------------ + /abc/c:abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/c:abc/./../def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('c:abc'); + test_canonicalize_path +------------------------ + c:abc +(1 row) + +SELECT * FROM test_canonicalize_path('c:abc/def'); + test_canonicalize_path +------------------------ + c:abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./c:abc/./def'); + test_canonicalize_path +------------------------ + c:abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('../c:abc/./../def'); + test_canonicalize_path +------------------------ + ../def +(1 row) + +SELECT * FROM test_canonicalize_path('/..a/b..'); + test_canonicalize_path +------------------------ + /..a/b.. +(1 row) + +-- Windows +-- prefix is 'C:\' or '\\network' +SELECT * FROM test_canonicalize_path('C:'); + test_canonicalize_path +------------------------ + C: +(1 row) + +SELECT * FROM test_canonicalize_path('C:\'); + test_canonicalize_path +------------------------ + C:/ +(1 row) + +SELECT * FROM test_canonicalize_path('C:\\'); + test_canonicalize_path +------------------------ + C:/ +(1 row) + +SELECT * FROM test_canonicalize_path('C:\\\'); + test_canonicalize_path +------------------------ + C:/ +(1 row) + +SELECT * FROM test_canonicalize_path('C:\\.\abc\def'); + test_canonicalize_path +------------------------ + C:/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\\.\..\abc\def'); + test_canonicalize_path +------------------------ + C:/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\.\..\..\abc\def'); + test_canonicalize_path +------------------------ + C:/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\..\abc\def'); + test_canonicalize_path +------------------------ + C:/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\..\..\abc\def'); + test_canonicalize_path +------------------------ + C:/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\abc\..\abc\def'); + test_canonicalize_path +------------------------ + C:/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\abc\.\..\abc\def'); + test_canonicalize_path +------------------------ + C:/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\abc\..\..\abc\def'); + test_canonicalize_path +------------------------ + C:/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\abc\def\..\..\abc\def'); + test_canonicalize_path +------------------------ + C:/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\abc\def\..\..\..\..\abc\def'); + test_canonicalize_path +------------------------ + C:/abc/def +(1 row) + +-- prefix is '\\network' +SELECT * FROM test_canonicalize_path('\\127.0.0.1'); + test_canonicalize_path +------------------------ + //127.0.0.1 +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\'); + test_canonicalize_path +------------------------ + //127.0.0.1/ +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\\'); + test_canonicalize_path +------------------------ + //127.0.0.1/ +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\\\'); + test_canonicalize_path +------------------------ + //127.0.0.1/ +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\\.\abc\def'); + test_canonicalize_path +------------------------ + //127.0.0.1/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\\.\..\abc\def'); + test_canonicalize_path +------------------------ + //127.0.0.1/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\.\..\..\abc\def'); + test_canonicalize_path +------------------------ + //127.0.0.1/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\..\abc\def'); + test_canonicalize_path +------------------------ + //127.0.0.1/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\..\..\abc\def'); + test_canonicalize_path +------------------------ + //127.0.0.1/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\..\abc\def'); + test_canonicalize_path +------------------------ + //127.0.0.1/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\.\..\abc\def'); + test_canonicalize_path +------------------------ + //127.0.0.1/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\..\..\abc\def'); + test_canonicalize_path +------------------------ + //127.0.0.1/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\def\..\..\abc\def'); + test_canonicalize_path +------------------------ + //127.0.0.1/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\def\..\..\..\..\abc\def'); + test_canonicalize_path +------------------------ + //127.0.0.1/abc/def +(1 row) + +-- prefix is 'C:\abc' or '\\network\abc' +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\.\abc\def'); + test_canonicalize_path +------------------------- + //127.0.0.1/abc/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\abc\..\abc\def'); + test_canonicalize_path +------------------------- + //127.0.0.1/abc/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\abc\.\..\abc\def'); + test_canonicalize_path +------------------------- + //127.0.0.1/abc/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\abc\def\..\..\abc\def'); + test_canonicalize_path +------------------------- + //127.0.0.1/abc/abc/def +(1 row) + diff --git a/src/test/modules/test_path/sql/test_path.sql b/src/test/modules/test_path/sql/test_path.sql new file mode 100644 index 0000000000..b0f2134570 --- /dev/null +++ b/src/test/modules/test_path/sql/test_path.sql @@ -0,0 +1,101 @@ +CREATE EXTENSION test_path; + +SELECT * FROM test_platform(); + +-- prefix is '/' +SELECT * FROM test_canonicalize_path('/'); +SELECT * FROM test_canonicalize_path('//'); +SELECT * FROM test_canonicalize_path('/./abc/def/'); +SELECT * FROM test_canonicalize_path('/./../abc/def'); +SELECT * FROM test_canonicalize_path('/./../../abc/def/'); +SELECT * FROM test_canonicalize_path('/../abc/def'); +SELECT * FROM test_canonicalize_path('/../../abc/def'); +SELECT * FROM test_canonicalize_path('/abc/../abc/def'); +SELECT * FROM test_canonicalize_path('/abc/./../abc/def//'); +SELECT * FROM test_canonicalize_path('/abc/../../abc/def'); +SELECT * FROM test_canonicalize_path('/abc/def/../../abc/def'); +SELECT * FROM test_canonicalize_path('/abc/def/../../../../abc/def'); + +-- prefix is '/abc' +SELECT * FROM test_canonicalize_path('/abc/./abc/def'); +SELECT * FROM test_canonicalize_path('/abc/abc/../abc/def'); +SELECT * FROM test_canonicalize_path('/abc/abc/./../abc/def'); +SELECT * FROM test_canonicalize_path('/abc/abc/def/../../abc/def'); + +-- prefix is '.' +SELECT * FROM test_canonicalize_path('.'); +SELECT * FROM test_canonicalize_path('./'); +SELECT * FROM test_canonicalize_path('./abc/..'); + +-- prefix is '..' +SELECT * FROM test_canonicalize_path('..'); +SELECT * FROM test_canonicalize_path('../'); +SELECT * FROM test_canonicalize_path('../abc/def'); +SELECT * FROM test_canonicalize_path('./../abc/def'); +SELECT * FROM test_canonicalize_path('./abc/../../abc/def'); +SELECT * FROM test_canonicalize_path('./abc/./def/.././ghi/../../../abc/def'); + +-- prefix is 'abc' +SELECT * FROM test_canonicalize_path('abc'); +SELECT * FROM test_canonicalize_path('abc/'); +SELECT * FROM test_canonicalize_path('./abc/def'); +SELECT * FROM test_canonicalize_path('./abc/../abc/def'); +SELECT * FROM test_canonicalize_path('./abc/./../abc/def'); +SELECT * FROM test_canonicalize_path('./abc/./def/.././../abc/def'); +SELECT * FROM test_canonicalize_path('abc/def'); +SELECT * FROM test_canonicalize_path('abc/../abc/def'); +SELECT * FROM test_canonicalize_path('abc/./../abc/def'); +SELECT * FROM test_canonicalize_path('abc/./def/.././../abc/def'); + +-- special +SELECT * FROM test_canonicalize_path('/abc/c:abc'); +SELECT * FROM test_canonicalize_path('/abc/c:/abc'); +SELECT * FROM test_canonicalize_path('/abc/c:\abc'); +SELECT * FROM test_canonicalize_path('/abc/c:abc/def'); +SELECT * FROM test_canonicalize_path('/abc/c:abc/./def'); +SELECT * FROM test_canonicalize_path('/abc/c:abc/./../def'); +SELECT * FROM test_canonicalize_path('c:abc'); +SELECT * FROM test_canonicalize_path('c:abc/def'); +SELECT * FROM test_canonicalize_path('./c:abc/./def'); +SELECT * FROM test_canonicalize_path('../c:abc/./../def'); +SELECT * FROM test_canonicalize_path('/..a/b..'); + + +-- Windows +-- prefix is 'C:\' or '\\network' +SELECT * FROM test_canonicalize_path('C:'); +SELECT * FROM test_canonicalize_path('C:\'); +SELECT * FROM test_canonicalize_path('C:\\'); +SELECT * FROM test_canonicalize_path('C:\\\'); +SELECT * FROM test_canonicalize_path('C:\\.\abc\def'); +SELECT * FROM test_canonicalize_path('C:\\.\..\abc\def'); +SELECT * FROM test_canonicalize_path('C:\.\..\..\abc\def'); +SELECT * FROM test_canonicalize_path('C:\..\abc\def'); +SELECT * FROM test_canonicalize_path('C:\..\..\abc\def'); +SELECT * FROM test_canonicalize_path('C:\abc\..\abc\def'); +SELECT * FROM test_canonicalize_path('C:\abc\.\..\abc\def'); +SELECT * FROM test_canonicalize_path('C:\abc\..\..\abc\def'); +SELECT * FROM test_canonicalize_path('C:\abc\def\..\..\abc\def'); +SELECT * FROM test_canonicalize_path('C:\abc\def\..\..\..\..\abc\def'); + +-- prefix is '\\network' +SELECT * FROM test_canonicalize_path('\\127.0.0.1'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\\'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\\\'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\\.\abc\def'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\\.\..\abc\def'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\.\..\..\abc\def'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\..\abc\def'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\..\..\abc\def'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\..\abc\def'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\.\..\abc\def'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\..\..\abc\def'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\def\..\..\abc\def'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\def\..\..\..\..\abc\def'); + +-- prefix is 'C:\abc' or '\\network\abc' +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\.\abc\def'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\abc\..\abc\def'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\abc\.\..\abc\def'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\abc\def\..\..\abc\def'); \ No newline at end of file diff --git a/src/test/modules/test_path/test_path--1.0.sql b/src/test/modules/test_path/test_path--1.0.sql new file mode 100644 index 0000000000..042ff2439b --- /dev/null +++ b/src/test/modules/test_path/test_path--1.0.sql @@ -0,0 +1,14 @@ +/* src/test/modules/test_path/test_path--1.0.sql */ + +-- complain if script is sourced in psql, rather than via CREATE EXTENSION +\echo Use "CREATE EXTENSION test_path" to load this file. \quit + +CREATE FUNCTION test_canonicalize_path(text) +RETURNS text +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +CREATE FUNCTION test_platform() +RETURNS text +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; \ No newline at end of file diff --git a/src/test/modules/test_path/test_path.c b/src/test/modules/test_path/test_path.c new file mode 100644 index 0000000000..12b1300dc7 --- /dev/null +++ b/src/test/modules/test_path/test_path.c @@ -0,0 +1,53 @@ +/*------------------------------------------------------------------------- + * + * test_parser.c + * test canonicalize_path + * + * Copyright (c) 2007-2021, PostgreSQL Global Development Group + * + * IDENTIFICATION + * src/test/modules/test_path/test_path.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "fmgr.h" +#include "utils/builtins.h" + +PG_MODULE_MAGIC; + +/* + * functions + */ +PG_FUNCTION_INFO_V1(test_canonicalize_path); +PG_FUNCTION_INFO_V1(test_platform); + +Datum +test_canonicalize_path(PG_FUNCTION_ARGS) +{ + char *path = strdup(text_to_cstring(PG_GETARG_TEXT_PP(0))); + + if (!path) + ereport(ERROR, + (errcode(ERRCODE_OUT_OF_MEMORY), + errmsg("out of memory"))); + + canonicalize_path(path); + + PG_RETURN_TEXT_P(cstring_to_text(path)); +} + +Datum +test_platform(PG_FUNCTION_ARGS) +{ + char *platform; + +#ifdef WIN32 + platform = strdup("Win32"); +#else + platform = strdup("Linux"); +#endif + + PG_RETURN_TEXT_P(cstring_to_text(platform)); +} \ No newline at end of file diff --git a/src/test/modules/test_path/test_path.control b/src/test/modules/test_path/test_path.control new file mode 100644 index 0000000000..31ab323033 --- /dev/null +++ b/src/test/modules/test_path/test_path.control @@ -0,0 +1,5 @@ +# test_path extension +comment = 'test canonicalize_path' +default_version = '1.0' +module_pathname = '$libdir/test_path' +relocatable = true -- 2.26.2
0001-v1-make-canonicalize_path-remove-all-.-in-path.patch (application/octet-stream, 6.1K) download From a4b0e3a140d6d750aea4f568679ecfd2a8e98d0e Mon Sep 17 00:00:00 2001 From: Shenhao Wang <wangsh.fnst@fujitsu.com> Date: Sun, 26 Sep 2021 15:42:11 +0800 Subject: [PATCH 1/3] v1 make canonicalize_path remove all '..' in path --- src/port/path.c | 193 +++++++++++++++++++++++++++--------------------- 1 file changed, 109 insertions(+), 84 deletions(-) diff --git a/src/port/path.c b/src/port/path.c index c39d4688cd..d6741181ba 100644 --- a/src/port/path.c +++ b/src/port/path.c @@ -46,7 +46,7 @@ static void make_relative_path(char *ret_path, const char *target_path, const char *bin_path, const char *my_exec_path); -static void trim_directory(char *path); +static int trim_directory(char *path); static void trim_trailing_separator(char *path); @@ -240,6 +240,69 @@ join_path_components(char *ret_path, } } +static inline void +canonicalize_path_sub(bool isabs, char *path, int *pathlen, + char *sub, int sublen, int *nstrips) +{ + bool need_copy = true; + + if (sublen == '\0') + return; + + if (strcmp(sub, ".") == 0) + { + /* Don't remove the leading '.' if this is a relative path */ + + if (isabs || path[0] != '\0') + need_copy = false; + } + else if (strcmp(sub, "..") == 0 ) + { + if (*nstrips == 0) + { + if (isabs) + need_copy = false; + + /* handle leading './..', this should be '..' */ + else if (strcmp(path, ".") == 0) + *pathlen = *pathlen - 1; + } + else + { + *nstrips = *nstrips - 1; + *pathlen = trim_directory(path); + + /* foo/.. should become ".", not empty */ + if (*pathlen == 0) + { + sub = "."; + sublen = 1; + } + else + need_copy = false; + } + } + else + { + /* handle leading './dir', this should be 'dir' */ + if (!isabs && strcmp(path, ".") == 0) + *pathlen = *pathlen - 1; + + *nstrips = *nstrips + 1; + } + + if (need_copy) + { + if (*pathlen > 0 && path[*pathlen-1] != '/') + { + strcpy(&path[*pathlen], "/"); + *pathlen = *pathlen + 1; + } + + strcpy(&path[*pathlen], sub); + *pathlen = *pathlen + sublen; + } +} /* * Clean up path by: @@ -247,17 +310,18 @@ join_path_components(char *ret_path, * o remove trailing quote on Win32 * o remove trailing slash * o remove duplicate adjacent separators - * o remove trailing '.' - * o process trailing '..' ourselves + * o remove '.' (excpet leading '.' in relative path) + * o remove '..' (excpet leading '..' in relative path) */ void canonicalize_path(char *path) { - char *p, - *to_p; + char *p; char *spath; - bool was_sep = false; - int pending_strips; + char *tmppath; + bool isabs; + int nstrips = 0; + int len = 0; #ifdef WIN32 @@ -279,89 +343,47 @@ canonicalize_path(char *path) *(p - 1) = '/'; #endif - /* - * Removing the trailing slash on a path means we never get ugly double - * trailing slashes. Also, Win32 can't stat() a directory with a trailing - * slash. Don't remove a leading slash, though. - */ - trim_trailing_separator(path); - - /* - * Remove duplicate adjacent separators - */ - p = path; -#ifdef WIN32 - /* Don't remove leading double-slash on Win32 */ - if (*p) - p++; -#endif - to_p = p; - for (; *p; p++, to_p++) + isabs = is_absolute_path(path); + tmppath = strdup(path); + if (!tmppath) { - /* Handle many adjacent slashes, like "/a///b" */ - while (*p == '/' && was_sep) - p++; - if (to_p != p) - *to_p = *p; - was_sep = (*p == '/'); +#ifndef FRONTEND + ereport(ERROR, + (errcode(ERRCODE_OUT_OF_MEMORY), + errmsg("out of memory"))); +#else + fprintf(stderr, _("out of memory\n")); + return; +#endif } - *to_p = '\0'; - /* - * Remove any trailing uses of "." and process ".." ourselves - * - * Note that "/../.." should reduce to just "/", while "../.." has to be - * kept as-is. In the latter case we put back mistakenly trimmed ".." - * components below. Also note that we want a Windows drive spec to be - * visible to trim_directory(), but it's not part of the logic that's - * looking at the name components; hence distinction between path and - * spath. - */ - spath = skip_drive(path); - pending_strips = 0; - for (;;) - { - int len = strlen(spath); + if (isabs) + len = skip_drive(path) - path + 1; - if (len >= 2 && strcmp(spath + len - 2, "/.") == 0) - trim_directory(path); - else if (strcmp(spath, ".") == 0) - { - /* Want to leave "." alone, but "./.." has to become ".." */ - if (pending_strips > 0) - *spath = '\0'; - break; - } - else if ((len >= 3 && strcmp(spath + len - 3, "/..") == 0) || - strcmp(spath, "..") == 0) - { - trim_directory(path); - pending_strips++; - } - else if (pending_strips > 0 && *spath != '\0') + path[len] = '\0'; + + if (isabs) + spath = skip_drive(tmppath) + 1; + else + spath = tmppath; + + for (p = spath; *p; p++) + { + if (IS_DIR_SEP(*p)) { - /* trim a regular directory name canceled by ".." */ - trim_directory(path); - pending_strips--; - /* foo/.. should become ".", not empty */ - if (*spath == '\0') - strcpy(spath, "."); + *p = '\0'; + + canonicalize_path_sub(isabs, path, &len, + spath, strlen(spath), &nstrips); + + spath = p + 1; } - else - break; } - if (pending_strips > 0) - { - /* - * We could only get here if path is now totally empty (other than a - * possible drive specifier on Windows). We have to put back one or - * more ".."'s that we took off. - */ - while (--pending_strips > 0) - strcat(path, "../"); - strcat(path, ".."); - } + canonicalize_path_sub(isabs, path, &len, + spath, strlen(spath), &nstrips); + + free(tmppath); } /* @@ -864,15 +886,16 @@ get_parent_directory(char *path) * the last pathname component, and the slash just ahead of it --- but never * remove a leading slash. */ -static void +static int trim_directory(char *path) { char *p; + char *opath = path; path = skip_drive(path); if (path[0] == '\0') - return; + return 0; /* back up over trailing slash(es) */ for (p = path + strlen(path) - 1; IS_DIR_SEP(*p) && p > path; p--) @@ -887,6 +910,8 @@ trim_directory(char *path) if (p == path && IS_DIR_SEP(*p)) p++; *p = '\0'; + + return (int)(p - opath); } -- 2.26.2
^ permalink raw reply [nested|flat] 23+ messages in thread

* Re: drop tablespace failed when location contains .. on win32
  2021-09-08 10:16 drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-08 12:54 ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-09 02:19   ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 02:35     ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-09 03:44       ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 12:30         ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-12 07:33           ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-13 07:06             ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-13 08:36               ` Re: drop tablespace failed when location contains .. on win32 Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2021-09-26 08:40                 ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
@ 2021-11-10 22:43                   ` Tom Lane <tgl@sss.pgh.pa.us>
  2021-11-19 07:50                     ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 23+ messages in thread

From: Tom Lane @ 2021-11-10 22:43 UTC (permalink / raw)
  To: wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>; +Cc: Kyotaro Horiguchi <horikyota.ntt@gmail.com>; michael@paquier.xyz <michael@paquier.xyz>; andrew@dunslane.net <andrew@dunslane.net>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>

"wangsh.fnst@fujitsu.com" <wangsh.fnst@fujitsu.com> writes:
> I don't know this.  After some test, I think it's better to consider '/hoge/foo/bar'
> as a absolute path.

Agreed.  I think we are considering "absolute path" here as a
syntactic concept; Windows' weird rules about drive letters
don't really matter for the purposes of path canonicalization.

> 0001 and 0002 are the are the bugfix patches.
> 0003 is the test patch what I have tested on Linux and Windows.
> Waiting for some comment.

I tried to read 0001 but really couldn't make sense of the logic
at all, because it's seriously underdocumented.  At minimum you
need an API spec comment for canonicalize_path_sub, explaining
what it's supposed to do and why.  This is a significant rewrite
of what was already tricky code, so we can't skimp on
documentation.  I'd put some effort into choosing more descriptive
names, too ("sub" doesn't mean much, especially here where it's
not clear if it means "subroutine" or "path component").

I did notice that you dropped the separate step to collapse
adjacent separators (i.e, reduce "foo//bar" to "foo/bar"), which
seems like probably a bad idea.  I think such cases might confuse
canonicalize_path_sub, and even if it manages to do the right
thing, that requirement will complicate its invariants won't it?

Another thing I happened to notice is that join_path_components
is going out of its way to not generate "foo/./bar", but if
we are fixing canonicalize_path to be able to delete the "./",
that seems like a waste of code now.

I am not entirely convinced that 0002 isn't re-introducing the
security hole that the existing code seeks to plug.  That one
is going to require more justification.

I concur with the upthread comments that there's little chance
we'll commit 0003 as-is; the code-to-benefit ratio is too high.
Instead, you might consider adding test_canonicalize_path in
src/test/regress/regress.c, and then adding a smaller number
of regression test cases using that.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 23+ messages in thread

* Re: drop tablespace failed when location contains .. on win32
  2021-09-08 10:16 drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-08 12:54 ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-09 02:19   ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 02:35     ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-09 03:44       ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 12:30         ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-12 07:33           ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-13 07:06             ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-13 08:36               ` Re: drop tablespace failed when location contains .. on win32 Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2021-09-26 08:40                 ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-11-10 22:43                   ` Re: drop tablespace failed when location contains .. on win32 Tom Lane <tgl@sss.pgh.pa.us>
@ 2021-11-19 07:50                     ` Michael Paquier <michael@paquier.xyz>
  2022-01-17 00:40                       ` Re: drop tablespace failed when location contains .. on win32 Julien Rouhaud <rjuju123@gmail.com>
  0 siblings, 1 reply; 23+ messages in thread

From: Michael Paquier @ 2021-11-19 07:50 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; andrew@dunslane.net <andrew@dunslane.net>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>

On Wed, Nov 10, 2021 at 05:43:31PM -0500, Tom Lane wrote:
> Another thing I happened to notice is that join_path_components
> is going out of its way to not generate "foo/./bar", but if
> we are fixing canonicalize_path to be able to delete the "./",
> that seems like a waste of code now.
> 
> I am not entirely convinced that 0002 isn't re-introducing the
> security hole that the existing code seeks to plug.  That one
> is going to require more justification.

At the same time, do we have any need for doing 0002 at all if
we do 0001?  The paths are canonicalized before checking them in
path_contains_parent_reference().

> I concur with the upthread comments that there's little chance
> we'll commit 0003 as-is; the code-to-benefit ratio is too high.
> Instead, you might consider adding test_canonicalize_path in
> src/test/regress/regress.c, and then adding a smaller number
> of regression test cases using that.

Sounds like a good idea to me.  I would move these in misc.source for
anything that require an absolute path.

0001 is indeed in need of more comments and documentation so as one
does not get lost if reading through this code in the future.  Changes
in trim_directory(), for example, should explain what is returned and
why.

+       isabs = is_absolute_path(path);
+       tmppath = strdup(path);
If possible, it would be nice to cut any need for malloc() allocations
in this code.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 23+ messages in thread

* Re: drop tablespace failed when location contains .. on win32
  2021-09-08 10:16 drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-08 12:54 ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-09 02:19   ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 02:35     ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-09 03:44       ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 12:30         ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-12 07:33           ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-13 07:06             ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-13 08:36               ` Re: drop tablespace failed when location contains .. on win32 Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2021-09-26 08:40                 ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-11-10 22:43                   ` Re: drop tablespace failed when location contains .. on win32 Tom Lane <tgl@sss.pgh.pa.us>
  2021-11-19 07:50                     ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
@ 2022-01-17 00:40                       ` Julien Rouhaud <rjuju123@gmail.com>
  2022-01-18 01:08                         ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  0 siblings, 1 reply; 23+ messages in thread

From: Julien Rouhaud @ 2022-01-17 00:40 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; andrew@dunslane.net <andrew@dunslane.net>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>

Hi,

This patch is a wanted bugfix and has been waiting for an update for 2 months.

Do you plan to send a new version soon?





^ permalink  raw  reply  [nested|flat] 23+ messages in thread

* RE: drop tablespace failed when location contains .. on win32
  2021-09-08 10:16 drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-08 12:54 ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-09 02:19   ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 02:35     ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-09 03:44       ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 12:30         ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-12 07:33           ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-13 07:06             ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-13 08:36               ` Re: drop tablespace failed when location contains .. on win32 Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2021-09-26 08:40                 ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-11-10 22:43                   ` Re: drop tablespace failed when location contains .. on win32 Tom Lane <tgl@sss.pgh.pa.us>
  2021-11-19 07:50                     ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2022-01-17 00:40                       ` Re: drop tablespace failed when location contains .. on win32 Julien Rouhaud <rjuju123@gmail.com>
@ 2022-01-18 01:08                         ` wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2022-01-18 01:19                           ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 23+ messages in thread

From: wangsh.fnst@fujitsu.com @ 2022-01-18 01:08 UTC (permalink / raw)
  To: Julien Rouhaud <rjuju123@gmail.com>; Michael Paquier <michael@paquier.xyz>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; andrew@dunslane.net <andrew@dunslane.net>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>

Hi

> This patch is a wanted bugfix and has been waiting for an update for 2 months.
> 
> Do you plan to send a new version soon?

Yes, I will send a new version before next weekend

Regards

Shenhao Wang





^ permalink  raw  reply  [nested|flat] 23+ messages in thread

* Re: drop tablespace failed when location contains .. on win32
  2021-09-08 10:16 drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-08 12:54 ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-09 02:19   ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 02:35     ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-09 03:44       ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 12:30         ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-12 07:33           ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-13 07:06             ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-13 08:36               ` Re: drop tablespace failed when location contains .. on win32 Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2021-09-26 08:40                 ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-11-10 22:43                   ` Re: drop tablespace failed when location contains .. on win32 Tom Lane <tgl@sss.pgh.pa.us>
  2021-11-19 07:50                     ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2022-01-17 00:40                       ` Re: drop tablespace failed when location contains .. on win32 Julien Rouhaud <rjuju123@gmail.com>
  2022-01-18 01:08                         ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
@ 2022-01-18 01:19                           ` Michael Paquier <michael@paquier.xyz>
  2022-01-24 11:21                             ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  0 siblings, 1 reply; 23+ messages in thread

From: Michael Paquier @ 2022-01-18 01:19 UTC (permalink / raw)
  To: wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>; +Cc: Julien Rouhaud <rjuju123@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; andrew@dunslane.net <andrew@dunslane.net>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>

On Tue, Jan 18, 2022 at 01:08:01AM +0000, wangsh.fnst@fujitsu.com wrote:
> Yes, I will send a new version before next weekend

Thanks!
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 23+ messages in thread

* RE: drop tablespace failed when location contains .. on win32
  2021-09-08 10:16 drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-08 12:54 ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-09 02:19   ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 02:35     ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-09 03:44       ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 12:30         ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-12 07:33           ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-13 07:06             ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-13 08:36               ` Re: drop tablespace failed when location contains .. on win32 Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2021-09-26 08:40                 ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-11-10 22:43                   ` Re: drop tablespace failed when location contains .. on win32 Tom Lane <tgl@sss.pgh.pa.us>
  2021-11-19 07:50                     ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2022-01-17 00:40                       ` Re: drop tablespace failed when location contains .. on win32 Julien Rouhaud <rjuju123@gmail.com>
  2022-01-18 01:08                         ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2022-01-18 01:19                           ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
@ 2022-01-24 11:21                             ` wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2022-01-24 11:46                               ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 23+ messages in thread

From: wangsh.fnst@fujitsu.com @ 2022-01-24 11:21 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Julien Rouhaud <rjuju123@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; andrew@dunslane.net <andrew@dunslane.net>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>

Hi, 

The new version is attached.

Tom Lane <tgl@sss.pgh.pa.us> wrote:
> I tried to read 0001 but really couldn't make sense of the logic
> at all, because it's seriously underdocumented.  At minimum you
> need an API spec comment for canonicalize_path_sub, explaining
> what it's supposed to do and why.

I have added some comments, but I'm not sure these comments are enough
or easy understand.

> I did notice that you dropped the separate step to collapse
> adjacent separators (i.e, reduce "foo//bar" to "foo/bar"), which
> seems like probably a bad idea.

Add these sources back.

Michael Paquier <michael@paquier.xyz> wrote:
> for example, should explain what is returned and
> why.
> +       isabs = is_absolute_path(path);
> +       tmppath = strdup(path);
> If possible, it would be nice to cut any need for malloc() allocations
> in this code.

Thank you for advice. In this version, I do not use the malloc().

> > I concur with the upthread comments that there's little chance
> > we'll commit 0003 as-is; the code-to-benefit ratio is too high.
> > Instead, you might consider adding test_canonicalize_path in
> > src/test/regress/regress.c, and then adding a smaller number
> > of regression test cases using that.
> 
> Sounds like a good idea to me.  I would move these in misc.source for
> anything that require an absolute path.

I'm not fully understand this. So, I do not change the test patch.

Regards,
Shenhao Wang

Attachments: download all (2 files, .tar.gz)

0002-v2-some-canonicalize_path-tests.patch (application/octet-stream, 32.1K) download From 7a533810c9700721b1db2063445ffad486a85c55 Mon Sep 17 00:00:00 2001 From: Shenhao Wang <wangsh.fnst@fujitsu.com> Date: Mon, 24 Jan 2022 17:30:40 +0800 Subject: [PATCH 2/2] v2 some canonicalize_path tests --- src/test/modules/Makefile | 1 + src/test/modules/test_path/.gitignore | 4 + src/test/modules/test_path/Makefile | 23 + .../modules/test_path/expected/test_path.out | 485 ++++++++++++++++++ .../test_path/expected/test_path_1.out | 485 ++++++++++++++++++ src/test/modules/test_path/sql/test_path.sql | 101 ++++ src/test/modules/test_path/test_path--1.0.sql | 14 + src/test/modules/test_path/test_path.c | 53 ++ src/test/modules/test_path/test_path.control | 5 + 9 files changed, 1171 insertions(+) create mode 100644 src/test/modules/test_path/.gitignore create mode 100644 src/test/modules/test_path/Makefile create mode 100644 src/test/modules/test_path/expected/test_path.out create mode 100644 src/test/modules/test_path/expected/test_path_1.out create mode 100644 src/test/modules/test_path/sql/test_path.sql create mode 100644 src/test/modules/test_path/test_path--1.0.sql create mode 100644 src/test/modules/test_path/test_path.c create mode 100644 src/test/modules/test_path/test_path.control diff --git a/src/test/modules/Makefile b/src/test/modules/Makefile index dffc79b2d9..1317d97049 100644 --- a/src/test/modules/Makefile +++ b/src/test/modules/Makefile @@ -21,6 +21,7 @@ SUBDIRS = \ test_integerset \ test_misc \ test_parser \ + test_path \ test_pg_dump \ test_predtest \ test_rbtree \ diff --git a/src/test/modules/test_path/.gitignore b/src/test/modules/test_path/.gitignore new file mode 100644 index 0000000000..5dcb3ff972 --- /dev/null +++ b/src/test/modules/test_path/.gitignore @@ -0,0 +1,4 @@ +# Generated subdirectories +/log/ +/results/ +/tmp_check/ diff --git a/src/test/modules/test_path/Makefile b/src/test/modules/test_path/Makefile new file mode 100644 index 0000000000..3f2d4ff001 --- /dev/null +++ b/src/test/modules/test_path/Makefile @@ -0,0 +1,23 @@ +# src/test/modules/test_path/Makefile + +MODULE_big = test_path +OBJS = \ + $(WIN32RES) \ + test_path.o +PGFILEDESC = "test_path - test canonicalize_path" + +EXTENSION = test_path +DATA = test_path--1.0.sql + +REGRESS = test_path + +ifdef USE_PGXS +PG_CONFIG = pg_config +PGXS := $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) +else +subdir = src/test/modules/test_path +top_builddir = ../../../.. +include $(top_builddir)/src/Makefile.global +include $(top_srcdir)/contrib/contrib-global.mk +endif diff --git a/src/test/modules/test_path/expected/test_path.out b/src/test/modules/test_path/expected/test_path.out new file mode 100644 index 0000000000..c60eac452b --- /dev/null +++ b/src/test/modules/test_path/expected/test_path.out @@ -0,0 +1,485 @@ +CREATE EXTENSION test_path; +SELECT * FROM test_platform(); + test_platform +--------------- + Linux +(1 row) + +-- prefix is '/' +SELECT * FROM test_canonicalize_path('/'); + test_canonicalize_path +------------------------ + / +(1 row) + +SELECT * FROM test_canonicalize_path('//'); + test_canonicalize_path +------------------------ + / +(1 row) + +SELECT * FROM test_canonicalize_path('/./abc/def/'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/./../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/./../../abc/def/'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/../../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/./../abc/def//'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/../../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/def/../../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/def/../../../../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +-- prefix is '/abc' +SELECT * FROM test_canonicalize_path('/abc/./abc/def'); + test_canonicalize_path +------------------------ + /abc/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/abc/../abc/def'); + test_canonicalize_path +------------------------ + /abc/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/abc/./../abc/def'); + test_canonicalize_path +------------------------ + /abc/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/abc/def/../../abc/def'); + test_canonicalize_path +------------------------ + /abc/abc/def +(1 row) + +-- prefix is '.' +SELECT * FROM test_canonicalize_path('.'); + test_canonicalize_path +------------------------ + . +(1 row) + +SELECT * FROM test_canonicalize_path('./'); + test_canonicalize_path +------------------------ + . +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/..'); + test_canonicalize_path +------------------------ + . +(1 row) + +-- prefix is '..' +SELECT * FROM test_canonicalize_path('..'); + test_canonicalize_path +------------------------ + .. +(1 row) + +SELECT * FROM test_canonicalize_path('../'); + test_canonicalize_path +------------------------ + .. +(1 row) + +SELECT * FROM test_canonicalize_path('../abc/def'); + test_canonicalize_path +------------------------ + ../abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./../abc/def'); + test_canonicalize_path +------------------------ + ../abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/../../abc/def'); + test_canonicalize_path +------------------------ + ../abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/./def/.././ghi/../../../abc/def'); + test_canonicalize_path +------------------------ + ../abc/def +(1 row) + +-- prefix is 'abc' +SELECT * FROM test_canonicalize_path('abc'); + test_canonicalize_path +------------------------ + abc +(1 row) + +SELECT * FROM test_canonicalize_path('abc/'); + test_canonicalize_path +------------------------ + abc +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/../abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/./../abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/./def/.././../abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('abc/../abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('abc/./../abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('abc/./def/.././../abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +-- special +SELECT * FROM test_canonicalize_path('/abc/c:abc'); + test_canonicalize_path +------------------------ + /abc/c:abc +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/c:/abc'); + test_canonicalize_path +------------------------ + /abc/c:/abc +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/c:\abc'); + test_canonicalize_path +------------------------ + /abc/c:\abc +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/c:abc/def'); + test_canonicalize_path +------------------------ + /abc/c:abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/c:abc/./def'); + test_canonicalize_path +------------------------ + /abc/c:abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/c:abc/./../def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('c:abc'); + test_canonicalize_path +------------------------ + c:abc +(1 row) + +SELECT * FROM test_canonicalize_path('c:abc/def'); + test_canonicalize_path +------------------------ + c:abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./c:abc/./def'); + test_canonicalize_path +------------------------ + c:abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('../c:abc/./../def'); + test_canonicalize_path +------------------------ + ../def +(1 row) + +SELECT * FROM test_canonicalize_path('/..a/b..'); + test_canonicalize_path +------------------------ + /..a/b.. +(1 row) + +-- Windows +-- prefix is 'C:\' or '\\network' +SELECT * FROM test_canonicalize_path('C:'); + test_canonicalize_path +------------------------ + C: +(1 row) + +SELECT * FROM test_canonicalize_path('C:\'); + test_canonicalize_path +------------------------ + C:\ +(1 row) + +SELECT * FROM test_canonicalize_path('C:\\'); + test_canonicalize_path +------------------------ + C:\\ +(1 row) + +SELECT * FROM test_canonicalize_path('C:\\\'); + test_canonicalize_path +------------------------ + C:\\\ +(1 row) + +SELECT * FROM test_canonicalize_path('C:\\.\abc\def'); + test_canonicalize_path +------------------------ + C:\\.\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\\.\..\abc\def'); + test_canonicalize_path +------------------------ + C:\\.\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\.\..\..\abc\def'); + test_canonicalize_path +------------------------ + C:\.\..\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\..\abc\def'); + test_canonicalize_path +------------------------ + C:\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\..\..\abc\def'); + test_canonicalize_path +------------------------ + C:\..\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\abc\..\abc\def'); + test_canonicalize_path +------------------------ + C:\abc\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\abc\.\..\abc\def'); + test_canonicalize_path +------------------------ + C:\abc\.\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\abc\..\..\abc\def'); + test_canonicalize_path +------------------------ + C:\abc\..\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\abc\def\..\..\abc\def'); + test_canonicalize_path +-------------------------- + C:\abc\def\..\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\abc\def\..\..\..\..\abc\def'); + test_canonicalize_path +-------------------------------- + C:\abc\def\..\..\..\..\abc\def +(1 row) + +-- prefix is '\\network' +SELECT * FROM test_canonicalize_path('\\127.0.0.1'); + test_canonicalize_path +------------------------ + \\127.0.0.1 +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\'); + test_canonicalize_path +------------------------ + \\127.0.0.1\ +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\\'); + test_canonicalize_path +------------------------ + \\127.0.0.1\\ +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\\\'); + test_canonicalize_path +------------------------ + \\127.0.0.1\\\ +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\\.\abc\def'); + test_canonicalize_path +------------------------ + \\127.0.0.1\\.\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\\.\..\abc\def'); + test_canonicalize_path +--------------------------- + \\127.0.0.1\\.\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\.\..\..\abc\def'); + test_canonicalize_path +----------------------------- + \\127.0.0.1\.\..\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\..\abc\def'); + test_canonicalize_path +------------------------ + \\127.0.0.1\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\..\..\abc\def'); + test_canonicalize_path +--------------------------- + \\127.0.0.1\..\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\..\abc\def'); + test_canonicalize_path +---------------------------- + \\127.0.0.1\abc\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\.\..\abc\def'); + test_canonicalize_path +------------------------------ + \\127.0.0.1\abc\.\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\..\..\abc\def'); + test_canonicalize_path +------------------------------- + \\127.0.0.1\abc\..\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\def\..\..\abc\def'); + test_canonicalize_path +----------------------------------- + \\127.0.0.1\abc\def\..\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\def\..\..\..\..\abc\def'); + test_canonicalize_path +----------------------------------------- + \\127.0.0.1\abc\def\..\..\..\..\abc\def +(1 row) + +-- prefix is 'C:\abc' or '\\network\abc' +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\.\abc\def'); + test_canonicalize_path +--------------------------- + \\127.0.0.1\abc\.\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\abc\..\abc\def'); + test_canonicalize_path +-------------------------------- + \\127.0.0.1\abc\abc\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\abc\.\..\abc\def'); + test_canonicalize_path +---------------------------------- + \\127.0.0.1\abc\abc\.\..\abc\def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\abc\def\..\..\abc\def'); + test_canonicalize_path +--------------------------------------- + \\127.0.0.1\abc\abc\def\..\..\abc\def +(1 row) + diff --git a/src/test/modules/test_path/expected/test_path_1.out b/src/test/modules/test_path/expected/test_path_1.out new file mode 100644 index 0000000000..6a15af3780 --- /dev/null +++ b/src/test/modules/test_path/expected/test_path_1.out @@ -0,0 +1,485 @@ +CREATE EXTENSION test_path; +SELECT * FROM test_platform(); + test_platform +--------------- + Win32 +(1 row) + +-- prefix is '/' +SELECT * FROM test_canonicalize_path('/'); + test_canonicalize_path +------------------------ + / +(1 row) + +SELECT * FROM test_canonicalize_path('//'); + test_canonicalize_path +------------------------ + // +(1 row) + +SELECT * FROM test_canonicalize_path('/./abc/def/'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/./../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/./../../abc/def/'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/../../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/./../abc/def//'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/../../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/def/../../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/def/../../../../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +-- prefix is '/abc' +SELECT * FROM test_canonicalize_path('/abc/./abc/def'); + test_canonicalize_path +------------------------ + /abc/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/abc/../abc/def'); + test_canonicalize_path +------------------------ + /abc/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/abc/./../abc/def'); + test_canonicalize_path +------------------------ + /abc/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/abc/def/../../abc/def'); + test_canonicalize_path +------------------------ + /abc/abc/def +(1 row) + +-- prefix is '.' +SELECT * FROM test_canonicalize_path('.'); + test_canonicalize_path +------------------------ + . +(1 row) + +SELECT * FROM test_canonicalize_path('./'); + test_canonicalize_path +------------------------ + . +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/..'); + test_canonicalize_path +------------------------ + . +(1 row) + +-- prefix is '..' +SELECT * FROM test_canonicalize_path('..'); + test_canonicalize_path +------------------------ + .. +(1 row) + +SELECT * FROM test_canonicalize_path('../'); + test_canonicalize_path +------------------------ + .. +(1 row) + +SELECT * FROM test_canonicalize_path('../abc/def'); + test_canonicalize_path +------------------------ + ../abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./../abc/def'); + test_canonicalize_path +------------------------ + ../abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/../../abc/def'); + test_canonicalize_path +------------------------ + ../abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/./def/.././ghi/../../../abc/def'); + test_canonicalize_path +------------------------ + ../abc/def +(1 row) + +-- prefix is 'abc' +SELECT * FROM test_canonicalize_path('abc'); + test_canonicalize_path +------------------------ + abc +(1 row) + +SELECT * FROM test_canonicalize_path('abc/'); + test_canonicalize_path +------------------------ + abc +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/../abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/./../abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/./def/.././../abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('abc/../abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('abc/./../abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('abc/./def/.././../abc/def'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +-- special +SELECT * FROM test_canonicalize_path('/abc/c:abc'); + test_canonicalize_path +------------------------ + /abc/c:abc +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/c:/abc'); + test_canonicalize_path +------------------------ + /abc/c:/abc +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/c:\abc'); + test_canonicalize_path +------------------------ + /abc/c:/abc +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/c:abc/def'); + test_canonicalize_path +------------------------ + /abc/c:abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/c:abc/./def'); + test_canonicalize_path +------------------------ + /abc/c:abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/c:abc/./../def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('c:abc'); + test_canonicalize_path +------------------------ + c:abc +(1 row) + +SELECT * FROM test_canonicalize_path('c:abc/def'); + test_canonicalize_path +------------------------ + c:abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./c:abc/./def'); + test_canonicalize_path +------------------------ + c:abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('../c:abc/./../def'); + test_canonicalize_path +------------------------ + ../def +(1 row) + +SELECT * FROM test_canonicalize_path('/..a/b..'); + test_canonicalize_path +------------------------ + /..a/b.. +(1 row) + +-- Windows +-- prefix is 'C:\' or '\\network' +SELECT * FROM test_canonicalize_path('C:'); + test_canonicalize_path +------------------------ + C: +(1 row) + +SELECT * FROM test_canonicalize_path('C:\'); + test_canonicalize_path +------------------------ + C:/ +(1 row) + +SELECT * FROM test_canonicalize_path('C:\\'); + test_canonicalize_path +------------------------ + C:/ +(1 row) + +SELECT * FROM test_canonicalize_path('C:\\\'); + test_canonicalize_path +------------------------ + C:/ +(1 row) + +SELECT * FROM test_canonicalize_path('C:\\.\abc\def'); + test_canonicalize_path +------------------------ + C:/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\\.\..\abc\def'); + test_canonicalize_path +------------------------ + C:/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\.\..\..\abc\def'); + test_canonicalize_path +------------------------ + C:/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\..\abc\def'); + test_canonicalize_path +------------------------ + C:/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\..\..\abc\def'); + test_canonicalize_path +------------------------ + C:/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\abc\..\abc\def'); + test_canonicalize_path +------------------------ + C:/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\abc\.\..\abc\def'); + test_canonicalize_path +------------------------ + C:/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\abc\..\..\abc\def'); + test_canonicalize_path +------------------------ + C:/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\abc\def\..\..\abc\def'); + test_canonicalize_path +------------------------ + C:/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('C:\abc\def\..\..\..\..\abc\def'); + test_canonicalize_path +------------------------ + C:/abc/def +(1 row) + +-- prefix is '\\network' +SELECT * FROM test_canonicalize_path('\\127.0.0.1'); + test_canonicalize_path +------------------------ + //127.0.0.1 +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\'); + test_canonicalize_path +------------------------ + //127.0.0.1/ +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\\'); + test_canonicalize_path +------------------------ + //127.0.0.1/ +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\\\'); + test_canonicalize_path +------------------------ + //127.0.0.1/ +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\\.\abc\def'); + test_canonicalize_path +------------------------ + //127.0.0.1/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\\.\..\abc\def'); + test_canonicalize_path +------------------------ + //127.0.0.1/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\.\..\..\abc\def'); + test_canonicalize_path +------------------------ + //127.0.0.1/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\..\abc\def'); + test_canonicalize_path +------------------------ + //127.0.0.1/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\..\..\abc\def'); + test_canonicalize_path +------------------------ + //127.0.0.1/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\..\abc\def'); + test_canonicalize_path +------------------------ + //127.0.0.1/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\.\..\abc\def'); + test_canonicalize_path +------------------------ + //127.0.0.1/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\..\..\abc\def'); + test_canonicalize_path +------------------------ + //127.0.0.1/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\def\..\..\abc\def'); + test_canonicalize_path +------------------------ + //127.0.0.1/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\def\..\..\..\..\abc\def'); + test_canonicalize_path +------------------------ + //127.0.0.1/abc/def +(1 row) + +-- prefix is 'C:\abc' or '\\network\abc' +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\.\abc\def'); + test_canonicalize_path +------------------------- + //127.0.0.1/abc/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\abc\..\abc\def'); + test_canonicalize_path +------------------------- + //127.0.0.1/abc/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\abc\.\..\abc\def'); + test_canonicalize_path +------------------------- + //127.0.0.1/abc/abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\abc\def\..\..\abc\def'); + test_canonicalize_path +------------------------- + //127.0.0.1/abc/abc/def +(1 row) + diff --git a/src/test/modules/test_path/sql/test_path.sql b/src/test/modules/test_path/sql/test_path.sql new file mode 100644 index 0000000000..b0f2134570 --- /dev/null +++ b/src/test/modules/test_path/sql/test_path.sql @@ -0,0 +1,101 @@ +CREATE EXTENSION test_path; + +SELECT * FROM test_platform(); + +-- prefix is '/' +SELECT * FROM test_canonicalize_path('/'); +SELECT * FROM test_canonicalize_path('//'); +SELECT * FROM test_canonicalize_path('/./abc/def/'); +SELECT * FROM test_canonicalize_path('/./../abc/def'); +SELECT * FROM test_canonicalize_path('/./../../abc/def/'); +SELECT * FROM test_canonicalize_path('/../abc/def'); +SELECT * FROM test_canonicalize_path('/../../abc/def'); +SELECT * FROM test_canonicalize_path('/abc/../abc/def'); +SELECT * FROM test_canonicalize_path('/abc/./../abc/def//'); +SELECT * FROM test_canonicalize_path('/abc/../../abc/def'); +SELECT * FROM test_canonicalize_path('/abc/def/../../abc/def'); +SELECT * FROM test_canonicalize_path('/abc/def/../../../../abc/def'); + +-- prefix is '/abc' +SELECT * FROM test_canonicalize_path('/abc/./abc/def'); +SELECT * FROM test_canonicalize_path('/abc/abc/../abc/def'); +SELECT * FROM test_canonicalize_path('/abc/abc/./../abc/def'); +SELECT * FROM test_canonicalize_path('/abc/abc/def/../../abc/def'); + +-- prefix is '.' +SELECT * FROM test_canonicalize_path('.'); +SELECT * FROM test_canonicalize_path('./'); +SELECT * FROM test_canonicalize_path('./abc/..'); + +-- prefix is '..' +SELECT * FROM test_canonicalize_path('..'); +SELECT * FROM test_canonicalize_path('../'); +SELECT * FROM test_canonicalize_path('../abc/def'); +SELECT * FROM test_canonicalize_path('./../abc/def'); +SELECT * FROM test_canonicalize_path('./abc/../../abc/def'); +SELECT * FROM test_canonicalize_path('./abc/./def/.././ghi/../../../abc/def'); + +-- prefix is 'abc' +SELECT * FROM test_canonicalize_path('abc'); +SELECT * FROM test_canonicalize_path('abc/'); +SELECT * FROM test_canonicalize_path('./abc/def'); +SELECT * FROM test_canonicalize_path('./abc/../abc/def'); +SELECT * FROM test_canonicalize_path('./abc/./../abc/def'); +SELECT * FROM test_canonicalize_path('./abc/./def/.././../abc/def'); +SELECT * FROM test_canonicalize_path('abc/def'); +SELECT * FROM test_canonicalize_path('abc/../abc/def'); +SELECT * FROM test_canonicalize_path('abc/./../abc/def'); +SELECT * FROM test_canonicalize_path('abc/./def/.././../abc/def'); + +-- special +SELECT * FROM test_canonicalize_path('/abc/c:abc'); +SELECT * FROM test_canonicalize_path('/abc/c:/abc'); +SELECT * FROM test_canonicalize_path('/abc/c:\abc'); +SELECT * FROM test_canonicalize_path('/abc/c:abc/def'); +SELECT * FROM test_canonicalize_path('/abc/c:abc/./def'); +SELECT * FROM test_canonicalize_path('/abc/c:abc/./../def'); +SELECT * FROM test_canonicalize_path('c:abc'); +SELECT * FROM test_canonicalize_path('c:abc/def'); +SELECT * FROM test_canonicalize_path('./c:abc/./def'); +SELECT * FROM test_canonicalize_path('../c:abc/./../def'); +SELECT * FROM test_canonicalize_path('/..a/b..'); + + +-- Windows +-- prefix is 'C:\' or '\\network' +SELECT * FROM test_canonicalize_path('C:'); +SELECT * FROM test_canonicalize_path('C:\'); +SELECT * FROM test_canonicalize_path('C:\\'); +SELECT * FROM test_canonicalize_path('C:\\\'); +SELECT * FROM test_canonicalize_path('C:\\.\abc\def'); +SELECT * FROM test_canonicalize_path('C:\\.\..\abc\def'); +SELECT * FROM test_canonicalize_path('C:\.\..\..\abc\def'); +SELECT * FROM test_canonicalize_path('C:\..\abc\def'); +SELECT * FROM test_canonicalize_path('C:\..\..\abc\def'); +SELECT * FROM test_canonicalize_path('C:\abc\..\abc\def'); +SELECT * FROM test_canonicalize_path('C:\abc\.\..\abc\def'); +SELECT * FROM test_canonicalize_path('C:\abc\..\..\abc\def'); +SELECT * FROM test_canonicalize_path('C:\abc\def\..\..\abc\def'); +SELECT * FROM test_canonicalize_path('C:\abc\def\..\..\..\..\abc\def'); + +-- prefix is '\\network' +SELECT * FROM test_canonicalize_path('\\127.0.0.1'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\\'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\\\'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\\.\abc\def'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\\.\..\abc\def'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\.\..\..\abc\def'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\..\abc\def'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\..\..\abc\def'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\..\abc\def'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\.\..\abc\def'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\..\..\abc\def'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\def\..\..\abc\def'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\def\..\..\..\..\abc\def'); + +-- prefix is 'C:\abc' or '\\network\abc' +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\.\abc\def'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\abc\..\abc\def'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\abc\.\..\abc\def'); +SELECT * FROM test_canonicalize_path('\\127.0.0.1\abc\abc\def\..\..\abc\def'); \ No newline at end of file diff --git a/src/test/modules/test_path/test_path--1.0.sql b/src/test/modules/test_path/test_path--1.0.sql new file mode 100644 index 0000000000..042ff2439b --- /dev/null +++ b/src/test/modules/test_path/test_path--1.0.sql @@ -0,0 +1,14 @@ +/* src/test/modules/test_path/test_path--1.0.sql */ + +-- complain if script is sourced in psql, rather than via CREATE EXTENSION +\echo Use "CREATE EXTENSION test_path" to load this file. \quit + +CREATE FUNCTION test_canonicalize_path(text) +RETURNS text +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +CREATE FUNCTION test_platform() +RETURNS text +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; \ No newline at end of file diff --git a/src/test/modules/test_path/test_path.c b/src/test/modules/test_path/test_path.c new file mode 100644 index 0000000000..12b1300dc7 --- /dev/null +++ b/src/test/modules/test_path/test_path.c @@ -0,0 +1,53 @@ +/*------------------------------------------------------------------------- + * + * test_parser.c + * test canonicalize_path + * + * Copyright (c) 2007-2021, PostgreSQL Global Development Group + * + * IDENTIFICATION + * src/test/modules/test_path/test_path.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "fmgr.h" +#include "utils/builtins.h" + +PG_MODULE_MAGIC; + +/* + * functions + */ +PG_FUNCTION_INFO_V1(test_canonicalize_path); +PG_FUNCTION_INFO_V1(test_platform); + +Datum +test_canonicalize_path(PG_FUNCTION_ARGS) +{ + char *path = strdup(text_to_cstring(PG_GETARG_TEXT_PP(0))); + + if (!path) + ereport(ERROR, + (errcode(ERRCODE_OUT_OF_MEMORY), + errmsg("out of memory"))); + + canonicalize_path(path); + + PG_RETURN_TEXT_P(cstring_to_text(path)); +} + +Datum +test_platform(PG_FUNCTION_ARGS) +{ + char *platform; + +#ifdef WIN32 + platform = strdup("Win32"); +#else + platform = strdup("Linux"); +#endif + + PG_RETURN_TEXT_P(cstring_to_text(platform)); +} \ No newline at end of file diff --git a/src/test/modules/test_path/test_path.control b/src/test/modules/test_path/test_path.control new file mode 100644 index 0000000000..31ab323033 --- /dev/null +++ b/src/test/modules/test_path/test_path.control @@ -0,0 +1,5 @@ +# test_path extension +comment = 'test canonicalize_path' +default_version = '1.0' +module_pathname = '$libdir/test_path' +relocatable = true -- 2.26.2
0001-v2-make-canonicalize-path-remove-all-.-in-path.patch (application/octet-stream, 10.1K) download From f1092363ca283ba024289d53e094a97ef9cdba8b Mon Sep 17 00:00:00 2001 From: Shenhao Wang <wangsh.fnst@fujitsu.com> Date: Mon, 24 Jan 2022 09:57:34 +0800 Subject: [PATCH 1/2] v2 make canonicalize path remove all . in path --- contrib/adminpack/expected/adminpack.out | 2 +- src/port/path.c | 294 +++++++++++++++++++---- 2 files changed, 244 insertions(+), 52 deletions(-) diff --git a/contrib/adminpack/expected/adminpack.out b/contrib/adminpack/expected/adminpack.out index edf3ebfcba..76aafe6316 100644 --- a/contrib/adminpack/expected/adminpack.out +++ b/contrib/adminpack/expected/adminpack.out @@ -51,7 +51,7 @@ SELECT pg_file_write(current_setting('data_directory') || '/test_file4', 'test4' (1 row) SELECT pg_file_write(current_setting('data_directory') || '/../test_file4', 'test4', false); -ERROR: reference to parent directory ("..") not allowed +ERROR: absolute path not allowed RESET ROLE; REVOKE EXECUTE ON FUNCTION pg_file_write(text,text,bool) FROM regress_user1; REVOKE pg_read_all_settings FROM regress_user1; diff --git a/src/port/path.c b/src/port/path.c index 69bb8fe40b..e9f1c2e2a2 100644 --- a/src/port/path.c +++ b/src/port/path.c @@ -46,7 +46,7 @@ static void make_relative_path(char *ret_path, const char *target_path, const char *bin_path, const char *my_exec_path); -static void trim_directory(char *path); +static char *trim_directory(char *path); static void trim_trailing_separator(char *path); @@ -240,6 +240,56 @@ join_path_components(char *ret_path, } } +/* + * Get next dir name start from path. + * This function will replace the next dir separator to '\0'. + */ +static char* +canonicalize_path_get_next_dir(char *path) +{ + while(!IS_DIR_SEP(*path) && *path != '\0') + path++; + + if (*path != '\0') + { + *path = '\0'; + return path + 1; + } + else + return path; +} + +/* + * Append the dir after path for canonicalize_path. + */ +static char* +canonicalize_path_append_path(char *path, char *folder) +{ + int len = strlen(folder); + + /* No need to use memmove if path and folder are the same. */ + if (path != folder) + memmove(path, folder, len); + + return path + len; +} + +typedef enum { + ABSOLUTE_PATH_INIT = 0, /* Init state for absolute path, in this state, + * leading slash is handled(include driver on win32)*/ + ABSOLUTE_WITH_N_DEPTH, /* Absoulte path with at least one dirname */ + RELATIVE_PATH_INIT, /* Init state for relative path */ + RELATIVE_WITH_N_DEPTH, /* Relative path with at least one dirname, eg: + * a/b/c or ../../a/b/c */ + RELATIVE_WITH_CURRENT_REF, /* Relative path only has dot, eg: . */ + RELATIVE_WITH_PARENT_REF /* Relative path only has double-dot, eg: ../.. */ +} canonicalize_state; + +typedef enum { + TYPE_CURRENT_DIR = 0, + TYPE_PARENT_DIR, + TYPE_NORMAL_DIR +} path_type; /* * Clean up path by: @@ -247,18 +297,23 @@ join_path_components(char *ret_path, * o remove trailing quote on Win32 * o remove trailing slash * o remove duplicate adjacent separators - * o remove trailing '.' + * o remove '.' (except for only '.') * o process trailing '..' ourselves */ void canonicalize_path(char *path) { - char *p, - *to_p; char *spath; + char *parsed; + char *unparse; + char *unparse_next; + char *p, *to_p; bool was_sep = false; - int pending_strips; + canonicalize_state state; + path_type type; + int pathdepth = 0; /* used when state is ABSOLUTE_WITH_N_DEPTH or + * RELATIVE_WITH_CURRENT_REF */ #ifdef WIN32 /* @@ -308,60 +363,194 @@ canonicalize_path(char *path) *to_p = '\0'; /* - * Remove any trailing uses of "." and process ".." ourselves - * - * Note that "/../.." should reduce to just "/", while "../.." has to be - * kept as-is. In the latter case we put back mistakenly trimmed ".." - * components below. Also note that we want a Windows drive spec to be - * visible to trim_directory(), but it's not part of the logic that's - * looking at the name components; hence distinction between path and - * spath. + * Remove any uses of "." and process ".." ourselves */ spath = skip_drive(path); - pending_strips = 0; - for (;;) + if (*spath == '\0') + return; + + if (is_absolute_path(path)) + { + state = ABSOLUTE_PATH_INIT; + /* Skip the leading slash for absolute path */ + parsed = unparse = (spath + 1); + } + else { - int len = strlen(spath); + state = RELATIVE_PATH_INIT; + parsed = unparse = spath; + } - if (len >= 2 && strcmp(spath + len - 2, "/.") == 0) - trim_directory(path); - else if (strcmp(spath, ".") == 0) - { - /* Want to leave "." alone, but "./.." has to become ".." */ - if (pending_strips > 0) - *spath = '\0'; - break; - } - else if ((len >= 3 && strcmp(spath + len - 3, "/..") == 0) || - strcmp(spath, "..") == 0) + while(*unparse != '\0') + { + unparse_next = canonicalize_path_get_next_dir(unparse); + + if (strcmp(unparse, ".") == 0) + type = TYPE_CURRENT_DIR; + else if (strcmp(unparse, "..") == 0) + type = TYPE_PARENT_DIR; + else if (*unparse != '\0') + type = TYPE_NORMAL_DIR; + else { - trim_directory(path); - pending_strips++; + /* duplicate adjacent separators has been handled before */ + Assert(false); } - else if (pending_strips > 0 && *spath != '\0') + + switch(state) { - /* trim a regular directory name canceled by ".." */ - trim_directory(path); - pending_strips--; - /* foo/.. should become ".", not empty */ - if (*spath == '\0') - strcpy(spath, "."); + case ABSOLUTE_PATH_INIT: + { + if (type == TYPE_NORMAL_DIR) + { + /* + * Append next dir name, we don't need to append a slash + * when state is ABSOLUTE_PATH_INIT. + */ + parsed = canonicalize_path_append_path(parsed, unparse); + state = ABSOLUTE_WITH_N_DEPTH; + pathdepth ++; + } + + /* Ignore dot(.) and double-dot(..) case */ + break; + } + case RELATIVE_PATH_INIT: + { + if (type == TYPE_CURRENT_DIR) + { + /* Append next dot(.) */ + parsed = canonicalize_path_append_path(parsed, unparse); + state = RELATIVE_WITH_CURRENT_REF; + } + else if (type == TYPE_PARENT_DIR) + { + /* Append next double-dot(..) */ + parsed = canonicalize_path_append_path(parsed, unparse); + state = RELATIVE_WITH_PARENT_REF; + } + else if (type == TYPE_NORMAL_DIR) + { + /* Append next dir */ + parsed = canonicalize_path_append_path(parsed, unparse); + state = RELATIVE_WITH_N_DEPTH; + pathdepth ++; + } + break; + } + case ABSOLUTE_WITH_N_DEPTH: + { + if (type == TYPE_PARENT_DIR) + { + /* trim_directory never remove the leading slash. */ + *parsed = '\0'; + parsed = trim_directory(path); + if (-- pathdepth == 0) + { + state = ABSOLUTE_PATH_INIT; + } + else + state = ABSOLUTE_WITH_N_DEPTH; + } + else if (type == TYPE_NORMAL_DIR) + { + /* Append next dir */ + *parsed ++ = '/'; + parsed = canonicalize_path_append_path(parsed, unparse); + state = ABSOLUTE_WITH_N_DEPTH; + pathdepth ++; + } + + /* Ignore dot(.) case */ + break; + } + case RELATIVE_WITH_N_DEPTH: + { + if (type == TYPE_PARENT_DIR) + { + /* Remove last parsed dir */ + *parsed = '\0'; + parsed = trim_directory(path); + if (--pathdepth == 0) + { + /* + * There are only two cases when pathdepth becomes 0. + * - "dir/.." (leading dot(.) is always be removed.) + * - "../dir/.." + */ + if (parsed != spath) + state = RELATIVE_WITH_PARENT_REF; + else + { + /* + * spath becomes an empty string, append a dot(.) + * which makes the spath never become an empty string. + */ + *parsed ++ = '.'; + state = RELATIVE_WITH_CURRENT_REF; + } + } + else + state = RELATIVE_WITH_N_DEPTH; + } + else if (type == TYPE_NORMAL_DIR) + { + /* Append next dir */ + *parsed ++ = '/'; + parsed = canonicalize_path_append_path(parsed, unparse); + state = RELATIVE_WITH_N_DEPTH; + pathdepth ++; + } + + /* Ignore dot(.) case */ + break; + } + case RELATIVE_WITH_CURRENT_REF: + { + if (type == TYPE_PARENT_DIR) + { + /* Leading "./.." will be converted to ".." */ + parsed = canonicalize_path_append_path(parsed - 1, unparse); + state = RELATIVE_WITH_PARENT_REF; + } + else if (type == TYPE_NORMAL_DIR) + { + /* Leading "./dir" will be converted ot "dir" */ + parsed = canonicalize_path_append_path(parsed - 1, unparse); + state = RELATIVE_WITH_N_DEPTH; + pathdepth = 1; + } + + /* Ignore dot(.) case */ + break; + } + case RELATIVE_WITH_PARENT_REF: + { + if (type == TYPE_PARENT_DIR) + { + /* Append next double-dot(..) */ + *parsed ++ = '/'; + parsed = canonicalize_path_append_path(parsed, unparse); + state = RELATIVE_WITH_PARENT_REF; + } + else if (type == TYPE_NORMAL_DIR) + { + /* Append next dir */ + *parsed ++ = '/'; + parsed = canonicalize_path_append_path(parsed, unparse); + state = RELATIVE_WITH_N_DEPTH; + pathdepth = 1; + } + + /* Ignore dot(.) case */ + break; + } } - else - break; - } - if (pending_strips > 0) - { - /* - * We could only get here if path is now totally empty (other than a - * possible drive specifier on Windows). We have to put back one or - * more ".."'s that we took off. - */ - while (--pending_strips > 0) - strcat(path, "../"); - strcat(path, ".."); + unparse = unparse_next; } + + *parsed = '\0'; } /* @@ -866,7 +1055,7 @@ get_parent_directory(char *path) * the last pathname component, and the slash just ahead of it --- but never * remove a leading slash. */ -static void +static char * trim_directory(char *path) { char *p; @@ -874,7 +1063,7 @@ trim_directory(char *path) path = skip_drive(path); if (path[0] == '\0') - return; + return path; /* back up over trailing slash(es) */ for (p = path + strlen(path) - 1; IS_DIR_SEP(*p) && p > path; p--) @@ -889,6 +1078,9 @@ trim_directory(char *path) if (p == path && IS_DIR_SEP(*p)) p++; *p = '\0'; + + /* return the end of the path for canonicalize_path */ + return p; } -- 2.26.2
^ permalink raw reply [nested|flat] 23+ messages in thread

* Re: drop tablespace failed when location contains .. on win32
  2021-09-08 10:16 drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-08 12:54 ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-09 02:19   ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 02:35     ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-09 03:44       ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 12:30         ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-12 07:33           ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-13 07:06             ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-13 08:36               ` Re: drop tablespace failed when location contains .. on win32 Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2021-09-26 08:40                 ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-11-10 22:43                   ` Re: drop tablespace failed when location contains .. on win32 Tom Lane <tgl@sss.pgh.pa.us>
  2021-11-19 07:50                     ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2022-01-17 00:40                       ` Re: drop tablespace failed when location contains .. on win32 Julien Rouhaud <rjuju123@gmail.com>
  2022-01-18 01:08                         ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2022-01-18 01:19                           ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2022-01-24 11:21                             ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
@ 2022-01-24 11:46                               ` Michael Paquier <michael@paquier.xyz>
  2022-01-30 21:50                                 ` Re: drop tablespace failed when location contains .. on win32 Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 23+ messages in thread

From: Michael Paquier @ 2022-01-24 11:46 UTC (permalink / raw)
  To: wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>; +Cc: Julien Rouhaud <rjuju123@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; andrew@dunslane.net <andrew@dunslane.net>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>

On Mon, Jan 24, 2022 at 11:21:12AM +0000, wangsh.fnst@fujitsu.com wrote:
> Tom Lane <tgl@sss.pgh.pa.us> wrote:
>>> I concur with the upthread comments that there's little chance
>>> we'll commit 0003 as-is; the code-to-benefit ratio is too high.
>>> Instead, you might consider adding test_canonicalize_path in
>>> src/test/regress/regress.c, and then adding a smaller number
>>> of regression test cases using that.
>> 
>> Sounds like a good idea to me.  I would move these in misc.source for
>> anything that require an absolute path.
> 
> I'm not fully understand this. So, I do not change the test patch.

In order to make the tests cheap, there is no need to have a separate
module in src/test/modules/ as your patch 0002 is doing.  Instead, you
should move the C code of your SQL function test_canonicalize_path()
to src/test/regress/regress.c, then add some tests in
src/test/regress/sql/, with a SQL function created in the test script
that feeds from what would be added to regress.so.

Please note that my previous comment has become incorrect as of
dc9c3b0, that has removed the concept of input/output files in the
regression tests, but you can do the same with a \getenv to get access
to absolute paths for the tests.  There are many examples in the tree
for that, one is copy.sql.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 23+ messages in thread

* Re: drop tablespace failed when location contains .. on win32
  2021-09-08 10:16 drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-08 12:54 ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-09 02:19   ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 02:35     ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-09 03:44       ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 12:30         ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-12 07:33           ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-13 07:06             ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-13 08:36               ` Re: drop tablespace failed when location contains .. on win32 Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2021-09-26 08:40                 ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-11-10 22:43                   ` Re: drop tablespace failed when location contains .. on win32 Tom Lane <tgl@sss.pgh.pa.us>
  2021-11-19 07:50                     ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2022-01-17 00:40                       ` Re: drop tablespace failed when location contains .. on win32 Julien Rouhaud <rjuju123@gmail.com>
  2022-01-18 01:08                         ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2022-01-18 01:19                           ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2022-01-24 11:21                             ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2022-01-24 11:46                               ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
@ 2022-01-30 21:50                                 ` Tom Lane <tgl@sss.pgh.pa.us>
  2022-01-31 08:15                                   ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2022-01-31 16:58                                   ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  0 siblings, 2 replies; 23+ messages in thread

From: Tom Lane @ 2022-01-30 21:50 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>; Julien Rouhaud <rjuju123@gmail.com>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; andrew@dunslane.net <andrew@dunslane.net>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>

Michael Paquier <michael@paquier.xyz> writes:
> In order to make the tests cheap, there is no need to have a separate
> module in src/test/modules/ as your patch 0002 is doing.  Instead, you
> should move the C code of your SQL function test_canonicalize_path()
> to src/test/regress/regress.c, then add some tests in
> src/test/regress/sql/, with a SQL function created in the test script
> that feeds from what would be added to regress.so.

Here's a revised patch version that does it like that.  I also
reviewed and simplified the canonicalize_path logic.  I think
this is committable.

(I suspect that adminpack's checks for unsafe file names could
now be simplified substantially, because many of the corner cases
it worries about are no longer possible, as evidenced by the change
in error message there.  I've not pursued that, however.)

			regards, tom lane

Attachments:

v3-improve-canonicalize-path.patch (text/x-diff, 16.3K) download diff --git a/contrib/adminpack/expected/adminpack.out b/contrib/adminpack/expected/adminpack.out index edf3ebfcba..76aafe6316 100644 --- a/contrib/adminpack/expected/adminpack.out +++ b/contrib/adminpack/expected/adminpack.out @@ -51,7 +51,7 @@ SELECT pg_file_write(current_setting('data_directory') || '/test_file4', 'test4' (1 row) SELECT pg_file_write(current_setting('data_directory') || '/../test_file4', 'test4', false); -ERROR: reference to parent directory ("..") not allowed +ERROR: absolute path not allowed RESET ROLE; REVOKE EXECUTE ON FUNCTION pg_file_write(text,text,bool) FROM regress_user1; REVOKE pg_read_all_settings FROM regress_user1; diff --git a/src/port/path.c b/src/port/path.c index 69bb8fe40b..e8b0364b13 100644 --- a/src/port/path.c +++ b/src/port/path.c @@ -46,8 +46,9 @@ static void make_relative_path(char *ret_path, const char *target_path, const char *bin_path, const char *my_exec_path); -static void trim_directory(char *path); +static char *trim_directory(char *path); static void trim_trailing_separator(char *path); +static char *append_subdir_to_path(char *path, char *subdir); /* @@ -222,13 +223,9 @@ join_path_components(char *ret_path, strlcpy(ret_path, head, MAXPGPATH); /* - * Remove any leading "." in the tail component. - * - * Note: we used to try to remove ".." as well, but that's tricky to get - * right; now we just leave it to be done by canonicalize_path() later. + * We used to try to simplify some cases involving "." and "..", but now + * we just leave that to be done by canonicalize_path() later. */ - while (tail[0] == '.' && IS_DIR_SEP(tail[1])) - tail += 2; if (*tail) { @@ -241,14 +238,27 @@ join_path_components(char *ret_path, } +/* State machine states for canonicalize_path */ +typedef enum +{ + ABSOLUTE_PATH_INIT, /* Just past the leading '/' (and Windows + * drive name if any) of an absolute path */ + ABSOLUTE_WITH_N_DEPTH, /* We collected 'pathdepth' directories in an + * absolute path */ + RELATIVE_PATH_INIT, /* At start of a relative path */ + RELATIVE_WITH_N_DEPTH, /* We collected 'pathdepth' directories in a + * relative path */ + RELATIVE_WITH_PARENT_REF /* Relative path containing only double-dots */ +} canonicalize_state; + /* * Clean up path by: * o make Win32 path use Unix slashes * o remove trailing quote on Win32 * o remove trailing slash - * o remove duplicate adjacent separators - * o remove trailing '.' - * o process trailing '..' ourselves + * o remove duplicate (adjacent) separators + * o remove '.' (unless path reduces to only '.') + * o process '..' ourselves */ void canonicalize_path(char *path) @@ -256,8 +266,11 @@ canonicalize_path(char *path) char *p, *to_p; char *spath; + char *parsed; + char *unparse; bool was_sep = false; - int pending_strips; + canonicalize_state state; + int pathdepth = 0; /* counts collected regular directory names */ #ifdef WIN32 @@ -308,60 +321,172 @@ canonicalize_path(char *path) *to_p = '\0'; /* - * Remove any trailing uses of "." and process ".." ourselves + * Remove any uses of "." and process ".." ourselves * * Note that "/../.." should reduce to just "/", while "../.." has to be - * kept as-is. In the latter case we put back mistakenly trimmed ".." - * components below. Also note that we want a Windows drive spec to be - * visible to trim_directory(), but it's not part of the logic that's - * looking at the name components; hence distinction between path and - * spath. + * kept as-is. Also note that we want a Windows drive spec to be visible + * to trim_directory(), but it's not part of the logic that's looking at + * the name components; hence distinction between path and spath. + * + * This loop overwrites the path in-place. This is safe since we'll never + * make the path longer. "unparse" points to where we are reading the + * path, "parse" to where we are writing. */ spath = skip_drive(path); - pending_strips = 0; - for (;;) + if (*spath == '\0') + return; /* empty path is returned as-is */ + + if (IS_DIR_SEP(*spath)) { - int len = strlen(spath); + state = ABSOLUTE_PATH_INIT; + /* Skip the leading slash for absolute path */ + parsed = unparse = (spath + 1); + } + else + { + state = RELATIVE_PATH_INIT; + parsed = unparse = spath; + } - if (len >= 2 && strcmp(spath + len - 2, "/.") == 0) - trim_directory(path); - else if (strcmp(spath, ".") == 0) + while (*unparse != '\0') + { + char *unparse_next; + bool is_double_dot; + + /* Split off this dir name, and set unparse_next to the next one */ + unparse_next = unparse; + while (*unparse_next && !IS_DIR_SEP(*unparse_next)) + unparse_next++; + if (*unparse_next != '\0') + *unparse_next++ = '\0'; + + /* Identify type of this dir name */ + if (strcmp(unparse, ".") == 0) { - /* Want to leave "." alone, but "./.." has to become ".." */ - if (pending_strips > 0) - *spath = '\0'; - break; + /* We can ignore "." components in all cases */ + unparse = unparse_next; + continue; } - else if ((len >= 3 && strcmp(spath + len - 3, "/..") == 0) || - strcmp(spath, "..") == 0) + + if (strcmp(unparse, "..") == 0) + is_double_dot = true; + else { - trim_directory(path); - pending_strips++; + /* adjacent separators were eliminated above */ + Assert(*unparse != '\0'); + is_double_dot = false; } - else if (pending_strips > 0 && *spath != '\0') + + switch (state) { - /* trim a regular directory name canceled by ".." */ - trim_directory(path); - pending_strips--; - /* foo/.. should become ".", not empty */ - if (*spath == '\0') - strcpy(spath, "."); + case ABSOLUTE_PATH_INIT: + /* We can ignore ".." immediately after / */ + if (!is_double_dot) + { + /* Append first dir name (we already have leading slash) */ + parsed = append_subdir_to_path(parsed, unparse); + state = ABSOLUTE_WITH_N_DEPTH; + pathdepth++; + } + break; + case ABSOLUTE_WITH_N_DEPTH: + if (is_double_dot) + { + /* trim_directory won't remove the leading slash. */ + *parsed = '\0'; + parsed = trim_directory(path); + if (--pathdepth == 0) + state = ABSOLUTE_PATH_INIT; + } + else + { + /* Append normal dir */ + *parsed++ = '/'; + parsed = append_subdir_to_path(parsed, unparse); + pathdepth++; + } + break; + case RELATIVE_PATH_INIT: + if (is_double_dot) + { + /* Append irreducible double-dot (..) */ + parsed = append_subdir_to_path(parsed, unparse); + state = RELATIVE_WITH_PARENT_REF; + } + else + { + /* Append normal dir */ + parsed = append_subdir_to_path(parsed, unparse); + state = RELATIVE_WITH_N_DEPTH; + pathdepth++; + } + break; + case RELATIVE_WITH_N_DEPTH: + if (is_double_dot) + { + /* Remove last parsed dir */ + *parsed = '\0'; + parsed = trim_directory(path); + if (--pathdepth == 0) + { + /* + * If the output path is now empty, we're back to the + * INIT state. However, we could have processed a + * path like "../dir/.." and now be down to "..", in + * which case enter the correct state for that. + */ + if (parsed == spath) + state = RELATIVE_PATH_INIT; + else + state = RELATIVE_WITH_PARENT_REF; + } + } + else + { + /* Append normal dir */ + *parsed++ = '/'; + parsed = append_subdir_to_path(parsed, unparse); + pathdepth++; + } + break; + case RELATIVE_WITH_PARENT_REF: + if (is_double_dot) + { + /* Append next irreducible double-dot (..) */ + *parsed++ = '/'; + parsed = append_subdir_to_path(parsed, unparse); + } + else + { + /* Append normal dir */ + *parsed++ = '/'; + parsed = append_subdir_to_path(parsed, unparse); + + /* + * We can now start counting normal dirs. But if later + * double-dots make us remove this dir again, we'd better + * revert to RELATIVE_WITH_PARENT_REF not INIT state. + */ + state = RELATIVE_WITH_N_DEPTH; + pathdepth = 1; + } + break; } - else - break; - } - if (pending_strips > 0) - { - /* - * We could only get here if path is now totally empty (other than a - * possible drive specifier on Windows). We have to put back one or - * more ".."'s that we took off. - */ - while (--pending_strips > 0) - strcat(path, "../"); - strcat(path, ".."); + unparse = unparse_next; } + + /* + * If our output path is empty at this point, insert ".". We don't want + * to do this any earlier because it'd result in an extra dot in corner + * cases such as "../dir/..". Since we rejected the wholly-empty-path + * case above, there is certainly room. + */ + if (parsed == spath) + *parsed++ = '.'; + + /* And finally, ensure the output path is nul-terminated. */ + *parsed = '\0'; } /* @@ -865,8 +990,11 @@ get_parent_directory(char *path) * Trim trailing directory from path, that is, remove any trailing slashes, * the last pathname component, and the slash just ahead of it --- but never * remove a leading slash. + * + * For the convenience of canonicalize_path, the path's new end location + * is returned. */ -static void +static char * trim_directory(char *path) { char *p; @@ -874,7 +1002,7 @@ trim_directory(char *path) path = skip_drive(path); if (path[0] == '\0') - return; + return path; /* back up over trailing slash(es) */ for (p = path + strlen(path) - 1; IS_DIR_SEP(*p) && p > path; p--) @@ -889,6 +1017,7 @@ trim_directory(char *path) if (p == path && IS_DIR_SEP(*p)) p++; *p = '\0'; + return p; } @@ -908,3 +1037,25 @@ trim_trailing_separator(char *path) for (p--; p > path && IS_DIR_SEP(*p); p--) *p = '\0'; } + +/* + * append_subdir_to_path + * + * Append the currently-considered subdirectory name to the output + * path in canonicalize_path. Return the new end location of the + * output path. + * + * Since canonicalize_path updates the path in-place, we must use + * memmove not memcpy, and we don't yet terminate the path with '\0'. + */ +static char * +append_subdir_to_path(char *path, char *subdir) +{ + size_t len = strlen(subdir); + + /* No need to copy data if path and subdir are the same. */ + if (path != subdir) + memmove(path, subdir, len); + + return path + len; +} diff --git a/src/test/regress/expected/create_function_1.out b/src/test/regress/expected/create_function_1.out index 5345ed0840..6141b7060b 100644 --- a/src/test/regress/expected/create_function_1.out +++ b/src/test/regress/expected/create_function_1.out @@ -28,3 +28,7 @@ CREATE FUNCTION int44out(city_budget) AS :'regresslib' LANGUAGE C STRICT IMMUTABLE; NOTICE: argument type city_budget is only a shell +CREATE FUNCTION test_canonicalize_path(text) + RETURNS text + AS :'regresslib' + LANGUAGE C STRICT IMMUTABLE; diff --git a/src/test/regress/expected/misc_functions.out b/src/test/regress/expected/misc_functions.out index e2c77d0ac8..cce92b014f 100644 --- a/src/test/regress/expected/misc_functions.out +++ b/src/test/regress/expected/misc_functions.out @@ -133,6 +133,129 @@ ERROR: function num_nulls() does not exist LINE 1: SELECT num_nulls(); ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts. +-- +-- canonicalize_path() +-- +SELECT * FROM test_canonicalize_path('/'); + test_canonicalize_path +------------------------ + / +(1 row) + +SELECT * FROM test_canonicalize_path('/./abc/def/'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/./../abc/def'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/./../../abc/def/'); + test_canonicalize_path +------------------------ + /abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/.././def/ghi'); + test_canonicalize_path +------------------------ + /def/ghi +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/./../def/ghi//'); + test_canonicalize_path +------------------------ + /def/ghi +(1 row) + +SELECT * FROM test_canonicalize_path('/abc/def/../../../../ghi/jkl'); + test_canonicalize_path +------------------------ + /ghi/jkl +(1 row) + +SELECT * FROM test_canonicalize_path('.'); + test_canonicalize_path +------------------------ + . +(1 row) + +SELECT * FROM test_canonicalize_path('./'); + test_canonicalize_path +------------------------ + . +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/..'); + test_canonicalize_path +------------------------ + . +(1 row) + +SELECT * FROM test_canonicalize_path('abc/../'); + test_canonicalize_path +------------------------ + . +(1 row) + +SELECT * FROM test_canonicalize_path('abc/../def'); + test_canonicalize_path +------------------------ + def +(1 row) + +SELECT * FROM test_canonicalize_path('..'); + test_canonicalize_path +------------------------ + .. +(1 row) + +SELECT * FROM test_canonicalize_path('../abc/def'); + test_canonicalize_path +------------------------ + ../abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('../abc/..'); + test_canonicalize_path +------------------------ + .. +(1 row) + +SELECT * FROM test_canonicalize_path('../abc/../def'); + test_canonicalize_path +------------------------ + ../def +(1 row) + +SELECT * FROM test_canonicalize_path('../abc/../../def/ghi'); + test_canonicalize_path +------------------------ + ../../def/ghi +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/./def/.'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/././def/.'); + test_canonicalize_path +------------------------ + abc/def +(1 row) + +SELECT * FROM test_canonicalize_path('./abc/./def/.././ghi/../../../jkl/mno'); + test_canonicalize_path +------------------------ + ../jkl/mno +(1 row) + -- -- pg_log_backend_memory_contexts() -- diff --git a/src/test/regress/regress.c b/src/test/regress/regress.c index eefbd0f0df..0802fb9136 100644 --- a/src/test/regress/regress.c +++ b/src/test/regress/regress.c @@ -531,6 +531,16 @@ int44out(PG_FUNCTION_ARGS) PG_RETURN_CSTRING(result); } +PG_FUNCTION_INFO_V1(test_canonicalize_path); +Datum +test_canonicalize_path(PG_FUNCTION_ARGS) +{ + char *path = text_to_cstring(PG_GETARG_TEXT_PP(0)); + + canonicalize_path(path); + PG_RETURN_TEXT_P(cstring_to_text(path)); +} + PG_FUNCTION_INFO_V1(make_tuple_indirect); Datum make_tuple_indirect(PG_FUNCTION_ARGS) diff --git a/src/test/regress/sql/create_function_1.sql b/src/test/regress/sql/create_function_1.sql index 4170b16fe6..34cc7c6efc 100644 --- a/src/test/regress/sql/create_function_1.sql +++ b/src/test/regress/sql/create_function_1.sql @@ -29,3 +29,8 @@ CREATE FUNCTION int44out(city_budget) RETURNS cstring AS :'regresslib' LANGUAGE C STRICT IMMUTABLE; + +CREATE FUNCTION test_canonicalize_path(text) + RETURNS text + AS :'regresslib' + LANGUAGE C STRICT IMMUTABLE; diff --git a/src/test/regress/sql/misc_functions.sql b/src/test/regress/sql/misc_functions.sql index 1159f6b585..448114808d 100644 --- a/src/test/regress/sql/misc_functions.sql +++ b/src/test/regress/sql/misc_functions.sql @@ -30,6 +30,31 @@ SELECT num_nulls(VARIADIC '{}'::int[]); SELECT num_nonnulls(); SELECT num_nulls(); +-- +-- canonicalize_path() +-- + +SELECT * FROM test_canonicalize_path('/'); +SELECT * FROM test_canonicalize_path('/./abc/def/'); +SELECT * FROM test_canonicalize_path('/./../abc/def'); +SELECT * FROM test_canonicalize_path('/./../../abc/def/'); +SELECT * FROM test_canonicalize_path('/abc/.././def/ghi'); +SELECT * FROM test_canonicalize_path('/abc/./../def/ghi//'); +SELECT * FROM test_canonicalize_path('/abc/def/../../../../ghi/jkl'); +SELECT * FROM test_canonicalize_path('.'); +SELECT * FROM test_canonicalize_path('./'); +SELECT * FROM test_canonicalize_path('./abc/..'); +SELECT * FROM test_canonicalize_path('abc/../'); +SELECT * FROM test_canonicalize_path('abc/../def'); +SELECT * FROM test_canonicalize_path('..'); +SELECT * FROM test_canonicalize_path('../abc/def'); +SELECT * FROM test_canonicalize_path('../abc/..'); +SELECT * FROM test_canonicalize_path('../abc/../def'); +SELECT * FROM test_canonicalize_path('../abc/../../def/ghi'); +SELECT * FROM test_canonicalize_path('./abc/./def/.'); +SELECT * FROM test_canonicalize_path('./abc/././def/.'); +SELECT * FROM test_canonicalize_path('./abc/./def/.././ghi/../../../jkl/mno'); + -- -- pg_log_backend_memory_contexts() --
^ permalink raw reply [nested|flat] 23+ messages in thread

* Re: drop tablespace failed when location contains .. on win32
  2021-09-08 10:16 drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-08 12:54 ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-09 02:19   ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 02:35     ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-09 03:44       ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 12:30         ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-12 07:33           ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-13 07:06             ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-13 08:36               ` Re: drop tablespace failed when location contains .. on win32 Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2021-09-26 08:40                 ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-11-10 22:43                   ` Re: drop tablespace failed when location contains .. on win32 Tom Lane <tgl@sss.pgh.pa.us>
  2021-11-19 07:50                     ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2022-01-17 00:40                       ` Re: drop tablespace failed when location contains .. on win32 Julien Rouhaud <rjuju123@gmail.com>
  2022-01-18 01:08                         ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2022-01-18 01:19                           ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2022-01-24 11:21                             ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2022-01-24 11:46                               ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2022-01-30 21:50                                 ` Re: drop tablespace failed when location contains .. on win32 Tom Lane <tgl@sss.pgh.pa.us>
@ 2022-01-31 08:15                                   ` Michael Paquier <michael@paquier.xyz>
  2022-01-31 12:23                                     ` Re: drop tablespace failed when location contains .. on win32 Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 1 reply; 23+ messages in thread

From: Michael Paquier @ 2022-01-31 08:15 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>; Julien Rouhaud <rjuju123@gmail.com>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; andrew@dunslane.net <andrew@dunslane.net>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>

On Sun, Jan 30, 2022 at 04:50:03PM -0500, Tom Lane wrote:
> Here's a revised patch version that does it like that.  I also
> reviewed and simplified the canonicalize_path logic.  I think
> this is committable.

Thanks for the updated version.  The range of the tests looks fine
enough, and the CF bot does not complain.  The code is
straight-forward and pretty clear in terms of the handling of ".",
".." and the N-depth handling necessary.

Should we have tests for WIN32 (aka for driver letters and "//")?
This could be split into its own separate test file to limit the
damage with the alternate outputs, and the original complain was from
there.

> (I suspect that adminpack's checks for unsafe file names could
> now be simplified substantially, because many of the corner cases
> it worries about are no longer possible, as evidenced by the change
> in error message there.  I've not pursued that, however.)

Fine by me to let this part for later.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 23+ messages in thread

* Re: drop tablespace failed when location contains .. on win32
  2021-09-08 10:16 drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-08 12:54 ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-09 02:19   ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 02:35     ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-09 03:44       ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 12:30         ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-12 07:33           ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-13 07:06             ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-13 08:36               ` Re: drop tablespace failed when location contains .. on win32 Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2021-09-26 08:40                 ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-11-10 22:43                   ` Re: drop tablespace failed when location contains .. on win32 Tom Lane <tgl@sss.pgh.pa.us>
  2021-11-19 07:50                     ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2022-01-17 00:40                       ` Re: drop tablespace failed when location contains .. on win32 Julien Rouhaud <rjuju123@gmail.com>
  2022-01-18 01:08                         ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2022-01-18 01:19                           ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2022-01-24 11:21                             ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2022-01-24 11:46                               ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2022-01-30 21:50                                 ` Re: drop tablespace failed when location contains .. on win32 Tom Lane <tgl@sss.pgh.pa.us>
  2022-01-31 08:15                                   ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
@ 2022-01-31 12:23                                     ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 23+ messages in thread

From: Tom Lane @ 2022-01-31 12:23 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>; Julien Rouhaud <rjuju123@gmail.com>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; andrew@dunslane.net <andrew@dunslane.net>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>

Michael Paquier <michael@paquier.xyz> writes:
> Should we have tests for WIN32 (aka for driver letters and "//")?
> This could be split into its own separate test file to limit the
> damage with the alternate outputs, and the original complain was from
> there.

I thought about it and concluded that the value couldn't justify
the pain-in-the-neck factor of adding a platform-specific variant
result file.  skip_drive() is pretty simple and decoupled from what
we're trying to test here, plus it hasn't changed in decades and
is unlikely to do so in future.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 23+ messages in thread

* Re: drop tablespace failed when location contains .. on win32
  2021-09-08 10:16 drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-08 12:54 ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-09 02:19   ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 02:35     ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-09 03:44       ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 12:30         ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-12 07:33           ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-13 07:06             ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-13 08:36               ` Re: drop tablespace failed when location contains .. on win32 Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2021-09-26 08:40                 ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-11-10 22:43                   ` Re: drop tablespace failed when location contains .. on win32 Tom Lane <tgl@sss.pgh.pa.us>
  2021-11-19 07:50                     ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2022-01-17 00:40                       ` Re: drop tablespace failed when location contains .. on win32 Julien Rouhaud <rjuju123@gmail.com>
  2022-01-18 01:08                         ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2022-01-18 01:19                           ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2022-01-24 11:21                             ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2022-01-24 11:46                               ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2022-01-30 21:50                                 ` Re: drop tablespace failed when location contains .. on win32 Tom Lane <tgl@sss.pgh.pa.us>
@ 2022-01-31 16:58                                   ` Andrew Dunstan <andrew@dunslane.net>
  2022-01-31 17:18                                     ` Re: drop tablespace failed when location contains .. on win32 Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 1 reply; 23+ messages in thread

From: Andrew Dunstan @ 2022-01-31 16:58 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; Michael Paquier <michael@paquier.xyz>; +Cc: wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>; Julien Rouhaud <rjuju123@gmail.com>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>


On 1/30/22 16:50, Tom Lane wrote:
> Michael Paquier <michael@paquier.xyz> writes:
>> In order to make the tests cheap, there is no need to have a separate
>> module in src/test/modules/ as your patch 0002 is doing.  Instead, you
>> should move the C code of your SQL function test_canonicalize_path()
>> to src/test/regress/regress.c, then add some tests in
>> src/test/regress/sql/, with a SQL function created in the test script
>> that feeds from what would be added to regress.so.
> Here's a revised patch version that does it like that.  I also
> reviewed and simplified the canonicalize_path logic.  I think
> this is committable.


LGTM


cheers


andrew

--
Andrew Dunstan
EDB: https://www.enterprisedb.com






^ permalink  raw  reply  [nested|flat] 23+ messages in thread

* Re: drop tablespace failed when location contains .. on win32
  2021-09-08 10:16 drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-08 12:54 ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-09 02:19   ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 02:35     ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-09 03:44       ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-09 12:30         ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
  2021-09-12 07:33           ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-09-13 07:06             ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2021-09-13 08:36               ` Re: drop tablespace failed when location contains .. on win32 Kyotaro Horiguchi <horikyota.ntt@gmail.com>
  2021-09-26 08:40                 ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2021-11-10 22:43                   ` Re: drop tablespace failed when location contains .. on win32 Tom Lane <tgl@sss.pgh.pa.us>
  2021-11-19 07:50                     ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2022-01-17 00:40                       ` Re: drop tablespace failed when location contains .. on win32 Julien Rouhaud <rjuju123@gmail.com>
  2022-01-18 01:08                         ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2022-01-18 01:19                           ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2022-01-24 11:21                             ` RE: drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
  2022-01-24 11:46                               ` Re: drop tablespace failed when location contains .. on win32 Michael Paquier <michael@paquier.xyz>
  2022-01-30 21:50                                 ` Re: drop tablespace failed when location contains .. on win32 Tom Lane <tgl@sss.pgh.pa.us>
  2022-01-31 16:58                                   ` Re: drop tablespace failed when location contains .. on win32 Andrew Dunstan <andrew@dunslane.net>
@ 2022-01-31 17:18                                     ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 23+ messages in thread

From: Tom Lane @ 2022-01-31 17:18 UTC (permalink / raw)
  To: Andrew Dunstan <andrew@dunslane.net>; +Cc: Michael Paquier <michael@paquier.xyz>; wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>; Julien Rouhaud <rjuju123@gmail.com>; Kyotaro Horiguchi <horikyota.ntt@gmail.com>; pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>

Andrew Dunstan <andrew@dunslane.net> writes:
> On 1/30/22 16:50, Tom Lane wrote:
>> Here's a revised patch version that does it like that.  I also
>> reviewed and simplified the canonicalize_path logic.  I think
>> this is committable.

> LGTM

Pushed, thanks for looking.

I think I'm also going to have a look at simplifying some of the
dependent code, just because it feels weird to leave that unfinished.
In particular, Shenhao-san suggested upthread that we could remove
path_contains_parent_reference().  I complained about that at the
time, but I hadn't quite absorbed the fact that an absolute path
is now *guaranteed* not to have any ".." after canonicalize_path.
So the existing calls in adminpack.c and genfile.c are certainly
dead code.  We probably want to keep path_contains_parent_reference()
in case some extension is using it, but seeing that its API spec
already requires the input to be canonicalized, it could be simplified
to just check for ".." at the start.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 23+ messages in thread


end of thread, other threads:[~2022-01-31 17:18 UTC | newest]

Thread overview: 23+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-09-08 10:16 drop tablespace failed when location contains .. on win32 wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
2021-09-08 12:54 ` Andrew Dunstan <andrew@dunslane.net>
2021-09-09 02:19   ` Michael Paquier <michael@paquier.xyz>
2021-09-09 02:35     ` wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
2021-09-09 03:44       ` Michael Paquier <michael@paquier.xyz>
2021-09-09 04:34         ` Kyotaro Horiguchi <horikyota.ntt@gmail.com>
2021-09-09 12:30         ` Andrew Dunstan <andrew@dunslane.net>
2021-09-12 07:33           ` wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
2021-09-13 07:06             ` Michael Paquier <michael@paquier.xyz>
2021-09-13 08:36               ` Kyotaro Horiguchi <horikyota.ntt@gmail.com>
2021-09-26 08:40                 ` wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
2021-11-10 22:43                   ` Tom Lane <tgl@sss.pgh.pa.us>
2021-11-19 07:50                     ` Michael Paquier <michael@paquier.xyz>
2022-01-17 00:40                       ` Julien Rouhaud <rjuju123@gmail.com>
2022-01-18 01:08                         ` wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
2022-01-18 01:19                           ` Michael Paquier <michael@paquier.xyz>
2022-01-24 11:21                             ` wangsh.fnst@fujitsu.com <wangsh.fnst@fujitsu.com>
2022-01-24 11:46                               ` Michael Paquier <michael@paquier.xyz>
2022-01-30 21:50                                 ` Tom Lane <tgl@sss.pgh.pa.us>
2022-01-31 08:15                                   ` Michael Paquier <michael@paquier.xyz>
2022-01-31 12:23                                     ` Tom Lane <tgl@sss.pgh.pa.us>
2022-01-31 16:58                                   ` Andrew Dunstan <andrew@dunslane.net>
2022-01-31 17:18                                     ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox