jpsim / Yams

A Sweet and Swifty YAML parser.
https://jpsim.com/Yams
MIT License
1.12k stars 144 forks source link

Issue with sexagesimal support #337

Closed kean closed 8 months ago

kean commented 2 years ago

Hi,

I've encountered an issue because of the sexagesimal support in Yams. When you work with scalars, you can expect the Int initializer to return nil in the following scenarios:

Int("a") // return nil
Int("a:b") // returns nil

But YAMLDecoder has support for "sexagesimal" numbers (which was dropped from the YAML spec in 2009). If you use singleValueContainer and try to decode Int using YAMLDecoder, like this:

let container = try decoder.singleValueContainer()
let value = try? container.decode(Int.self)

It hits the scalar.contains(":") condition and at this point, the parser is convinced it is a number and never bails out, even if it's not a proper sexagesimal number. So the Int.self decoding produces the following result:

decodeIntFrom("a") // returns nil
decodeIntFrom("a:b") // returns `0`
decodeIntFrom("https://example.com/webhook") // returns `0`

It breaks certain assumptions that are true for JSONDecoder. I'm not sure if I did a great job describing the issue, but I suggest revising support of pre YAML 1.2 features.

kean commented 2 years ago

I found the following note on wiki:

In version 1.1[17] of the YAML data storage format, sexagesimals are supported for plain scalars, and formally specified both for integers[18] and floating point numbers.[19] This has led to confusion, as e.g. some MAC addresses would be recognised as sexagesimals and loaded as integers, where others were not and loaded as strings. In YAML 1.2 support for sexagesimals was dropped.[20]

That's basically exactly the issue I'm encountering. But I think it's a bit worse in Yams because it thinks that anything with ":" is an integer, e.g. "https://example.com/webhook" is an integer.