From 8f9aeed232d70955d7f0dd95a317d6acc26af23a Mon Sep 17 00:00:00 2001 From: "Chao Li (Evan)" Date: Tue, 1 Dec 2026 10:43:23 +0800 Subject: [PATCH v7-REL-18 1/2] Warn about 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. Warn and skip those elements, while continuing to search the remaining directories. Preserve the documented $system default for an entirely empty setting. Use WARNING rather than the ERROR used on master to keep existing configurations working while notifying users that they should fix their setting. Unknown-language hint lookups also warn and skip empty elements, preserving the original CREATE FUNCTION or DO error and allowing hints from the remaining directories. Document the behavior and add TAP coverage for warnings, continued extension lookups, and unknown-language errors and hints. 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 | 2 + src/backend/commands/extension.c | 54 +++++++++------ .../t/001_extension_control_path.pl | 68 +++++++++++++++++++ 3 files changed, 103 insertions(+), 21 deletions(-) diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index 851ff1e274d..142a312230d 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -11225,6 +11225,8 @@ 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 generate a warning and are + ignored. diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c index 94383d6473f..59f0e64ca57 100644 --- a/src/backend/commands/extension.c +++ b/src/backend/commands/extension.c @@ -470,9 +470,11 @@ is_extension_script_filename(const char *filename) /* * Return a list of directories declared on 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) { char sharepath[MAXPGPATH]; char *system_dir; @@ -495,7 +497,6 @@ get_extension_control_directories(void) for (;;) { int len; - char *mangled; char *piece = first_path_var_separator(ecp); /* Get the length of the next path on ecp */ @@ -504,24 +505,34 @@ 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, "$system") == 0) - mangled = substitute_path_macro(piece, "$system", system_dir); + if (len == 0) + ereport(elevel, + errcode(ERRCODE_INVALID_NAME), + errmsg("zero-length component in parameter \"%s\"", + "extension_control_path")); else - mangled = psprintf("%s/extension", piece); + { + char *mangled; + + /* Copy the next path found on ecp */ + piece = palloc(len + 1); + strlcpy(piece, ecp, len + 1); - pfree(piece); + /* + * Substitute the path macro if needed or append "extension" + * suffix if it is a custom extension control path. + */ + if (strcmp(piece, "$system") == 0) + mangled = substitute_path_macro(piece, "$system", system_dir); + else + mangled = psprintf("%s/extension", piece); - /* Canonicalize the path based on the OS and add to the list */ - canonicalize_path(mangled); - paths = lappend(paths, mangled); + pfree(piece); + + /* Canonicalize the path based on the OS and add to the list */ + canonicalize_path(mangled); + paths = lappend(paths, mangled); + } /* Break if ecp is empty or move to the next path on ecp */ if (ecp[len] == '\0') @@ -550,7 +561,7 @@ find_extension_control_filename(ExtensionControlFile *control) basename = psprintf("%s.control", control->name); - paths = get_extension_control_directories(); + paths = get_extension_control_directories(WARNING); result = find_in_paths(basename, paths); if (result) @@ -2472,7 +2483,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(WARNING); foreach_ptr(char, location, locations) { @@ -2570,7 +2581,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(WARNING); foreach_ptr(char, location, locations) { @@ -2756,7 +2767,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(char, 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 7fbe5bde332..27eeb31a7a9 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 @@ -25,6 +25,10 @@ my $ext_name2 = "test_custom_ext_paths_using_directory"; mkpath("$ext_dir/$ext_name2"); create_extension($ext_name2, $ext_dir, $ext_name2); +# Use Unix slashes in SQL path values on Windows. +my $ext_dir_canonicalized = $ext_dir; +$ext_dir_canonicalized =~ s!\\!/!g if $windows_os; + # Use the correct separator and escape \ when running on Windows. my $sep = $windows_os ? ";" : ":"; $node->append_conf( @@ -84,6 +88,70 @@ is($ret, "t", "\$system extension is installed correctly on pg_available_extensions with empty extension_control_path" ); +# Empty elements warn, but the remaining directories are still searched. +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) +{ + my ($code, $stdout, $stderr) = $node->psql( + 'postgres', + "set extension_control_path = '$path'; + select count(*) from pg_available_extensions where name = '$empty_path_ext'"); + is($code, 0, "empty element in path '$path' does not prevent listing extensions"); + is($stdout, $path eq $sep ? '0' : '1', + 'only the remaining directories are searched'); + like($stderr, + qr/WARNING: zero-length component in parameter "extension_control_path"/, + 'warning identifies the empty path element'); + + if ($path ne $sep) + { + ($code, $stdout, $stderr) = $node->psql( + 'postgres', + "set extension_control_path = '$path'; create extension $empty_path_ext"); + is($code, 0, "extension can be loaded using path '$path'"); + like($stderr, + qr/WARNING: zero-length component in parameter "extension_control_path"/, + 'warning identifies the empty path element during creation'); + $node->safe_psql('postgres', "drop extension $empty_path_ext"); + } +} + +# 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)