Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-8115] PropertyListSerialization handle Data type error #3668

Closed
swift-ci opened this issue Jun 27, 2018 · 2 comments
Closed

[SR-8115] PropertyListSerialization handle Data type error #3668

swift-ci opened this issue Jun 27, 2018 · 2 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-8115
Radar None
Original Reporter davidw (JIRA User)
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

Ubuntu 16.04

$ cat /etc/*release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.4 LTS"
NAME="Ubuntu"
VERSION="16.04.4 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.4 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial
$ swift --version
Swift version 4.1.2 (swift-4.1.2-RELEASE)
Target: x86_64-unknown-linux-gnu

macOS 10.14 Beta (18A314h)

$ swift --version
Apple Swift version 4.2 (swiftlang-1000.0.16.7 clang-1000.10.25.3)
Target: x86_64-apple-darwin18.0.0
Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: 997ac8a236abe1350659cdaa41a1c63f

Issue Description:

///// main.swift
import Foundation

let name: String = "my name"
let age: UInt32 = 100
let myData: Data = "helloworld".data(using: .utf8)!

let properties: [String: Any] = [
    "name": name,
    "age": age,
    "data": myData
]

do {
    // pack
    let packedData = try PropertyListSerialization.data(fromPropertyList: properties, format: .binary, options: 0)
 
    // unpack
    let unpackedProps = try PropertyListSerialization.propertyList(from: packedData, options: [], format: nil)
 
    guard let newProperties = unpackedProps as? [String: Any] else \{
        fatalError("error: not a dictionary type")
    }
 
    for item in newProperties \{
        print("# \(item.key) => \(item.value)")
    }
 
} catch \{
    print("serialize message error: \(error)")
    exit(0)
}

print("all done")

the output result on my macOS:

> name => my name
> age => 100
> data => <68656c6c 6f776f72 6c64>

the output result on Ubuntu server:

> name => my name
> age => 100
> data => 0 bytes . // <---- HERE

Problem here is the data decoded is "0 bytes"

@belkadan
Copy link

cc @millenomi

@spevans
Copy link
Collaborator

spevans commented Apr 28, 2019

This is now fixed in 4.2.4 by #1958

import Foundation

let name: String = "my name"
let age: UInt32 = 100
let myData: Data = "helloworld".data(using: .utf8)!
let properties: [String: Any] = [
    "name": name,
    "age": age,
    "data": myData
]

do {
    // pack
    let packedData = try PropertyListSerialization.data(fromPropertyList: properties, format: .binary, options: 0)
 
    // unpack
    let unpackedProps = try PropertyListSerialization.propertyList(from: packedData, options: [], format: nil)
 
    guard let newProperties = unpackedProps as? [String: Any] else {
        fatalError("error: not a dictionary type")
    }


    for item in newProperties {
        print("# \(item.key) => \(item.value)")
    }

    print("name:", newProperties["name"] as! String)
    print("age:", newProperties["age"] as! UInt32)
    print("data:", String(data: newProperties["data"] as! Data, encoding: .utf8)!)
 
} catch {
    print("serialize message error: \(error)")
    exit(0)
}

$ ~/swift-4.2.4-RELEASE-ubuntu18.04/usr/bin/swift sr-8115.swift
# age => 100
# name => my name
# data => 10 bytes
name: my name
age: 100
data: helloworld
 

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants