Skip to content

Commit

Permalink
fix: Asterisk wildcard crash
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Smallman <srsmallman@mac.com>
  • Loading branch information
sammysmallman committed Oct 2, 2023
1 parent 8a03c09 commit 7b134c3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Sources/CoreOSC/LICENSE.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
24 changes: 3 additions & 21 deletions Sources/CoreOSC/OSCMatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/CoreOSCTests/CoreOSCTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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("<https://www.gnu.org/licenses/why-not-lgpl.html>.\n"))
}

Expand Down
24 changes: 22 additions & 2 deletions Tests/CoreOSCTests/OSCMatchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions Tests/CoreOSCTests/OSCMessageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,15 @@ 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("<https://www.gnu.org/licenses/why-not-lgpl.html>.\n"))

let parsedPacket = try OSCParser.packet(from: message.data())
let parsedMessage = parsedPacket as! OSCMessage

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("<https://www.gnu.org/licenses/why-not-lgpl.html>.\n"))
}

Expand Down

0 comments on commit 7b134c3

Please sign in to comment.