Skip to content

Using String(describing: typedData) on a Starknet typed data containing array or object elements will crash #240

@mluisbrown

Description

@mluisbrown

What happened

StarknetTypedData.Element implements CustomStringConvertible as follows:

extension StarknetTypedData.Element: CustomStringConvertible {
    public var description: String {
        switch self {
        case let .string(s):
            s
        case let .decimal(n):
            String(n)
        case let .signedDecimal(n):
            String(n)
        case let .felt(f):
            f.toHex()
        case let .signedFelt(f):
            f.toHex()
        case let .bool(b):
            String(b)
        case .object:
            String(describing: self)
        case .array:
            String(describing: self)
        }
    }
}

If the element is an object or an array, this will result in an infinite loop, since String(describing: self) will just call description from the CustomStringConvertible implementation again, and again until the stack overflows.

If you add the following test to TypedDataTests.swift you will see the crash:

    func testDescription()  {
        let description = String(describing: Self.CasesRev0.tdFeltArr)
        print(description)
    }

I chose Self.CasesRev0.tdFeltArr as it contains an array element.

Stack trace

Image

Steps to reproduce

  1. Create a StarknetTypedData that has an array or object element.
  2. Pass it to String(describing:)

It will crash.

SDK Version

v0.13.1

Language version

Swift 6.2

Is there an existing issue for this?

  • I have searched the existing issues and verified no issue exits for this problem.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions