XMLParsing icon indicating copy to clipboard operation
XMLParsing copied to clipboard

Incorrect unboxing of integer hex values

Open pierremolinaro opened this issue 6 years ago • 0 comments

Thank you very much for your work.

I think I have found a little bug, decoding a hexadecimal value can be rounded, for example 0x400ff040 returns 0x400ff000.

The bug is in the inbox functions for integer types, such as internal func unbox(_ value: Any, as type: UInt32.Type) throws -> UInt32? function.

If valueencodes an hex integer number, it is internally converted in a float number by the instruction let number = NSNumber(value: value): just print number for checking.

So I have modified the function as :

internal func unbox(_ value: Any, as type: UInt32.Type) throws -> UInt32? {
  if let string = value as? String {
    if string.starts (with: "0x"), let uint32 = UInt32 (string.dropFirst (2), radix: 16) {
      return uint32
    }else if let uint32 = UInt32 (string) { // Decimal
      return uint32
    }else{
      throw DecodingError._typeMismatch (at: self.codingPath, expectation: type, reality: value)
    }
  }else{
    return nil
  }
}

Best regards,

Pierre Molinaro

pierremolinaro avatar Dec 08 '19 11:12 pierremolinaro