From 4b6c936375557cfa9083402f7357e5a4a84098ab Mon Sep 17 00:00:00 2001 From: ruomenger Date: Sat, 7 Oct 2023 10:50:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E4=B8=80=E4=BA=9B=E8=AD=A6=E5=91=8A=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. yoda conditions,go中不需要将常量放在等于判断的前部 2. 使用 strings.Contains 替换 strings.Index 判断子串是否存在 3. 使用 unsafe.Slice 替换 reflect.SliceHeader 4. 移除未使用的函数 unpatch 5. 获取 map 中的 value 后并未使用 --- monkey.go | 10 +++------- monkey_test.go | 18 +++++++++--------- replace.go | 7 +------ 3 files changed, 13 insertions(+), 22 deletions(-) diff --git a/monkey.go b/monkey.go index eb953c7..25e763b 100644 --- a/monkey.go +++ b/monkey.go @@ -84,7 +84,7 @@ func checkStructMonkeyType(a, b reflect.Type) bool { t1 := a.In(0).String() t2 := b.In(0).String() - if strings.Index(t2, "__monkey__") == -1 { + if !strings.Contains(t2, "__monkey__") { return false } @@ -158,12 +158,12 @@ func PatchEmpty(target interface{}) { t := reflect.ValueOf(target).Pointer() - p, ok := patches[t] + _, ok := patches[t] if ok { return } - p = &patch{from: t} + p := &patch{from: t} patches[t] = p p.Apply() } @@ -207,10 +207,6 @@ func unpatchValue(target reflect.Value) bool { return patch.Del() } -func unpatch(target uintptr, p *patch) { - copyToLocation(target, p.original) -} - type patch struct { from uintptr realFrom uintptr diff --git a/monkey_test.go b/monkey_test.go index 5900fbf..50258e1 100644 --- a/monkey_test.go +++ b/monkey_test.go @@ -178,19 +178,19 @@ func TestEmpty(t *testing.T) { func TestG(t *testing.T) { monkey.Patch(foo, bar) - assert(t, -1 == foo(1, 2)) + assert(t, foo(1, 2) == -1) var wg sync.WaitGroup wg.Add(2) go func() { defer wg.Done() - assert(t, 3 == foo(1, 2)) + assert(t, foo(1, 2) == 3) }() go func() { monkey.Patch(foo, func(a, b int) int { return a * b }) defer wg.Done() - assert(t, 2 == foo(1, 2)) + assert(t, foo(1, 2) == 2) }() wg.Wait() } @@ -204,12 +204,12 @@ func TestGlobal(t *testing.T) { wg.Add(1) go func() { defer wg.Done() - assert(t, 2 == math.Abs(1)) + assert(t, math.Abs(1) == 2) }() wg.Wait() g0.Unpatch() - assert(t, 1 == math.Abs(1)) + assert(t, math.Abs(1) == 1) } func add2[T int | float64](i, j T) T { @@ -222,15 +222,15 @@ func (s *S2__monkey__[T]) Foo() T { return s.I * 2 } func TestGeneric(t *testing.T) { g1 := monkey.Patch(demo.Add[int], add2[int], monkey.OptGeneric) - assert(t, -1 == demo.Add(1, 2)) + assert(t, demo.Add(1, 2) == -1) g1.Unpatch() - assert(t, 3 == demo.Add(1, 2)) + assert(t, demo.Add(1, 2) == 3) g2 := monkey.Patch((*demo.S2[int]).Foo, (*S2__monkey__[int]).Foo, monkey.OptGeneric) s := demo.S2[int]{I: 2} - assert(t, 4 == s.Foo()) + assert(t, s.Foo() == 4) g2.Unpatch() - assert(t, 2 == s.Foo()) + assert(t, s.Foo() == 2) } type TreeNode struct { diff --git a/replace.go b/replace.go index da176c3..2784098 100644 --- a/replace.go +++ b/replace.go @@ -1,17 +1,12 @@ package monkey import ( - "reflect" "syscall" "unsafe" ) func rawMemoryAccess(p uintptr, length int) []byte { - return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{ - Data: p, - Len: length, - Cap: length, - })) + return unsafe.Slice((*byte)(unsafe.Pointer(p)), length) } func pageStart(ptr uintptr) uintptr {