Skip to content

Commit 3f35526

Browse files
committed
Don't try to trim "../" in join_path_components().
join_path_components() tried to remove leading ".." components from its tail argument, but it was not nearly bright enough to do so correctly unless 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 no correctness reason to remove "..", and cosmetic concerns can be taken care of by a subsequent canonicalize_path() call. Per bug #6715 from Greg Davidson. Back-patch to all supported branches. It appears that pre-9.2, this function is only used with absolute paths as head arguments, which is why we'd not noticed the breakage before. However, third-party code might be expecting this function to work in more general cases, so it seems wise to back-patch. In HEAD and 9.2, also make some minor cosmetic improvements to callers.
1 parent b4234f8 commit 3f35526

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
@@ -159,6 +159,8 @@ make_native_path(char *filename)
159159
/*
160160
* join_path_components - join two path components, inserting a slash
161161
*
162+
* We omit the slash if either given component is empty.
163+
*
162164
* ret_path is the output area (must be of size MAXPGPATH)
163165
*
164166
* ret_path can be the same as head, but not the same as tail.
@@ -171,37 +173,22 @@ join_path_components(char *ret_path,
171173
strlcpy(ret_path, head, MAXPGPATH);
172174

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

207194

@@ -693,6 +680,15 @@ get_home_path(char *ret_path)
693680
*
694681
* Modify the given string in-place to name the parent directory of the
695682
* named file.
683+
*
684+
* If the input is just a file name with no directory part, the result is
685+
* an empty string, not ".". This is appropriate when the next step is
686+
* join_path_components(), but might need special handling otherwise.
687+
*
688+
* Caution: this will not produce desirable results if the string ends
689+
* with "..". For most callers this is not a problem since the string
690+
* is already known to name a regular file. If in doubt, apply
691+
* canonicalize_path() first.
696692
*/
697693
void
698694
get_parent_directory(char *path)

0 commit comments

Comments
 (0)