From 328e13473e27b2091aefbb61f3d6b3ed3794ef06 Mon Sep 17 00:00:00 2001 From: "Chao Li (Evan)" Date: Tue, 3 Nov 2026 09:55:18 +0800 Subject: [PATCH v7 1/2] Reject empty elements in extension_control_path Empty path elements were expanded to /extension by the automatic suffix addition, causing an unintended search of that directory. Report an error instead when locating or listing extensions. When checking for an extension to provide an unknown-language hint, issue a warning and skip empty elements. This preserves the original CREATE FUNCTION or DO error while allowing a hint to be found using the remaining directories. Preserve the documented $system default for an entirely empty setting. Document the restriction and add TAP coverage for empty elements and unknown-language errors and hints. Backpatch with WARNING instead of ERROR for ordinary extension lookups, skipping empty elements so existing configurations continue to work. Author: Chao Li Reviewed-by: Prashant Chinnam Reviewed-by: Surya Poondla Reviewed-by: Matheus Alcantara Reviewed-by: Andres Freund Reviewed-by: Peter Eisentraut Discussion: https://postgr.es/m/DC2D9FA5-45C4-494F-99FC-F9B95AD4FF55@gmail.com --- doc/src/sgml/config.sgml | 1 + src/backend/commands/extension.c | 68 +++++++++++-------- .../t/001_extension_control_path.pl | 54 +++++++++++++++ 3 files changed, 95 insertions(+), 28 deletions(-) diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index 77cc523ce50..48a8953b16b 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -11806,6 +11806,7 @@ extension_control_path = 'C:\tools\postgresql;H:\my_project\share;$system' The default value for this parameter is '$system'. If the value is set to an empty string, the default '$system' is also assumed. + Empty elements within a nonempty value are not allowed. diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c index 35443dabecf..3cff5155889 100644 --- a/src/backend/commands/extension.c +++ b/src/backend/commands/extension.c @@ -511,9 +511,11 @@ is_extension_script_filename(const char *filename) /* * Return a list of directories declared in the extension_control_path GUC. + * Report empty elements at elevel. If elevel is less than ERROR, skip them + * and continue searching the remaining directories. */ static List * -get_extension_control_directories(void) +get_extension_control_directories(int elevel) { #define EXTENSION_SYSTEM_MACRO "$system" char sharepath[MAXPGPATH]; @@ -541,9 +543,7 @@ get_extension_control_directories(void) for (;;) { int len; - char *mangled; char *piece = first_path_var_separator(ecp); - ExtensionLocation *location = palloc_object(ExtensionLocation); /* Get the length of the next path on ecp */ if (piece == NULL) @@ -551,30 +551,41 @@ get_extension_control_directories(void) else len = piece - ecp; - /* Copy the next path found on ecp */ - piece = palloc(len + 1); - strlcpy(piece, ecp, len + 1); - - /* - * Substitute the path macro if needed or append "extension" - * suffix if it is a custom extension control path. - */ - if (strcmp(piece, EXTENSION_SYSTEM_MACRO) == 0) - { - location->macro = pstrdup(piece); - mangled = substitute_path_macro(piece, EXTENSION_SYSTEM_MACRO, system_dir); - } + if (len == 0) + ereport(elevel, + errcode(ERRCODE_INVALID_NAME), + errmsg("zero-length component in parameter \"%s\"", + "extension_control_path")); else { - location->macro = NULL; - mangled = psprintf("%s/extension", piece); - } - pfree(piece); + char *mangled; + ExtensionLocation *location = palloc_object(ExtensionLocation); + + /* Copy the next path found on ecp */ + piece = palloc(len + 1); + strlcpy(piece, ecp, len + 1); + + /* + * Substitute the path macro if needed or append "extension" + * suffix if it is a custom extension control path. + */ + if (strcmp(piece, EXTENSION_SYSTEM_MACRO) == 0) + { + location->macro = pstrdup(piece); + mangled = substitute_path_macro(piece, EXTENSION_SYSTEM_MACRO, system_dir); + } + else + { + location->macro = NULL; + mangled = psprintf("%s/extension", piece); + } + pfree(piece); - /* Canonicalize the path based on the OS and add to the list */ - canonicalize_path(mangled); - location->loc = mangled; - paths = lappend(paths, location); + /* Canonicalize the path based on the OS and add to the list */ + canonicalize_path(mangled); + location->loc = mangled; + paths = lappend(paths, location); + } /* Break if ecp is empty or move to the next path on ecp */ if (ecp[len] == '\0') @@ -604,7 +615,7 @@ find_extension_control_filename(ExtensionControlFile *control) basename = psprintf("%s.control", control->name); - paths = get_extension_control_directories(); + paths = get_extension_control_directories(ERROR); result = find_in_paths(basename, paths); if (result) @@ -2527,7 +2538,7 @@ pg_available_extensions(PG_FUNCTION_ARGS) /* Build tuplestore to hold the result rows */ InitMaterializedSRF(fcinfo, 0); - locations = get_extension_control_directories(); + locations = get_extension_control_directories(ERROR); foreach_ptr(ExtensionLocation, location, locations) { @@ -2629,7 +2640,7 @@ pg_available_extension_versions(PG_FUNCTION_ARGS) /* Build tuplestore to hold the result rows */ InitMaterializedSRF(fcinfo, 0); - locations = get_extension_control_directories(); + locations = get_extension_control_directories(ERROR); foreach_ptr(ExtensionLocation, location, locations) { @@ -2821,7 +2832,8 @@ extension_file_exists(const char *extensionName) DIR *dir; struct dirent *de; - locations = get_extension_control_directories(); + /* Empty path elements must not replace the original error. */ + locations = get_extension_control_directories(WARNING); foreach_ptr(ExtensionLocation, location, locations) { diff --git a/src/test/modules/test_extensions/t/001_extension_control_path.pl b/src/test/modules/test_extensions/t/001_extension_control_path.pl index 4a013a7da4b..f42d76b7ecf 100644 --- a/src/test/modules/test_extensions/t/001_extension_control_path.pl +++ b/src/test/modules/test_extensions/t/001_extension_control_path.pl @@ -115,6 +115,60 @@ is($ret, "\$system", "\$system location is shown correctly in pg_available_extensions with empty extension_control_path" ); +# Empty path elements are rejected, rather than expanded to /extension. +my $empty_path_ext = 'test_empty_path'; +create_extension($empty_path_ext, $ext_dir); +for my $path ( + "$sep$ext_dir_canonicalized", + "$ext_dir_canonicalized$sep", + $sep) +{ + for my $sql ( + 'select count(*) from pg_available_extensions', + "create extension $empty_path_ext") + { + my ($code, $stdout, $stderr) = $node->psql( + 'postgres', + "set extension_control_path = '$path'; $sql"); + is($code, 3, "empty element in path '$path' is rejected by $sql"); + like($stderr, + qr/ERROR: zero-length component in parameter "extension_control_path"/, + 'error identifies the empty path element'); + } +} + +# Warn about empty elements while preserving the unknown-language error and +# allowing a hint to be found using the remaining directories. +for my $sql ( + "create function test_invalid_language() returns int language $ext_name as 'x'", + "do language $ext_name 'x'") +{ + my ($code, $stdout, $stderr) = $node->psql( + 'postgres', + "set extension_control_path = '$sep$ext_dir_canonicalized$sep'; $sql"); + is($code, 3, "unknown language is rejected by $sql"); + like($stderr, + qr/WARNING: zero-length component in parameter "extension_control_path"/, + 'warning identifies the empty path element during hint lookup'); + like($stderr, qr/ERROR: language "\Q$ext_name\E" does not exist/, + "invalid path does not replace the unknown-language error"); + like($stderr, + qr/HINT: Use CREATE EXTENSION to load the language into the database\./, + "remaining directory provides the extension hint"); + + # With no remaining directories, preserve the error without adding a hint. + ($code, $stdout, $stderr) = $node->psql('postgres', + "set extension_control_path = '$sep'; $sql"); + is($code, 3, "unknown language is rejected with no search directories by $sql"); + like($stderr, + qr/WARNING: zero-length component in parameter "extension_control_path"/, + 'warning identifies the empty elements with no search directories'); + like($stderr, qr/ERROR: language "\Q$ext_name\E" does not exist/, + 'empty path list does not replace the unknown-language error'); + unlike($stderr, qr/HINT:/, + 'no extension hint is provided without search directories'); +} + # Test with an extension that does not exists my ($code, $stdout, $stderr) = $node->psql('postgres', "CREATE EXTENSION invalid"); -- 2.50.1 (Apple Git-155)