Skip to content

Incorrect unboxing of integer hex values #51

@pierremolinaro

Description

@pierremolinaro

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions