From 8474eddd3aa7822ac10c5578d6fcc83eca1271a6 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Fri, 9 May 2025 12:24:45 +0800 Subject: [PATCH] use the slices package to simply QUIC version comparisons (#5130) No functional change expected. --- connection.go | 15 +++++++-------- internal/protocol/version.go | 14 ++++---------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/connection.go b/connection.go index 28a2bb46..c6116fa9 100644 --- a/connection.go +++ b/connection.go @@ -9,6 +9,7 @@ import ( "io" "net" "reflect" + "slices" "sync" "sync/atomic" "time" @@ -1278,15 +1279,13 @@ func (s *connection) handleVersionNegotiationPacket(p receivedPacket) { return } - for _, v := range supportedVersions { - if v == s.version { - if s.tracer != nil && s.tracer.DroppedPacket != nil { - s.tracer.DroppedPacket(logging.PacketTypeVersionNegotiation, protocol.InvalidPacketNumber, p.Size(), logging.PacketDropUnexpectedVersion) - } - // The Version Negotiation packet contains the version that we offered. - // This might be a packet sent by an attacker, or it was corrupted. - return + if slices.Contains(supportedVersions, s.version) { + if s.tracer != nil && s.tracer.DroppedPacket != nil { + s.tracer.DroppedPacket(logging.PacketTypeVersionNegotiation, protocol.InvalidPacketNumber, p.Size(), logging.PacketDropUnexpectedVersion) } + // The Version Negotiation packet contains the version that we offered. + // This might be a packet sent by an attacker, or it was corrupted. + return } s.logger.Infof("Received a Version Negotiation packet. Supported Versions: %s", supportedVersions) diff --git a/internal/protocol/version.go b/internal/protocol/version.go index 646587f3..c9254a04 100644 --- a/internal/protocol/version.go +++ b/internal/protocol/version.go @@ -6,6 +6,7 @@ import ( "fmt" "math" mrand "math/rand/v2" + "slices" "sync" ) @@ -64,12 +65,7 @@ func (vn Version) toGQUICVersion() int { // IsSupportedVersion returns true if the server supports this version func IsSupportedVersion(supported []Version, v Version) bool { - for _, t := range supported { - if t == v { - return true - } - } - return false + return slices.Contains(supported, v) } // ChooseSupportedVersion finds the best version in the overlap of ours and theirs @@ -78,10 +74,8 @@ func IsSupportedVersion(supported []Version, v Version) bool { // The bool returned indicates if a matching version was found. func ChooseSupportedVersion(ours, theirs []Version) (Version, bool) { for _, ourVer := range ours { - for _, theirVer := range theirs { - if ourVer == theirVer { - return ourVer, true - } + if slices.Contains(theirs, ourVer) { + return ourVer, true } } return 0, false