]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
fix: Exit more gracefully on invalid sharded remote storage URL
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 29 Aug 2023 19:06:23 +0000 (21:06 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 29 Aug 2023 19:36:16 +0000 (21:36 +0200)
As mentioned in #1321.

src/storage/Storage.cpp

index 189e21e27d41e98d19ea3bde3577402c396e9c45..65ce3a70ffbd0e6050b72c1343f57d87299de6af 100644 (file)
@@ -113,6 +113,20 @@ to_string(const RemoteStorageConfig& entry)
   return result;
 }
 
+static Url
+url_from_string(const std::string& url_string)
+{
+  // The Url class is parsing the URL object lazily. Check if the URL is valid
+  // now to avoid exceptions later.
+  try {
+    Url url(url_string);
+    std::ignore = url.str();
+    return url;
+  } catch (const std::exception& e) {
+    throw core::Error(FMT("Cannot parse URL {}: {}", url_string, e.what()));
+  }
+}
+
 static RemoteStorageConfig
 parse_storage_config(const std::string_view entry)
 {
@@ -126,15 +140,7 @@ parse_storage_config(const std::string_view entry)
 
   RemoteStorageConfig result;
   const auto url_str = std::string(parts[0]);
-  result.params.url = url_str;
-
-  // The Url class is parsing the URL object lazily. Check if the URL is valid
-  // now to avoid exceptions later.
-  try {
-    std::ignore = result.params.url.str();
-  } catch (const std::exception& e) {
-    throw core::Error(FMT("Cannot parse URL {}: {}", url_str, e.what()));
-  }
+  result.params.url = url_from_string(url_str);
 
   if (result.params.url.scheme().empty()) {
     throw core::Error(FMT("URL scheme must not be empty: {}", entry));
@@ -365,7 +371,7 @@ get_shard_url(const Hash::Digest& key,
     }
   }
 
-  return util::replace_first(url, "*", best_shard);
+  return url_from_string(util::replace_first(url, "*", best_shard));
 }
 
 RemoteStorageBackendEntry*