-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ruleguard: pass imports table to gogrep pattern compiler (#343)
This avoids any miscompiled patterns as well as allows more precise matching of package symbols in function call contexts.
- Loading branch information
Showing
6 changed files
with
63 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package imports | ||
|
||
import ( | ||
crand "crypto/rand" | ||
"math/rand" | ||
) | ||
|
||
func _() { | ||
_, _ = crand.Read(nil) // want `\Qcrypto/rand` | ||
_, _ = rand.Read(nil) // want `\Qmath/rand` | ||
} | ||
|
||
func _() { | ||
_, _ = rand.Read(nil) // want `\Qmath/rand` | ||
_, _ = crand.Read(nil) // want `\Qcrypto/rand` | ||
} | ||
|
||
func _() { | ||
var rand distraction | ||
_, _ = rand.Read(nil) | ||
} | ||
|
||
type distraction struct{} | ||
|
||
func (distraction) Read(p []byte) (int, error) { | ||
return 0, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//go:build ignore | ||
// +build ignore | ||
|
||
package gorules | ||
|
||
import ( | ||
"github.com/quasilyte/go-ruleguard/dsl" | ||
) | ||
|
||
func testMathRand(m dsl.Matcher) { | ||
m.Match(`rand.Read($*_)`).Report(`math/rand`) | ||
} | ||
|
||
func testCryptoRand(m dsl.Matcher) { | ||
m.Import(`crypto/rand`) | ||
m.Match(`rand.Read($*_)`).Report(`crypto/rand`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters