This is an automated email from the ASF dual-hosted git repository.

amoeba pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-adbc.git


The following commit(s) were added to refs/heads/main by this push:
     new decc3da8aa fix(c/driver_manager): handle long filenames instead of 
throwing (#4995)
decc3da8aa is described below

commit decc3da8aa7bb9617334bb3351e1cedd695e263d
Author: Bryce Mecum <[email protected]>
AuthorDate: Thu Nov 5 16:49:22 2026 -0800

    fix(c/driver_manager): handle long filenames instead of throwing (#4995)
    
    Copilot review on
    https://github.com/apache/arrow-adbc/pull/4939#discussion_r4344728019
    found an edge case in the C++ driver manager when strings that are too
    long to be valid operating-system-specific file paths are used. The
    current implementation uses the variant of `std::filesystem::exists`
    that throws and we weren't catching those.
    
    This PR converts those uses to `bool exists( const
    std::filesystem::path& p, std::error_code& ec ) noexcept;` and either
    ignores the error or handles it an enhances the driver manager error.
    
    This PR was detected with Copilot and I drove Opus 5.5 around. The
    changes were tested in CI, see comments in the PR thread.
---
 c/driver_manager/adbc_driver_manager.cc            |  4 +-
 .../adbc_driver_manager_driver_loading.cc          | 25 +++++-
 c/driver_manager/adbc_driver_manager_profiles.cc   | 18 ++++-
 c/driver_manager/adbc_driver_manager_test.cc       | 89 ++++++++++++++++++++++
 go/adbc/drivermgr/adbc_driver_manager.cc           |  4 +-
 .../adbc_driver_manager_driver_loading.cc          | 25 +++++-
 go/adbc/drivermgr/adbc_driver_manager_profiles.cc  | 18 ++++-
 7 files changed, 167 insertions(+), 16 deletions(-)

diff --git a/c/driver_manager/adbc_driver_manager.cc 
b/c/driver_manager/adbc_driver_manager.cc
index 5accc002b9..90d060beb2 100644
--- a/c/driver_manager/adbc_driver_manager.cc
+++ b/c/driver_manager/adbc_driver_manager.cc
@@ -64,6 +64,7 @@
 #include <filesystem>
 #include <functional>
 #include <string>
+#include <system_error>
 #include <unordered_map>
 #include <utility>
 #include <vector>
@@ -507,7 +508,8 @@ std::optional<ParseDriverUriResult> 
InternalAdbcParseDriverUri(std::string_view
   }
 
 #ifdef _WIN32
-  if (std::filesystem::exists(std::filesystem::path(str))) {
+  std::error_code ec;  // Treat errors like !exists
+  if (std::filesystem::exists(std::filesystem::path(str), ec)) {
     // No scheme, just a path
     return ParseDriverUriResult{str, std::nullopt, std::nullopt};
   }
diff --git a/c/driver_manager/adbc_driver_manager_driver_loading.cc 
b/c/driver_manager/adbc_driver_manager_driver_loading.cc
index 9b2c84c975..b3caee05e5 100644
--- a/c/driver_manager/adbc_driver_manager_driver_loading.cc
+++ b/c/driver_manager/adbc_driver_manager_driver_loading.cc
@@ -38,6 +38,7 @@
 #include <cctype>
 #include <filesystem>
 #include <string>
+#include <system_error>
 #include <utility>
 #include <vector>
 
@@ -294,8 +295,13 @@ SearchPaths GetSearchPaths(const AdbcLoadFlags levels) {
   if (levels & ADBC_LOAD_FLAG_SEARCH_USER) {
     // Check the user configuration directory
     std::filesystem::path user_config_dir = InternalAdbcUserConfigDir() / 
kDriversSubdir;
-    if (!user_config_dir.empty() && std::filesystem::exists(user_config_dir)) {
+    std::error_code ec;
+    if (!user_config_dir.empty() && std::filesystem::exists(user_config_dir, 
ec)) {
       paths.emplace_back(SearchPathSource::kUser, std::move(user_config_dir));
+    } else if (ec) {
+      paths.emplace_back(
+          SearchPathSource::kOtherError,
+          "user config dir: " + user_config_dir.string() + " (" + ec.message() 
+ ")");
     } else {
       paths.emplace_back(SearchPathSource::kDoesNotExist, 
std::move(user_config_dir));
     }
@@ -307,8 +313,13 @@ SearchPaths GetSearchPaths(const AdbcLoadFlags levels) {
     // as we're not on Windows
 #if !defined(_WIN32)
     const std::filesystem::path system_config_dir = 
InternalAdbcSystemConfigDir();
-    if (std::filesystem::exists(system_config_dir)) {
+    std::error_code ec;  // Treat errors like !exists
+    if (std::filesystem::exists(system_config_dir, ec)) {
       paths.emplace_back(SearchPathSource::kSystem, 
std::move(system_config_dir));
+    } else if (ec) {
+      paths.emplace_back(
+          SearchPathSource::kOtherError,
+          "system config dir: " + system_config_dir.string() + " (" + 
ec.message() + ")");
     } else {
       paths.emplace_back(SearchPathSource::kDoesNotExist, 
std::move(system_config_dir));
     }
@@ -376,7 +387,8 @@ AdbcStatusCode ManagedLibrary::GetDriverInfo(
     // if we have an absolute path without an extension, first see if there's a
     // toml file with the same name.
     driver_path.replace_extension(".toml");
-    if (std::filesystem::exists(driver_path)) {
+    std::error_code ec;  // Treat errors like !exists
+    if (std::filesystem::exists(driver_path, ec)) {
       auto status = LoadDriverManifest(driver_path, info, error);
       if (status == ADBC_STATUS_OK) {
         return Load(info.lib_path.native(), {}, error);
@@ -443,7 +455,8 @@ AdbcStatusCode ManagedLibrary::SearchPathsForDriver(
 
     // check for toml first, then dll
     full_path.replace_extension(".toml");
-    if (std::filesystem::exists(full_path)) {
+    std::error_code ec;
+    if (std::filesystem::exists(full_path, ec)) {
       OwnedError intermediate_error;
 
       auto status = LoadDriverManifest(full_path, info, 
&intermediate_error.error);
@@ -487,6 +500,10 @@ AdbcStatusCode ManagedLibrary::SearchPathsForDriver(
       }
 
       extra_debug_info.emplace_back(SearchPathSource::kOtherError, 
std::move(message));
+    } else if (ec) {
+      extra_debug_info.emplace_back(
+          SearchPathSource::kOtherError,
+          "could not access " + full_path.string() + " (" + ec.message() + 
")");
     }
 
     // remove the .toml extension; Load will add the DLL/SO/DYLIB suffix
diff --git a/c/driver_manager/adbc_driver_manager_profiles.cc 
b/c/driver_manager/adbc_driver_manager_profiles.cc
index b5597d0d87..6de9877f59 100644
--- a/c/driver_manager/adbc_driver_manager_profiles.cc
+++ b/c/driver_manager/adbc_driver_manager_profiles.cc
@@ -25,6 +25,7 @@
 #include <filesystem>
 #include <locale>
 #include <string>
+#include <system_error>
 #include <unordered_map>
 #include <utility>
 #include <vector>
@@ -427,8 +428,14 @@ AdbcStatusCode AdbcProfileProviderFilesystem(const char* 
profile_name,
   std::filesystem::path profile_path(profile_name);
   if (profile_path.has_extension()) {
     if (HasExtension(profile_path, ".toml")) {
-      if (!std::filesystem::exists(profile_path)) {
-        SetError(error, "Profile file does not exist: " + 
profile_path.string());
+      std::error_code ec;
+      if (!std::filesystem::exists(profile_path, ec)) {
+        if (ec) {
+          SetError(error, "Could not access profile file " + 
profile_path.string() +
+                              ": " + ec.message());
+        } else {
+          SetError(error, "Profile file does not exist: " + 
profile_path.string());
+        }
         return ADBC_STATUS_NOT_FOUND;
       }
 
@@ -461,7 +468,8 @@ AdbcStatusCode AdbcProfileProviderFilesystem(const char* 
profile_name,
 
     std::filesystem::path full_path = search_path / profile_path;
     full_path.replace_extension(".toml");
-    if (std::filesystem::exists(full_path)) {
+    std::error_code ec;
+    if (std::filesystem::exists(full_path, ec)) {
       OwnedError intermediate_error;
 
       FilesystemProfile profile;
@@ -493,6 +501,10 @@ AdbcStatusCode AdbcProfileProviderFilesystem(const char* 
profile_name,
         message += "could not load the profile";
       }
       extra_debug_info.emplace_back(SearchPathSource::kOtherError, 
std::move(message));
+    } else if (ec) {
+      extra_debug_info.emplace_back(
+          SearchPathSource::kOtherError,
+          "could not access " + full_path.string() + " (" + ec.message() + 
")");
     }
   }
 
diff --git a/c/driver_manager/adbc_driver_manager_test.cc 
b/c/driver_manager/adbc_driver_manager_test.cc
index d81df1ff24..88fc84a99b 100644
--- a/c/driver_manager/adbc_driver_manager_test.cc
+++ b/c/driver_manager/adbc_driver_manager_test.cc
@@ -1440,6 +1440,54 @@ TEST(DriverManagerNotFound, 
RelativePathWithLibraryExtension) {
                               ADBC_LOAD_FLAG_DEFAULT);
 }
 
+TEST(DriverManagerFilesystemError, LongNameInSearchPath) {
+  std::filesystem::path search_dir =
+      std::filesystem::temp_directory_path() / "adbc_filesystem_error_test";
+  std::filesystem::create_directories(search_dir);
+  std::string driver_name(300, 'x');
+
+  struct AdbcDriver driver = {};
+  struct AdbcError error = {};
+  AdbcStatusCode status = ADBC_STATUS_OK;
+  EXPECT_NO_THROW(status =
+                      AdbcFindLoadDriver(driver_name.c_str(), nullptr, 
ADBC_VERSION_1_1_0,
+                                         ADBC_LOAD_FLAG_DEFAULT,
+                                         search_dir.string().c_str(), &driver, 
&error));
+  EXPECT_EQ(status, ADBC_STATUS_NOT_FOUND);
+#if !defined(_WIN32)
+  // MSVC's std::filesystem reports a too-long name as "not found" rather than
+  // an error, so there's no error message to check on Windows.
+  EXPECT_THAT(error.message, ::testing::HasSubstr(
+                                 "could not access " +
+                                 (search_dir / (driver_name + 
".toml")).string() + " ("));
+#endif
+
+  if (error.release) {
+    error.release(&error);
+  }
+  std::filesystem::remove_all(search_dir);
+}
+
+TEST(DriverManagerFilesystemError, LongNameAbsolutePath) {
+  std::filesystem::path search_dir =
+      std::filesystem::temp_directory_path() / "adbc_filesystem_error_test";
+  std::filesystem::create_directories(search_dir);
+  std::string driver_path = (search_dir / std::string(300, 'x')).string();
+
+  struct AdbcDriver driver = {};
+  struct AdbcError error = {};
+  AdbcStatusCode status = ADBC_STATUS_OK;
+  EXPECT_NO_THROW(status = AdbcFindLoadDriver(driver_path.c_str(), nullptr,
+                                              ADBC_VERSION_1_1_0, 
ADBC_LOAD_FLAG_DEFAULT,
+                                              nullptr, &driver, &error));
+  EXPECT_EQ(status, ADBC_STATUS_NOT_FOUND);
+
+  if (error.release) {
+    error.release(&error);
+  }
+  std::filesystem::remove_all(search_dir);
+}
+
 TEST_F(DriverManifest, ImplicitUri) {
   auto filepath = temp_dir / "postgresql.toml";
   std::ofstream test_manifest_file(filepath);
@@ -2137,6 +2185,47 @@ TEST_F(ConnectionProfiles, ProfileNotFound) {
   UnsetProfilePath();
 }
 
+TEST_F(ConnectionProfiles, LongProfileFileName) {
+  std::string profile_path = (temp_dir / (std::string(300, 'x') + 
".toml")).string();
+
+  adbc_validation::Handle<struct AdbcDatabase> database;
+  ASSERT_THAT(AdbcDatabaseNew(&database.value, &error), IsOkStatus(&error));
+  ASSERT_THAT(
+      AdbcDatabaseSetOption(&database.value, "profile", profile_path.c_str(), 
&error),
+      IsOkStatus(&error));
+  AdbcStatusCode status = ADBC_STATUS_OK;
+  EXPECT_NO_THROW(status = AdbcDatabaseInit(&database.value, &error));
+  EXPECT_EQ(status, ADBC_STATUS_NOT_FOUND);
+#if !defined(_WIN32)
+  // MSVC's std::filesystem reports a too-long name as "not found" rather than
+  // an error, so there's no error message to check on Windows.
+  EXPECT_THAT(error.message, ::testing::HasSubstr("Could not access profile 
file " +
+                                                  profile_path + ": "));
+#endif
+}
+
+TEST_F(ConnectionProfiles, LongProfileNameInSearchPath) {
+  SetProfilePath(temp_dir.string().c_str());
+  std::string profile_name(300, 'x');
+
+  adbc_validation::Handle<struct AdbcDatabase> database;
+  ASSERT_THAT(AdbcDatabaseNew(&database.value, &error), IsOkStatus(&error));
+  ASSERT_THAT(
+      AdbcDatabaseSetOption(&database.value, "profile", profile_name.c_str(), 
&error),
+      IsOkStatus(&error));
+  AdbcStatusCode status = ADBC_STATUS_OK;
+  EXPECT_NO_THROW(status = AdbcDatabaseInit(&database.value, &error));
+  EXPECT_EQ(status, ADBC_STATUS_NOT_FOUND);
+#if !defined(_WIN32)
+  // MSVC's std::filesystem reports a too-long name as "not found" rather than
+  // an error, so there's no error message to check on Windows.
+  EXPECT_THAT(error.message, ::testing::HasSubstr(
+                                 "could not access " +
+                                 (temp_dir / (profile_name + 
".toml")).string() + " ("));
+#endif
+  UnsetProfilePath();
+}
+
 TEST_F(ConnectionProfiles, CondaProfileTest) {
 #if ADBC_CONDA_BUILD
   constexpr bool is_conda_build = true;
diff --git a/go/adbc/drivermgr/adbc_driver_manager.cc 
b/go/adbc/drivermgr/adbc_driver_manager.cc
index 5accc002b9..90d060beb2 100644
--- a/go/adbc/drivermgr/adbc_driver_manager.cc
+++ b/go/adbc/drivermgr/adbc_driver_manager.cc
@@ -64,6 +64,7 @@
 #include <filesystem>
 #include <functional>
 #include <string>
+#include <system_error>
 #include <unordered_map>
 #include <utility>
 #include <vector>
@@ -507,7 +508,8 @@ std::optional<ParseDriverUriResult> 
InternalAdbcParseDriverUri(std::string_view
   }
 
 #ifdef _WIN32
-  if (std::filesystem::exists(std::filesystem::path(str))) {
+  std::error_code ec;  // Treat errors like !exists
+  if (std::filesystem::exists(std::filesystem::path(str), ec)) {
     // No scheme, just a path
     return ParseDriverUriResult{str, std::nullopt, std::nullopt};
   }
diff --git a/go/adbc/drivermgr/adbc_driver_manager_driver_loading.cc 
b/go/adbc/drivermgr/adbc_driver_manager_driver_loading.cc
index 9b2c84c975..b3caee05e5 100644
--- a/go/adbc/drivermgr/adbc_driver_manager_driver_loading.cc
+++ b/go/adbc/drivermgr/adbc_driver_manager_driver_loading.cc
@@ -38,6 +38,7 @@
 #include <cctype>
 #include <filesystem>
 #include <string>
+#include <system_error>
 #include <utility>
 #include <vector>
 
@@ -294,8 +295,13 @@ SearchPaths GetSearchPaths(const AdbcLoadFlags levels) {
   if (levels & ADBC_LOAD_FLAG_SEARCH_USER) {
     // Check the user configuration directory
     std::filesystem::path user_config_dir = InternalAdbcUserConfigDir() / 
kDriversSubdir;
-    if (!user_config_dir.empty() && std::filesystem::exists(user_config_dir)) {
+    std::error_code ec;
+    if (!user_config_dir.empty() && std::filesystem::exists(user_config_dir, 
ec)) {
       paths.emplace_back(SearchPathSource::kUser, std::move(user_config_dir));
+    } else if (ec) {
+      paths.emplace_back(
+          SearchPathSource::kOtherError,
+          "user config dir: " + user_config_dir.string() + " (" + ec.message() 
+ ")");
     } else {
       paths.emplace_back(SearchPathSource::kDoesNotExist, 
std::move(user_config_dir));
     }
@@ -307,8 +313,13 @@ SearchPaths GetSearchPaths(const AdbcLoadFlags levels) {
     // as we're not on Windows
 #if !defined(_WIN32)
     const std::filesystem::path system_config_dir = 
InternalAdbcSystemConfigDir();
-    if (std::filesystem::exists(system_config_dir)) {
+    std::error_code ec;  // Treat errors like !exists
+    if (std::filesystem::exists(system_config_dir, ec)) {
       paths.emplace_back(SearchPathSource::kSystem, 
std::move(system_config_dir));
+    } else if (ec) {
+      paths.emplace_back(
+          SearchPathSource::kOtherError,
+          "system config dir: " + system_config_dir.string() + " (" + 
ec.message() + ")");
     } else {
       paths.emplace_back(SearchPathSource::kDoesNotExist, 
std::move(system_config_dir));
     }
@@ -376,7 +387,8 @@ AdbcStatusCode ManagedLibrary::GetDriverInfo(
     // if we have an absolute path without an extension, first see if there's a
     // toml file with the same name.
     driver_path.replace_extension(".toml");
-    if (std::filesystem::exists(driver_path)) {
+    std::error_code ec;  // Treat errors like !exists
+    if (std::filesystem::exists(driver_path, ec)) {
       auto status = LoadDriverManifest(driver_path, info, error);
       if (status == ADBC_STATUS_OK) {
         return Load(info.lib_path.native(), {}, error);
@@ -443,7 +455,8 @@ AdbcStatusCode ManagedLibrary::SearchPathsForDriver(
 
     // check for toml first, then dll
     full_path.replace_extension(".toml");
-    if (std::filesystem::exists(full_path)) {
+    std::error_code ec;
+    if (std::filesystem::exists(full_path, ec)) {
       OwnedError intermediate_error;
 
       auto status = LoadDriverManifest(full_path, info, 
&intermediate_error.error);
@@ -487,6 +500,10 @@ AdbcStatusCode ManagedLibrary::SearchPathsForDriver(
       }
 
       extra_debug_info.emplace_back(SearchPathSource::kOtherError, 
std::move(message));
+    } else if (ec) {
+      extra_debug_info.emplace_back(
+          SearchPathSource::kOtherError,
+          "could not access " + full_path.string() + " (" + ec.message() + 
")");
     }
 
     // remove the .toml extension; Load will add the DLL/SO/DYLIB suffix
diff --git a/go/adbc/drivermgr/adbc_driver_manager_profiles.cc 
b/go/adbc/drivermgr/adbc_driver_manager_profiles.cc
index b5597d0d87..6de9877f59 100644
--- a/go/adbc/drivermgr/adbc_driver_manager_profiles.cc
+++ b/go/adbc/drivermgr/adbc_driver_manager_profiles.cc
@@ -25,6 +25,7 @@
 #include <filesystem>
 #include <locale>
 #include <string>
+#include <system_error>
 #include <unordered_map>
 #include <utility>
 #include <vector>
@@ -427,8 +428,14 @@ AdbcStatusCode AdbcProfileProviderFilesystem(const char* 
profile_name,
   std::filesystem::path profile_path(profile_name);
   if (profile_path.has_extension()) {
     if (HasExtension(profile_path, ".toml")) {
-      if (!std::filesystem::exists(profile_path)) {
-        SetError(error, "Profile file does not exist: " + 
profile_path.string());
+      std::error_code ec;
+      if (!std::filesystem::exists(profile_path, ec)) {
+        if (ec) {
+          SetError(error, "Could not access profile file " + 
profile_path.string() +
+                              ": " + ec.message());
+        } else {
+          SetError(error, "Profile file does not exist: " + 
profile_path.string());
+        }
         return ADBC_STATUS_NOT_FOUND;
       }
 
@@ -461,7 +468,8 @@ AdbcStatusCode AdbcProfileProviderFilesystem(const char* 
profile_name,
 
     std::filesystem::path full_path = search_path / profile_path;
     full_path.replace_extension(".toml");
-    if (std::filesystem::exists(full_path)) {
+    std::error_code ec;
+    if (std::filesystem::exists(full_path, ec)) {
       OwnedError intermediate_error;
 
       FilesystemProfile profile;
@@ -493,6 +501,10 @@ AdbcStatusCode AdbcProfileProviderFilesystem(const char* 
profile_name,
         message += "could not load the profile";
       }
       extra_debug_info.emplace_back(SearchPathSource::kOtherError, 
std::move(message));
+    } else if (ec) {
+      extra_debug_info.emplace_back(
+          SearchPathSource::kOtherError,
+          "could not access " + full_path.string() + " (" + ec.message() + 
")");
     }
   }
 

Reply via email to