diff options
author | Nick Zolnierz <nicholas.zolnierz@mongodb.com> | 2018-10-11 13:19:15 -0400 |
---|---|---|
committer | Nick Zolnierz <nicholas.zolnierz@mongodb.com> | 2018-10-11 16:39:39 -0400 |
commit | 952bf4a9daa5535f2c51caf33f54748a8867db5f (patch) | |
tree | 5c453c60c0eaa603cb667c6afdb33c1d2b25d96e /src | |
parent | 8e48340301c668742a5a5bb2a8f1bb68553faaf2 (diff) | |
download | mongo-952bf4a9daa5535f2c51caf33f54748a8867db5f.tar.gz |
SERVER-37058: Update with numeric field names inside an array can cause validation to fail
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/db/update_index_data.cpp | 25 | ||||
-rw-r--r-- | src/mongo/db/update_index_data_test.cpp | 22 |
2 files changed, 46 insertions, 1 deletions
diff --git a/src/mongo/db/update_index_data.cpp b/src/mongo/db/update_index_data.cpp index 2b1144d40a3..ade76f0c50d 100644 --- a/src/mongo/db/update_index_data.cpp +++ b/src/mongo/db/update_index_data.cpp @@ -149,12 +149,35 @@ bool getCanonicalIndexField(StringData fullName, string* out) { while (j + 1 < fullName.size() && isdigit(fullName[j + 1])) j++; - if (j + 1 == fullName.size() || fullName[j + 1] == '.') { + if (j + 1 == fullName.size()) { // only digits found, skip forward i = j; modified = true; continue; } + + // Check for consecutive digits separated by a period. + if (fullName[j + 1] == '.') { + // Peek ahead to see if the next set of characters are also numeric. + size_t k = j + 2; + while (k < fullName.size() && isdigit(fullName[k])) { + k++; + } + + // The second set of digits may end at the end of the path or a '.'. + if (k == fullName.size() || fullName[k] == '.') { + // Found consecutive numerical path components. Since this implies a numeric + // field name, return the prefix as the canonical index field. This is meant to + // fix SERVER-37058. + modified = true; + break; + } + + // Only one numerical path component, skip forward. + i = j; + modified = true; + continue; + } } buf << c; diff --git a/src/mongo/db/update_index_data_test.cpp b/src/mongo/db/update_index_data_test.cpp index 167cb632ab1..f01f495ed1f 100644 --- a/src/mongo/db/update_index_data_test.cpp +++ b/src/mongo/db/update_index_data_test.cpp @@ -126,4 +126,26 @@ TEST(UpdateIndexDataTest, getCanonicalIndexField1) { ASSERT_FALSE(getCanonicalIndexField("a.", &x)); } + +TEST(UpdateIndexDataTest, CanonicalIndexFieldForConsecutiveDigits) { + std::string indexField; + + ASSERT_TRUE(getCanonicalIndexField("a.0.0", &indexField)); + ASSERT_EQ(indexField, "a"); + + ASSERT_TRUE(getCanonicalIndexField("a.55.01", &indexField)); + ASSERT_EQ(indexField, "a"); + + ASSERT_TRUE(getCanonicalIndexField("a.0.0.b.1", &indexField)); + ASSERT_EQ(indexField, "a"); + + ASSERT_TRUE(getCanonicalIndexField("a.0b.1", &indexField)); + ASSERT_EQ(indexField, "a.0b"); + + ASSERT_TRUE(getCanonicalIndexField("a.0.b.1.2", &indexField)); + ASSERT_EQ(indexField, "a.b"); + + ASSERT_TRUE(getCanonicalIndexField("a.0.11b", &indexField)); + ASSERT_EQ(indexField, "a.11b"); +} } |