Skip to content

Commit 6bb5b20

Browse files
committed
remote(server): fix crash when invalid port is specified
Fixes #6584
1 parent c197864 commit 6bb5b20

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

Platform/macOS/SettingsView.swift

+4-1
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,12 @@ struct ServerSettingsView: View {
223223
.multilineTextAlignment(.trailing)
224224
.help("Specify a port number to listen on. This is required if external clients are permitted.")
225225
.onChange(of: serverPort) { newValue in
226-
if serverPort == 0 {
226+
if newValue == 0 {
227227
isServerExternal = false
228228
}
229+
if newValue < 0 || newValue >= UInt16.max {
230+
serverPort = defaultPort
231+
}
229232
}
230233
}
231234
Section(header: Text("Authentication")) {

Remote/UTMRemoteServer.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ actor UTMRemoteServer {
131131
registerNotifications()
132132
listener = Task {
133133
await withErrorNotification {
134-
if isServerExternal && serverPort > 0 {
134+
if isServerExternal && serverPort > 0 && serverPort <= UInt16.max {
135135
natPort = Port.TCP(internalPort: UInt16(serverPort))
136136
natPort!.mappingChangedHandler = { port in
137137
Task {
@@ -146,7 +146,7 @@ actor UTMRemoteServer {
146146
}
147147
}
148148
}
149-
let port = serverPort > 0 ? NWEndpoint.Port(integerLiteral: UInt16(serverPort)) : .any
149+
let port = serverPort > 0 && serverPort <= UInt16.max ? NWEndpoint.Port(integerLiteral: UInt16(serverPort)) : .any
150150
for try await connection in Connection.advertise(on: port, forServiceType: service, txtRecord: metadata, connectionQueue: connectionQueue, identity: keyManager.identity) {
151151
let connection = try? await Connection(connection: connection, connectionQueue: connectionQueue) { connection, error in
152152
Task {

0 commit comments

Comments
 (0)