Replace ArrayView::subview usage with subspan in dcsctp As a step towards migrating to standard std::span Unlike subview, subspan requires parameters to represent a valid range. Most dsctp usage already ensure that. Bug: webrtc:439801349 Change-Id: I2e26f2ec235bea77ea7beae0c644a551aaaa91d0 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/446240 Commit-Queue: Danil Chapovalov <danilchap@webrtc.org> Reviewed-by: Victor Boivie <boivie@webrtc.org> Cr-Commit-Position: refs/heads/main@{#46828}
diff --git a/net/dcsctp/fuzzers/dcsctp_fuzzers.cc b/net/dcsctp/fuzzers/dcsctp_fuzzers.cc index 90ad3f1..3f8c4d1 100644 --- a/net/dcsctp/fuzzers/dcsctp_fuzzers.cc +++ b/net/dcsctp/fuzzers/dcsctp_fuzzers.cc
@@ -430,7 +430,7 @@ // Set the socket in a specified valid starting state SetSocketState(socket, cb, static_cast<StartingState>(data[0])); - FuzzState state(data.subview(1)); + FuzzState state(data.subspan(1)); while (!state.empty()) { switch (state.GetByte()) {
diff --git a/net/dcsctp/packet/bounded_byte_reader.h b/net/dcsctp/packet/bounded_byte_reader.h index 26f4ff9..deeb090 100644 --- a/net/dcsctp/packet/bounded_byte_reader.h +++ b/net/dcsctp/packet/bounded_byte_reader.h
@@ -83,14 +83,14 @@ RTC_CHECK(FixedSize + variable_offset + SubSize <= data_.size()); webrtc::ArrayView<const uint8_t> sub_span = - data_.subview(FixedSize + variable_offset, SubSize); + data_.subspan(FixedSize + variable_offset, SubSize); return BoundedByteReader<SubSize>(sub_span); } size_t variable_data_size() const { return data_.size() - FixedSize; } webrtc::ArrayView<const uint8_t> variable_data() const { - return data_.subview(FixedSize, data_.size() - FixedSize); + return data_.subspan(FixedSize); } private:
diff --git a/net/dcsctp/packet/bounded_byte_writer.h b/net/dcsctp/packet/bounded_byte_writer.h index 738b35b..8f1ad1e 100644 --- a/net/dcsctp/packet/bounded_byte_writer.h +++ b/net/dcsctp/packet/bounded_byte_writer.h
@@ -88,7 +88,7 @@ RTC_CHECK(FixedSize + variable_offset + SubSize <= data_.size()); return BoundedByteWriter<SubSize>( - data_.subview(FixedSize + variable_offset, SubSize)); + data_.subspan(FixedSize + variable_offset, SubSize)); } void CopyToVariableData(webrtc::ArrayView<const uint8_t> source) {
diff --git a/net/dcsctp/packet/parameter/parameter.cc b/net/dcsctp/packet/parameter/parameter.cc index fadcbd4..ad787f9 100644 --- a/net/dcsctp/packet/parameter/parameter.cc +++ b/net/dcsctp/packet/parameter/parameter.cc
@@ -43,12 +43,12 @@ BoundedByteReader<kParameterHeaderSize> header(span); uint16_t type = header.Load16<0>(); uint16_t length = header.Load16<2>(); - result.emplace_back(type, span.subview(0, length)); + result.emplace_back(type, span.subspan(0, length)); size_t length_with_padding = RoundUpTo4(length); if (length_with_padding > span.size()) { break; } - span = span.subview(length_with_padding); + span = span.subspan(length_with_padding); } return result; } @@ -72,7 +72,7 @@ if (length_with_padding > span.size()) { break; } - span = span.subview(length_with_padding); + span = span.subspan(length_with_padding); } return Parameters(std::vector<uint8_t>(data.begin(), data.end())); }
diff --git a/net/dcsctp/packet/sctp_packet.cc b/net/dcsctp/packet/sctp_packet.cc index 0440f36..6a6802b 100644 --- a/net/dcsctp/packet/sctp_packet.cc +++ b/net/dcsctp/packet/sctp_packet.cc
@@ -163,7 +163,7 @@ std::vector<ChunkDescriptor> descriptors; descriptors.reserve(kExpectedDescriptorCount); webrtc::ArrayView<const uint8_t> descriptor_data = - webrtc::ArrayView<const uint8_t>(data_copy).subview(kHeaderSize); + webrtc::ArrayView<const uint8_t>(data_copy).subspan(kHeaderSize); while (!descriptor_data.empty()) { if (descriptor_data.size() < kChunkTlvHeaderSize) { RTC_DLOG(LS_WARNING) << "Too small chunk"; @@ -183,8 +183,8 @@ return std::nullopt; } descriptors.emplace_back(type, flags, - descriptor_data.subview(0, padded_length)); - descriptor_data = descriptor_data.subview(padded_length); + descriptor_data.subspan(0, padded_length)); + descriptor_data = descriptor_data.subspan(padded_length); } // Note that iterators (and pointer) are guaranteed to be stable when moving a
diff --git a/net/dcsctp/packet/tlv_trait.h b/net/dcsctp/packet/tlv_trait.h index cbe047d..e032c8e 100644 --- a/net/dcsctp/packet/tlv_trait.h +++ b/net/dcsctp/packet/tlv_trait.h
@@ -119,7 +119,7 @@ return std::nullopt; } } - return BoundedByteReader<Config::kHeaderSize>(data.subview(0, length)); + return BoundedByteReader<Config::kHeaderSize>(data.subspan(0, length)); } // Allocates space for data with a static header size, as defined by
diff --git a/net/dcsctp/rx/reassembly_queue_test.cc b/net/dcsctp/rx/reassembly_queue_test.cc index ff93625..4d28b51 100644 --- a/net/dcsctp/rx/reassembly_queue_test.cc +++ b/net/dcsctp/rx/reassembly_queue_test.cc
@@ -108,7 +108,7 @@ ReassemblyQueue reasm("log: ", kBufferSize); for (size_t i = 0; i < tsns.size(); i++) { - auto span = payload.subview((tsns[i] - 10) * 4, 4); + auto span = payload.subspan((tsns[i] - 10) * 4, 4); Data::IsBeginning is_beginning(tsns[i] == 10); Data::IsEnd is_end(tsns[i] == 13); @@ -142,7 +142,7 @@ do { ReassemblyQueue reasm("log: ", kBufferSize); for (size_t i = 0; i < tsns.size(); i++) { - auto span = payload.subview((tsns[i] - 10) * 4, 4); + auto span = payload.subspan((tsns[i] - 10) * 4, 4); Data::IsBeginning is_beginning(true); Data::IsEnd is_end(true); @@ -154,10 +154,10 @@ } EXPECT_THAT( FlushMessages(reasm), - ElementsAre(SctpMessageIs(kStreamID, kPPID, payload.subview(0, 4)), - SctpMessageIs(kStreamID, kPPID, payload.subview(4, 4)), - SctpMessageIs(kStreamID, kPPID, payload.subview(8, 4)), - SctpMessageIs(kStreamID, kPPID, payload.subview(12, 4)))); + ElementsAre(SctpMessageIs(kStreamID, kPPID, payload.subspan(0, 4)), + SctpMessageIs(kStreamID, kPPID, payload.subspan(4, 4)), + SctpMessageIs(kStreamID, kPPID, payload.subspan(8, 4)), + SctpMessageIs(kStreamID, kPPID, payload.subspan(12, 4)))); } while (std::next_permutation(std::begin(tsns), std::end(tsns))); } @@ -353,7 +353,7 @@ ReassemblyQueue reasm("log: ", kBufferSize, /*use_message_interleaving=*/true); for (int i : indexes) { - auto span = payload.subview(*fsns[i] * 2, 2); + auto span = payload.subspan(*fsns[i] * 2, 2); Data::IsBeginning is_beginning(fsns[i] == FSN(0)); Data::IsEnd is_end(fsns[i] == FSN(2)); reasm.Add(tsns[i], Data(stream_ids[i], SSN(0), MID(0), fsns[i], kPPID,
diff --git a/net/dcsctp/tx/rr_send_queue.cc b/net/dcsctp/tx/rr_send_queue.cc index 537f5f3..d27094d 100644 --- a/net/dcsctp/tx/rr_send_queue.cc +++ b/net/dcsctp/tx/rr_send_queue.cc
@@ -9,6 +9,7 @@ */ #include "net/dcsctp/tx/rr_send_queue.h" +#include <algorithm> #include <cstddef> #include <cstdint> #include <deque> @@ -174,9 +175,10 @@ } // Grab the next `max_size` fragment from this message and calculate flags. - webrtc::ArrayView<const uint8_t> chunk_payload = - item.message.payload().subview(item.remaining_offset, max_size); webrtc::ArrayView<const uint8_t> message_payload = message.payload(); + webrtc::ArrayView<const uint8_t> chunk_payload = message_payload.subspan( + item.remaining_offset, + std::min(message_payload.size() - item.remaining_offset, max_size)); Data::IsBeginning is_beginning(chunk_payload.data() == message_payload.data()); Data::IsEnd is_end((chunk_payload.data() + chunk_payload.size()) ==