From a13c35041d48558c621662a20b42c68f286d6afa Mon Sep 17 00:00:00 2001 From: Alex Useche <1915998+hex0punk@users.noreply.github.com> Date: Fri, 14 Jul 2023 03:46:17 -0700 Subject: [PATCH] added a fix for FPs found by anonymous-race-condition (#28) --- go/anonymous-race-condition.go | 33 ++++++++++++++++++++++++++++++++ go/anonymous-race-condition.yaml | 20 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/go/anonymous-race-condition.go b/go/anonymous-race-condition.go index 700b2c0..1bf3af0 100644 --- a/go/anonymous-race-condition.go +++ b/go/anonymous-race-condition.go @@ -173,3 +173,36 @@ func AnonRaceCond_4_FP() { wg.Wait() } + + +func AnonRaceCond_5() { + var wg sync.WaitGroup + // ok: anonymous-race-condition + for idx, val := range values { + wg.Add(1) + idx, val := idx, val + go func() { + val.s.Method("test") + fmt.Println("Completed index", idx) + wg.Done() + }() + } + + wg.Wait() +} + +func AnonRaceCond_6() { + var wg sync.WaitGroup + // ok: anonymous-race-condition + for idx, val := range values { + wg.Add(1) + idx, val := idx, val + go func() { + fmt.Println(val.s) + fmt.Println("Completed index", idx) + wg.Done() + }() + } + + wg.Wait() +} diff --git a/go/anonymous-race-condition.yaml b/go/anonymous-race-condition.yaml index c4c194c..9f5618d 100644 --- a/go/anonymous-race-condition.yaml +++ b/go/anonymous-race-condition.yaml @@ -112,3 +112,23 @@ rules: }(...) ... } + - pattern-not: | + for $Y, $X := range ... { + ... + $Y, $X := $Y, $X + ... + go func(...){ + ... + <... $X.$VAR ...> + }(...) + } + - pattern-not: | + for $Y, $X := range ... { + ... + $Y, $X := $Y, $X + ... + go func(...){ + ... + <... $Y.$VAR ...> + }(...) + }