summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTess Avitabile <tess.avitabile@mongodb.com>2017-03-29 14:21:44 -0400
committerTess Avitabile <tess.avitabile@mongodb.com>2017-03-29 18:11:23 -0400
commite165f49a7e9d29c2bd2b8a776b8d4f5cb5f9a340 (patch)
treed30518652fc238fa4a575c7a4b432d2213400ab7
parent3412b99e9f81199a7265fd0483d9b01432dbe53b (diff)
downloadmongo-e165f49a7e9d29c2bd2b8a776b8d4f5cb5f9a340.tar.gz
SERVER-25717 Negative push index out of bounds should default to beginning of array
-rw-r--r--src/mongo/db/ops/modifier_push.cpp9
-rw-r--r--src/mongo/db/ops/modifier_push_test.cpp6
2 files changed, 9 insertions, 6 deletions
diff --git a/src/mongo/db/ops/modifier_push.cpp b/src/mongo/db/ops/modifier_push.cpp
index e2298b25ba8..d2d816776fa 100644
--- a/src/mongo/db/ops/modifier_push.cpp
+++ b/src/mongo/db/ops/modifier_push.cpp
@@ -557,11 +557,14 @@ Status ModifierPush::apply() const {
if (actualPosition < 0) {
actualPosition = _preparedState->arrayPreModSize + _position;
}
- // Default to adding to the end of the array, even if we're out of bounds because a negative
- // position was too negative.
- if (actualPosition < 0 || actualPosition > int32_t(_preparedState->arrayPreModSize)) {
+ // Default to adding to the end of the array if the position was too high.
+ if (actualPosition > int32_t(_preparedState->arrayPreModSize)) {
actualPosition = _preparedState->arrayPreModSize;
}
+ // Default to adding to the beginning of the array if the position was too low.
+ if (actualPosition < 0) {
+ actualPosition = 0;
+ }
_preparedState->actualPosition = actualPosition;
// 2. Add new elements to the array either by going over the $each array or by
diff --git a/src/mongo/db/ops/modifier_push_test.cpp b/src/mongo/db/ops/modifier_push_test.cpp
index 8ef8e1488cd..c4a05aafa7f 100644
--- a/src/mongo/db/ops/modifier_push_test.cpp
+++ b/src/mongo/db/ops/modifier_push_test.cpp
@@ -1535,7 +1535,7 @@ TEST(ToPosition, NegativePositionMultiElementArray) {
ASSERT_EQUALS(fromjson("{$set: {'a': [0, 1, 2, 5, 3, 4]}}"), logDoc);
}
-TEST(ToPosition, NegativePositionOutOfBoundsDefaultsToEnd) {
+TEST(ToPosition, NegativePositionOutOfBoundsDefaultsToBeginning) {
Document doc(fromjson("{a: [0]}"));
Mod pushMod(fromjson("{$push: {a: {$each: [1], $position: -2}}}"));
@@ -1547,13 +1547,13 @@ TEST(ToPosition, NegativePositionOutOfBoundsDefaultsToEnd) {
ASSERT_OK(pushMod.apply());
ASSERT_FALSE(doc.isInPlaceModeEnabled());
- ASSERT_EQUALS(fromjson("{a: [0, 1]}"), doc);
+ ASSERT_EQUALS(fromjson("{a: [1, 0]}"), doc);
Document logDoc;
LogBuilder logBuilder(logDoc.root());
ASSERT_OK(pushMod.log(&logBuilder));
ASSERT_EQUALS(countChildren(logDoc.root()), 1u);
- ASSERT_EQUALS(fromjson("{$set: {'a.1': 1}}"), logDoc);
+ ASSERT_EQUALS(fromjson("{$set: {'a': [1, 0]}}"), logDoc);
}
TEST(ToPosition, NegativePositionPushMultipleElements) {