From 7b134c3a698264e1c637d6b215c6d7f6ce778b24 Mon Sep 17 00:00:00 2001 From: Sam Smallman Date: Mon, 2 Oct 2023 09:14:50 +0100 Subject: [PATCH] fix: Asterisk wildcard crash Signed-off-by: Sam Smallman --- Sources/CoreOSC/LICENSE.md | 2 +- Sources/CoreOSC/OSCMatch.swift | 24 +++--------------------- Tests/CoreOSCTests/CoreOSCTests.swift | 2 +- Tests/CoreOSCTests/OSCMatchTests.swift | 24 ++++++++++++++++++++++-- Tests/CoreOSCTests/OSCMessageTests.swift | 4 ++-- 5 files changed, 29 insertions(+), 27 deletions(-) diff --git a/Sources/CoreOSC/LICENSE.md b/Sources/CoreOSC/LICENSE.md index 1779cf4..4e11011 100644 --- a/Sources/CoreOSC/LICENSE.md +++ b/Sources/CoreOSC/LICENSE.md @@ -1,4 +1,4 @@ -Copyright © 2022 Sam Smallman. https://github.com/SammySmallman +Copyright © 2023 Sam Smallman. https://github.com/SammySmallman This file is part of CoreOSC diff --git a/Sources/CoreOSC/OSCMatch.swift b/Sources/CoreOSC/OSCMatch.swift index 8650952..ee2078b 100644 --- a/Sources/CoreOSC/OSCMatch.swift +++ b/Sources/CoreOSC/OSCMatch.swift @@ -140,37 +140,19 @@ public enum OSCMatch { patternCharacterOffset: inout String.Index, address: String, addressCharacterOffset: inout String.Index) -> Bool { - var numberOfAsterisks = 0 if addressCharacterOffset == address.endIndex { return false } + // Move address index up to next "/" while addressCharacterOffset != address.endIndex && address[addressCharacterOffset] != "/" { addressCharacterOffset = address.index(after: addressCharacterOffset) } + // Move pattern index up to next "/" while patternCharacterOffset != pattern.endIndex && pattern[patternCharacterOffset] != "/" { - if pattern[patternCharacterOffset] == "*" { - numberOfAsterisks += 1 - } patternCharacterOffset = pattern.index(after: patternCharacterOffset) } - patternCharacterOffset = pattern.index(before: patternCharacterOffset) - addressCharacterOffset = address.index(before: addressCharacterOffset) - switch numberOfAsterisks { - case 1: - var casePatternCharacterOffset: String.Index = patternCharacterOffset - var caseAddressCharacterOffsetStart: String.Index = addressCharacterOffset - while pattern[casePatternCharacterOffset] != "*" { - if matchCharacters(pattern: pattern, - patternCharacterOffset: &casePatternCharacterOffset, - address: address, - addressCharacterOffset: &caseAddressCharacterOffsetStart) == false { - return false - } - } - break - default: return false - } + // TODO: Match patterns backwards if the last character before the "/" is not a "*" return true } diff --git a/Tests/CoreOSCTests/CoreOSCTests.swift b/Tests/CoreOSCTests/CoreOSCTests.swift index 0aa8bc9..fd04a13 100644 --- a/Tests/CoreOSCTests/CoreOSCTests.swift +++ b/Tests/CoreOSCTests/CoreOSCTests.swift @@ -35,7 +35,7 @@ class CoreOSCTests: XCTestCase { func testLicense() { let license = CoreOSC.license - XCTAssertTrue(license.hasPrefix("Copyright © 2022 Sam Smallman. https://github.com/SammySmallman")) + XCTAssertTrue(license.hasPrefix("Copyright © 2023 Sam Smallman. https://github.com/SammySmallman")) XCTAssertTrue(license.hasSuffix(".\n")) } diff --git a/Tests/CoreOSCTests/OSCMatchTests.swift b/Tests/CoreOSCTests/OSCMatchTests.swift index 0aaa769..29e7b39 100644 --- a/Tests/CoreOSCTests/OSCMatchTests.swift +++ b/Tests/CoreOSCTests/OSCMatchTests.swift @@ -68,6 +68,18 @@ final class OSCMatchTests: XCTestCase { OSCPatternMatch(match: .fullMatch, patternCharactersMatched: "/*/*/*".count, addressCharactersMatched: "/abc/def/hij".count)) + + XCTAssertEqual(OSCMatch.match(addressPattern: "/abc/*", + address: "/abc/def"), + OSCPatternMatch(match: .fullMatch, + patternCharactersMatched: "/abc/*".count, + addressCharactersMatched: "/abc/def".count)) + + XCTAssertEqual(OSCMatch.match(addressPattern: "/a/*cd", + address: "/a/bcd"), + OSCPatternMatch(match: .fullMatch, + patternCharactersMatched: "/a/*cd".count, + addressCharactersMatched: "/a/bcd".count)) } func testAsteriskPartialAddressMatch() { @@ -85,15 +97,23 @@ final class OSCMatchTests: XCTestCase { patternCharactersMatched: "/a/b/c".count, addressCharactersMatched: "/a/b/c".count)) } - + func testAsteriskUnmatched() { XCTAssertEqual(OSCMatch.match(addressPattern: "/*/abc", address: "/abc/def"), OSCPatternMatch(match: .unmatched, patternCharactersMatched: "/*/".count, addressCharactersMatched: "/abc/".count)) + +// TODO: Match patterns backwards if the last character before the "/" is not a "*" +// OSCMatch.swift:156 +// XCTAssertEqual(OSCMatch.match(addressPattern: "/a/*cd", +// address: "/a/bef"), +// OSCPatternMatch(match: .unmatched, +// patternCharactersMatched: "/a/*".count, +// addressCharactersMatched: "/a/b".count)) } - + // MARK: - Question Mark Wildcard OSC Address Pattern Tests func testQuestionMarkFullMatch() { diff --git a/Tests/CoreOSCTests/OSCMessageTests.swift b/Tests/CoreOSCTests/OSCMessageTests.swift index ad5df8b..e4a1830 100644 --- a/Tests/CoreOSCTests/OSCMessageTests.swift +++ b/Tests/CoreOSCTests/OSCMessageTests.swift @@ -185,7 +185,7 @@ class OSCMessageTests: XCTestCase { XCTAssertEqual(message.arguments.count, 1) let license1 = message.arguments[0] as! String - XCTAssertTrue(license1.hasPrefix("Copyright © 2022 Sam Smallman. https://github.com/SammySmallman")) + XCTAssertTrue(license1.hasPrefix("Copyright © 2023 Sam Smallman. https://github.com/SammySmallman")) XCTAssertTrue(license1.hasSuffix(".\n")) let parsedPacket = try OSCParser.packet(from: message.data()) @@ -193,7 +193,7 @@ class OSCMessageTests: XCTestCase { XCTAssertEqual(parsedMessage.arguments.count, 1) let license2 = parsedMessage.arguments[0] as! String - XCTAssertTrue(license2.hasPrefix("Copyright © 2022 Sam Smallman. https://github.com/SammySmallman")) + XCTAssertTrue(license2.hasPrefix("Copyright © 2023 Sam Smallman. https://github.com/SammySmallman")) XCTAssertTrue(license2.hasSuffix(".\n")) }