Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/more anon race cond improvements #35

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
64 changes: 23 additions & 41 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,58 +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, ...
...
go func(...){
...
$FOO(..., $X, ...)
Vasco-jofra marked this conversation as resolved.
Show resolved Hide resolved
...
}(...)
...
}(...)
...
}
- pattern-not: |
for $Y, $X := range ... {
Expand All @@ -121,6 +101,7 @@ rules:
...
<... $X.$VAR ...>
}(...)
...
}
- pattern-not: |
for $Y, $X := range ... {
Expand All @@ -131,4 +112,5 @@ rules:
...
<... $Y.$VAR ...>
}(...)
...
}
Loading