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

strtoul calls not handled properly #28

Open
pacu opened this issue Sep 2, 2020 · 0 comments
Open

strtoul calls not handled properly #28

pacu opened this issue Sep 2, 2020 · 0 comments

Comments

@pacu
Copy link

pacu commented Sep 2, 2020

from man strtoul

RETURN VALUES
The strtoul(), strtoull(), strtoumax() and strtouq() functions return
either the result of the conversion or, if there was a leading minus
sign, the negation of the result of the conversion, unless the original
(non-negated) value would overflow; in the latter case, strtoul() returns
ULONG_MAX, strtoull() returns ULLONG_MAX, strtoumax() returns
UINTMAX_MAX, and strtouq() returns ULLONG_MAX. In all cases, errno is
set to ERANGE. If no conversion could be performed, 0 is returned and
the global variable errno is set to EINVAL (the last feature is not por-
table across all platforms).

strtoul function returns values on error which are taken as valid inputs and carried over to the result of the function call

public extension String {

public extension String {
  func mnemonicData() -> Data {
    let length = self.count
    let dataLength = length / 2
    var dataToReturn = Data(capacity: dataLength)

    var outIndex = 0
    var outChars = ""
    for (_, char) in enumerated() {
      outChars += String(char)
      if outIndex % 2 == 1 {
        let i: UInt8 = UInt8(strtoul(outChars, nil, 16))
        dataToReturn.append(i)
        outChars = ""
      }
      outIndex += 1
    }

    return dataToReturn
  }
}
  • solution *
    Use Int(outChars, radix: 16) instead and care for nil value
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