xhd2015/xgo

Error while mocking json.Marshall

Closed this issue · 2 comments

No of fields for results object should be equal to 2 but got 1, giving index out of bound error

Marshal(v any) ([]byte, error) {}

mock.Mock(json.Marshal, func(ctx context.Context, fn *core.FuncInfo, args core.Object, results core.Object) error {
					fmt.Println(fn.Name)
					fmt.Printf("Args: %+v\n", fn.ArgNames)
					fmt.Println(fmt.Sprintf("No of fields: %d", results.NumField()))
					results.GetFieldIndex(0).Set(nil)
					results.GetFieldIndex(1).Set(errors.New("error"))
					return nil
				})

Marshal
Args: [v]
No of fields: 1
--- FAIL: TestHandlePortScan (0.00s)
    --- FAIL: TestHandlePortScan/2:_error_during_json_marshalling (0.00s)
panic: runtime error: index out of range [1] with length 1 [recovered]
        panic: runtime error: index out of range [1] with length 1

goroutine 30 [running]:
testing.tRunner.func1.2({0x10e01aae0, 0x1400097eb10})
        /Users/shubham.dogra/.xgo/go-instrument/go1.22.0_us_lo_go_fa839386/go1.22.0/src/testing/testing.go:1642 +0x270
testing.tRunner.func1()
        /Users/shubham.dogra/.xgo/go-instrument/go1.22.0_us_lo_go_fa839386/go1.22.0/src/testing/testing.go:1645 +0x3b0
panic({0x10e01aae0?, 0x1400097eb10?})
        /Users/shubham.dogra/.xgo/go-instrument/go1.22.0_us_lo_go_fa839386/go1.22.0/src/runtime/panic.go:770 +0x124
github.com/xhd2015/xgo/runtime/trap.object.GetFieldIndex({0x14003c2ab00?, 0x140003ca420?, 0x0?}, 0x0?)
        /Users/shubham.dogra/go/pkg/mod/github.com/xhd2015/xgo/runtime@v1.0.43/trap/object.go:55 +0x54
exploit/internal/server.TestHandlePortScan.func1.1({0x10e307b60, 0x110d81800}, 0x140002e9680, {0x10e303480, 0x140003ca438}, {0x10e3034b0, 0x14003c2ab40})
        /Users/shubham.dogra/GolandProjects/exploit/internal/server/controller_test.go:139 +0x46c
github.com/xhd2015/xgo/runtime/mock.Mock.mock.func1({0x10e307b60, 0x110d81800}, 0x140002e9680, {0x10e303480, 0x140003ca438}, {0x10e3034b0, 0x14003c2ab40})
        /Users/shubham.dogra/go/pkg/mod/github.com/xhd2015/xgo/runtime@v1.0.43/mock/mock.go:133 +0x1e8
github.com/xhd2015/xgo/runtime/trap.trap(0x140002e9680, 0x1023b2950, {0x0, 0x0}, {0x1400295f6d0, 0x1, 0x1}, {0x1400216a8e0, 0x2, 0x2})
        /Users/shubham.dogra/go/pkg/mod/github.com/xhd2015/xgo/runtime@v1.0.43/trap/trap.go:307 +0x6f8
github.com/xhd2015/xgo/runtime/trap.trapFunc({0x10710b848, 0xd}, {0x10709f757, 0x7}, 0x0, 0x1023b2950, {0x0, 0x0}, {0x1400295f6d0, 0x1, ...}, ...)
        /Users/shubham.dogra/go/pkg/mod/github.com/xhd2015/xgo/runtime@v1.0.43/trap/trap.go:144 +0x170
encoding/json.__xgo_link_trap_for_generated({0x10710b848?, 0x1?}, 0x1?, {0x10709f757?, 0x50?}, 0x30?, {0x0?, 0x0?}, {0x1400295f6d0?, 0x140027837c0?, ...}, ...)
        /Users/shubham.dogra/GolandProjects/exploit/__xgo_autogen_register_func_helper.go:51 +0x38
encoding/json.Marshal({0x10e05f9c0, 0x140027837c0})
        /Users/shubham.dogra/.xgo/go-instrument/go1.22.0_us_lo_go_fa839386/go1.22.0/src/encoding/json/encode.go:159 +0x168
exploit/internal/server.HandlePortScan(0x1400153aa00)
        /Users/shubham.dogra/GolandProjects/exploit/internal/server/controller.go:84 +0x294
exploit/internal/server.TestHandlePortScan.func2(0x1400249ab60)
        /Users/shubham.dogra/GolandProjects/exploit/internal/server/controller_test.go:154 +0x1dc
testing.tRunner(0x1400249ab60, 0x14002937810)
        /Users/shubham.dogra/.xgo/go-instrument/go1.22.0_us_lo_go_fa839386/go1.22.0/src/testing/testing.go:1709 +0x23c
created by testing.(*T).Run in goroutine 29
        /Users/shubham.dogra/.xgo/go-instrument/go1.22.0_us_lo_go_fa839386/go1.22.0/src/testing/testing.go:1762 +0x320

When the last result is error, it will be not available through fields.

You should use results.(core.ObjectWithErr).GetErr().Set(errors.New("error"))

this is fixed thanks!