Add static analyzer to discourage use of panic() (#15075)

* Implement static analysis to prevent panics

* Add nopanic to nogo

* Fix violations and add exclusions

Fix violations and add exclusions for all

* Changelog fragment

* Use pass.Report instead of pass.Reportf

* Remove strings.ToLower for checking init method name

* Add exclusion for herumi init

* Move api/client/beacon template function to init and its own file

* Fix nopanic testcase
This commit is contained in:
Preston Van Loon
2025-03-19 13:04:15 -05:00
committed by GitHub
parent 16d5abd21b
commit 2aa52fb56a
74 changed files with 329 additions and 104 deletions

View File

@@ -3,6 +3,7 @@ package herumi
import "github.com/herumi/bls-eth-go-binary/bls"
// Init allows the required curve orders and appropriate sub-groups to be initialized.
// lint:nopanic -- This method is called at init time only.
func Init() {
if err := bls.Init(bls.BLS12_381); err != nil {
panic(err)

View File

@@ -13,7 +13,7 @@ func hashParallel(inputList [][32]byte, outputList [][32]byte, wg *sync.WaitGrou
defer wg.Done()
err := gohashtree.Hash(outputList, inputList)
if err != nil {
panic(err)
panic(err) // lint:nopanic -- This should never panic.
}
}
@@ -27,7 +27,7 @@ func VectorizedSha256(inputList [][32]byte) [][32]byte {
if len(inputList) < minSliceSizeToParallelize {
err := gohashtree.Hash(outputList, inputList)
if err != nil {
panic(err)
panic(err) // lint:nopanic -- This should never panic.
}
return outputList
}
@@ -40,7 +40,7 @@ func VectorizedSha256(inputList [][32]byte) [][32]byte {
}
err := gohashtree.Hash(outputList[n*groupSize:], inputList[n*2*groupSize:])
if err != nil {
panic(err)
panic(err) // lint:nopanic -- This should never panic.
}
wg.Wait()
return outputList

View File

@@ -130,7 +130,7 @@ func EncryptKey(key *Key, password string, scryptN, scryptP int) ([]byte, error)
authArray := []byte(password)
salt := make([]byte, 32)
if _, err := io.ReadFull(rand.Reader, salt); err != nil {
panic("reading from crypto/rand failed: " + err.Error())
panic("reading from crypto/rand failed: " + err.Error()) // lint:nopanic -- This should never happen.
}
derivedKey, err := scrypt.Key(authArray, salt, scryptN, scryptR, scryptP, scryptDKLen)

View File

@@ -57,7 +57,7 @@ func (_ *source) Uint64() (val uint64) {
lock.RLock()
defer lock.RUnlock()
if err := binary.Read(rand.Reader, binary.BigEndian, &val); err != nil {
panic(err)
panic(err) // lint:nopanic -- Panic risk is communicated in the godoc commentary.
}
return
}