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

Incorrect unboxing of integer hex values #51

Open
pierremolinaro opened this issue Dec 8, 2019 · 0 comments
Open

Incorrect unboxing of integer hex values #51

pierremolinaro opened this issue Dec 8, 2019 · 0 comments

Comments

@pierremolinaro
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant