diff --git a/src/lib/nfs/Base.cxx b/src/lib/nfs/Base.cxx index 18728ccc8..3faf4c90a 100644 --- a/src/lib/nfs/Base.cxx +++ b/src/lib/nfs/Base.cxx @@ -3,6 +3,7 @@ #include "Base.hxx" +#include // for std::copy() #include #include @@ -13,21 +14,17 @@ static std::array nfs_base_export_name; static size_t nfs_base_export_name_length; void -nfs_set_base(const char *server, const char *export_name) noexcept +nfs_set_base(std::string_view server, std::string_view export_name) noexcept { - assert(server != nullptr); - assert(export_name != nullptr); - - const size_t server_length = strlen(server); - const size_t export_name_length = strlen(export_name); - - if (server_length >= nfs_base_server.size() || - export_name_length > nfs_base_export_name.size()) + if (server.size() >= nfs_base_server.size() || + export_name.size() > nfs_base_export_name.size()) return; - memcpy(nfs_base_server.data(), server, server_length + 1); - memcpy(nfs_base_export_name.data(), export_name, export_name_length); - nfs_base_export_name_length = export_name_length; + *std::copy(server.begin(), server.end(), + nfs_base_server.begin()) = '\0'; + std::copy(export_name.begin(), export_name.end(), + nfs_base_export_name.begin()); + nfs_base_export_name_length = export_name.size(); } const char * diff --git a/src/lib/nfs/Base.hxx b/src/lib/nfs/Base.hxx index 4e277cd12..736678327 100644 --- a/src/lib/nfs/Base.hxx +++ b/src/lib/nfs/Base.hxx @@ -1,8 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-or-later // Copyright The Music Player Daemon Project -#ifndef MPD_NFS_BASE_HXX -#define MPD_NFS_BASE_HXX +#include /** * Set the "base" NFS server and export name. This will be the @@ -12,7 +11,7 @@ * This is a kludge that is not truly thread-safe. */ void -nfs_set_base(const char *server, const char *export_name) noexcept; +nfs_set_base(std::string_view server, std::string_view export_name) noexcept; /** * Check if the given server and path are inside the "base" @@ -23,5 +22,3 @@ nfs_set_base(const char *server, const char *export_name) noexcept; [[gnu::pure]] const char * nfs_check_base(const char *server, const char *path) noexcept; - -#endif diff --git a/src/storage/plugins/NfsStorage.cxx b/src/storage/plugins/NfsStorage.cxx index 882cdb2c2..968f39835 100644 --- a/src/storage/plugins/NfsStorage.cxx +++ b/src/storage/plugins/NfsStorage.cxx @@ -403,7 +403,7 @@ CreateNfsStorageURI(EventLoop &event_loop, const char *base) const std::string server(p, mount); - nfs_set_base(server.c_str(), mount); + nfs_set_base(server, mount); return std::make_unique(event_loop, base, server.c_str(), mount);