cezheng / Fuzi

A fast & lightweight XML & HTML parser in Swift with XPath & CSS support

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fuzi 2.0.0, iOS11, XCode 9.1beta - crash due to nil value - issue occurred after migration to Swift 4

LukaszGrela opened this issue · comments

Description:

I have migrated my code to Swift 4 and updated Fuzi v2.0.0, my code:

//...
            let manifest = try String(contentsOfFile: "\(destination)/imsmanifest.xml");
            
            
            let document = try XMLDocument(string: manifest)
            
            if let root = document.root {
                printlog("\(root.tag)")
                // launcher
                if let launch = root.firstChild(tag: "resources")?.firstChild(tag: "resource")?.attr("href") {
                    launcher = launch
                    printlog("Found: \(launch)")
                }
                
                //mastery score
                if let _mastery = root.firstChild(tag: "organizations")?.firstChild(tag: "organization")?.firstChild(tag: "item")?.firstChild(tag: "masteryscore", inNamespace: "adlcp") {
                    printlog("Masteryscore: \(_mastery.numberValue?.intValue)")
                    mastery = _mastery.numberValue?.intValue
                }
//...

manifest file with commented out masteryscore

    <manifest xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2" xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" identifier="adapt_manifest" version="1" xsi:schemaLocation="http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd">
        <metadata>
            <schema>ADL SCORM</schema>
            <schemaversion>1.2</schemaversion>
            <lom xmlns="http://www.imsglobal.org/xsd/imsmd_rootv1p2p1" xsi:schemaLocation="http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd">
                <general>
                    <title>
                        <langstring xml:lang="x-none"><![CDATA[title]]></langstring>
                    </title>
                    <description>
                        <langstring xml:lang="x-none"><![CDATA[Test!]]></langstring>
                    </description>
                </general>
            </lom>
        </metadata>
        <organizations default="adapt_scorm">
            <organization identifier="adapt_scorm">
                <title><![CDATA[Adapt Version 2.0 demonstration]]></title>
                <item identifier="item_1" isvisible="true" identifierref="res1">
                    <title><![CDATA[Adapt Version 2.0 demonstration]]></title>
                     <!-- <adlcp:masteryscore>70</adlcp:masteryscore> -->
                </item>
            </organization>
        </organizations>
        <resources>
            <resource identifier="res1" type="webcontent" href="index_lms.html" adlcp:scormtype="sco">
                <file href="index_lms.html"/>
            </resource>
        </resources>
    </manifest>
  • Expected behaviour:
    To work as it was in previous version, before migration:)

  • Actual behaviour:
    error is pointed to the
    guard let prefix = node?.pointee.ns.pointee.prefix else {
    Line 142 in Helpers.swift
    Thread 32: Fatal error: Unexpectedly found nil while unwrapping an Optional value

I've noticed that on 1.0.1 there is no "smiling helper" on that line if that matters:)

Environment

  • Package Manager:

    • Carthage, version:
    • [X ] CocoaPods, version:
    • Manually
  • Fuzi version: 2.0.0

  • Xcode version: 9.1beta (9b37)

How to reproduce:

@banjun Would you mind taking a look at this?

reproduces with Xcode 9.0.
^-^ is not needed in the line because its type is not Swift String and has nothing to do with the issue 😄

this code crashes when parsing text node after <item> below:

<item identifier="item_1" isvisible="true" identifierref="res1">(following text node starting with \n...)

screen shot 2017-10-13 10 39 38

Anyway this patch can fix the issue. node?.pointee.ns is an Implicitly Unwrapped Optional UnsafeMutablePointer<xmlNs>!.

guard let prefix = node?.pointee.ns?.pointee.prefix else {

@banjun @LukaszGrela Cool, would any one of you mind putting up a PR for this fix and a new test case? Sure I can do it myself sometime later when I have some free time, but that's going to take a while.

@LukaszGrela do you write a PR with your test case?

I have no experience in writing test cases (shame on me:)

ok, I'll try to patch this.