github-vet / github-vet/rangeloop-pointer-findings

Tenderly/tenderly-cli: stacktrace/core.go; 91 LoC

Open
#11,801 0 comments 0 reactions 0 assignees View on GitHub
fresh medium
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [Tenderly/tenderly-cli](https://www.github.com/Tenderly/tenderly-cli) at [stacktrace/core.go](https://github.com/Tenderly/tenderly-cli/blob/fee9b0ccde98fc153b10174aaa98bf69ce86528a/stacktrace/core.go#L106-L196)

Below is the message reported by the analyzer for this snippet of code. Beware that the analyzer only reports the first
issue it finds, so please do not limit your consideration to the contents of the below message.

> reference to state was used in a composite literal at line 174

[Click here to see the code in its original context.](https://github.com/Tenderly/tenderly-cli/blob/fee9b0ccde98fc153b10174aaa98bf69ce86528a/stacktrace/core.go#L106-L196)

Click here to show the 91 line(s) of Go which triggered the analyzer.

```go
for _, state := range txTrace.States() {
contract := c.stack.Get()

op := vm.OpCode(contract.Bytecode[state.Pc()])

switch op {
case vm.CALL:
stack := state.Stack()
if stack == nil {
log.Println("didn't find stack but expected one: ", contractHash)
c.stack.Push(contract)
break
}

newAddress := "0x" + stack[len(stack)-2][24:]

newContract, err := c.Contracts.Get(newAddress)
if err != nil {
return nil, fmt.Errorf("cannot call contract [%s]: %s", newAddress, err)
}

c.stack.Push(newContract)
case vm.RETURN, INVALID_OPCODE, vm.REVERT, vm.STOP:
c.stack.Pop()
}

// If last in calling block && not a terminating op, it's an invalid opcode situation.
if parityState, ok := state.(*parity.VmState); ok && parityState.Terminating {
switch op {
case vm.RETURN, vm.REVERT, vm.STOP:
default:
log.Printf("Previous opcode: %s, changing to INVALID OPCODE", op.String())
op = INVALID_OPCODE
}
}

if op == vm.REVERT || op == INVALID_OPCODE {
recordStackFrames = true
}

im := contract.SourceMap[int(state.Pc())]
if im == nil {
//@TODO: Abort, with error message.
log.Printf("MISSING SOURCE MAP: %s %d", contractHash, int(state.Pc()))
continue
}

if im.FileIndex == -1 {
//@TODO: Check if reverts keep getting excluded due to this.
//fmt.Printf("INTERNAL => %d\t%s\n", state.Pc(), op.String())
continue
}

code := string(contract.Source[im.Start : im.Start+im.Length])

frame := &Frame{
File: contract.Name,

Line: im.Line,
Column: im.Column,

Start: im.Start,
Length: im.Length,

Text: strings.Split(code, "\n")[0],

Op: op.String(),

State: &state,
Mapping: im,
}

if EnableDebugLogging {
log.Printf("%d\t%s\n", state.Pc(), frame)
}

if recordStackFrames {
stackFrames = append(stackFrames, &StackFrame{
ContractAddress: NewContractAddress(contract.Hash),
ContractName: contract.Name,
Line: frame.Line,

Code: getLineFromContract(contract.Source, frame.Line),
Op: op.String(),
Start: frame.Start,
Length: frame.Length,
})
}

stackTrace.PushFrame(frame)
}

```

Leave a reaction on this issue to contribute to the project by classifying this instance as a **Bug** :-1:, **Mitigated** :+1:, or **Desirable Behavior** :rocket:
See the descriptions of the classifications [here](https://github.com/github-vet/rangeclosure-findings#how-can-i-help) for more information.

commit ID: fee9b0ccde98fc153b10174aaa98bf69ce86528a

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.