diff options
author | Justin Seyster <justin.seyster@mongodb.com> | 2018-03-30 14:12:58 -0400 |
---|---|---|
committer | Justin Seyster <justin.seyster@mongodb.com> | 2018-05-08 19:05:40 -0400 |
commit | eed8ec8d73fca312fa63893f28b1b51aba772ca1 (patch) | |
tree | b8daa6e0c5e05b5387f924d858a6387125f57acc | |
parent | 2f523f843dbfa351d5ef6867017d14478d7a0d6d (diff) | |
download | mongo-eed8ec8d73fca312fa63893f28b1b51aba772ca1.tar.gz |
SERVER-34218 Always reinitialize fields in FieldRef::parse().
Previously, calling FieldRef::FieldRef(StringData) never initialized
_cachedSize, resulting in unpredictable build failures.
Additionally, calling FieldRef::parse(StringData) with the empty
string did not reinitialize the FieldRef.
(cherry picked from commit c22357338a6f192deb3e24e2a07672419cc7dacb)
-rw-r--r-- | src/mongo/db/field_ref.cpp | 8 | ||||
-rw-r--r-- | src/mongo/db/field_ref_test.cpp | 9 |
2 files changed, 12 insertions, 5 deletions
diff --git a/src/mongo/db/field_ref.cpp b/src/mongo/db/field_ref.cpp index 3a759febe3c..3070aafebb3 100644 --- a/src/mongo/db/field_ref.cpp +++ b/src/mongo/db/field_ref.cpp @@ -38,19 +38,17 @@ const size_t FieldRef::kReserveAhead; FieldRef::FieldRef() : _size(0), _cachedSize(0) {} -FieldRef::FieldRef(StringData path) : _size(0) { +FieldRef::FieldRef(StringData path) { parse(path); } void FieldRef::parse(StringData path) { + clear(); + if (path.size() == 0) { return; } - if (_size != 0) { - clear(); - } - // We guarantee that accesses through getPart() will be valid while 'this' is. So we // keep a copy in a local sting. diff --git a/src/mongo/db/field_ref_test.cpp b/src/mongo/db/field_ref_test.cpp index fe28c5fc5e1..60c143d5da8 100644 --- a/src/mongo/db/field_ref_test.cpp +++ b/src/mongo/db/field_ref_test.cpp @@ -76,6 +76,15 @@ TEST(Empty, EmptyFieldName) { ASSERT_EQUALS(fieldRef.dottedField(), field); } +TEST(Empty, ReinitializeWithEmptyString) { + FieldRef fieldRef("a.b.c.d.e"); + ASSERT_EQUALS(fieldRef.numParts(), 5U); + + fieldRef.parse(""); + ASSERT_EQUALS(fieldRef.numParts(), 0U); + ASSERT_EQUALS(fieldRef.dottedField(), ""); +} + TEST(Normal, SinglePart) { string field = "a"; FieldRef fieldRef(field); |