Skip to content

Commit

Permalink
go lock rules - defer FP fix
Browse files Browse the repository at this point in the history
  • Loading branch information
GrosQuildu committed Apr 3, 2024
1 parent 17b707c commit b7ed449
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 0 deletions.
70 changes: 70 additions & 0 deletions go/missing-runlock-on-rwmutex.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func (c *RWContainer) inc(name string) error {
return fmt.Errorf("letter not allowed")
}
c.mu.RUnlock()
// ok: missing-runlock-on-rwmutex
return nil
}

Expand All @@ -84,6 +85,7 @@ func (c *RWContainer) inc_FP(name string) error {
return fmt.Errorf("letter not allowed")
}
c.mu.RUnlock()
// ok: missing-runlock-on-rwmutex
return nil
}

Expand All @@ -102,3 +104,71 @@ func (c *RWContainer) inc5(name string) error {
// ok: missing-runlock-on-rwmutex
return nil
}

func (c *RWContainer) inc6(name string) error {
c.mu.RLock()
c.counters[name]++
defer func() {
fmt.Println("before runlock")
c.mu.RUnlock()
fmt.Println("after runlock")
}()
// ok: missing-runlock-on-rwmutex
return nil
}

func (c *RWContainer) inc6b(name string) error {
c.mu.RLock()
unlocker := c.mu.RUnlock
c.counters[name]++
defer func() {
fmt.Println("before runlock")
unlocker()
fmt.Println("after runlock")
}()
// todook: missing-runlock-on-rwmutex
return nil
}

func (c *RWContainer) inc7(name string) error {
c.mu.RLock()
c.counters[name]++
defer func(earlyExit bool) {
fmt.Println("before runlock")
if (earlyExit) {
// todoruleid: missing-runlock-on-rwmutex
return
}
c.mu.RUnlock()
fmt.Println("after runlock")
}(false)
// ok: missing-runlock-on-rwmutex
return nil
}

func (c *RWContainer) inc8(name string) error {
c.mu.RLock()
c.counters[name]++
_, err := fmt.Println("test if")
if err != nil {
c.mu.RUnlock()
// ok: missing-runlock-on-rwmutex
return nil, err
}
// ruleid: missing-runlock-on-rwmutex
return nil
}

func (c *RWContainer) inc8b(name string) error {
c.mu.RLock()
c.counters[name]++
unlocker := c.mu.RUnlock
_, err := fmt.Println("test if")
if err != nil {
unlocker()
// todook: missing-runlock-on-rwmutex
return nil, err
}
// ruleid: missing-runlock-on-rwmutex
return nil
}
7 changes: 7 additions & 0 deletions go/missing-runlock-on-rwmutex.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ rules:
- pattern-not-inside: |
defer $T.RUnlock()
...
- pattern-not-inside: |
defer func(...) {
...
$T.RUnlock()
...
}(...)
...
- pattern-not-inside: |
$FOO(..., ..., func(...) {
...
Expand Down
70 changes: 70 additions & 0 deletions go/missing-unlock-before-return.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func (c *Container) inc(name string) error {
return fmt.Errorf("letter not allowed")
}
c.mu.Unlock()
// ok: missing-unlock-before-return
return nil
}

Expand All @@ -73,6 +74,7 @@ func (c *Container) inc_FP(name string) error {
return fmt.Errorf("letter not allowed")
}
c.mu.Unlock()
// ok: missing-unlock-before-return
return nil
}

Expand All @@ -86,6 +88,7 @@ func (c *Container) inc2(name string) error {
panic("letter not allowed")
}
c.mu.Unlock()
// ok: missing-unlock-before-return
return nil
}

Expand All @@ -98,6 +101,7 @@ func (c *Container) inc2_FP(name string) error {
panic("letter not allowed")
}
c.mu.Unlock()
// ok: missing-unlock-before-return
return nil
}

Expand Down Expand Up @@ -129,4 +133,70 @@ func (c *Container) inc5(name string) error {
return nil
}

func (c *Container) inc6(name string) error {
c.mu.Lock()
c.counters[name]++
defer func() {
fmt.Println("before unlock")
c.mu.Unlock()
fmt.Println("after unlock")
}()
// ok: missing-unlock-before-return
return nil
}

func (c *Container) inc6b(name string) error {
c.mu.Lock()
unlocker := c.mu.Unlock
c.counters[name]++
defer func() {
fmt.Println("before unlock")
unlocker()
fmt.Println("after unlock")
}()
// todook: missing-unlock-before-return
return nil
}

func (c *Container) inc7(name string) error {
c.mu.Lock()
c.counters[name]++
defer func(earlyExit bool) {
fmt.Println("before unlock")
if (earlyExit) {
// todoruleid: missing-unlock-before-return
return
}
c.mu.Unlock()
fmt.Println("after unlock")
}(false)
// ok: missing-unlock-before-return
return nil
}

func (c *Container) inc8(name string) error {
c.mu.Lock()
c.counters[name]++
_, err := fmt.Println("test if")
if err != nil {
c.mu.Unlock()
// ok: missing-unlock-before-return
return nil, err
}
// ruleid: missing-unlock-before-return
return nil
}

func (c *Container) inc8b(name string) error {
c.mu.Lock()
c.counters[name]++
unlocker := c.mu.Unlock
_, err := fmt.Println("test if")
if err != nil {
unlocker()
// todook: missing-unlock-before-return
return nil, err
}
// ruleid: missing-unlock-before-return
return nil
}
7 changes: 7 additions & 0 deletions go/missing-unlock-before-return.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ rules:
- pattern-not-inside: |
defer $T.Unlock()
...
- pattern-not-inside: |
defer func(...) {
...
$T.Unlock()
...
}(...)
...
- pattern-not-inside: |
$FOO(..., ..., func(...) {
...
Expand Down

0 comments on commit b7ed449

Please sign in to comment.