Skip to content

Commit

Permalink
Fix crash when scanning TD1 document
Browse files Browse the repository at this point in the history
without optional data 2 field
  • Loading branch information
romanmazeev committed Feb 2, 2025
1 parent 9b1d492 commit c33e3f3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/MRZParser/Private/MRZCode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct MRZCode {

result += [birthdateField, expiryDateField]

if optionalValidatedFields.count > 0 {
if optionalValidatedFields.count > 1 {
result.append(optionalValidatedFields[1])
}
return result
Expand Down
27 changes: 27 additions & 0 deletions Tests/MRZParserIntegrationTests/MRZParserIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,31 @@ final class MRZParserTests: XCTestCase {
XCTAssertEqual(MRZParser.parse(mrzString: mrzString, isOCRCorrectionEnabled: true), result)
XCTAssertNil(MRZParser.parse(mrzString: mrzString, isOCRCorrectionEnabled: false))
}

func testTD1withoutOptionalData2() throws {
let mrzString = """
I<LVAPA99929216121282<88882<<<
8212122M1703054LVA<<<<<<<<<<<0
PARAUDZINS<<ANDRIS<<<<<<<<<<<<
"""
let result = MRZResult(
mrzKey: "PA9992921682121221703054",
format: .td1,
documentType: .id,
documentTypeAdditional: nil,
countryCode: "LVA",
surnames: "PARAUDZINS",
givenNames: "ANDRIS",
documentNumber: "PA9992921",
nationalityCountryCode: "LVA",
birthdate: try XCTUnwrap(dateFormatter.date(from: "821212")),
sex: .male,
expiryDate: try XCTUnwrap(dateFormatter.date(from: "170305")),
optionalData: "121282 88882",
optionalData2: nil
)

XCTAssertEqual(MRZParser.parse(mrzString: mrzString, isOCRCorrectionEnabled: true), result)
XCTAssertEqual(MRZParser.parse(mrzString: mrzString, isOCRCorrectionEnabled: false), result)
}
}

0 comments on commit c33e3f3

Please sign in to comment.