Kitura / Kitura-redis

Swift Redis library
Apache License 2.0
95 stars 26 forks source link

Crash in Linux setting an empty value #13

Closed dsperling closed 8 years ago

dsperling commented 8 years ago

The following code crashes on Linux, but works on the Mac:

redis.set("key", value: "") {(wasSet: Bool, error: NSError?) in
}

The resulting error is: fatal error: unexpectedly found nil while unwrapping an Optional value

This is caused by the Linux version of NSString.data(encoding:) returning nil on an empty string.

The function causing the issue is issueCommand() in RedisResp.swift

internal func issueCommand(_ stringArgs: [String], callback: (RedisResponse) -> Void) {
    ...
    for arg in stringArgs {
        addAsBulkString(StringUtils.toUtf8String(arg)!, to: buffer)
    }
    ...

This fix could be in issueCommand, or lower in KituraSys -> StringUtils.toUtf8String.