Skip to content

Commit 8d288b7

Browse files
committed
fix(libevm/legacy): disallow remaining gas higher than input gas in PrecompiledStatefulContract
1 parent e2b0abb commit 8d288b7

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

libevm/legacy/legacy.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,31 @@
1818
// equivalents.
1919
package legacy
2020

21-
import "github.com/ava-labs/libevm/core/vm"
21+
import (
22+
"errors"
23+
"fmt"
24+
25+
"github.com/ava-labs/libevm/core/vm"
26+
)
2227

2328
// PrecompiledStatefulContract is the legacy signature of
2429
// [vm.PrecompiledStatefulContract], which explicitly accepts and returns gas
2530
// values. Instances SHOULD NOT use the [vm.PrecompileEnvironment]
2631
// gas-management methods as this may result in unexpected behaviour.
2732
type PrecompiledStatefulContract func(env vm.PrecompileEnvironment, input []byte, suppliedGas uint64) (ret []byte, remainingGas uint64, err error)
2833

34+
var (
35+
ErrGasRemainingExceedsGasSupplied = errors.New("remaining gas exceeds supplied gas")
36+
)
37+
2938
// Upgrade converts the legacy precompile signature into the now-required form.
3039
func (c PrecompiledStatefulContract) Upgrade() vm.PrecompiledStatefulContract {
3140
return func(env vm.PrecompileEnvironment, input []byte) ([]byte, error) {
3241
gas := env.Gas()
3342
ret, remainingGas, err := c(env, input, gas)
43+
if remainingGas > gas {
44+
return nil, fmt.Errorf("%w: %d > %d", ErrGasRemainingExceedsGasSupplied, remainingGas, gas)
45+
}
3446
if used := gas - remainingGas; used < gas {
3547
env.UseGas(used)
3648
}

0 commit comments

Comments
 (0)