Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit490cb05

Browse files
committed
Don't try to trim "../" in join_path_components().
join_path_components() tried to remove leading ".." components from itstail argument, but it was not nearly bright enough to do so correctlyunless the head argument was (a) absolute and (b) canonicalized.Rather than try to fix that logic, let's just get rid of it: there is nocorrectness reason to remove "..", and cosmetic concerns can be takencare of by a subsequent canonicalize_path() call. Per bug #6715 fromGreg Davidson.Back-patch to all supported branches. It appears that pre-9.2, thisfunction is only used with absolute paths as head arguments, which is whywe'd not noticed the breakage before. However, third-party code might beexpecting this function to work in more general cases, so it seems wiseto back-patch.In HEAD and 9.2, also make some minor cosmetic improvements to callers.
1 parent13797cb commit490cb05

File tree

1 file changed

+24
-28
lines changed

1 file changed

+24
-28
lines changed

‎src/port/path.c

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ make_native_path(char *filename)
165165
/*
166166
* join_path_components - join two path components, inserting a slash
167167
*
168+
* We omit the slash if either given component is empty.
169+
*
168170
* ret_path is the output area (must be of size MAXPGPATH)
169171
*
170172
* ret_path can be the same as head, but not the same as tail.
@@ -177,37 +179,22 @@ join_path_components(char *ret_path,
177179
strlcpy(ret_path,head,MAXPGPATH);
178180

179181
/*
180-
* Remove any leading "." and ".." in the tail component, adjusting head
181-
* as needed.
182+
* Remove any leading "." in the tail component.
183+
*
184+
* Note: we used to try to remove ".." as well, but that's tricky to get
185+
* right; now we just leave it to be done by canonicalize_path() later.
182186
*/
183-
for (;;)
184-
{
185-
if (tail[0]=='.'&&IS_DIR_SEP(tail[1]))
186-
{
187-
tail+=2;
188-
}
189-
elseif (tail[0]=='.'&&tail[1]=='\0')
190-
{
191-
tail+=1;
192-
break;
193-
}
194-
elseif (tail[0]=='.'&&tail[1]=='.'&&IS_DIR_SEP(tail[2]))
195-
{
196-
trim_directory(ret_path);
197-
tail+=3;
198-
}
199-
elseif (tail[0]=='.'&&tail[1]=='.'&&tail[2]=='\0')
200-
{
201-
trim_directory(ret_path);
202-
tail+=2;
203-
break;
204-
}
205-
else
206-
break;
207-
}
187+
while (tail[0]=='.'&&IS_DIR_SEP(tail[1]))
188+
tail+=2;
189+
208190
if (*tail)
191+
{
192+
/* only separate with slash if head wasn't empty */
209193
snprintf(ret_path+strlen(ret_path),MAXPGPATH-strlen(ret_path),
210-
"/%s",tail);
194+
"%s%s",
195+
(*(skip_drive(head))!='\0') ?"/" :"",
196+
tail);
197+
}
211198
}
212199

213200

@@ -665,6 +652,15 @@ get_home_path(char *ret_path)
665652
*
666653
* Modify the given string in-place to name the parent directory of the
667654
* named file.
655+
*
656+
* If the input is just a file name with no directory part, the result is
657+
* an empty string, not ".". This is appropriate when the next step is
658+
* join_path_components(), but might need special handling otherwise.
659+
*
660+
* Caution: this will not produce desirable results if the string ends
661+
* with "..". For most callers this is not a problem since the string
662+
* is already known to name a regular file. If in doubt, apply
663+
* canonicalize_path() first.
668664
*/
669665
void
670666
get_parent_directory(char*path)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp