Skip to content

Commit e37bdca

Browse files
committed
- Safe getting bounds from data slices in decoding added
1 parent e33de2e commit e37bdca

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

Sources/Web3Core/Contract/ContractProtocol.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ extension ContractProtocol {
210210

211211
func decodeInputData(_ data: Data) -> [String: Any]? {
212212
guard data.count >= 4 else { return nil }
213-
let methodId = data[0..<4].toHexString()
214-
let data = data[4...]
213+
let methodId = data[data.indices.startIndex..<data.indices.startIndex + 4].toHexString()
214+
let data = data[data.indices.startIndex + 4...]
215215
return decodeInputData(methodId, data: data)
216216
}
217217
}
@@ -333,14 +333,14 @@ extension DefaultContractProtocol {
333333

334334
public func decodeInputData(_ data: Data) -> [String: Any]? {
335335
guard data.count % 32 == 4 else { return nil }
336-
let methodSignature = data[0..<4].toHexString().addHexPrefix().lowercased()
336+
let methodSignature = data[data.indices.startIndex ..<data.indices.startIndex + 4].toHexString().addHexPrefix().lowercased()
337337

338338
guard let function = methods[methodSignature]?.first else { return nil }
339-
return function.decodeInputData(Data(data[4 ..< data.count]))
339+
return function.decodeInputData(Data(data[data.indices.startIndex + 4 ..< data.indices.startIndex + data.count]))
340340
}
341341

342342
public func getFunctionCalled(_ data: Data) -> ABI.Element.Function? {
343343
guard data.count >= 4 else { return nil }
344-
return methods[data[0..<4].toHexString().addHexPrefix()]?.first
344+
return methods[data[data.indices.startIndex..<data.indices.startIndex + 4].toHexString().addHexPrefix()]?.first
345345
}
346346
}

Sources/Web3Core/EthereumABI/ABIElements.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,9 @@ extension ABI.Element.Function {
397397
/// 4) `messageLength` is used to determine where message bytes end to decode string correctly.
398398
/// 5) The rest of the `data` must be 0 bytes or empty.
399399
if data.bytes.count >= 100,
400-
Data(data[0..<4]) == Data.fromHex("08C379A0"),
401-
BigInt(data[4..<36]) == 32,
402-
let messageLength = Int(Data(data[36..<68]).toHexString(), radix: 16),
400+
Data(data[data.indices.startIndex..<data.indices.startIndex + 4]) == Data.fromHex("08C379A0"),
401+
BigInt(data[data.indices.startIndex + 4..<data.indices.startIndex + 36]) == 32,
402+
let messageLength = Int(Data(data[data.indices.startIndex + 36..<data.indices.startIndex + 68]).toHexString(), radix: 16),
403403
let message = String(bytes: data.bytes[68..<(68+messageLength)], encoding: .utf8),
404404
(68+messageLength == data.count || data.bytes[68+messageLength..<data.count].reduce(0) { $0 + $1 } == 0) {
405405
return ["_success": false,
@@ -410,11 +410,11 @@ extension ABI.Element.Function {
410410

411411
if data.count >= 4,
412412
let errors = errors,
413-
let customError = errors[data[0..<4].toHexString().stripHexPrefix()] {
413+
let customError = errors[data[data.indices.startIndex + 0..<data.indices.startIndex + 4].toHexString().stripHexPrefix()] {
414414
var errorResponse: [String: Any] = ["_success": false, "_abortedByRevertOrRequire": true, "_error": customError.errorDeclaration]
415415

416416
if (data.count > 32 && !customError.inputs.isEmpty),
417-
let decodedInputs = ABIDecoder.decode(types: customError.inputs, data: Data(data[4..<data.count])) {
417+
let decodedInputs = ABIDecoder.decode(types: customError.inputs, data: Data(data[data.indices.startIndex + 4..<data.indices.startIndex + data.count])) {
418418
for idx in decodedInputs.indices {
419419
errorResponse["\(idx)"] = decodedInputs[idx]
420420
if !customError.inputs[idx].name.isEmpty {

0 commit comments

Comments
 (0)