summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdam Cooper <adam.cooper@mongodb.com>2019-10-25 23:06:01 +0000
committerevergreen <evergreen@mongodb.com>2019-10-25 23:06:01 +0000
commitdd31fcdd553149e034c94351344d3209b99650aa (patch)
tree78e5bef8408127255a92887d12cee45fcf23cc17 /src
parent1d109fca4d92fae456fe221c8c1d9038c8aeb5a3 (diff)
downloadmongo-dd31fcdd553149e034c94351344d3209b99650aa.tar.gz
SERVER-44064 Perform explicit cast on MessageCompressorManager parameter
(cherry picked from commit ffc6e38bc90750cc639317fc713e7bc4e0bf596e)
Diffstat (limited to 'src')
-rw-r--r--src/mongo/transport/message_compressor_manager.cpp5
-rw-r--r--src/mongo/transport/message_compressor_manager_test.cpp22
2 files changed, 26 insertions, 1 deletions
diff --git a/src/mongo/transport/message_compressor_manager.cpp b/src/mongo/transport/message_compressor_manager.cpp
index 212e008475c..5165082d7b2 100644
--- a/src/mongo/transport/message_compressor_manager.cpp
+++ b/src/mongo/transport/message_compressor_manager.cpp
@@ -167,7 +167,10 @@ StatusWith<Message> MessageCompressorManager::decompressMessage(const Message& m
return {ErrorCodes::BadValue, "Decompressed message would be negative in size"};
}
- size_t bufferSize = compressionHeader.uncompressedSize + MsgData::MsgDataHeaderSize;
+ // Explicitly promote `uncompressedSize` to a 64-bit integer before addition in order to
+ // avoid potential overflow.
+ size_t bufferSize =
+ static_cast<size_t>(compressionHeader.uncompressedSize) + MsgData::MsgDataHeaderSize;
if (bufferSize > MaxMessageSizeBytes) {
return {ErrorCodes::BadValue,
"Decompressed message would be larger than maximum message size"};
diff --git a/src/mongo/transport/message_compressor_manager_test.cpp b/src/mongo/transport/message_compressor_manager_test.cpp
index f884fd8b25c..71619388119 100644
--- a/src/mongo/transport/message_compressor_manager_test.cpp
+++ b/src/mongo/transport/message_compressor_manager_test.cpp
@@ -317,6 +317,28 @@ TEST(MessageCompressorManager, MessageSizeTooLarge) {
ASSERT_NOT_OK(status);
}
+TEST(MessageCompressorManager, MessageSizeMax32Bit) {
+ auto registry = buildRegistry();
+ MessageCompressorManager compManager(&registry);
+
+ auto badMessageBuffer = SharedBuffer::allocate(128);
+ MsgData::View badMessage(badMessageBuffer.get());
+ badMessage.setId(1);
+ badMessage.setResponseToMsgId(0);
+ badMessage.setOperation(dbCompressed);
+ badMessage.setLen(128);
+
+ DataRangeCursor cursor(badMessage.data(), badMessage.data() + badMessage.dataLen());
+ uassertStatusOK(cursor.writeAndAdvance<LittleEndian<int32_t>>(dbQuery));
+ uassertStatusOK(
+ cursor.writeAndAdvance<LittleEndian<int32_t>>(std::numeric_limits<int32_t>::max()));
+ uassertStatusOK(
+ cursor.writeAndAdvance<LittleEndian<uint8_t>>(registry.getCompressor("noop")->getId()));
+
+ auto status = compManager.decompressMessage(Message(badMessageBuffer), nullptr).getStatus();
+ ASSERT_NOT_OK(status);
+}
+
TEST(MessageCompressorManager, MessageSizeTooSmall) {
auto registry = buildRegistry();
MessageCompressorManager compManager(&registry);