Skip to content

strtoul calls not handled properly #28

@pacu

Description

@pacu

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

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