leonbreedt / FavIcon

Swift library to detect icons supported by a website.
Other
126 stars 37 forks source link

crash in XML.swift #23

Closed JEXcome closed 5 years ago

JEXcome commented 6 years ago

Hello there!

I found another source of crashes in updated 3.0.4 version. It is in XML.swift:37.

   init(data: Data) {
        ensureLibXMLErrorHandlingSuppressed()

        guard data.count > 0 else { return }

        _document = data.withUnsafeBytes { (p: UnsafePointer<Int8>) -> htmlDocPtr in
            return xmlReadMemory(p, Int32(data.count), nil, nil, 0) // <- here
        }
    }

Seems like invalid xml response (for example, when processing browserconfig.xml) causes it.

You can reproduce this crash by downloadAll for URL:https://jex3.netlify.com

I fave fixed it this way:

    init(data: Data)
    {
        ensureLibXMLErrorHandlingSuppressed()

        guard data.count > 0 else { return }

        var ptr : xmlDocPtr? = nil

        data.withUnsafeBytes
        { (p: UnsafePointer<Int8>) in

            ptr = xmlReadMemory(p, Int32(data.count), nil, nil, 0)
        }

        guard let p = ptr else { return }

        _document = p
    }

PS: same issue potentially can occur in HTML.swift

leonbreedt commented 5 years ago

Thanks a lot @JEXcome, I have released 3.0.5 which should fix the XML and HTML potential crash.