Made length checks more robusts + bumped sizes, fixes #496

This commit is contained in:
emeric
2024-07-28 11:24:18 +02:00
parent 9bca05fb51
commit b3807f6a53
12 changed files with 151 additions and 30 deletions
+15 -5
View File
@@ -183,10 +183,10 @@ namespace lms::db
} // namespace
Artist::Artist(const std::string& name, const std::optional<core::UUID>& MBID)
: _name{ std::string(name, 0, _maxNameLength) }
, _sortName{ _name }
, _MBID{ MBID ? MBID->getAsString() : "" }
: _MBID{ MBID ? MBID->getAsString() : "" }
{
setName(name);
_sortName = _name;
}
Artist::pointer Artist::create(Session& session, const std::string& name, const std::optional<core::UUID>& MBID)
@@ -360,9 +360,19 @@ namespace lms::db
return res;
}
void Artist::setSortName(const std::string& sortName)
void Artist::setName(std::string_view name)
{
_sortName = std::string(sortName, 0, _maxNameLength);
_name.assign(name, 0, _maxNameLength);
if (name.size() > _maxNameLength)
LMS_LOG(DB, WARNING, "Artist name too long, truncated to '" << _name << "'");
}
void Artist::setSortName(std::string_view sortName)
{
_sortName.assign(sortName, 0, _maxNameLength);
if (sortName.size() > _maxNameLength)
LMS_LOG(DB, WARNING, "Artist sort name too long, truncated to '" << _sortName << "'");
}
void Artist::setImage(ObjectPtr<Image> image)
+13 -1
View File
@@ -96,9 +96,12 @@ namespace lms::db
} // namespace
Cluster::Cluster(ObjectPtr<ClusterType> type, std::string_view name)
: _name{ std::string{ name, 0, _maxNameLength } }
: _name{ name }
, _clusterType{ getDboPtr(type) }
{
// As we use the name to uniquely identify clusters and cluster types, we must throw (and not truncate)
if (name.size() > maxNameLength)
throw Exception{ "Cluster name is too long: " + std::string{ name } + "'" };
}
Cluster::pointer Cluster::create(Session& session, ObjectPtr<ClusterType> type, std::string_view name)
@@ -183,6 +186,9 @@ namespace lms::db
ClusterType::ClusterType(std::string_view name)
: _name{ name }
{
// As we use the name to uniquely identify clusters and cluster types, we must throw
if (name.size() > maxNameLength)
throw Exception{ "ClusterType name is too long: " + std::string{ name } + "'" };
}
ClusterType::pointer ClusterType::create(Session& session, std::string_view name)
@@ -230,6 +236,9 @@ namespace lms::db
{
session.checkReadTransaction();
if (name.size() > maxNameLength)
throw Exception{ "Requested ClusterType name is too long: " + std::string{ name } + "'" };
return utils::fetchQuerySingleResult(session.getDboSession()->find<ClusterType>().where("name = ?").bind(name));
}
@@ -254,6 +263,9 @@ namespace lms::db
assert(self());
assert(session());
if (name.size() > Cluster::maxNameLength)
throw Exception{ "Requested Cluster name is too long: " + std::string{ name } + "'" };
return utils::fetchQuerySingleResult(session()->find<Cluster>().where("name = ?").bind(name).where("cluster_type_id = ?").bind(getId()));
}
+7 -1
View File
@@ -217,8 +217,11 @@ namespace lms::db
} // namespace
ReleaseType::ReleaseType(std::string_view name)
: _name{ std::string(name, 0, _maxNameLength) }
: _name{ name }
{
// As we use the name to uniquely identoify release type, we must throw (and not truncate)
if (name.size() > _maxNameLength)
throw Exception{ "ReleaseType name is too long: " + std::string{ name } + "'" };
}
ReleaseType::pointer ReleaseType::create(Session& session, std::string_view name)
@@ -237,6 +240,9 @@ namespace lms::db
{
session.checkReadTransaction();
if (name.size() > _maxNameLength)
throw Exception{ "Requeted ReleaseType name is too long: " + std::string{ name } + "'" };
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<ReleaseType>>("SELECT r_t from release_type r_t").where("r_t.name = ?").bind(name));
}
+21
View File
@@ -377,6 +377,27 @@ namespace lms::db
_relativeFilePath = filePath;
}
void Track::setName(std::string_view name)
{
_name = std::string{ name, 0, _maxNameLength };
if (name.size() > _maxNameLength)
LMS_LOG(DB, WARNING, "Track name too long, truncated to '" << _name << "'");
}
void Track::setCopyright(std::string_view copyright)
{
_copyright = std::string{ copyright, 0, _maxCopyrightLength };
if (copyright.size() > _maxCopyrightLength)
LMS_LOG(DB, WARNING, "Track copyright too long, truncated to '" << _copyright << "'");
}
void Track::setCopyrightURL(std::string_view copyrightURL)
{
_copyrightURL = std::string{ copyrightURL, 0, _maxCopyrightURLLength };
if (copyrightURL.size() > _maxCopyrightURLLength)
LMS_LOG(DB, WARNING, "Track copyright URL too long, truncated to '" << _copyrightURL << "'");
}
void Track::clearArtistLinks()
{
_trackArtistLinks.clear();