diff options
author | Daniel Gottlieb <daniel.gottlieb@mongodb.com> | 2020-03-16 15:08:25 -0400 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-03-17 19:40:02 +0000 |
commit | 10b9987d9f2bf37ff76b3fd2271e1585c124d587 (patch) | |
tree | 3caf57ce385bdb3a50ee26c449841b0b6016ca6c /src/mongo | |
parent | 759af0dcb8dd4e13fdcdb8a77907b6dabaf7101e (diff) | |
download | mongo-10b9987d9f2bf37ff76b3fd2271e1585c124d587.tar.gz |
SERVER-46641: Limit collection names to 255 characters.
Diffstat (limited to 'src/mongo')
-rw-r--r-- | src/mongo/db/catalog/database_impl.cpp | 5 | ||||
-rw-r--r-- | src/mongo/db/catalog/rename_collection_test.cpp | 2 | ||||
-rw-r--r-- | src/mongo/db/namespace_string.cpp | 2 | ||||
-rw-r--r-- | src/mongo/db/namespace_string.h | 13 | ||||
-rw-r--r-- | src/mongo/db/namespace_string_test.cpp | 2 | ||||
-rw-r--r-- | src/mongo/db/repl/storage_interface_impl_test.cpp | 3 |
6 files changed, 19 insertions, 8 deletions
diff --git a/src/mongo/db/catalog/database_impl.cpp b/src/mongo/db/catalog/database_impl.cpp index bbebfc054f1..b1f70b658c9 100644 --- a/src/mongo/db/catalog/database_impl.cpp +++ b/src/mongo/db/catalog/database_impl.cpp @@ -584,6 +584,11 @@ void DatabaseImpl::_checkCanCreateCollection(OperationContext* opCtx, str::stream() << "cannot create a collection with an empty name on db: " << nss.db(), !nss.coll().empty()); + uassert(17361, + str::stream() << "Fully qualified namespace is too long. Namespace: " << nss.ns() + << " Max: " << NamespaceString::MaxNsCollectionLen, + !nss.isNormalCollection() || nss.size() <= NamespaceString::MaxNsCollectionLen); + uassert(28838, "cannot create a non-capped oplog collection", options.capped || !nss.isOplog()); uassert(ErrorCodes::DatabaseDropPending, str::stream() << "Cannot create collection " << nss diff --git a/src/mongo/db/catalog/rename_collection_test.cpp b/src/mongo/db/catalog/rename_collection_test.cpp index 6aaa602a075..0f8965a76db 100644 --- a/src/mongo/db/catalog/rename_collection_test.cpp +++ b/src/mongo/db/catalog/rename_collection_test.cpp @@ -549,7 +549,7 @@ TEST_F(RenameCollectionTest, RenameCollectionReturnsNotMasterIfNotPrimary) { TEST_F(RenameCollectionTest, TargetCollectionNameLong) { _createCollection(_opCtx.get(), _sourceNss); - const std::string targetCollectionName(500, 'a'); + const std::string targetCollectionName(255, 'a'); NamespaceString longTargetNss(_sourceNss.db(), targetCollectionName); ASSERT_OK(renameCollection(_opCtx.get(), _sourceNss, longTargetNss, {})); } diff --git a/src/mongo/db/namespace_string.cpp b/src/mongo/db/namespace_string.cpp index dd8f965afb5..54b333ed0a7 100644 --- a/src/mongo/db/namespace_string.cpp +++ b/src/mongo/db/namespace_string.cpp @@ -162,7 +162,7 @@ bool NamespaceString::checkLengthForFCV() const { if (serverGlobalParams.featureCompatibility.getVersion() == ServerGlobalParams::FeatureCompatibility::Version::kFullyUpgradedTo44) { - return true; + return size() <= MaxNsCollectionLen; } return false; diff --git a/src/mongo/db/namespace_string.h b/src/mongo/db/namespace_string.h index 4145ac25882..598ff87c4f0 100644 --- a/src/mongo/db/namespace_string.h +++ b/src/mongo/db/namespace_string.h @@ -43,10 +43,12 @@ namespace mongo { -const size_t MaxDatabaseNameLen = 128; // max str len for the db name, including null char - class NamespaceString { public: + constexpr static size_t MaxDatabaseNameLen = + 128; // max str len for the db name, including null char + constexpr static size_t MaxNsCollectionLen = 255; + // Reserved system namespaces // Namespace for the admin database @@ -470,10 +472,13 @@ StringBuilder& operator<<(StringBuilder& builder, const NamespaceStringOrUUID& n inline StringData nsToDatabaseSubstring(StringData ns) { size_t i = ns.find('.'); if (i == std::string::npos) { - massert(10078, "nsToDatabase: db too long", ns.size() < MaxDatabaseNameLen); + massert( + 10078, "nsToDatabase: db too long", ns.size() < NamespaceString::MaxDatabaseNameLen); return ns; } - massert(10088, "nsToDatabase: db too long", i < static_cast<size_t>(MaxDatabaseNameLen)); + massert(10088, + "nsToDatabase: db too long", + i < static_cast<size_t>(NamespaceString::MaxDatabaseNameLen)); return ns.substr(0, i); } diff --git a/src/mongo/db/namespace_string_test.cpp b/src/mongo/db/namespace_string_test.cpp index baba77e9855..b849284b99d 100644 --- a/src/mongo/db/namespace_string_test.cpp +++ b/src/mongo/db/namespace_string_test.cpp @@ -150,7 +150,7 @@ TEST(NamespaceStringTest, MakeDropPendingNamespace) { NamespaceString{"test.foo"}.makeDropPendingNamespace( repl::OpTime(Timestamp(Seconds(1234567), 8U), 9LL))); - std::string collName(8192, 't'); + std::string collName(NamespaceString::MaxNsCollectionLen, 't'); NamespaceString nss("test", collName); ASSERT_EQUALS(NamespaceString{"test.system.drop.1234567i8t9." + collName}, nss.makeDropPendingNamespace(repl::OpTime(Timestamp(Seconds(1234567), 8U), 9LL))); diff --git a/src/mongo/db/repl/storage_interface_impl_test.cpp b/src/mongo/db/repl/storage_interface_impl_test.cpp index 6c91cc5e090..9656b154a88 100644 --- a/src/mongo/db/repl/storage_interface_impl_test.cpp +++ b/src/mongo/db/repl/storage_interface_impl_test.cpp @@ -77,7 +77,8 @@ BSONObj makeIdIndexSpec(const NamespaceString& nss) { */ template <typename T> NamespaceString makeNamespace(const T& t, const std::string& suffix = "") { - return NamespaceString(std::string("local." + t.getSuiteName() + "_" + t.getTestName()) + + return NamespaceString(std::string("local." + t.getSuiteName() + "_" + t.getTestName()) + .substr(0, NamespaceString::MaxNsCollectionLen - suffix.length()) + suffix); } |