Remove optional::value(), map::at() from quic. QUICHE is not supposed to have exceptions. std::optional::value can throw bad_optional_access, and absl_flat_hashmap::at can throw out_of_range. There are other places where we use exception-throwing APIs in QUICHE, but I'll tackle those in future changes. PiperOrigin-RevId: 590739628
diff --git a/quiche/quic/core/http/end_to_end_test.cc b/quiche/quic/core/http/end_to_end_test.cc index 656252d..78eeffc 100644 --- a/quiche/quic/core/http/end_to_end_test.cc +++ b/quiche/quic/core/http/end_to_end_test.cc
@@ -6051,10 +6051,9 @@ EXPECT_CALL(visitor, OnTransportParametersSent(_)) .WillOnce(Invoke([kCustomParameter]( const TransportParameters& transport_parameters) { - ASSERT_NE(transport_parameters.custom_parameters.find(kCustomParameter), - transport_parameters.custom_parameters.end()); - EXPECT_EQ(transport_parameters.custom_parameters.at(kCustomParameter), - "test"); + auto it = transport_parameters.custom_parameters.find(kCustomParameter); + ASSERT_NE(it, transport_parameters.custom_parameters.end()); + EXPECT_EQ(it->second, "test"); })); EXPECT_CALL(visitor, OnTransportParametersReceived(_)).Times(1); ASSERT_TRUE(Initialize()); @@ -6070,12 +6069,10 @@ ADD_FAILURE() << "Missing server session"; } if (server_config != nullptr) { - if (server_config->received_custom_transport_parameters().find( - kCustomParameter) != - server_config->received_custom_transport_parameters().end()) { - EXPECT_EQ(server_config->received_custom_transport_parameters().at( - kCustomParameter), - "test"); + if (auto it = server_config->received_custom_transport_parameters().find( + kCustomParameter); + it != server_config->received_custom_transport_parameters().end()) { + EXPECT_EQ(it->second, "test"); } else { ADD_FAILURE() << "Did not find custom parameter"; }
diff --git a/quiche/quic/moqt/moqt_framer.cc b/quiche/quic/moqt/moqt_framer.cc index 4111d78..a0b5f40 100644 --- a/quiche/quic/moqt/moqt_framer.cc +++ b/quiche/quic/moqt/moqt_framer.cc
@@ -180,12 +180,12 @@ if (message.role.has_value()) { WriteVarIntParameter(writer, static_cast<uint64_t>(MoqtSetupParameter::kRole), - static_cast<uint64_t>(message.role.value())); + static_cast<uint64_t>(*message.role)); } if (!using_webtrans_ && message.path.has_value()) { WriteStringParameter(writer, static_cast<uint64_t>(MoqtSetupParameter::kPath), - message.path.value()); + *message.path); } QUICHE_DCHECK(writer.remaining() == 0); return buffer; @@ -210,7 +210,7 @@ if (message.role.has_value()) { WriteVarIntParameter(writer, static_cast<uint64_t>(MoqtSetupParameter::kRole), - static_cast<uint64_t>(message.role.value())); + static_cast<uint64_t>(*message.role)); } QUICHE_DCHECK(writer.remaining() == 0); return buffer; @@ -257,7 +257,7 @@ WriteStringParameter( writer, static_cast<uint64_t>(MoqtTrackRequestParameter::kAuthorizationInfo), - message.authorization_info.value()); + *message.authorization_info); } QUICHE_DCHECK(writer.remaining() == 0); return buffer; @@ -380,7 +380,7 @@ WriteStringParameter( writer, static_cast<uint64_t>(MoqtTrackRequestParameter::kAuthorizationInfo), - message.authorization_info.value()); + *message.authorization_info); } QUICHE_DCHECK(writer.remaining() == 0); return buffer;
diff --git a/quiche/quic/moqt/moqt_parser.cc b/quiche/quic/moqt/moqt_parser.cc index b672489..8a9213c 100644 --- a/quiche/quic/moqt/moqt_parser.cc +++ b/quiche/quic/moqt/moqt_parser.cc
@@ -62,7 +62,7 @@ QUICHE_DCHECK(buffered_message_.empty()); if (!object_metadata_->payload_length.has_value()) { // Deliver the data and exit. - visitor_.OnObjectMessage(object_metadata_.value(), data, fin); + visitor_.OnObjectMessage(*object_metadata_, data, fin); if (fin) { object_metadata_.reset(); } @@ -70,13 +70,13 @@ } if (data.length() < payload_length_remaining_) { // Does not finish the payload; deliver and exit. - visitor_.OnObjectMessage(object_metadata_.value(), data, false); + visitor_.OnObjectMessage(*object_metadata_, data, false); payload_length_remaining_ -= data.length(); return; } // Finishes the payload. Deliver and continue. reader.emplace(data); - visitor_.OnObjectMessage(object_metadata_.value(), + visitor_.OnObjectMessage(*object_metadata_, data.substr(0, payload_length_remaining_), true); reader->Seek(payload_length_remaining_); object_metadata_.reset(); @@ -186,7 +186,7 @@ ? reader.BytesRemaining() : *object_metadata_->payload_length; visitor_.OnObjectMessage( - object_metadata_.value(), + *object_metadata_, reader.PeekRemainingPayload().substr(0, payload_to_draw), received_complete_message); if (received_complete_message) {
diff --git a/quiche/quic/moqt/moqt_parser_test.cc b/quiche/quic/moqt/moqt_parser_test.cc index 50a0357..32643d4 100644 --- a/quiche/quic/moqt/moqt_parser_test.cc +++ b/quiche/quic/moqt/moqt_parser_test.cc
@@ -99,7 +99,7 @@ messages_received_++; MoqtClientSetup client_setup = message; if (client_setup.path.has_value()) { - string0_ = std::string(client_setup.path.value()); + string0_ = std::string(*client_setup.path); client_setup.path = absl::string_view(string0_); } last_message_ = TestMessageBase::MessageStructuredData(client_setup); @@ -119,7 +119,7 @@ string1_ = std::string(subscribe_request.track_name); subscribe_request.track_name = absl::string_view(string1_); if (subscribe_request.authorization_info.has_value()) { - string2_ = std::string(subscribe_request.authorization_info.value()); + string2_ = std::string(*subscribe_request.authorization_info); subscribe_request.authorization_info = absl::string_view(string2_); } last_message_ = TestMessageBase::MessageStructuredData(subscribe_request); @@ -185,7 +185,7 @@ string0_ = std::string(announce.track_namespace); announce.track_namespace = absl::string_view(string0_); if (announce.authorization_info.has_value()) { - string1_ = std::string(announce.authorization_info.value()); + string1_ = std::string(*announce.authorization_info); announce.authorization_info = absl::string_view(string1_); } last_message_ = TestMessageBase::MessageStructuredData(announce); @@ -300,7 +300,7 @@ std::unique_ptr<TestMessageBase> message = MakeMessage(message_type_); parser_.ProcessData(message->PacketSample(), true); EXPECT_EQ(visitor_.messages_received_, 1); - EXPECT_TRUE(message->EqualFieldValues(visitor_.last_message_.value())); + EXPECT_TRUE(message->EqualFieldValues(*visitor_.last_message_)); EXPECT_TRUE(visitor_.end_of_message_); if (IsObjectMessage(message_type_)) { // Check payload message. @@ -314,7 +314,7 @@ message->ExpandVarints(); parser_.ProcessData(message->PacketSample(), true); EXPECT_EQ(visitor_.messages_received_, 1); - EXPECT_TRUE(message->EqualFieldValues(visitor_.last_message_.value())); + EXPECT_TRUE(message->EqualFieldValues(*visitor_.last_message_)); EXPECT_TRUE(visitor_.end_of_message_); if (IsObjectMessage(message_type_)) { // Check payload message. @@ -337,7 +337,7 @@ first_data_size, message->total_message_size() - first_data_size), true); EXPECT_EQ(visitor_.messages_received_, 1); - EXPECT_TRUE(message->EqualFieldValues(visitor_.last_message_.value())); + EXPECT_TRUE(message->EqualFieldValues(*visitor_.last_message_)); if (IsObjectMessage(message_type_)) { EXPECT_EQ(visitor_.object_payload_, "foo"); } @@ -362,7 +362,7 @@ parser_.ProcessData(absl::string_view(), true); // Needs the FIN EXPECT_EQ(visitor_.messages_received_, kObjectPayloadSize + 2); } - EXPECT_TRUE(message->EqualFieldValues(visitor_.last_message_.value())); + EXPECT_TRUE(message->EqualFieldValues(*visitor_.last_message_)); EXPECT_TRUE(visitor_.end_of_message_); EXPECT_FALSE(visitor_.parsing_error_.has_value()); } @@ -385,7 +385,7 @@ parser_.ProcessData(absl::string_view(), true); // Needs the FIN EXPECT_EQ(visitor_.messages_received_, kObjectPayloadSize + 2); } - EXPECT_TRUE(message->EqualFieldValues(visitor_.last_message_.value())); + EXPECT_TRUE(message->EqualFieldValues(*visitor_.last_message_)); EXPECT_TRUE(visitor_.end_of_message_); EXPECT_FALSE(visitor_.parsing_error_.has_value()); } @@ -430,14 +430,14 @@ auto message = std::make_unique<ObjectMessageWithoutLength>(); parser.ProcessData(message->PacketSample(), false); EXPECT_EQ(visitor_.messages_received_, 1); - EXPECT_TRUE(message->EqualFieldValues(visitor_.last_message_.value())); + EXPECT_TRUE(message->EqualFieldValues(*visitor_.last_message_)); EXPECT_TRUE(visitor_.object_payload_.has_value()); EXPECT_EQ(*(visitor_.object_payload_), "foo"); EXPECT_FALSE(visitor_.end_of_message_); parser.ProcessData(absl::string_view(), true); // send the FIN EXPECT_EQ(visitor_.messages_received_, 2); - EXPECT_TRUE(message->EqualFieldValues(visitor_.last_message_.value())); + EXPECT_TRUE(message->EqualFieldValues(*visitor_.last_message_)); EXPECT_TRUE(visitor_.object_payload_.has_value()); EXPECT_EQ(*(visitor_.object_payload_), ""); EXPECT_TRUE(visitor_.end_of_message_); @@ -451,7 +451,7 @@ auto message = std::make_unique<ObjectMessageWithoutLength>(); parser.ProcessData(message->PacketSample(), false); EXPECT_EQ(visitor_.messages_received_, 1); - EXPECT_TRUE(message->EqualFieldValues(visitor_.last_message_.value())); + EXPECT_TRUE(message->EqualFieldValues(*visitor_.last_message_)); EXPECT_FALSE(visitor_.end_of_message_); EXPECT_TRUE(visitor_.object_payload_.has_value()); EXPECT_EQ(*(visitor_.object_payload_), "foo"); @@ -459,7 +459,7 @@ // second part parser.ProcessData("bar", false); EXPECT_EQ(visitor_.messages_received_, 2); - EXPECT_TRUE(message->EqualFieldValues(visitor_.last_message_.value())); + EXPECT_TRUE(message->EqualFieldValues(*visitor_.last_message_)); EXPECT_FALSE(visitor_.end_of_message_); EXPECT_TRUE(visitor_.object_payload_.has_value()); EXPECT_EQ(*(visitor_.object_payload_), "bar"); @@ -467,7 +467,7 @@ // third part includes FIN parser.ProcessData("deadbeef", true); EXPECT_EQ(visitor_.messages_received_, 3); - EXPECT_TRUE(message->EqualFieldValues(visitor_.last_message_.value())); + EXPECT_TRUE(message->EqualFieldValues(*visitor_.last_message_)); EXPECT_TRUE(visitor_.end_of_message_); EXPECT_TRUE(visitor_.object_payload_.has_value()); EXPECT_EQ(*(visitor_.object_payload_), "deadbeef"); @@ -489,7 +489,7 @@ message->PacketSample().substr(4, message->total_message_size() - 4), false); EXPECT_EQ(visitor_.messages_received_, 1); - EXPECT_TRUE(message->EqualFieldValues(visitor_.last_message_.value())); + EXPECT_TRUE(message->EqualFieldValues(*visitor_.last_message_)); EXPECT_FALSE(visitor_.end_of_message_); EXPECT_TRUE(visitor_.object_payload_.has_value()); EXPECT_EQ(visitor_.object_payload_->length(), 95); @@ -497,7 +497,7 @@ // third part includes FIN parser.ProcessData("bar", true); EXPECT_EQ(visitor_.messages_received_, 2); - EXPECT_TRUE(message->EqualFieldValues(visitor_.last_message_.value())); + EXPECT_TRUE(message->EqualFieldValues(*visitor_.last_message_)); EXPECT_TRUE(visitor_.end_of_message_); EXPECT_TRUE(visitor_.object_payload_.has_value()); EXPECT_EQ(*(visitor_.object_payload_), "bar"); @@ -820,8 +820,7 @@ false); EXPECT_EQ(visitor_.messages_received_, fully_received); if (prev_message != nullptr) { - EXPECT_TRUE( - prev_message->EqualFieldValues(visitor_.last_message_.value())); + EXPECT_TRUE(prev_message->EqualFieldValues(*visitor_.last_message_)); } fully_received++; read = new_read; @@ -831,7 +830,7 @@ // Deliver the rest parser.ProcessData(absl::string_view(buffer + read, write - read), true); EXPECT_EQ(visitor_.messages_received_, fully_received); - EXPECT_TRUE(prev_message->EqualFieldValues(visitor_.last_message_.value())); + EXPECT_TRUE(prev_message->EqualFieldValues(*visitor_.last_message_)); EXPECT_FALSE(visitor_.parsing_error_.has_value()); }
diff --git a/quiche/quic/moqt/moqt_subscribe_windows.h b/quiche/quic/moqt/moqt_subscribe_windows.h index 4ed173c..4bf5f76 100644 --- a/quiche/quic/moqt/moqt_subscribe_windows.h +++ b/quiche/quic/moqt/moqt_subscribe_windows.h
@@ -31,7 +31,7 @@ if (seq < start) { return false; } - if (!end.has_value() || seq < end.value()) { + if (!end.has_value() || seq < *end) { return true; } return false;
diff --git a/quiche/quic/moqt/tools/chat_client_bin.cc b/quiche/quic/moqt/tools/chat_client_bin.cc index 87b9af9..536709c 100644 --- a/quiche/quic/moqt/tools/chat_client_bin.cc +++ b/quiche/quic/moqt/tools/chat_client_bin.cc
@@ -101,7 +101,7 @@ << " "; } if (reason_phrase.has_value()) { - std::cout << "REJECTED, reason = " << reason_phrase.value() << "\n"; + std::cout << "REJECTED, reason = " << *reason_phrase << "\n"; } else { std::cout << "ACCEPTED\n"; } @@ -146,7 +146,7 @@ [&](absl::string_view track_namespace, std::optional<absl::string_view> message) { if (message.has_value()) { - std::cout << "ANNOUNCE rejected, " << message.value() << "\n"; + std::cout << "ANNOUNCE rejected, " << *message << "\n"; session_->Error("Local ANNOUNCE rejected"); return; }
diff --git a/quiche/quic/test_tools/crypto_test_utils.cc b/quiche/quic/test_tools/crypto_test_utils.cc index 62c3a92..e3963c4 100644 --- a/quiche/quic/test_tools/crypto_test_utils.cc +++ b/quiche/quic/test_tools/crypto_test_utils.cc
@@ -644,7 +644,7 @@ return 0; } - return QuicUtils::FNV1a_64_Hash(chain->certs.at(0)); + return QuicUtils::FNV1a_64_Hash(chain->certs[0]); } void FillInDummyReject(CryptoHandshakeMessage* rej) {
diff --git a/quiche/quic/tools/connect_udp_tunnel.cc b/quiche/quic/tools/connect_udp_tunnel.cc index 4cec8b8..702963f 100644 --- a/quiche/quic/tools/connect_udp_tunnel.cc +++ b/quiche/quic/tools/connect_udp_tunnel.cc
@@ -81,7 +81,7 @@ } // Empty host checked above after path split. Expect decoding to never result // in an empty decoded host from non-empty encoded host. - QUICHE_DCHECK(!decoded_host.value().empty()); + QUICHE_DCHECK(!decoded_host->empty()); std::optional<std::string> decoded_port = quiche::AsciiUrlDecode(path_split[5]); @@ -92,25 +92,22 @@ } // Empty port checked above after path split. Expect decoding to never result // in an empty decoded port from non-empty encoded port. - QUICHE_DCHECK(!decoded_port.value().empty()); + QUICHE_DCHECK(!decoded_port->empty()); - int parsed_port_number = - url::ParsePort(decoded_port.value().data(), - url::Component(0, decoded_port.value().size())); + int parsed_port_number = url::ParsePort( + decoded_port->data(), url::Component(0, decoded_port->size())); // Negative result is either invalid or unspecified, either of which is // disallowed for this parse. Port 0 is technically valid but reserved and not // really usable in practice, so easiest to just disallow it here. if (parsed_port_number <= 0) { - QUICHE_DVLOG(1) << "CONNECT-UDP request with bad port: " - << decoded_port.value(); + QUICHE_DVLOG(1) << "CONNECT-UDP request with bad port: " << *decoded_port; return std::nullopt; } // Expect url::ParsePort() to validate port is uint16_t and otherwise return // negative number checked for above. QUICHE_DCHECK_LE(parsed_port_number, std::numeric_limits<uint16_t>::max()); - return QuicServerId(decoded_host.value(), - static_cast<uint16_t>(parsed_port_number)); + return QuicServerId(*decoded_host, static_cast<uint16_t>(parsed_port_number)); } // Validate header expectations from RFC 9298, section 3.4. @@ -200,7 +197,7 @@ return; } - if (!ValidateTarget(target.value(), acceptable_targets_)) { + if (!ValidateTarget(*target, acceptable_targets_)) { SendErrorResponse("403", "destination_ip_prohibited", "disallowed proxy target"); return; @@ -208,7 +205,7 @@ // TODO(ericorth): Validate that the IP address doesn't fall into diallowed // ranges per RFC 9298, Section 7. - QuicSocketAddress address = tools::LookupAddress(AF_UNSPEC, target.value()); + QuicSocketAddress address = tools::LookupAddress(AF_UNSPEC, *target); if (!address.IsInitialized()) { SendErrorResponse("500", "dns_error", "host resolution error"); return; @@ -231,7 +228,7 @@ QUICHE_DVLOG(1) << "CONNECT-UDP tunnel opened from stream " << client_stream_request_handler_->stream_id() << " to " - << target.value().ToHostPortString(); + << target->ToHostPortString(); client_stream_request_handler_->GetStream()->RegisterHttp3DatagramVisitor( this); @@ -290,8 +287,7 @@ } QUICHE_DCHECK(client_stream_request_handler_); - quiche::ConnectUdpDatagramUdpPacketPayload payload( - data.value().AsStringView()); + quiche::ConnectUdpDatagramUdpPacketPayload payload(data->AsStringView()); client_stream_request_handler_->GetStream()->SendHttp3Datagram( payload.Serialize()); @@ -354,7 +350,7 @@ std::optional<std::string> capsule_protocol_value = structured_headers::SerializeItem(structured_headers::Item(true)); QUICHE_CHECK(capsule_protocol_value.has_value()); - response_headers["Capsule-Protocol"] = capsule_protocol_value.value(); + response_headers["Capsule-Protocol"] = *capsule_protocol_value; QuicBackendResponse response; response.set_headers(std::move(response_headers)); @@ -398,7 +394,7 @@ std::optional<std::string> proxy_status_value = structured_headers::SerializeList({proxy_status_member}); QUICHE_CHECK(proxy_status_value.has_value()); - headers["Proxy-Status"] = proxy_status_value.value(); + headers["Proxy-Status"] = *proxy_status_value; QuicBackendResponse response; response.set_headers(std::move(headers));
diff --git a/quiche/quic/tools/quic_toy_server.cc b/quiche/quic/tools/quic_toy_server.cc index 419fb14..bfe9c89 100644 --- a/quiche/quic/tools/quic_toy_server.cc +++ b/quiche/quic/tools/quic_toy_server.cc
@@ -90,8 +90,7 @@ std::optional<QuicServerId> destination_server_id = QuicServerId::ParseFromHostPortString(destination); QUICHE_CHECK(destination_server_id.has_value()); - connect_proxy_destinations.insert( - std::move(destination_server_id).value()); + connect_proxy_destinations.insert(*std::move(destination_server_id)); } absl::flat_hash_set<QuicServerId> connect_udp_proxy_targets; @@ -101,7 +100,7 @@ std::optional<QuicServerId> target_server_id = QuicServerId::ParseFromHostPortString(target); QUICHE_CHECK(target_server_id.has_value()); - connect_udp_proxy_targets.insert(std::move(target_server_id).value()); + connect_udp_proxy_targets.insert(*std::move(target_server_id)); } QUICHE_CHECK(!connect_proxy_destinations.empty() ||