Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

UUID.properties() crashes because "0" is not a valid UUID #87

@hashemi

Description

@hashemi

This code crashes in the beta branch:

import CodableKit
import Foundation

_ = UUID.properties()

Producing the following error:

Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "Attempted to decode UUID from invalid UUID string.", underlyingError: nil))

This also happens whenever Decodable.properties() is called on a type that has a UUID field.

This happens because CodingKeyCollector works by pretending to decode a type. When it encounters a String, it tries to decode "0":

    func decode(_ type: String.Type) throws -> String {
        result.add(type: type, atPath: codingPath)
        return "0"
    }

I was able to temporarily fix the crasher by changing that function in CodingKeyCollectorSingleValueDecoder to:

    func decode(_ type: String.Type) throws -> String {
        result.add(type: type, atPath: codingPath)
        return "00000000-0000-0000-0000-000000000000"
    }

A slightly more robust solution is to add a decode(_ type: UUID.Type) -> UUID that handles that special case.

Still, this only fixes this one case and doesn't address the class of potential errors that arise from assuming that any Decodable can be successfully decoded with an arbitrary combination of values (false, "0", 0).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions