Use AbsoluteConfigLocation() when building an included path in hba.c
authorMichael Paquier <michael@paquier.xyz>
Tue, 8 Nov 2022 23:47:02 +0000 (08:47 +0900)
committerMichael Paquier <michael@paquier.xyz>
Tue, 8 Nov 2022 23:47:02 +0000 (08:47 +0900)
The code building an absolute path to a file included, as prefixed by
'@' in authentication files, for user and database lists uses the same
logic as for GUCs, except that it has no need to know about DataDir as
there is always a calling file to rely to build the base directory path.
The refactoring done in a1a7bb8 makes this move straight-forward, and
unifies the code used for GUCs and authentication files, and the
intention is to rely also on that for the upcoming patch to be able to
include full files from HBA or ident files.

Note that this gets rid of an inconsistency introduced in 370f909, that
copied the logic coming from GUCs but applied it for files included in
authentication files, where the result buffer given to
join_path_components() must have a size of MAXPGPATH.  Based on a
double-check of the existing code, all the other callers of
join_path_components() already do that, except the code path changed
here.

Discussion: https://postgr.es/m/Y2igk7q8OMpg+Yta@paquier.xyz

src/backend/libpq/hba.c
src/backend/utils/misc/conffiles.c

index e9fc0af7c9ed88c57558ec769cf5badb92aca894..a9f87ab5bf705b5a77f0a2cc739fdc77219f00eb 100644 (file)
@@ -41,6 +41,7 @@
 #include "storage/fd.h"
 #include "utils/acl.h"
 #include "utils/builtins.h"
+#include "utils/conffiles.h"
 #include "utils/guc.h"
 #include "utils/lsyscache.h"
 #include "utils/memutils.h"
@@ -466,21 +467,7 @@ tokenize_inc_file(List *tokens,
    ListCell   *inc_line;
    MemoryContext linecxt;
 
-   if (is_absolute_path(inc_filename))
-   {
-       /* absolute path is taken as-is */
-       inc_fullname = pstrdup(inc_filename);
-   }
-   else
-   {
-       /* relative path is relative to dir of calling file */
-       inc_fullname = (char *) palloc(strlen(outer_filename) + 1 +
-                                      strlen(inc_filename) + 1);
-       strcpy(inc_fullname, outer_filename);
-       get_parent_directory(inc_fullname);
-       join_path_components(inc_fullname, inc_fullname, inc_filename);
-       canonicalize_path(inc_fullname);
-   }
+   inc_fullname = AbsoluteConfigLocation(inc_filename, outer_filename);
 
    inc_file = AllocateFile(inc_fullname, "r");
    if (inc_file == NULL)
index 4a99a1961e30d7491c9a1bd9123f6afc7194c28a..027d481df871d8b4a60d6419699556d2adef7098 100644 (file)
 char *
 AbsoluteConfigLocation(const char *location, const char *calling_file)
 {
-   char        abs_path[MAXPGPATH];
-
    if (is_absolute_path(location))
        return pstrdup(location);
    else
    {
+       char        abs_path[MAXPGPATH];
+
        if (calling_file != NULL)
        {
            strlcpy(abs_path, calling_file, sizeof(abs_path));