diff options
author | Jonathan Reams <jbreams@mongodb.com> | 2019-03-27 12:06:48 -0400 |
---|---|---|
committer | Jonathan Reams <jbreams@mongodb.com> | 2019-04-06 12:40:33 -0400 |
commit | 83383eb160f904c699b399ac59ccbbf103ad6102 (patch) | |
tree | d82abc4d7f7f10181b103461c936682813787e56 | |
parent | 652d22504d2990b2322aefc272b489e731edd789 (diff) | |
download | mongo-83383eb160f904c699b399ac59ccbbf103ad6102.tar.gz |
SERVER-40356 Make ConstDataRange constructable from any byte-like type
39 files changed, 369 insertions, 271 deletions
diff --git a/buildscripts/idl/idl/cpp_types.py b/buildscripts/idl/idl/cpp_types.py index 98e5cc83dc0..4cd5489409e 100644 --- a/buildscripts/idl/idl/cpp_types.py +++ b/buildscripts/idl/idl/cpp_types.py @@ -338,9 +338,8 @@ class _CppTypeVector(CppTypeBase): def get_getter_body(self, member_name): # type: (unicode) -> unicode - return common.template_args( - 'return ConstDataRange(reinterpret_cast<const char*>(${member_name}.data()), ${member_name}.size());', - member_name=member_name) + return common.template_args('return ConstDataRange(${member_name});', + member_name=member_name) def get_setter_body(self, member_name, validator_method_name): # type: (unicode, unicode) -> unicode @@ -352,7 +351,7 @@ class _CppTypeVector(CppTypeBase): def get_transform_to_getter_type(self, expression): # type: (unicode) -> Optional[unicode] - return common.template_args('makeCDR(${expression});', expression=expression) + return common.template_args('ConstDataRange(${expression});', expression=expression) def get_transform_to_storage_type(self, expression): # type: (unicode) -> Optional[unicode] @@ -681,7 +680,7 @@ class _BinDataBsonCppTypeBase(BsonCppTypeBase): expression=expression, method_name=method_name)) else: indented_writer.write_line( - common.template_args('ConstDataRange tempCDR = makeCDR(${expression});', + common.template_args('ConstDataRange tempCDR(${expression});', expression=expression)) return common.template_args( diff --git a/src/mongo/base/data_builder.h b/src/mongo/base/data_builder.h index 00292c92a99..92a0ba73d0a 100644 --- a/src/mongo/base/data_builder.h +++ b/src/mongo/base/data_builder.h @@ -95,11 +95,11 @@ public: Status write(const T& value, std::size_t offset = 0) { _ensureStorage(); - auto status = _unwrittenSpaceCursor.write(value, offset); + auto status = _unwrittenSpaceCursor.writeNoThrow(value, offset); if (!status.isOK()) { reserve(_getSerializedSize(value)); - status = _unwrittenSpaceCursor.write(value, offset); + status = _unwrittenSpaceCursor.writeNoThrow(value, offset); } return status; @@ -117,11 +117,11 @@ public: // 1. A way to check if the type has a constant size // 2. A way to perform a runtime write which can fail with "too little // size" without status generation - auto status = _unwrittenSpaceCursor.writeAndAdvance(value); + auto status = _unwrittenSpaceCursor.writeAndAdvanceNoThrow(value); if (!status.isOK()) { reserve(_getSerializedSize(value)); - status = _unwrittenSpaceCursor.writeAndAdvance(value); + status = _unwrittenSpaceCursor.writeAndAdvanceNoThrow(value); } return status; diff --git a/src/mongo/base/data_builder_test.cpp b/src/mongo/base/data_builder_test.cpp index 91adfabe25b..ed001bd31b1 100644 --- a/src/mongo/base/data_builder_test.cpp +++ b/src/mongo/base/data_builder_test.cpp @@ -67,11 +67,10 @@ TEST(DataBuilder, Basic) { ConstDataRangeCursor cdrc = db.getCursor(); - ASSERT_EQUALS(static_cast<uint16_t>(1), cdrc.readAndAdvance<uint16_t>().getValue()); - ASSERT_EQUALS(static_cast<uint32_t>(2), - cdrc.readAndAdvance<LittleEndian<uint32_t>>().getValue()); - ASSERT_EQUALS(static_cast<uint64_t>(3), cdrc.readAndAdvance<BigEndian<uint64_t>>().getValue()); - ASSERT_EQUALS(false, cdrc.readAndAdvance<char>().isOK()); + ASSERT_EQUALS(static_cast<uint16_t>(1), cdrc.readAndAdvance<uint16_t>()); + ASSERT_EQUALS(static_cast<uint32_t>(2), cdrc.readAndAdvance<LittleEndian<uint32_t>>()); + ASSERT_EQUALS(static_cast<uint64_t>(3), cdrc.readAndAdvance<BigEndian<uint64_t>>()); + ASSERT_NOT_OK(cdrc.readAndAdvanceNoThrow<char>()); } TEST(DataBuilder, ResizeDown) { @@ -86,8 +85,8 @@ TEST(DataBuilder, ResizeDown) { ConstDataRangeCursor cdrc = db.getCursor(); - ASSERT_EQUALS(static_cast<uint16_t>(1), cdrc.readAndAdvance<uint16_t>().getValue()); - ASSERT_EQUALS(false, cdrc.readAndAdvance<char>().isOK()); + ASSERT_EQUALS(static_cast<uint16_t>(1), cdrc.readAndAdvance<uint16_t>()); + ASSERT_NOT_OK(cdrc.readAndAdvanceNoThrow<char>()); } TEST(DataBuilder, ResizeUp) { @@ -102,9 +101,9 @@ TEST(DataBuilder, ResizeUp) { ConstDataRangeCursor cdrc = db.getCursor(); - ASSERT_EQUALS(static_cast<uint16_t>(1), cdrc.readAndAdvance<uint16_t>().getValue()); - ASSERT_EQUALS(static_cast<uint64_t>(2), cdrc.readAndAdvance<uint64_t>().getValue()); - ASSERT_EQUALS(false, cdrc.readAndAdvance<char>().isOK()); + ASSERT_EQUALS(static_cast<uint16_t>(1), cdrc.readAndAdvance<uint16_t>()); + ASSERT_EQUALS(static_cast<uint64_t>(2), cdrc.readAndAdvance<uint64_t>()); + ASSERT_NOT_OK(cdrc.readAndAdvanceNoThrow<char>()); } TEST(DataBuilder, Reserve) { @@ -148,7 +147,7 @@ TEST(DataBuilder, Clear) { ASSERT_EQUALS(0u, db.size()); ConstDataRangeCursor cdrc = db.getCursor(); - ASSERT_EQUALS(false, cdrc.readAndAdvance<char>().isOK()); + ASSERT_NOT_OK(cdrc.readAndAdvanceNoThrow<char>()); } TEST(DataBuilder, Move) { @@ -160,7 +159,7 @@ TEST(DataBuilder, Move) { ConstDataRangeCursor cdrc = db2.getCursor(); - ASSERT_EQUALS(static_cast<uint16_t>(1), cdrc.readAndAdvance<uint16_t>().getValue()); + ASSERT_EQUALS(static_cast<uint16_t>(1), cdrc.readAndAdvance<uint16_t>()); ASSERT_EQUALS(2u, db2.capacity()); ASSERT_EQUALS(2u, db2.size()); diff --git a/src/mongo/base/data_range.h b/src/mongo/base/data_range.h index 9830b515091..eb53af9719a 100644 --- a/src/mongo/base/data_range.h +++ b/src/mongo/base/data_range.h @@ -40,9 +40,40 @@ #include "mongo/util/mongoutils/str.h" namespace mongo { - class ConstDataRange { +protected: + // These are helper types to make ConstDataRange's and friends constructable either from + // a range of byte-like pointers or from a container of byte-like values. + template <typename T> + constexpr static auto isByteV = ((std::is_integral_v<T> && sizeof(T) == 1) || + std::is_same_v<T, std::byte>); + + template <typename T, typename = void> + struct HasDataSize : std::false_type {}; + + template <typename T> + struct HasDataSize< + T, + std::enable_if_t<std::is_void_v< + std::void_t<decltype(std::declval<T>().data()), decltype(std::declval<T>().size())>>>> + : std::true_type {}; + + template <typename T, typename = void> + struct ContiguousContainerOfByteLike : std::false_type {}; + + template <typename T> + struct ContiguousContainerOfByteLike< + T, + std::void_t<decltype(std::declval<T>().data()), + std::enable_if_t<isByteV<typename T::value_type> && HasDataSize<T>::value>>> + : std::true_type {}; + public: + using byte_type = char; + + // You can construct a ConstDataRange from any byte-like sequence. Byte-like means an + // integral type with a size of one. + // // begin and end should point to the first and one past last bytes in // the range you wish to view. // @@ -51,28 +82,54 @@ public: // to a non-zero value, you'll change the Status messages that are // returned on failure to be offset by the amount passed to this // constructor. - ConstDataRange(const char* begin, const char* end, std::ptrdiff_t debug_offset = 0) - : _begin(begin), _end(end), _debug_offset(debug_offset) { + template <typename ByteLike, typename std::enable_if_t<isByteV<ByteLike>, int> = 0> + ConstDataRange(const ByteLike* begin, const ByteLike* end, std::ptrdiff_t debug_offset = 0) + : _begin(reinterpret_cast<const byte_type*>(begin)), + _end(reinterpret_cast<const byte_type*>(end)), + _debug_offset(debug_offset) { invariant(end >= begin); } - ConstDataRange(const char* begin, std::size_t length, std::ptrdiff_t debug_offset = 0) - : ConstDataRange(begin, begin + length, debug_offset) {} - - const char* data() const { + // Constructing from nullptr, nullptr initializes an empty ConstDataRange. + ConstDataRange(std::nullptr_t, std::nullptr_t, std::ptrdiff_t debug_offset = 0) + : _begin(nullptr), _end(nullptr), _debug_offset(debug_offset) {} + + // You can also construct from a pointer to a byte-like type and a size. + template <typename ByteLike, typename std::enable_if_t<isByteV<ByteLike>, int> = 0> + ConstDataRange(const ByteLike* begin, std::size_t length, std::ptrdiff_t debug_offset = 0) + : _begin(reinterpret_cast<const byte_type*>(begin)), + _end(reinterpret_cast<const byte_type*>(_begin + length)), + _debug_offset(debug_offset) {} + + // ConstDataRange can also act as a view of a container of byte-like values, such as a + // std::vector<uint8_t> or a std::array<char, size>. The requirements are that the + // value_type of the container is byte-like and that the values be contiguous - the container + // must have a data() function that returns a pointer to the front and a size() function + // that returns the number of elements. + template <typename Container, + typename std::enable_if_t<ContiguousContainerOfByteLike<Container>::value, int> = 0> + ConstDataRange(const Container& container, std::ptrdiff_t debug_offset = 0) + : ConstDataRange(container.data(), container.size(), debug_offset) {} + + // You can also construct from a C-style array, including string literals. + template <typename ByteLike, size_t N, typename std::enable_if_t<isByteV<ByteLike>, int> = 0> + ConstDataRange(const ByteLike (&arr)[N], std::ptrdiff_t debug_offset = 0) + : ConstDataRange(arr, N, debug_offset) {} + + const byte_type* data() const noexcept { return _begin; } - size_t length() const { + size_t length() const noexcept { return _end - _begin; } - bool empty() const { + bool empty() const noexcept { return length() == 0; } template <typename T> - Status readInto(T* t, size_t offset = 0) const { + Status readIntoNoThrow(T* t, size_t offset = 0) const noexcept { if (offset > length()) { return makeOffsetStatus(offset); } @@ -82,9 +139,14 @@ public: } template <typename T> - StatusWith<T> read(std::size_t offset = 0) const { + void readInto(T* t, size_t offset = 0) const { + uassertStatusOK(readIntoNoThrow(t, offset)); + } + + template <typename T> + StatusWith<T> readNoThrow(std::size_t offset = 0) const noexcept { T t(DataType::defaultConstruct<T>()); - Status s = readInto(&t, offset); + Status s = readIntoNoThrow(&t, offset); if (s.isOK()) { return StatusWith<T>(std::move(t)); @@ -93,6 +155,11 @@ public: } } + template <typename T> + T read(std::size_t offset = 0) const { + return uassertStatusOK(readNoThrow<T>(offset)); + } + friend bool operator==(const ConstDataRange& lhs, const ConstDataRange& rhs) { return std::tie(lhs._begin, lhs._end) == std::tie(rhs._begin, rhs._end); } @@ -103,8 +170,8 @@ public: protected: - const char* _begin; - const char* _end; + const byte_type* _begin; + const byte_type* _end; std::ptrdiff_t _debug_offset; Status makeOffsetStatus(size_t offset) const; @@ -112,16 +179,56 @@ protected: class DataRange : public ConstDataRange { public: - typedef char* bytes_type; - - DataRange(bytes_type begin, bytes_type end, std::ptrdiff_t debug_offset = 0) + // You can construct a DataRange from all the same types as ConstDataRange, except that the + // arguments may not be const (since this is the mutable version of ConstDataRange). + template <typename ByteLike> + DataRange(ByteLike* begin, ByteLike* end, std::ptrdiff_t debug_offset = 0) : ConstDataRange(begin, end, debug_offset) {} - DataRange(bytes_type begin, std::size_t length, std::ptrdiff_t debug_offset = 0) + template <typename ByteLike> + DataRange(const ByteLike*, const ByteLike* end, std::ptrdiff_t debug_offset) = delete; + + template <typename ByteLike> + DataRange(const ByteLike*, const ByteLike* end) = delete; + + template <typename ByteLike> + DataRange(ByteLike* begin, std::size_t length, std::ptrdiff_t debug_offset = 0) : ConstDataRange(begin, length, debug_offset) {} + template <typename ByteLike> + DataRange(const ByteLike*, size_t, std::ptrdiff_t debug_offset) = delete; + + template <typename ByteLike> + DataRange(const ByteLike*, size_t) = delete; + + DataRange(std::nullptr_t, std::nullptr_t, std::ptrdiff_t debug_offset = 0) + : ConstDataRange(nullptr, nullptr, debug_offset) {} + + template <typename Container, + typename std::enable_if_t<ContiguousContainerOfByteLike<Container>::value, int> = 0> + DataRange(Container& container, std::ptrdiff_t debug_offset = 0) + : ConstDataRange(std::forward<Container>(container), debug_offset) {} + + template <typename Container, + typename std::enable_if_t<ContiguousContainerOfByteLike<Container>::value, int> = 0> + DataRange(const Container&, std::ptrdiff_t) = delete; + + template <typename Container, + typename std::enable_if_t<ContiguousContainerOfByteLike<Container>::value, int> = 0> + DataRange(const Container&) = delete; + + template <typename ByteLike, size_t N> + DataRange(ByteLike (&arr)[N], std::ptrdiff_t debug_offset = 0) + : ConstDataRange(arr, N, debug_offset) {} + + template <typename ByteLike, size_t N> + DataRange(const ByteLike (&arr)[N], std::ptrdiff_t) = delete; + + template <typename ByteLike, size_t N> + DataRange(const ByteLike (&arr)[N]) = delete; + template <typename T> - Status write(const T& value, std::size_t offset = 0) { + Status writeNoThrow(const T& value, std::size_t offset = 0) noexcept { if (offset > length()) { return makeOffsetStatus(offset); } @@ -132,6 +239,11 @@ public: nullptr, offset + _debug_offset); } + + template <typename T> + void write(const T& value, std::size_t offset = 0) { + uassertStatusOK(writeNoThrow(value, offset)); + } }; struct DataRangeTypeHelper { diff --git a/src/mongo/base/data_range_cursor.h b/src/mongo/base/data_range_cursor.h index f4056e60573..13f0fdae4d3 100644 --- a/src/mongo/base/data_range_cursor.h +++ b/src/mongo/base/data_range_cursor.h @@ -42,12 +42,10 @@ namespace mongo { class ConstDataRangeCursor : public ConstDataRange { public: - ConstDataRangeCursor(const char* begin, const char* end, std::ptrdiff_t debug_offset = 0) - : ConstDataRange(begin, end, debug_offset) {} - + using ConstDataRange::ConstDataRange; ConstDataRangeCursor(ConstDataRange cdr) : ConstDataRange(cdr) {} - Status advance(size_t advance) { + Status advanceNoThrow(size_t advance) noexcept try { if (advance > length()) { return makeAdvanceStatus(advance); } @@ -56,10 +54,16 @@ public: _debug_offset += advance; return Status::OK(); + } catch (const DBException& e) { + return e.toStatus(); + } + + void advance(size_t advance) { + uassertStatusOK(advanceNoThrow(advance)); } template <typename T> - Status skip() { + Status skipNoThrow() noexcept { size_t advanced = 0; Status x = DataType::load<T>(nullptr, _begin, _end - _begin, &advanced, _debug_offset); @@ -73,7 +77,12 @@ public: } template <typename T> - Status readAndAdvance(T* t) { + void skip() { + uassertStatusOK(skipNoThrow<T>()); + } + + template <typename T> + Status readAndAdvanceNoThrow(T* t) noexcept { size_t advanced = 0; Status x = DataType::load(t, _begin, _end - _begin, &advanced, _debug_offset); @@ -87,9 +96,14 @@ public: } template <typename T> - StatusWith<T> readAndAdvance() { + void readAndAdvance(T* t) { + return uassertStatusOK(readAndAdvanceNoThrow(t)); + } + + template <typename T> + StatusWith<T> readAndAdvanceNoThrow() noexcept { T out(DataType::defaultConstruct<T>()); - Status x = readAndAdvance(&out); + Status x = readAndAdvanceNoThrow(&out); if (x.isOK()) { return StatusWith<T>(std::move(out)); @@ -98,22 +112,25 @@ public: } } + template <typename T> + T readAndAdvance() { + return uassertStatusOK(readAndAdvanceNoThrow<T>()); + } + private: Status makeAdvanceStatus(size_t advance) const; }; class DataRangeCursor : public DataRange { public: - DataRangeCursor(char* begin, char* end, std::ptrdiff_t debug_offset = 0) - : DataRange(begin, end, debug_offset) {} - + using DataRange::DataRange; DataRangeCursor(DataRange range) : DataRange(range) {} operator ConstDataRangeCursor() const { return ConstDataRangeCursor(ConstDataRange(_begin, _end, _debug_offset)); } - Status advance(size_t advance) { + Status advanceNoThrow(size_t advance) noexcept { if (advance > length()) { return makeAdvanceStatus(advance); } @@ -124,8 +141,12 @@ public: return Status::OK(); } + void advance(size_t advance) { + uassertStatusOK(advanceNoThrow(advance)); + } + template <typename T> - Status skip() { + Status skipNoThrow() noexcept { size_t advanced = 0; Status x = DataType::load<T>(nullptr, _begin, _end - _begin, &advanced, _debug_offset); @@ -139,7 +160,12 @@ public: } template <typename T> - Status readAndAdvance(T* t) { + void skip() { + uassertStatusOK(skipNoThrow<T>()); + } + + template <typename T> + Status readAndAdvanceNoThrow(T* t) noexcept { size_t advanced = 0; Status x = DataType::load(t, _begin, _end - _begin, &advanced, _debug_offset); @@ -153,9 +179,14 @@ public: } template <typename T> - StatusWith<T> readAndAdvance() { + void readAndAdvance(T* t) { + uassertStatusOK(readAndAdvanceNoThrow(t)); + } + + template <typename T> + StatusWith<T> readAndAdvanceNoThrow() noexcept { T out(DataType::defaultConstruct<T>()); - Status x = readAndAdvance(&out); + Status x = readAndAdvanceNoThrow(&out); if (x.isOK()) { return StatusWith<T>(std::move(out)); @@ -165,7 +196,12 @@ public: } template <typename T> - Status writeAndAdvance(const T& value) { + T readAndAdvance() { + return uassertStatusOK(readAndAdvanceNoThrow<T>()); + } + + template <typename T> + Status writeAndAdvanceNoThrow(const T& value) noexcept { size_t advanced = 0; Status x = DataType::store( @@ -179,6 +215,11 @@ public: return x; } + template <typename T> + void writeAndAdvance(const T& value) { + uassertStatusOK(writeAndAdvanceNoThrow(value)); + } + private: Status makeAdvanceStatus(size_t advance) const; }; diff --git a/src/mongo/base/data_range_cursor_test.cpp b/src/mongo/base/data_range_cursor_test.cpp index afd91e478a5..60543c6af6a 100644 --- a/src/mongo/base/data_range_cursor_test.cpp +++ b/src/mongo/base/data_range_cursor_test.cpp @@ -45,18 +45,16 @@ TEST(DataRangeCursor, ConstDataRangeCursor) { ConstDataRangeCursor cdrc(buf, buf + sizeof(buf)); ConstDataRangeCursor backup(cdrc); - ASSERT_EQUALS(static_cast<uint16_t>(1), cdrc.readAndAdvance<uint16_t>().getValue()); - ASSERT_EQUALS(static_cast<uint32_t>(2), - cdrc.readAndAdvance<LittleEndian<uint32_t>>().getValue()); - ASSERT_EQUALS(static_cast<uint64_t>(3), cdrc.readAndAdvance<BigEndian<uint64_t>>().getValue()); - ASSERT_EQUALS(false, cdrc.readAndAdvance<char>().isOK()); + ASSERT_EQUALS(static_cast<uint16_t>(1), cdrc.readAndAdvance<uint16_t>()); + ASSERT_EQUALS(static_cast<uint32_t>(2), cdrc.readAndAdvance<LittleEndian<uint32_t>>()); + ASSERT_EQUALS(static_cast<uint64_t>(3), cdrc.readAndAdvance<BigEndian<uint64_t>>()); + ASSERT_NOT_OK(cdrc.readAndAdvanceNoThrow<char>()); // test skip() cdrc = backup; - ASSERT_EQUALS(true, cdrc.skip<uint32_t>().isOK()); - ; - ASSERT_EQUALS(true, cdrc.advance(10).isOK()); - ASSERT_EQUALS(false, cdrc.readAndAdvance<char>().isOK()); + ASSERT_OK(cdrc.skipNoThrow<uint32_t>()); + ASSERT_OK(cdrc.advanceNoThrow(10)); + ASSERT_NOT_OK(cdrc.readAndAdvanceNoThrow<char>()); } TEST(DataRangeCursor, ConstDataRangeCursorType) { @@ -66,9 +64,7 @@ TEST(DataRangeCursor, ConstDataRangeCursorType) { ConstDataRangeCursor out(nullptr, nullptr); - auto inner = cdrc.readInto(&out); - - ASSERT_OK(inner); + ASSERT_OK(cdrc.readIntoNoThrow(&out)); ASSERT_EQUALS(buf, out.data()); } @@ -77,18 +73,17 @@ TEST(DataRangeCursor, DataRangeCursor) { DataRangeCursor dc(buf, buf + 14); - ASSERT_EQUALS(true, dc.writeAndAdvance<uint16_t>(1).isOK()); - ASSERT_EQUALS(true, dc.writeAndAdvance<LittleEndian<uint32_t>>(2).isOK()); - ASSERT_EQUALS(true, dc.writeAndAdvance<BigEndian<uint64_t>>(3).isOK()); - ASSERT_EQUALS(false, dc.writeAndAdvance<char>(1).isOK()); + ASSERT_OK(dc.writeAndAdvanceNoThrow<uint16_t>(1)); + ASSERT_OK(dc.writeAndAdvanceNoThrow<LittleEndian<uint32_t>>(2)); + ASSERT_OK(dc.writeAndAdvanceNoThrow<BigEndian<uint64_t>>(3)); + ASSERT_NOT_OK(dc.writeAndAdvanceNoThrow<char>(1)); ConstDataRangeCursor cdrc(buf, buf + sizeof(buf)); - ASSERT_EQUALS(static_cast<uint16_t>(1), cdrc.readAndAdvance<uint16_t>().getValue()); - ASSERT_EQUALS(static_cast<uint32_t>(2), - cdrc.readAndAdvance<LittleEndian<uint32_t>>().getValue()); - ASSERT_EQUALS(static_cast<uint64_t>(3), cdrc.readAndAdvance<BigEndian<uint64_t>>().getValue()); - ASSERT_EQUALS(static_cast<char>(0), cdrc.readAndAdvance<char>().getValue()); + ASSERT_EQUALS(static_cast<uint16_t>(1), cdrc.readAndAdvance<uint16_t>()); + ASSERT_EQUALS(static_cast<uint32_t>(2), cdrc.readAndAdvance<LittleEndian<uint32_t>>()); + ASSERT_EQUALS(static_cast<uint64_t>(3), cdrc.readAndAdvance<BigEndian<uint64_t>>()); + ASSERT_EQUALS(static_cast<char>(0), cdrc.readAndAdvance<char>()); } TEST(DataRangeCursor, DataRangeCursorType) { @@ -99,15 +94,12 @@ TEST(DataRangeCursor, DataRangeCursorType) { DataRangeCursor out(nullptr, nullptr); - Status status = drc.readInto(&out); - - ASSERT_OK(status); + ASSERT_OK(drc.readIntoNoThrow(&out)); ASSERT_EQUALS(buf, out.data()); drc = DataRangeCursor(buf2, buf2 + sizeof(buf2) + -1); - status = drc.write(out); + ASSERT_OK(drc.writeNoThrow(out)); - ASSERT_OK(status); ASSERT_EQUALS(std::string("fooZ"), buf2); } } // namespace mongo diff --git a/src/mongo/base/data_range_test.cpp b/src/mongo/base/data_range_test.cpp index 9c374768f2d..f45c6e26bbf 100644 --- a/src/mongo/base/data_range_test.cpp +++ b/src/mongo/base/data_range_test.cpp @@ -38,7 +38,7 @@ namespace mongo { TEST(DataRange, ConstDataRange) { - char buf[sizeof(uint32_t) * 3]; + unsigned char buf[sizeof(uint32_t) * 3]; uint32_t native = 1234; uint32_t le = endian::nativeToLittle(native); uint32_t be = endian::nativeToBig(native); @@ -49,11 +49,11 @@ TEST(DataRange, ConstDataRange) { ConstDataRange cdv(buf, buf + sizeof(buf)); - ASSERT_EQUALS(native, cdv.read<uint32_t>().getValue()); - ASSERT_EQUALS(native, cdv.read<LittleEndian<uint32_t>>(sizeof(uint32_t)).getValue()); - ASSERT_EQUALS(native, cdv.read<BigEndian<uint32_t>>(sizeof(uint32_t) * 2).getValue()); + ASSERT_EQUALS(native, cdv.read<uint32_t>()); + ASSERT_EQUALS(native, cdv.read<LittleEndian<uint32_t>>(sizeof(uint32_t))); + ASSERT_EQUALS(native, cdv.read<BigEndian<uint32_t>>(sizeof(uint32_t) * 2)); - auto result = cdv.read<uint32_t>(sizeof(uint32_t) * 3); + auto result = cdv.readNoThrow<uint32_t>(sizeof(uint32_t) * 3); ASSERT_EQUALS(false, result.isOK()); ASSERT_EQUALS(ErrorCodes::Overflow, result.getStatus().code()); } @@ -65,31 +65,31 @@ TEST(DataRange, ConstDataRangeType) { ConstDataRange out(nullptr, nullptr); - auto inner = cdr.readInto(&out); + auto inner = cdr.readIntoNoThrow(&out); ASSERT_OK(inner); ASSERT_EQUALS(buf, out.data()); } TEST(DataRange, DataRange) { - char buf[sizeof(uint32_t) * 3]; + uint8_t buf[sizeof(uint32_t) * 3]; uint32_t native = 1234; DataRange dv(buf, buf + sizeof(buf)); - ASSERT_EQUALS(true, dv.write(native).isOK()); - ASSERT_EQUALS(true, dv.write(LittleEndian<uint32_t>(native), sizeof(uint32_t)).isOK()); - ASSERT_EQUALS(true, dv.write(BigEndian<uint32_t>(native), sizeof(uint32_t) * 2).isOK()); + ASSERT_OK(dv.writeNoThrow(native)); + ASSERT_OK(dv.writeNoThrow(LittleEndian<uint32_t>(native), sizeof(uint32_t))); + ASSERT_OK(dv.writeNoThrow(BigEndian<uint32_t>(native), sizeof(uint32_t) * 2)); - auto result = dv.write(native, sizeof(uint32_t) * 3); - ASSERT_EQUALS(false, result.isOK()); + auto result = dv.writeNoThrow(native, sizeof(uint32_t) * 3); + ASSERT_NOT_OK(result); ASSERT_EQUALS(ErrorCodes::Overflow, result.code()); - ASSERT_EQUALS(native, dv.read<uint32_t>().getValue()); - ASSERT_EQUALS(native, dv.read<LittleEndian<uint32_t>>(sizeof(uint32_t)).getValue()); - ASSERT_EQUALS(native, dv.read<BigEndian<uint32_t>>(sizeof(uint32_t) * 2).getValue()); + ASSERT_EQUALS(native, dv.read<uint32_t>()); + ASSERT_EQUALS(native, dv.read<LittleEndian<uint32_t>>(sizeof(uint32_t))); + ASSERT_EQUALS(native, dv.read<BigEndian<uint32_t>>(sizeof(uint32_t) * 2)); - ASSERT_EQUALS(false, dv.read<uint32_t>(sizeof(uint32_t) * 3).isOK()); + ASSERT_NOT_OK(dv.readNoThrow<uint32_t>(sizeof(uint32_t) * 3)); } TEST(DataRange, DataRangeType) { @@ -100,16 +100,32 @@ TEST(DataRange, DataRangeType) { DataRange out(nullptr, nullptr); - Status status = dr.readInto(&out); + Status status = dr.readIntoNoThrow(&out); ASSERT_OK(status); ASSERT_EQUALS(buf, out.data()); dr = DataRange(buf2, buf2 + sizeof(buf2) + -1); - status = dr.write(out); + status = dr.writeNoThrow(out); ASSERT_OK(status); ASSERT_EQUALS(std::string("fooZ"), buf2); } +TEST(DataRange, InitFromContainer) { + std::vector<uint8_t> vec(20); + ConstDataRange dr(vec); + DataRange out(nullptr, nullptr); + + ASSERT_OK(dr.readIntoNoThrow(&out)); + + DataRange mutableDr(vec); + ASSERT_OK(mutableDr.writeNoThrow<int>(6)); + + std::array<char, 5> array; + DataRange arrDR(array); + auto status = arrDR.writeNoThrow<uint64_t>(std::numeric_limits<uint64_t>::max()); + ASSERT_EQUALS(status, ErrorCodes::Overflow); +} + } // namespace mongo diff --git a/src/mongo/base/data_type_string_data_test.cpp b/src/mongo/base/data_type_string_data_test.cpp index f88a581e6a2..b2fb8b29a50 100644 --- a/src/mongo/base/data_type_string_data_test.cpp +++ b/src/mongo/base/data_type_string_data_test.cpp @@ -45,9 +45,9 @@ TEST(DataTypeStringData, Basic) { { DataRangeCursor drc(buf, buf + sizeof(buf)); - ASSERT_OK(drc.writeAndAdvance(Terminated<'\0', StringData>(a))); - ASSERT_OK(drc.writeAndAdvance(Terminated<'\0', StringData>(b))); - ASSERT_OK(drc.writeAndAdvance(Terminated<'\0', StringData>(c))); + ASSERT_OK(drc.writeAndAdvanceNoThrow(Terminated<'\0', StringData>(a))); + ASSERT_OK(drc.writeAndAdvanceNoThrow(Terminated<'\0', StringData>(b))); + ASSERT_OK(drc.writeAndAdvanceNoThrow(Terminated<'\0', StringData>(c))); ASSERT_EQUALS(1 + 2 + 3 + 3, drc.data() - buf); } @@ -57,13 +57,13 @@ TEST(DataTypeStringData, Basic) { Terminated<'\0', StringData> tsd; - ASSERT_OK(cdrc.readAndAdvance(&tsd)); + ASSERT_OK(cdrc.readAndAdvanceNoThrow(&tsd)); ASSERT_EQUALS(a, tsd.value); - ASSERT_OK(cdrc.readAndAdvance(&tsd)); + ASSERT_OK(cdrc.readAndAdvanceNoThrow(&tsd)); ASSERT_EQUALS(b, tsd.value); - ASSERT_OK(cdrc.readAndAdvance(&tsd)); + ASSERT_OK(cdrc.readAndAdvanceNoThrow(&tsd)); ASSERT_EQUALS(c, tsd.value); } } diff --git a/src/mongo/base/data_type_terminated_test.cpp b/src/mongo/base/data_type_terminated_test.cpp index d5cf8d7967a..45635760c6e 100644 --- a/src/mongo/base/data_type_terminated_test.cpp +++ b/src/mongo/base/data_type_terminated_test.cpp @@ -229,7 +229,7 @@ TEST(DataTypeTerminated, ThroughDataRangeCursor) { auto buf_writer = DataRangeCursor(buf, buf + sizeof(buf)); for (const std::string& s : parts) { Terminated<'\0', ConstDataRange> tcdr(ConstDataRange(s.data(), s.data() + s.size())); - ASSERT_OK(buf_writer.writeAndAdvance(tcdr)); + ASSERT_OK(buf_writer.writeAndAdvanceNoThrow(tcdr)); } const auto written = std::string(static_cast<const char*>(buf), buf_writer.data()); ASSERT_EQUALS(written, serialized); @@ -238,7 +238,7 @@ TEST(DataTypeTerminated, ThroughDataRangeCursor) { auto buf_source = ConstDataRangeCursor(buf, buf + sizeof(buf)); for (const std::string& s : parts) { Terminated<'\0', ConstDataRange> tcdr; - ASSERT_OK(buf_source.readAndAdvance(&tcdr)); + ASSERT_OK(buf_source.readAndAdvanceNoThrow(&tcdr)); std::string read(tcdr.value.data(), tcdr.value.data() + tcdr.value.length()); ASSERT_EQUALS(s, read); } diff --git a/src/mongo/base/data_type_validated_test.cpp b/src/mongo/base/data_type_validated_test.cpp index 4ae8f4a58d4..e6e63f4592a 100644 --- a/src/mongo/base/data_type_validated_test.cpp +++ b/src/mongo/base/data_type_validated_test.cpp @@ -69,13 +69,13 @@ TEST(DataTypeValidated, SuccessfulValidation) { { DataRangeCursor drc(begin(buf), end(buf)); - ASSERT_OK(drc.writeAndAdvance(Validated<char>(0xFU))); + ASSERT_OK(drc.writeAndAdvanceNoThrow(Validated<char>(0xFU))); } { Validated<char> valid; ConstDataRangeCursor cdrc(begin(buf), end(buf)); - ASSERT_OK(cdrc.readAndAdvance(&valid)); + ASSERT_OK(cdrc.readAndAdvanceNoThrow(&valid)); ASSERT_EQUALS(valid.val, char{0xFU}); } } @@ -85,7 +85,7 @@ TEST(DataTypeValidated, FailedValidation) { { DataRangeCursor drc(begin(buf), end(buf)); - ASSERT_NOT_OK(drc.writeAndAdvance(Validated<char>(0x01))); + ASSERT_NOT_OK(drc.writeAndAdvanceNoThrow(Validated<char>(0x01))); } buf[0] = char{0x01}; @@ -93,7 +93,7 @@ TEST(DataTypeValidated, FailedValidation) { { Validated<char> valid; ConstDataRangeCursor cdrc(begin(buf), end(buf)); - ASSERT_NOT_OK(cdrc.readAndAdvance(&valid)); + ASSERT_NOT_OK(cdrc.readAndAdvanceNoThrow(&valid)); } } diff --git a/src/mongo/bson/bson_obj_data_type_test.cpp b/src/mongo/bson/bson_obj_data_type_test.cpp index e69d4bf3a6a..d0073d58f62 100644 --- a/src/mongo/bson/bson_obj_data_type_test.cpp +++ b/src/mongo/bson/bson_obj_data_type_test.cpp @@ -45,26 +45,26 @@ TEST(BSONObjDataType, ConstDataTypeRangeBSON) { BSONObjBuilder b; b.append("a", 1); - ASSERT_OK(drc.writeAndAdvance(b.obj())); + ASSERT_OK(drc.writeAndAdvanceNoThrow(b.obj())); } { BSONObjBuilder b; b.append("b", "fooo"); - ASSERT_OK(drc.writeAndAdvance(b.obj())); + ASSERT_OK(drc.writeAndAdvanceNoThrow(b.obj())); } { BSONObjBuilder b; b.append("c", 3); - ASSERT_OK(drc.writeAndAdvance(b.obj())); + ASSERT_OK(drc.writeAndAdvanceNoThrow(b.obj())); } ConstDataRangeCursor cdrc(buf, buf + sizeof(buf)); - ASSERT_EQUALS(1, cdrc.readAndAdvance<BSONObj>().getValue().getField("a").numberInt()); - ASSERT_EQUALS("fooo", cdrc.readAndAdvance<BSONObj>().getValue().getField("b").str()); - ASSERT_EQUALS(3, cdrc.readAndAdvance<BSONObj>().getValue().getField("c").numberInt()); + ASSERT_EQUALS(1, cdrc.readAndAdvance<BSONObj>().getField("a").numberInt()); + ASSERT_EQUALS("fooo", cdrc.readAndAdvance<BSONObj>().getField("b").str()); + ASSERT_EQUALS(3, cdrc.readAndAdvance<BSONObj>().getField("c").numberInt()); } } // namespace mongo diff --git a/src/mongo/bson/bson_obj_test.cpp b/src/mongo/bson/bson_obj_test.cpp index a061d67f67b..e966af9d559 100644 --- a/src/mongo/bson/bson_obj_test.cpp +++ b/src/mongo/bson/bson_obj_test.cpp @@ -689,7 +689,7 @@ TEST(BSONObj, sizeChecks) { auto generateBuffer = [](std::int32_t size) { std::vector<char> buffer(size); DataRange bufferRange(&buffer.front(), &buffer.back()); - ASSERT_OK(bufferRange.write(LittleEndian<int32_t>(size))); + ASSERT_OK(bufferRange.writeNoThrow(LittleEndian<int32_t>(size))); return buffer; }; diff --git a/src/mongo/bson/bsonelement_test.cpp b/src/mongo/bson/bsonelement_test.cpp index d8f311450fd..8901ab4339a 100644 --- a/src/mongo/bson/bsonelement_test.cpp +++ b/src/mongo/bson/bsonelement_test.cpp @@ -113,7 +113,7 @@ TEST(BSONElement, ExtractLargeSubObject) { std::int32_t size = 17 * 1024 * 1024; std::vector<char> buffer(size); DataRange bufferRange(&buffer.front(), &buffer.back()); - ASSERT_OK(bufferRange.write(LittleEndian<int32_t>(size))); + ASSERT_OK(bufferRange.writeNoThrow(LittleEndian<int32_t>(size))); BSONObj obj(buffer.data(), BSONObj::LargeSizeTrait{}); diff --git a/src/mongo/bson/bsonobjbuilder_test.cpp b/src/mongo/bson/bsonobjbuilder_test.cpp index e174a337a27..a2522d04b58 100644 --- a/src/mongo/bson/bsonobjbuilder_test.cpp +++ b/src/mongo/bson/bsonobjbuilder_test.cpp @@ -451,7 +451,7 @@ TEST(BSONObjBuilderTest, SizeChecks) { auto generateBuffer = [](std::int32_t size) { std::vector<char> buffer(size); DataRange bufferRange(&buffer.front(), &buffer.back()); - ASSERT_OK(bufferRange.write(LittleEndian<int32_t>(size))); + ASSERT_OK(bufferRange.writeNoThrow(LittleEndian<int32_t>(size))); return buffer; }; diff --git a/src/mongo/bson/oid.h b/src/mongo/bson/oid.h index 2c9da565fa1..d5df0c90ee9 100644 --- a/src/mongo/bson/oid.h +++ b/src/mongo/bson/oid.h @@ -226,7 +226,7 @@ public: } ConstDataRange toCDR() const { - return ConstDataRange(_data, kOIDSize); + return ConstDataRange(_data); } private: diff --git a/src/mongo/crypto/sha_block.h b/src/mongo/crypto/sha_block.h index e9a0ce338b9..2d2c3684e07 100644 --- a/src/mongo/crypto/sha_block.h +++ b/src/mongo/crypto/sha_block.h @@ -88,7 +88,7 @@ public: * Computes a hash of 'input' from one buffer. */ static SHABlock computeHash(const uint8_t* input, size_t inputLen) { - return computeHash({ConstDataRange(reinterpret_cast<const char*>(input), inputLen)}); + return computeHash({ConstDataRange(input, inputLen)}); } /** @@ -99,8 +99,7 @@ public: const uint8_t* input, size_t inputLen) { SHABlock output; - SHABlock::computeHmac( - key, keyLen, {ConstDataRange(reinterpret_cast<const char*>(input), inputLen)}, &output); + SHABlock::computeHmac(key, keyLen, {ConstDataRange(input, inputLen)}, &output); return output; } @@ -113,8 +112,7 @@ public: const uint8_t* input, size_t inputLen, SHABlock* const output) { - SHABlock::computeHmac( - key, keyLen, {ConstDataRange(reinterpret_cast<const char*>(input), inputLen)}, output); + SHABlock::computeHmac(key, keyLen, {ConstDataRange(input, inputLen)}, output); } static SHABlock computeHmac(const uint8_t* key, diff --git a/src/mongo/db/free_mon/free_mon_controller_test.cpp b/src/mongo/db/free_mon/free_mon_controller_test.cpp index 30473d27be4..892fbb7afa2 100644 --- a/src/mongo/db/free_mon/free_mon_controller_test.cpp +++ b/src/mongo/db/free_mon/free_mon_controller_test.cpp @@ -141,7 +141,7 @@ BSONArray decompressMetrics(ConstDataRange cdr) { snappy::Uncompress(cdr.data(), cdr.length(), &outBuffer); ConstDataRange raw(outBuffer.data(), outBuffer.data() + outBuffer.size()); - auto swObj = raw.read<Validated<BSONObj>>(); + auto swObj = raw.readNoThrow<Validated<BSONObj>>(); ASSERT_OK(swObj.getStatus()); return BSONArray(swObj.getValue().val["data"].Obj().getOwned()); diff --git a/src/mongo/db/free_mon/free_mon_mongod.cpp b/src/mongo/db/free_mon/free_mon_mongod.cpp index b8988ad53ea..9b86d33258f 100644 --- a/src/mongo/db/free_mon/free_mon_mongod.cpp +++ b/src/mongo/db/free_mon/free_mon_mongod.cpp @@ -113,10 +113,7 @@ public: auto blobSize = blob.size(); auto blobData = blob.release(); ConstDataRange cdr(blobData.get(), blobSize); - auto swDoc = cdr.read<Validated<BSONObj>>(); - uassertStatusOK(swDoc.getStatus()); - - BSONObj respObj(swDoc.getValue()); + BSONObj respObj = cdr.read<Validated<BSONObj>>(); auto resp = FreeMonRegistrationResponse::parse(IDLParserErrorContext("response"), respObj); @@ -140,10 +137,7 @@ public: auto blobData = blob.release(); ConstDataRange cdr(blobData.get(), blobSize); - auto swDoc = cdr.read<Validated<BSONObj>>(); - uassertStatusOK(swDoc.getStatus()); - - BSONObj respObj(swDoc.getValue()); + BSONObj respObj = cdr.read<Validated<BSONObj>>(); auto resp = FreeMonMetricsResponse::parse(IDLParserErrorContext("response"), respObj); @@ -160,7 +154,7 @@ private: auto status = _executor->scheduleWork( [ promise = std::move(pf.promise), url = std::move(url), data = std::move(data), this ]( const executor::TaskExecutor::CallbackArgs& cbArgs) mutable { - ConstDataRange cdr(reinterpret_cast<char*>(data->data()), data->size()); + ConstDataRange cdr(data->data(), data->size()); try { auto result = this->_client->post(url, cdr); promise.emplaceValue(std::move(result)); diff --git a/src/mongo/db/ftdc/block_compressor.cpp b/src/mongo/db/ftdc/block_compressor.cpp index c128dcf115e..ed7b80d5e4b 100644 --- a/src/mongo/db/ftdc/block_compressor.cpp +++ b/src/mongo/db/ftdc/block_compressor.cpp @@ -74,7 +74,7 @@ StatusWith<ConstDataRange> BlockCompressor::compress(ConstDataRange source) { return {ErrorCodes::ZLibError, str::stream() << "deflateEnd failed with " << err}; } - return ConstDataRange(reinterpret_cast<char*>(_buffer.data()), stream.total_out); + return ConstDataRange(_buffer.data(), stream.total_out); } StatusWith<ConstDataRange> BlockCompressor::uncompress(ConstDataRange source, @@ -112,7 +112,7 @@ StatusWith<ConstDataRange> BlockCompressor::uncompress(ConstDataRange source, return {ErrorCodes::ZLibError, str::stream() << "inflateEnd failed with " << err}; } - return ConstDataRange(reinterpret_cast<char*>(_buffer.data()), stream.total_out); + return ConstDataRange(_buffer.data(), stream.total_out); } } // namespace mongo diff --git a/src/mongo/db/ftdc/decompressor.cpp b/src/mongo/db/ftdc/decompressor.cpp index 3bba4db89e4..363064df5e6 100644 --- a/src/mongo/db/ftdc/decompressor.cpp +++ b/src/mongo/db/ftdc/decompressor.cpp @@ -42,19 +42,14 @@ namespace mongo { -StatusWith<std::vector<BSONObj>> FTDCDecompressor::uncompress(ConstDataRange buf) { +StatusWith<std::vector<BSONObj>> FTDCDecompressor::uncompress(ConstDataRange buf) try { ConstDataRangeCursor compressedDataRange(buf); // Read the length of the uncompressed buffer - auto swUncompressedLength = compressedDataRange.readAndAdvance<LittleEndian<std::uint32_t>>(); - if (!swUncompressedLength.isOK()) { - return {swUncompressedLength.getStatus()}; - } + auto uncompressedLength = compressedDataRange.readAndAdvance<LittleEndian<std::uint32_t>>(); // Now uncompress the data // Limit size of the buffer we need zlib - auto uncompressedLength = swUncompressedLength.getValue(); - if (uncompressedLength > 10000000) { return Status(ErrorCodes::InvalidLength, "Metrics chunk has exceeded the allowable size."); } @@ -68,28 +63,13 @@ StatusWith<std::vector<BSONObj>> FTDCDecompressor::uncompress(ConstDataRange buf ConstDataRangeCursor cdc = statusUncompress.getValue(); // The document is not part of any checksum so we must validate it is correct - auto swRef = cdc.readAndAdvance<Validated<BSONObj>>(); - if (!swRef.isOK()) { - return {swRef.getStatus()}; - } - - BSONObj ref = swRef.getValue(); + BSONObj ref = cdc.readAndAdvance<Validated<BSONObj>>(); // Read count of metrics - auto swMetricsCount = cdc.readAndAdvance<LittleEndian<std::uint32_t>>(); - if (!swMetricsCount.isOK()) { - return {swMetricsCount.getStatus()}; - } - - std::uint32_t metricsCount = swMetricsCount.getValue(); + auto metricsCount = cdc.readAndAdvance<LittleEndian<std::uint32_t>>(); // Read count of samples - auto swSampleCount = cdc.readAndAdvance<LittleEndian<std::uint32_t>>(); - if (!swSampleCount.isOK()) { - return {swSampleCount.getStatus()}; - } - - std::uint32_t sampleCount = swSampleCount.getValue(); + auto sampleCount = cdc.readAndAdvance<LittleEndian<std::uint32_t>>(); // Limit size of the buffer we need for metrics and samples if (metricsCount * sampleCount > 1000000) { @@ -138,23 +118,13 @@ StatusWith<std::vector<BSONObj>> FTDCDecompressor::uncompress(ConstDataRange buf continue; } - auto swDelta = cdrc.readAndAdvance<FTDCVarInt>(); - - if (!swDelta.isOK()) { - return swDelta.getStatus(); - } - - if (swDelta.getValue() == 0) { - auto swZero = cdrc.readAndAdvance<FTDCVarInt>(); - - if (!swZero.isOK()) { - return swDelta.getStatus(); - } - - zeroesCount = swZero.getValue(); + auto delta = cdrc.readAndAdvance<FTDCVarInt>(); + if (delta == 0) { + auto zero = cdrc.readAndAdvance<FTDCVarInt>(); + zeroesCount = zero; } - deltas[FTDCCompressor::getArrayOffset(sampleCount, j, i)] = swDelta.getValue(); + deltas[FTDCCompressor::getArrayOffset(sampleCount, j, i)] = delta; } } @@ -179,6 +149,8 @@ StatusWith<std::vector<BSONObj>> FTDCDecompressor::uncompress(ConstDataRange buf } return {docs}; +} catch (const DBException& e) { + return e.toStatus(); } } // namespace mongo diff --git a/src/mongo/db/ftdc/file_reader.cpp b/src/mongo/db/ftdc/file_reader.cpp index 9a1ada5628a..a5851680a49 100644 --- a/src/mongo/db/ftdc/file_reader.cpp +++ b/src/mongo/db/ftdc/file_reader.cpp @@ -202,7 +202,7 @@ StatusWith<BSONObj> FTDCFileReader::readDocument() { ConstDataRange cdr(_buffer.data(), _buffer.data() + bsonLength); // TODO: Validated only validates objects based on a flag which is the default at the moment - auto swl = cdr.read<Validated<BSONObj>>(); + auto swl = cdr.readNoThrow<Validated<BSONObj>>(); if (!swl.isOK()) { return swl.getStatus(); } diff --git a/src/mongo/db/logical_session_id_test.cpp b/src/mongo/db/logical_session_id_test.cpp index ad74f23d15c..d994458332f 100644 --- a/src/mongo/db/logical_session_id_test.cpp +++ b/src/mongo/db/logical_session_id_test.cpp @@ -381,7 +381,7 @@ TEST_F(LogicalSessionIdTest, InitializeOperationSessionInfo_VerifyUIDEvenIfDoNot LogicalSessionFromClient lsid; lsid.setId(UUID::gen()); - auto invalidDigest = SHA256Block::computeHash({ConstDataRange("hacker", 6)}); + auto invalidDigest = SHA256Block::computeHash({ConstDataRange("hacker")}); lsid.setUid(invalidDigest); ASSERT_THROWS_CODE(initializeOperationSessionInfo( diff --git a/src/mongo/db/storage/storage_engine_metadata.cpp b/src/mongo/db/storage/storage_engine_metadata.cpp index c461ca7a9a4..d48eb365625 100644 --- a/src/mongo/db/storage/storage_engine_metadata.cpp +++ b/src/mongo/db/storage/storage_engine_metadata.cpp @@ -176,7 +176,7 @@ Status StorageEngineMetadata::read() { } ConstDataRange cdr(&buffer[0], buffer.size()); - auto swObj = cdr.read<Validated<BSONObj>>(); + auto swObj = cdr.readNoThrow<Validated<BSONObj>>(); if (!swObj.isOK()) { return swObj.getStatus(); } diff --git a/src/mongo/db/traffic_reader.cpp b/src/mongo/db/traffic_reader.cpp index 2b2dcc9a732..7ce7ddcb112 100644 --- a/src/mongo/db/traffic_reader.cpp +++ b/src/mongo/db/traffic_reader.cpp @@ -122,12 +122,12 @@ boost::optional<TrafficReaderPacket> readPacket(char* buf, int fd) { ConstDataRangeCursor cdr(buf, buf + len); // Read the packet - uassertStatusOK(cdr.skip<LittleEndian<uint32_t>>()); - uint64_t id = uassertStatusOK(cdr.readAndAdvance<LittleEndian<uint64_t>>()); - StringData local = uassertStatusOK(cdr.readAndAdvance<Terminated<'\0', StringData>>()); - StringData remote = uassertStatusOK(cdr.readAndAdvance<Terminated<'\0', StringData>>()); - uint64_t date = uassertStatusOK(cdr.readAndAdvance<LittleEndian<uint64_t>>()); - uint64_t order = uassertStatusOK(cdr.readAndAdvance<LittleEndian<uint64_t>>()); + cdr.skip<LittleEndian<uint32_t>>(); + uint64_t id = cdr.readAndAdvance<LittleEndian<uint64_t>>(); + StringData local = cdr.readAndAdvance<Terminated<'\0', StringData>>(); + StringData remote = cdr.readAndAdvance<Terminated<'\0', StringData>>(); + uint64_t date = cdr.readAndAdvance<LittleEndian<uint64_t>>(); + uint64_t order = cdr.readAndAdvance<LittleEndian<uint64_t>>(); MsgData::ConstView message(cdr.data()); return TrafficReaderPacket{ diff --git a/src/mongo/db/traffic_recorder.cpp b/src/mongo/db/traffic_recorder.cpp index 8e73b91d37a..320f1f2b6ff 100644 --- a/src/mongo/db/traffic_recorder.cpp +++ b/src/mongo/db/traffic_recorder.cpp @@ -130,7 +130,7 @@ public: uassertStatusOK(db.writeAndAdvance<LittleEndian<uint64_t>>(packet.order)); auto size = db.size() + toWrite.size(); - uassertStatusOK(db.getCursor().write<LittleEndian<uint32_t>>(size)); + db.getCursor().write<LittleEndian<uint32_t>>(size); { stdx::lock_guard<stdx::mutex> lk(_mutex); diff --git a/src/mongo/idl/idl_parser.cpp b/src/mongo/idl/idl_parser.cpp index bd189ee3631..a55de9283f6 100644 --- a/src/mongo/idl/idl_parser.cpp +++ b/src/mongo/idl/idl_parser.cpp @@ -297,7 +297,7 @@ std::vector<ConstDataRange> transformVector(const std::vector<std::vector<std::u output.reserve(input.size()); std::transform(begin(input), end(input), std::back_inserter(output), [](auto&& vec) { - return makeCDR(vec); + return ConstDataRange(vec); }); return output; diff --git a/src/mongo/idl/idl_parser.h b/src/mongo/idl/idl_parser.h index b250c22f551..32a3f83b1af 100644 --- a/src/mongo/idl/idl_parser.h +++ b/src/mongo/idl/idl_parser.h @@ -239,15 +239,4 @@ std::vector<std::string> transformVector(const std::vector<StringData>& input); std::vector<ConstDataRange> transformVector(const std::vector<std::vector<std::uint8_t>>& input); std::vector<std::vector<std::uint8_t>> transformVector(const std::vector<ConstDataRange>& input); -/** - * Get a ConstDataRange from a vector or an array of bytes. - */ -inline ConstDataRange makeCDR(const std::vector<uint8_t>& value) { - return ConstDataRange(reinterpret_cast<const char*>(value.data()), value.size()); -} - -inline ConstDataRange makeCDR(const std::array<uint8_t, 16>& value) { - return ConstDataRange(reinterpret_cast<const char*>(value.data()), value.size()); -} - } // namespace mongo diff --git a/src/mongo/idl/idl_test.cpp b/src/mongo/idl/idl_test.cpp index 3663d2b137c..ab998137bf5 100644 --- a/src/mongo/idl/idl_test.cpp +++ b/src/mongo/idl/idl_test.cpp @@ -45,7 +45,7 @@ namespace mongo { namespace { bool isEquals(ConstDataRange left, const std::vector<uint8_t>& right) { - auto rightCDR = makeCDR(right); + ConstDataRange rightCDR(right); return std::equal(left.data(), left.data() + left.length(), rightCDR.data(), @@ -1157,7 +1157,7 @@ void TestBinDataVector() { { BSONObjBuilder builder; ParserT one_new; - one_new.setValue(makeCDR(expected)); + one_new.setValue(expected); testStruct.serialize(&builder); auto serializedDoc = builder.obj(); diff --git a/src/mongo/idl/idl_test_types.h b/src/mongo/idl/idl_test_types.h index 48a41e4383d..990d3d64406 100644 --- a/src/mongo/idl/idl_test_types.h +++ b/src/mongo/idl/idl_test_types.h @@ -103,7 +103,7 @@ public: } ConstDataRange serializeToBSON() const { - return makeCDR(_vec); + return ConstDataRange(_vec); } const std::vector<std::uint8_t>& getVector() const { diff --git a/src/mongo/rpc/object_check_test.cpp b/src/mongo/rpc/object_check_test.cpp index ee187286a20..4006db63bc1 100644 --- a/src/mongo/rpc/object_check_test.cpp +++ b/src/mongo/rpc/object_check_test.cpp @@ -62,22 +62,22 @@ TEST(DataTypeValidated, BSONValidationEnabled) { { Validated<BSONObj> v; ConstDataRangeCursor cdrc(begin(buf), end(buf)); - ASSERT_OK(cdrc.readAndAdvance(&v)); + ASSERT_OK(cdrc.readAndAdvanceNoThrow(&v)); } { // mess up the data DataRangeCursor drc(begin(buf), end(buf)); // skip past size so we don't trip any sanity checks. - drc.advance(4).transitional_ignore(); // skip size - while (drc.writeAndAdvance(0xFF).isOK()) + drc.advanceNoThrow(4).transitional_ignore(); // skip size + while (drc.writeAndAdvanceNoThrow(0xFF).isOK()) ; } { Validated<BSONObj> v; ConstDataRangeCursor cdrc(begin(buf), end(buf)); - ASSERT_NOT_OK(cdrc.readAndAdvance(&v)); + ASSERT_NOT_OK(cdrc.readAndAdvanceNoThrow(&v)); } { @@ -85,7 +85,7 @@ TEST(DataTypeValidated, BSONValidationEnabled) { setValidation(false); Validated<BSONObj> v; ConstDataRangeCursor cdrc(begin(buf), end(buf)); - ASSERT_OK(cdrc.readAndAdvance(&v)); + ASSERT_OK(cdrc.readAndAdvanceNoThrow(&v)); } } } diff --git a/src/mongo/transport/asio_utils.h b/src/mongo/transport/asio_utils.h index e44a43438cd..4bfbaee7e59 100644 --- a/src/mongo/transport/asio_utils.h +++ b/src/mongo/transport/asio_utils.h @@ -236,32 +236,33 @@ boost::optional<std::array<std::uint8_t, 7>> checkTLSRequest(const Buffer& buffe // Parse the record header. // Extract the ContentType from the header, and ensure it is a handshake. - StatusWith<std::uint8_t> record_ContentType = cdr.readAndAdvance<std::uint8_t>(); + StatusWith<std::uint8_t> record_ContentType = cdr.readAndAdvanceNoThrow<std::uint8_t>(); if (!record_ContentType.isOK() || record_ContentType.getValue() != ContentType_handshake) { return boost::none; } // Skip the record's ProtocolVersion. Clients tend to send TLS 1.0 in // the record, but then their real protocol version in the enclosed ClientHello. - StatusWith<ProtocolVersion> record_protocol_version = cdr.readAndAdvance<ProtocolVersion>(); + StatusWith<ProtocolVersion> record_protocol_version = + cdr.readAndAdvanceNoThrow<ProtocolVersion>(); if (!record_protocol_version.isOK()) { return boost::none; } // Parse the record length. It should be be larger than the remaining expected payload. - auto record_length = cdr.readAndAdvance<BigEndian<std::uint16_t>>(); + auto record_length = cdr.readAndAdvanceNoThrow<BigEndian<std::uint16_t>>(); if (!record_length.isOK() || record_length.getValue() < cdr.length()) { return boost::none; } // Parse the handshake header. // Extract the HandshakeType, and ensure it is a ClientHello. - StatusWith<std::uint8_t> handshake_type = cdr.readAndAdvance<std::uint8_t>(); + StatusWith<std::uint8_t> handshake_type = cdr.readAndAdvanceNoThrow<std::uint8_t>(); if (!handshake_type.isOK() || handshake_type.getValue() != HandshakeType_client_hello) { return boost::none; } // Extract the handshake length, and ensure it is larger than the remaining expected // payload. This requires a little work because the packet represents it with a uint24_t. StatusWith<std::array<std::uint8_t, 3>> handshake_length_bytes = - cdr.readAndAdvance<std::array<std::uint8_t, 3>>(); + cdr.readAndAdvanceNoThrow<std::array<std::uint8_t, 3>>(); if (!handshake_length_bytes.isOK()) { return boost::none; } @@ -273,7 +274,7 @@ boost::optional<std::array<std::uint8_t, 7>> checkTLSRequest(const Buffer& buffe if (handshake_length < cdr.length()) { return boost::none; } - StatusWith<ProtocolVersion> client_version = cdr.readAndAdvance<ProtocolVersion>(); + StatusWith<ProtocolVersion> client_version = cdr.readAndAdvanceNoThrow<ProtocolVersion>(); if (!client_version.isOK()) { return boost::none; } diff --git a/src/mongo/transport/message_compressor_manager.cpp b/src/mongo/transport/message_compressor_manager.cpp index aeeeb2c0beb..a78667c31b6 100644 --- a/src/mongo/transport/message_compressor_manager.cpp +++ b/src/mongo/transport/message_compressor_manager.cpp @@ -53,31 +53,23 @@ struct CompressionHeader { uint8_t compressorId; void serialize(DataRangeCursor* cursor) { - uassertStatusOK(cursor->writeAndAdvance<LittleEndian<int32_t>>(originalOpCode)); - uassertStatusOK(cursor->writeAndAdvance<LittleEndian<int32_t>>(uncompressedSize)); - uassertStatusOK(cursor->writeAndAdvance<LittleEndian<uint8_t>>(compressorId)); + cursor->writeAndAdvance<LittleEndian<int32_t>>(originalOpCode); + cursor->writeAndAdvance<LittleEndian<int32_t>>(uncompressedSize); + cursor->writeAndAdvance<LittleEndian<uint8_t>>(compressorId); } CompressionHeader(int32_t _opcode, int32_t _size, uint8_t _id) : originalOpCode{_opcode}, uncompressedSize{_size}, compressorId{_id} {} CompressionHeader(ConstDataRangeCursor* cursor) { - originalOpCode = _readWithChecking<LittleEndian<std::int32_t>>(cursor); - uncompressedSize = _readWithChecking<LittleEndian<std::int32_t>>(cursor); - compressorId = _readWithChecking<LittleEndian<uint8_t>>(cursor); + originalOpCode = cursor->readAndAdvance<LittleEndian<std::int32_t>>(); + uncompressedSize = cursor->readAndAdvance<LittleEndian<std::int32_t>>(); + compressorId = cursor->readAndAdvance<LittleEndian<uint8_t>>(); } static size_t size() { return sizeof(originalOpCode) + sizeof(uncompressedSize) + sizeof(compressorId); } - -private: - template <typename T> - T _readWithChecking(ConstDataRangeCursor* cursor) { - auto sw = cursor->readAndAdvance<T>(); - uassertStatusOK(sw.getStatus()); - return sw.getValue(); - } }; const transport::Session::Decoration<MessageCompressorManager> getForSession = diff --git a/src/mongo/transport/message_compressor_manager_test.cpp b/src/mongo/transport/message_compressor_manager_test.cpp index 1418af12272..bffde5f9c29 100644 --- a/src/mongo/transport/message_compressor_manager_test.cpp +++ b/src/mongo/transport/message_compressor_manager_test.cpp @@ -334,10 +334,9 @@ TEST(MessageCompressorManager, MessageSizeTooLarge) { badMessage.setLen(128); DataRangeCursor cursor(badMessage.data(), badMessage.data() + badMessage.dataLen()); - uassertStatusOK(cursor.writeAndAdvance<LittleEndian<int32_t>>(dbQuery)); - uassertStatusOK(cursor.writeAndAdvance<LittleEndian<int32_t>>(MaxMessageSizeBytes + 1)); - uassertStatusOK( - cursor.writeAndAdvance<LittleEndian<uint8_t>>(registry.getCompressor("noop")->getId())); + cursor.writeAndAdvance<LittleEndian<int32_t>>(dbQuery); + cursor.writeAndAdvance<LittleEndian<int32_t>>(MaxMessageSizeBytes + 1); + cursor.writeAndAdvance<LittleEndian<uint8_t>>(registry.getCompressor("noop")->getId()); auto status = compManager.decompressMessage(Message(badMessageBuffer), nullptr).getStatus(); ASSERT_NOT_OK(status); @@ -357,8 +356,8 @@ TEST(MessageCompressorManager, RuntMessage) { // This is a totally bogus compression header of just the orginal opcode + 0 byte uncompressed // size DataRangeCursor cursor(badMessage.data(), badMessage.data() + badMessage.dataLen()); - uassertStatusOK(cursor.writeAndAdvance<LittleEndian<int32_t>>(dbQuery)); - uassertStatusOK(cursor.writeAndAdvance<LittleEndian<int32_t>>(0)); + cursor.writeAndAdvance<LittleEndian<int32_t>>(dbQuery); + cursor.writeAndAdvance<LittleEndian<int32_t>>(0); auto status = compManager.decompressMessage(Message(badMessageBuffer), nullptr).getStatus(); ASSERT_NOT_OK(status); diff --git a/src/mongo/transport/message_compressor_noop.h b/src/mongo/transport/message_compressor_noop.h index 3eeb08f83b3..e8f07560d64 100644 --- a/src/mongo/transport/message_compressor_noop.h +++ b/src/mongo/transport/message_compressor_noop.h @@ -39,16 +39,20 @@ public: return inputSize; } - StatusWith<std::size_t> compressData(ConstDataRange input, DataRange output) override { - output.write(input).transitional_ignore(); + StatusWith<std::size_t> compressData(ConstDataRange input, DataRange output) override try { + output.write(input); counterHitCompress(input.length(), input.length()); return {input.length()}; + } catch (const DBException& e) { + return e.toStatus(); } - StatusWith<std::size_t> decompressData(ConstDataRange input, DataRange output) override { - output.write(input).transitional_ignore(); + StatusWith<std::size_t> decompressData(ConstDataRange input, DataRange output) override try { + output.write(input); counterHitDecompress(input.length(), input.length()); return {input.length()}; + } catch (const DBException& e) { + return e.toStatus(); } }; } // namespace mongo diff --git a/src/mongo/util/bufreader.h b/src/mongo/util/bufreader.h index 42ff79af60c..90e270e7f90 100644 --- a/src/mongo/util/bufreader.h +++ b/src/mongo/util/bufreader.h @@ -64,7 +64,7 @@ public: template <typename T> void read(T& t) { ConstDataRangeCursor cdrc(_pos, _end); - uassertStatusOK(cdrc.readAndAdvance(&t)); + cdrc.readAndAdvance(&t); _pos = cdrc.data(); } @@ -79,7 +79,7 @@ public: /** read in the object specified, but do not advance buffer pointer */ template <typename T> void peek(T& t) const { - uassertStatusOK(ConstDataRange(_pos, _end).readInto(&t)); + ConstDataRange(_pos, _end).readInto(&t); } /** read in and return an object of the specified type, but do not advance buffer pointer */ @@ -109,7 +109,7 @@ public: /** return current position pointer, and advance by len */ const void* skip(unsigned len) { ConstDataRangeCursor cdrc(_pos, _end); - uassertStatusOK(cdrc.advance(len)); + cdrc.advance(len); return std::exchange(_pos, cdrc.data()); } diff --git a/src/mongo/util/net/http_client_curl.cpp b/src/mongo/util/net/http_client_curl.cpp index b96018cbb01..1f3e6534dbe 100644 --- a/src/mongo/util/net/http_client_curl.cpp +++ b/src/mongo/util/net/http_client_curl.cpp @@ -174,7 +174,7 @@ size_t ReadMemoryCallback(char* buffer, size_t size, size_t nitems, void* instre if (cdrc->length() > 0) { size_t readSize = std::min(size * nitems, cdrc->length()); memcpy(buffer, cdrc->data(), readSize); - invariant(cdrc->advance(readSize).isOK()); + invariant(cdrc->advanceNoThrow(readSize).isOK()); ret = readSize; } diff --git a/src/mongo/util/net/ssl_manager.cpp b/src/mongo/util/net/ssl_manager.cpp index 85fc0ccd36b..d1b0cfe96df 100644 --- a/src/mongo/util/net/ssl_manager.cpp +++ b/src/mongo/util/net/ssl_manager.cpp @@ -825,7 +825,7 @@ struct DataType::Handler<DERToken> { namespace { StatusWith<std::string> readDERString(ConstDataRangeCursor& cdc) { - auto swString = cdc.readAndAdvance<DERToken>(); + auto swString = cdc.readAndAdvanceNoThrow<DERToken>(); if (!swString.isOK()) { return swString.getStatus(); } @@ -849,7 +849,7 @@ StatusWith<DERToken> DERToken::parse(ConstDataRange cdr, size_t* outLength) { ConstDataRangeCursor cdrc(cdr); - auto swTagByte = cdrc.readAndAdvance<char>(); + auto swTagByte = cdrc.readAndAdvanceNoThrow<char>(); if (!swTagByte.getStatus().isOK()) { return swTagByte.getStatus(); } @@ -893,7 +893,7 @@ StatusWith<DERToken> DERToken::parse(ConstDataRange cdr, size_t* outLength) { // Read length // Depending on the high bit, either read 1 byte or N bytes - auto swInitialLengthByte = cdrc.readAndAdvance<char>(); + auto swInitialLengthByte = cdrc.readAndAdvanceNoThrow<char>(); if (!swInitialLengthByte.getStatus().isOK()) { return swInitialLengthByte.getStatus(); } @@ -918,7 +918,7 @@ StatusWith<DERToken> DERToken::parse(ConstDataRange cdr, size_t* outLength) { // Ensure we have enough data for the length bytes const char* lengthLongFormPtr = cdrc.data(); - Status statusLength = cdrc.advance(lengthBytesCount); + Status statusLength = cdrc.advanceNoThrow(lengthBytesCount); if (!statusLength.isOK()) { return statusLength; } @@ -965,7 +965,7 @@ StatusWith<stdx::unordered_set<RoleName>> parsePeerRoles(ConstDataRange cdrExten * ...!UTF8String:"Unrecognized entity in MongoDBAuthorizationGrant" * } */ - auto swSet = cdcExtension.readAndAdvance<DERToken>(); + auto swSet = cdcExtension.readAndAdvanceNoThrow<DERToken>(); if (!swSet.isOK()) { return swSet.getStatus(); } @@ -986,7 +986,7 @@ StatusWith<stdx::unordered_set<RoleName>> parsePeerRoles(ConstDataRange cdrExten * database UTF8String * } */ - auto swSequence = cdcSet.readAndAdvance<DERToken>(); + auto swSequence = cdcSet.readAndAdvanceNoThrow<DERToken>(); if (!swSequence.isOK()) { return swSequence.getStatus(); } diff --git a/src/mongo/util/net/ssl_manager_test.cpp b/src/mongo/util/net/ssl_manager_test.cpp index 0614c41445e..acf4d04e438 100644 --- a/src/mongo/util/net/ssl_manager_test.cpp +++ b/src/mongo/util/net/ssl_manager_test.cpp @@ -114,8 +114,7 @@ TEST(SSLManager, MongoDBRolesParser) { unsigned char derData[] = {0x31, 0x1a, 0x30, 0x18, 0x0c, 0x09, 0x72, 0x6f, 0x6c, 0x65, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x0c, 0x0b, 0x54, 0x68, 0x69, 0x72, 0x64, 0x20, 0x66, 0x69, 0x65, 0x6c, 0x64}; - auto swPeer = parsePeerRoles(ConstDataRange(reinterpret_cast<char*>(derData), - std::extent<decltype(derData)>::value)); + auto swPeer = parsePeerRoles(ConstDataRange(derData)); ASSERT_OK(swPeer.getStatus()); auto item = *(swPeer.getValue().begin()); ASSERT_EQ(item.getRole(), "role_name"); @@ -148,8 +147,7 @@ TEST(SSLManager, MongoDBRolesParser) { 0x6d, 0x65, 0x72, 0x6f, 0x6c, 0x65, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x72, 0x6f, 0x6c, 0x65, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x72, 0x6f, 0x6c, 0x65, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x0c, 0x0b, 0x54, 0x68, 0x69, 0x72, 0x64, 0x20, 0x66, 0x69, 0x65, 0x6c, 0x64}; - auto swPeer = parsePeerRoles(ConstDataRange(reinterpret_cast<char*>(derData), - std::extent<decltype(derData)>::value)); + auto swPeer = parsePeerRoles(ConstDataRange(derData)); ASSERT_OK(swPeer.getStatus()); auto item = *(swPeer.getValue().begin()); @@ -168,24 +166,21 @@ TEST(SSLManager, MongoDBRolesParser) { 0x6e, 0x61, 0x6d, 0x65, 0x0c, 0x0b, 0x54, 0x68, 0x69, 0x72, 0x64, 0x20, 0x66, 0x69, 0x65, 0x6c, 0x64}; - auto swPeer = parsePeerRoles(ConstDataRange(reinterpret_cast<char*>(derData), - std::extent<decltype(derData)>::value)); + auto swPeer = parsePeerRoles(ConstDataRange(derData)); ASSERT_NOT_OK(swPeer.getStatus()); } // Negative: Runt, only a tag { unsigned char derData[] = {0x31}; - auto swPeer = parsePeerRoles(ConstDataRange(reinterpret_cast<char*>(derData), - std::extent<decltype(derData)>::value)); + auto swPeer = parsePeerRoles(ConstDataRange(derData)); ASSERT_NOT_OK(swPeer.getStatus()); } // Negative: Runt, only a tag and short length { unsigned char derData[] = {0x31, 0x0b}; - auto swPeer = parsePeerRoles(ConstDataRange(reinterpret_cast<char*>(derData), - std::extent<decltype(derData)>::value)); + auto swPeer = parsePeerRoles(ConstDataRange(derData)); ASSERT_NOT_OK(swPeer.getStatus()); } @@ -194,8 +189,7 @@ TEST(SSLManager, MongoDBRolesParser) { unsigned char derData[] = { 0x31, 0x88, 0xff, 0xff, }; - auto swPeer = parsePeerRoles(ConstDataRange(reinterpret_cast<char*>(derData), - std::extent<decltype(derData)>::value)); + auto swPeer = parsePeerRoles(ConstDataRange(derData)); ASSERT_NOT_OK(swPeer.getStatus()); } @@ -204,8 +198,7 @@ TEST(SSLManager, MongoDBRolesParser) { unsigned char derData[] = { 0x31, 0x82, 0xff, 0xff, }; - auto swPeer = parsePeerRoles(ConstDataRange(reinterpret_cast<char*>(derData), - std::extent<decltype(derData)>::value)); + auto swPeer = parsePeerRoles(ConstDataRange(derData)); ASSERT_NOT_OK(swPeer.getStatus()); } @@ -213,8 +206,7 @@ TEST(SSLManager, MongoDBRolesParser) { { unsigned char derData[] = { 0x0c, 0x0b, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64}; - auto swPeer = parsePeerRoles(ConstDataRange(reinterpret_cast<char*>(derData), - std::extent<decltype(derData)>::value)); + auto swPeer = parsePeerRoles(ConstDataRange(derData)); ASSERT_NOT_OK(swPeer.getStatus()); } @@ -222,8 +214,7 @@ TEST(SSLManager, MongoDBRolesParser) { { unsigned char derData[] = { 0x16, 0x0b, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64}; - auto swPeer = parsePeerRoles(ConstDataRange(reinterpret_cast<char*>(derData), - std::extent<decltype(derData)>::value)); + auto swPeer = parsePeerRoles(ConstDataRange(derData)); ASSERT_NOT_OK(swPeer.getStatus()); } @@ -234,8 +225,7 @@ TEST(SSLManager, MongoDBRolesParser) { 0x6e, 0x30, 0x18, 0x0c, 0x0f, 0x72, 0x65, 0x61, 0x64, 0x41, 0x6e, 0x79, 0x44, 0x61, 0x74, 0x61, 0x62, 0x61, 0x73, 0x65, 0x0c, 0x05, 0x61, 0x64, 0x6d, 0x69, 0x6e}; - auto swPeer = parsePeerRoles(ConstDataRange(reinterpret_cast<char*>(derData), - std::extent<decltype(derData)>::value)); + auto swPeer = parsePeerRoles(ConstDataRange(derData)); ASSERT_OK(swPeer.getStatus()); auto roles = getSortedRoles(swPeer.getValue()); diff --git a/src/mongo/util/uuid.h b/src/mongo/util/uuid.h index 401738d4400..de3a9c3f543 100644 --- a/src/mongo/util/uuid.h +++ b/src/mongo/util/uuid.h @@ -125,7 +125,7 @@ public: * Returns a ConstDataRange view of the UUID. */ ConstDataRange toCDR() const { - return ConstDataRange(reinterpret_cast<const char*>(_uuid.data()), _uuid.size()); + return ConstDataRange(_uuid); } /** |