]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
Config file parsing: Fix bug when there are comments at the end
authorRoopesh Chander <roop@roopc.net>
Thu, 24 Jan 2019 10:53:07 +0000 (16:23 +0530)
committerRoopesh Chander <roop@roopc.net>
Thu, 24 Jan 2019 10:53:07 +0000 (16:23 +0530)
Signed-off-by: Roopesh Chander <roop@roopc.net>
WireGuard/Shared/Model/TunnelConfiguration+WgQuickConfig.swift

index 043914a432f46bd343d73f78c73516ff6f2e010d..65676f565b6a2b196cdc9f9f95cb5d3ea290c36c 100644 (file)
@@ -54,38 +54,38 @@ extension TunnelConfiguration {
             }
 
             trimmedLine = trimmedLine.trimmingCharacters(in: .whitespaces)
-
-            guard !trimmedLine.isEmpty else { continue }
-            let lowercasedLine = line.lowercased()
-
-            if let equalsIndex = line.firstIndex(of: "=") {
-                // Line contains an attribute
-                let keyWithCase = line[..<equalsIndex].trimmingCharacters(in: .whitespaces)
-                let key = keyWithCase.lowercased()
-                let value = line[line.index(equalsIndex, offsetBy: 1)...].trimmingCharacters(in: .whitespaces)
-                let keysWithMultipleEntriesAllowed: Set<String> = ["address", "allowedips", "dns"]
-                if let presentValue = attributes[key] {
-                    if keysWithMultipleEntriesAllowed.contains(key) {
-                        attributes[key] = presentValue + "," + value
+            let lowercasedLine = trimmedLine.lowercased()
+
+            if !trimmedLine.isEmpty {
+                if let equalsIndex = line.firstIndex(of: "=") {
+                    // Line contains an attribute
+                    let keyWithCase = line[..<equalsIndex].trimmingCharacters(in: .whitespaces)
+                    let key = keyWithCase.lowercased()
+                    let value = line[line.index(equalsIndex, offsetBy: 1)...].trimmingCharacters(in: .whitespaces)
+                    let keysWithMultipleEntriesAllowed: Set<String> = ["address", "allowedips", "dns"]
+                    if let presentValue = attributes[key] {
+                        if keysWithMultipleEntriesAllowed.contains(key) {
+                            attributes[key] = presentValue + "," + value
+                        } else {
+                            throw ParseError.multipleEntriesForKey(keyWithCase)
+                        }
                     } else {
-                        throw ParseError.multipleEntriesForKey(keyWithCase)
+                        attributes[key] = value
                     }
-                } else {
-                    attributes[key] = value
-                }
-                let interfaceSectionKeys: Set<String> = ["privatekey", "listenport", "address", "dns", "mtu"]
-                let peerSectionKeys: Set<String> = ["publickey", "presharedkey", "allowedips", "endpoint", "persistentkeepalive"]
-                if parserState == .inInterfaceSection {
-                    guard interfaceSectionKeys.contains(key) else {
-                        throw ParseError.interfaceHasUnrecognizedKey(keyWithCase)
-                    }
-                } else if parserState == .inPeerSection {
-                    guard peerSectionKeys.contains(key) else {
-                        throw ParseError.peerHasUnrecognizedKey(keyWithCase)
+                    let interfaceSectionKeys: Set<String> = ["privatekey", "listenport", "address", "dns", "mtu"]
+                    let peerSectionKeys: Set<String> = ["publickey", "presharedkey", "allowedips", "endpoint", "persistentkeepalive"]
+                    if parserState == .inInterfaceSection {
+                        guard interfaceSectionKeys.contains(key) else {
+                            throw ParseError.interfaceHasUnrecognizedKey(keyWithCase)
+                        }
+                    } else if parserState == .inPeerSection {
+                        guard peerSectionKeys.contains(key) else {
+                            throw ParseError.peerHasUnrecognizedKey(keyWithCase)
+                        }
                     }
+                } else if lowercasedLine != "[interface]" && lowercasedLine != "[peer]" {
+                    throw ParseError.invalidLine(line)
                 }
-            } else if lowercasedLine != "[interface]" && lowercasedLine != "[peer]" {
-                throw ParseError.invalidLine(line)
             }
 
             let isLastLine = lineIndex == lines.count - 1