8000 Don't try to trim "../" in join_path_components(). · jcsston/postgres@f882084 · GitHub
[go: up one dir, main page]

Skip to content

Commit f882084

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 7c58a5f commit f882084

File tree

3 files changed

+28
-33
lines changed

3 files changed

+28
-33
lines changed

src/bin/initdb/initdb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3314,7 +3314,7 @@ main(int argc, char *argv[])
33143314
fprintf(stderr, "%s", authwarning);
33153315

33163316
/* Get directory specification used to start this executable */
3317-
strcpy(bin_dir, argv[0]);
3317+
strlcpy(bin_dir, argv[0], sizeof(bin_dir));
33183318
get_parent_directory(bin_dir);
33193319

33203320
printf(_("\nSuccess. You can now start the database server using:\n\n"

src/bin/psql/command.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,10 +2051,10 @@ process_file(char *filename, bool single_txn, bool use_relative_path)
20512051
* relative pathname, then prepend all but the last pathname component
20522052
* of the current script to this pathname.
20532053
*/
2054-
if (use_relative_path && pset.inputfile && !is_absolute_path(filename)
2055-
&& !has_drive_prefix(filename))
2054+
if (use_relative_path && pset.inputfile &&
2055+
!is_absolute_path(filename) && !has_drive_prefix(filename))
20562056
{
2057-
snprintf(relpath, MAXPGPATH, "%s", pset.inputfile);
2057+
strlcpy(relpath, pset.inputfile, sizeof(relpath));
20582058
get_parent_directory(relpath);
20592059
join_path_components(relpath, relpath, filename);
20602060
canonicalize_path(relpath);

src/port/path.c

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ make_native_path(char *filename)
170170
/*
171171
* join_path_components - join two path components, inserting a slash
172172
*
173+
* We omit the slash if either given component is empty.
174+
*
173175
* ret_path is the output area (must be of size MAXPGPATH)
174176
*
175177
* ret_path can be the same as head, but not the same as tail.
@@ -182,38 +184,22 @@ join_path_components(char *ret_path,
182184
strlcpy(ret_path, head, MAXPGPATH);
183185

184186
/*
185-
* Remove any leading "." and ".." in the tail component, adjusting head
186-
* as needed.
187+
* Remove any leading "." in the tail component.
188+
*
189+
* Note: we used to try to remove ".." as well, but that's tricky to get
190+
* right; now we just leave it to be done by canonicalize_path() later.
187191
*/
188-
for (;;)
189-
{
190-
if (tail[0] == '.' && IS_DIR_SEP(tail[1]))
191-
{
192-
tail += 2;
193-
}
194-
else if (tail[0] == '.' && tail[1] == '\0')
195-
{
196-
tail += 1;
197-
break;
198-
}
199-
else if (tail[0] == '.' && tail[1] == '.' && IS_DIR_SEP(tail[2]))
200-
{
201-
trim_directory(ret_path);
202-
tail += 3;
203-
}
204-
else if (tail[0] == '.' && tail[1] == '.' && tail[2] == '\0')
205-
{
206-
trim_directory(ret_path);
207-
tail += 2;
208-
break;
209-
}
210-
else
211-
break;
212-
}
192+
while (tail[0] == '.' && IS_DIR_SEP(tail[1]))
193+
tail += 2;
194+
213195
if (*tail)
196+
{
197+
/* only separate with slash if head wasn't empty */
214198
snprintf(ret_path + strlen(ret_path), MAXPGPATH - strlen(ret_path),
215-
/* only add slash if there is something already in head */
216-
"%s%s", head[0] ? "/" : "", tail);
199+
"%s%s",
200+
(*(skip_drive(head)) != '\0') ? "/" : "",
201+
tail);
202+
}
217203
}
218204

219205

@@ -705,6 +691,15 @@ get_home_path(char *ret_path)
705691
*
706692
* Modify the given string in-place to name the parent directory of the
707693
* named file.
694+
*
695+
* If the input is just a file name with no directory part, the result is
696+
* an empty string, not ".". This is appropriate when the next step is
697+
* join_path_components(), but might need special handling otherwise.
698+
*
699+
* Caution: this will not produce desirable results if the string ends
700+
* with "..". For most callers this is not a problem since the string
701+
* is already known to name a regular file. If in doubt, apply
702+
* canonicalize_path() first.
708703
*/
709704
void
710705
get_parent_directory(char *path)

0 commit comments

Comments
 (0)
0