summaryrefslogtreecommitdiff
path: root/src/mongo/db
diff options
context:
space:
mode:
authorVesselina Ratcheva <vesselina.ratcheva@10gen.com>2022-12-05 17:18:41 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2023-01-23 00:27:20 +0000
commitc585f971a68e707da7420a6a5c19988bb65a6205 (patch)
tree28e0c798339c0c0095188ff6b781b526592fb21b /src/mongo/db
parent053c745ed3de8faa32f8b24fc1780a7521af932b (diff)
downloadmongo-c585f971a68e707da7420a6a5c19988bb65a6205.tar.gz
SERVER-70360 Set upper limit on wtimeout
(cherry picked from commit 85aa2d8d807d5472b1274971afc43f4c6ecd6923)
Diffstat (limited to 'src/mongo/db')
-rw-r--r--src/mongo/db/write_concern_options.cpp9
-rw-r--r--src/mongo/db/write_concern_options_test.cpp10
2 files changed, 12 insertions, 7 deletions
diff --git a/src/mongo/db/write_concern_options.cpp b/src/mongo/db/write_concern_options.cpp
index c87e21691fc..7cf9b373d61 100644
--- a/src/mongo/db/write_concern_options.cpp
+++ b/src/mongo/db/write_concern_options.cpp
@@ -233,11 +233,18 @@ void serializeWriteConcernW(const WriteConcernW& w, StringData fieldName, BSONOb
}
std::int64_t parseWTimeoutFromBSON(BSONElement element) {
+ // Store wTimeout as a 64-bit value but functionally limit it to int32 as values larger than
+ // than that do not make much sense to use and were not previously supported.
constexpr std::array<mongo::BSONType, 4> validTypes{
NumberLong, NumberInt, NumberDecimal, NumberDouble};
bool isValidType = std::any_of(
validTypes.begin(), validTypes.end(), [&](auto type) { return element.type() == type; });
- return isValidType ? element.safeNumberLong() : 0;
+
+ auto value = isValidType ? element.safeNumberLong() : 0;
+ uassert(ErrorCodes::FailedToParse,
+ "wtimeout must be a 32-bit integer",
+ value <= std::numeric_limits<int32_t>::max());
+ return value;
}
BSONObj WriteConcernOptions::toBSON() const {
diff --git a/src/mongo/db/write_concern_options_test.cpp b/src/mongo/db/write_concern_options_test.cpp
index abce6fa331e..87560b185d1 100644
--- a/src/mongo/db/write_concern_options_test.cpp
+++ b/src/mongo/db/write_concern_options_test.cpp
@@ -150,14 +150,12 @@ TEST(WriteConcernOptionsTest, ParseWTimeoutAsNaNDouble) {
ASSERT_EQUALS(WriteConcernOptions::kNoTimeout, options.wTimeout);
}
-TEST(WriteConcernOptionsTest, ParseWTimeoutAsDoubleLargerThanInt) {
+TEST(WriteConcernOptionsTest, ParseWTimeoutAsDoubleLargerThanIntFails) {
// Set wtimeout to a double with value larger than INT_MAX.
auto sw = WriteConcernOptions::parse(BSON("wtimeout" << 2999999999.0));
- ASSERT_OK(sw.getStatus());
- WriteConcernOptions options = sw.getValue();
- ASSERT_TRUE(WriteConcernOptions::SyncMode::UNSET == options.syncMode);
- ASSERT_EQUALS(1, stdx::get<int64_t>(options.w));
- ASSERT_EQUALS(options.wTimeout, Milliseconds{2999999999});
+ auto status = sw.getStatus();
+ ASSERT_NOT_OK(status);
+ ASSERT_EQUALS(ErrorCodes::FailedToParse, status.code());
}
TEST(WriteConcernOptionsTest, ParseReturnsFailedToParseOnUnknownField) {