Skip to content

Commit

Permalink
Merge pull request #35 from hex0punk/fix/more-anon-race-cond-improvem…
Browse files Browse the repository at this point in the history
…ents

Fix/more anon race cond improvements
  • Loading branch information
Vasco-jofra authored Aug 21, 2023
2 parents b365360 + 7db5d05 commit 626932d
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 57 deletions.
113 changes: 107 additions & 6 deletions go/anonymous-race-condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ func main() {
AnonRaceCond_3_FP()
AnonRaceCond_4()
AnonRaceCond_4_FP()
AnonRaceCond_5()
AnonRaceCond_6()
AnonRaceCond_7()
AnonRaceCond_7_FP()
AnonRaceCond_8()
AnonRaceCond_8_FP()
AnonRaceCond_9()
AnonRaceCond_9_FP()
}

func func1() {
Expand All @@ -44,7 +52,6 @@ func func2() {
fmt.Println("I am function func2")
}


func ConcurrentFunctions_TP(fns ...func()) {
var wg sync.WaitGroup
// ruleid: anonymous-race-condition
Expand Down Expand Up @@ -104,7 +111,7 @@ func AnonRaceCond_1_FP() {
func AnonRaceCond_2() {
var wg sync.WaitGroup
// ruleid: anonymous-race-condition
for i := 0; i< len(numbers); i++ {
for i := 0; i < len(numbers); i++ {
wg.Add(1)
go func() {
fmt.Println(i)
Expand All @@ -115,11 +122,10 @@ func AnonRaceCond_2() {
wg.Wait()
}


func AnonRaceCond_2_FP() {
var wg sync.WaitGroup
// ok: anonymous-race-condition
for i := 0; i< len(numbers); i++ {
for i := 0; i < len(numbers); i++ {
wg.Add(1)
go func(n int) {
fmt.Println(n)
Expand All @@ -133,7 +139,7 @@ func AnonRaceCond_2_FP() {
func AnonRaceCond_3_FP() {
var wg sync.WaitGroup
// ok: anonymous-race-condition
for i := 0; i< len(numbers); i++ {
for i := 0; i < len(numbers); i++ {
cpy := i
wg.Add(1)
go func() {
Expand Down Expand Up @@ -174,7 +180,6 @@ func AnonRaceCond_4_FP() {
wg.Wait()
}


func AnonRaceCond_5() {
var wg sync.WaitGroup
// ok: anonymous-race-condition
Expand Down Expand Up @@ -206,3 +211,99 @@ func AnonRaceCond_6() {

wg.Wait()
}

func AnonRaceCond_7() {
var wg sync.WaitGroup
// ruleid: anonymous-race-condition
for idx, val := range values {
wg.Add(1)
fmt.Println(val)
go func() {
fmt.Println("Completed index", idx)
wg.Done()
}()
}

wg.Wait()
}

func AnonRaceCond_7_FP() {
var wg sync.WaitGroup
// ok: anonymous-race-condition
for idx, val := range values {
wg.Add(1)
idx := idx
fmt.Println(val)
go func() {
fmt.Println("Completed index", idx)
wg.Done()
}()
}

wg.Wait()
}

func AnonRaceCond_8() {
var wg sync.WaitGroup
for _, num := range numbers {
// ruleid: anonymous-race-condition
for _, val := range values {
fmt.Println(num)
wg.Add(1)
go func() {
defer wg.Done()
fmt.Println(val)
}()
}
}
wg.Wait()
}

func AnonRaceCond_8_FP() {
var wg sync.WaitGroup
// ok: anonymous-race-condition
for _, num := range numbers {
for _, val := range values {
num, val := num, val
fmt.Println(num)
wg.Add(1)
go func() {
defer wg.Done()
fmt.Println(val)
}()
}
}
wg.Wait()
}

func AnonRaceCond_9() {
var wg sync.WaitGroup
// ruleid: anonymous-race-condition
for _, num := range numbers {
for _, val := range values {
fmt.Println(val)
wg.Add(1)
go func() {
defer wg.Done()
fmt.Println(num)
}()
}
}
wg.Wait()
}

func AnonRaceCond_9_FP() {
var wg sync.WaitGroup
for _, num := range numbers {
// ruleid: anonymous-race-condition
for _, val := range values {
fmt.Println(num)
wg.Add(1)
go func() {
defer wg.Done()
fmt.Println(val)
}()
}
}
wg.Wait()
}
72 changes: 21 additions & 51 deletions go/anonymous-race-condition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ rules:
}(...)
...
}
- pattern: |
for $Y, $X := range ... {
...
go func(...){
...
$FOO(..., $Y, ...)
...
}(...)
...
}
- pattern: |
for $Y, $X := range ... {
...
Expand Down Expand Up @@ -59,68 +69,28 @@ rules:
...
}
- pattern-not: |
for $X, $Y := range ... {
...
go func(..., $V, ...){
...
$FOO(..., $V, ...)
...
}(..., $Y, ...)
...
}
- pattern-not: |
for $Y, $X := range ... {
...
go func(..., $CP, ...){
...
$CP(...)
...
}($X)
...
}
- pattern-not: |
for $X:=...;$Y;$Z {
...
go func(..., $V, ...) {
...
$FOO(..., $V,...)
...
}(..., $X, ...)
...
}
- pattern-not: |
for $X, $Y := range ... {
for ..., $X := range ... {
...
$COPY := $Y
..., $X := ..., $X
...
go func(...){
...
$FOO(..., $COPY, ...)
$FOO(..., $X, ...)
...
}(...)
...
}
- pattern-not: |
for $X:=...;$Y;$Z {
...
$COPY := $X
...
go func(...) {
for ..., $X := range ... {
...
$FOO(..., $COPY,...)
$X, ... := $X, ...
...
}(...)
...
}
- pattern-not: |
for $Y, $X := range ... {
...
$Y, $X := $Y, $X
...
go func(...){
go func(...){
...
$FOO(..., $X, ...)
...
}(...)
...
<... $X.$VAR ...>
}(...)
}
- pattern-not: |
for $Y, $X := range ... {
Expand All @@ -129,6 +99,6 @@ rules:
...
go func(...){
...
<... $Y.$VAR ...>
}(...)
...
}

0 comments on commit 626932d

Please sign in to comment.