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 semgrep issues #1638

Merged
merged 34 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
55fe519
Fix more semgrep issues
nickeskov Mar 19, 2025
f706231
Extract state errors to new 'stateerr' package.
nickeskov Mar 21, 2025
f077738
Fix semgrep issues in 'pkg/ride/functions_proto.go'
nickeskov Mar 21, 2025
0c5c329
Fix tests in 'pkg/ride/functions_proto_test.go'.
nickeskov Mar 21, 2025
68f7ddc
Fix 'TestLigaDApp1'.
nickeskov Mar 21, 2025
13d1f2d
Fix error rewriting in 'cmd/wmd/internal/importer.go'.
nickeskov Mar 24, 2025
65a3a9b
Fix code-scanning/2247
nickeskov Mar 27, 2025
090c891
Fix code-scanning/2247
nickeskov Mar 27, 2025
3c9793b
Fix code-scanning/{2249,2251,2253,2254,2255}
nickeskov Mar 27, 2025
73e6f65
Fix code-scanning/2252
nickeskov Mar 27, 2025
2ce2a31
Fix code-scanning/2250
nickeskov Mar 27, 2025
e0afa70
Fix code-scanning/2256
nickeskov Mar 27, 2025
88748ec
Fix code-scanning/2257
nickeskov Mar 27, 2025
13abc33
Fix code-scanning/2269
nickeskov Mar 28, 2025
dd4f789
Fix code-scanning/{2258-2265}
nickeskov Mar 28, 2025
d888537
Fix code-scanning/{53,54,57,58,363,364,2106,2107}
nickeskov Mar 28, 2025
7c034b8
Fix code-scanning/{2266,2267}
nickeskov Mar 28, 2025
355c81e
Remove 'isHistoryEntryNotFoundError' function
nickeskov Mar 28, 2025
2f8c1fb
Fix wrong errors wrapping in 'balances.storeChallengeHeightForAddr'
nickeskov Mar 28, 2025
d1de1e0
Add error wrapping in 'DataWithProofs.bodyUnmarshalBinary'.
nickeskov Mar 28, 2025
7cee0b6
Fix wrong error wrapping in 'WrappedState.validateWavesBalance'.
nickeskov Mar 28, 2025
4f20676
Add 'return err' statement in 'if err != nil' block in 'txAppender.ch…
nickeskov Mar 28, 2025
ab24eeb
Properly handle 'ErrNotFound' error in 'batchedStorage.normalizeBatch…
nickeskov Mar 28, 2025
057f79c
Refactored 'recentTransactions.txInfoById'.
nickeskov Mar 28, 2025
a741459
Wrap error returned by 'CallVerifier' in 'script_caller.go'.
nickeskov Mar 28, 2025
5a4e575
Add Eth signature verification error to the return error message.
nickeskov Mar 28, 2025
c682d53
Handle order signature verification errors.
nickeskov Mar 28, 2025
12422b5
Merge branch 'master' into fix-more-semgrep-issues
nickeskov Mar 28, 2025
d82f8ca
Merge branch 'master' into fix-more-semgrep-issues
nickeskov Mar 28, 2025
7acf85c
Handle missed error in ride functions.
nickeskov Mar 29, 2025
e33e816
Merge branch 'master' into fix-more-semgrep-issues
nickeskov Mar 31, 2025
01d6ef5
Fix code-scanning/2088
nickeskov Mar 31, 2025
72ca030
Fix code-scanning/2087
nickeskov Mar 31, 2025
5631859
Improved a bit order signature verification error handling.
nickeskov Apr 1, 2025
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
2 changes: 1 addition & 1 deletion cmd/retransmitter/retransmit/behaviour.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ func (a *BehaviourImpl) SpawnKnownPeers(ctx context.Context) {
}

func (a *BehaviourImpl) errorHandler(p peer.Peer, e error) {
_ = p.Close()
if p != nil {
_ = p.Close()
a.activeConnections.Delete(p)
}
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/wmd/internal/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
"time"

"github.com/pkg/errors"
"go.uber.org/zap"

"github.com/wavesplatform/gowaves/cmd/wmd/internal/data"
"github.com/wavesplatform/gowaves/cmd/wmd/internal/state"
"github.com/wavesplatform/gowaves/pkg/crypto"
"github.com/wavesplatform/gowaves/pkg/proto"
"go.uber.org/zap"
)

type Importer struct {
Expand All @@ -39,9 +40,9 @@ func (im *Importer) Import(n string) error {
return errors.Wrapf(err, "failed to open blockchain file '%s'", n)
}
defer func() {
err = f.Close()
if err != nil {
zap.S().Errorf("Failed to close blockchain file: %s", err.Error())
closeErr := f.Close()
if closeErr != nil {
zap.S().Errorf("Failed to close blockchain file: %s", closeErr.Error())
}
}()

Expand Down
9 changes: 5 additions & 4 deletions pkg/api/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package api

import (
"github.com/pkg/errors"

apiErrs "github.com/wavesplatform/gowaves/pkg/api/errors"
"github.com/wavesplatform/gowaves/pkg/crypto"
"github.com/wavesplatform/gowaves/pkg/proto"
"github.com/wavesplatform/gowaves/pkg/state"
"github.com/wavesplatform/gowaves/pkg/state/stateerr"
)

type Score struct {
Expand Down Expand Up @@ -157,7 +158,7 @@ func (a *App) BlocksGenerators() (Generators, error) {
func (a *App) BlockByHeight(height proto.Height) (*proto.Block, error) {
block, err := a.state.BlockByHeight(height)
if err != nil {
if origErr := errors.Cause(err); state.IsInvalidInput(origErr) || state.IsNotFound(origErr) {
if origErr := errors.Cause(err); stateerr.IsInvalidInput(origErr) || stateerr.IsNotFound(origErr) {
return nil, notFound
}
return nil, errors.Wrapf(err, "failed to get block by height=%d", height)
Expand All @@ -168,7 +169,7 @@ func (a *App) BlockByHeight(height proto.Height) (*proto.Block, error) {
func (a *App) Block(id proto.BlockID) (*proto.Block, error) {
block, err := a.state.Block(id)
if err != nil {
if origErr := errors.Cause(err); state.IsNotFound(origErr) {
if origErr := errors.Cause(err); stateerr.IsNotFound(origErr) {
return nil, notFound
}
return nil, errors.Wrapf(err, "failed to get block by id=%s", id.String())
Expand All @@ -179,7 +180,7 @@ func (a *App) Block(id proto.BlockID) (*proto.Block, error) {
func (a *App) BlockIDToHeight(id proto.BlockID) (proto.Height, error) {
height, err := a.state.BlockIDToHeight(id)
if err != nil {
if origErr := errors.Cause(err); state.IsNotFound(origErr) {
if origErr := errors.Cause(err); stateerr.IsNotFound(origErr) {
return 0, notFound
}
return 0, errors.Wrapf(err, "failed to get block height for id=%s", id.String())
Expand Down
5 changes: 3 additions & 2 deletions pkg/api/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package api

import (
"github.com/pkg/errors"

"github.com/wavesplatform/gowaves/pkg/proto"
"github.com/wavesplatform/gowaves/pkg/proto/ethabi"
"github.com/wavesplatform/gowaves/pkg/ride/serialization"
"github.com/wavesplatform/gowaves/pkg/state"
"github.com/wavesplatform/gowaves/pkg/state/stateerr"
)

func (a *App) EthereumDAppMethods(addr proto.WavesAddress) (ethabi.MethodsMap, error) {
scriptInfo, err := a.state.ScriptInfoByAccount(proto.NewRecipientFromAddress(addr))
if err != nil {
if state.IsNotFound(err) {
if stateerr.IsNotFound(err) {
return ethabi.MethodsMap{}, errors.Wrap(notFound, "script is not found")
}
return ethabi.MethodsMap{}, err
Expand Down
9 changes: 5 additions & 4 deletions pkg/api/metamask/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/wavesplatform/gowaves/pkg/proto/ethabi"
"github.com/wavesplatform/gowaves/pkg/services"
"github.com/wavesplatform/gowaves/pkg/state"
"github.com/wavesplatform/gowaves/pkg/state/stateerr"
"github.com/wavesplatform/gowaves/pkg/util/common"
)

Expand Down Expand Up @@ -127,7 +128,7 @@ func (s RPCService) Eth_GetBlockByHash(blockIDBytes proto.HexBytes, filterTxObj
}
_, err = s.nodeRPCApp.State.BlockIDToHeight(blockID)
switch {
case state.IsNotFound(err):
case stateerr.IsNotFound(err):
return nil, nil // according to the scala node implementation
case err != nil:
return nil, errors.Wrapf(err, "failed to fetch heigh of block by blockID %q", blockID.String())
Expand Down Expand Up @@ -309,7 +310,7 @@ func (s RPCService) Eth_GetCode(ethAddr proto.EthereumAddress, blockOrTag string

si, err := s.nodeRPCApp.State.ScriptBasicInfoByAccount(proto.NewRecipientFromAddress(wavesAddr))
switch {
case state.IsNotFound(err):
case stateerr.IsNotFound(err):
// account has no script, trying fetch data as asset
assetID := proto.AssetID(ethAddr)
_, err := s.nodeRPCApp.State.AssetInfo(assetID)
Expand Down Expand Up @@ -428,7 +429,7 @@ type GetTransactionReceiptResponse struct {
func (s RPCService) Eth_GetTransactionReceipt(ethTxID proto.EthereumHash) (*GetTransactionReceiptResponse, error) {
txID := crypto.Digest(ethTxID)
tx, status, err := s.nodeRPCApp.State.TransactionByIDWithStatus(txID.Bytes())
if state.IsNotFound(err) {
if stateerr.IsNotFound(err) {
zap.S().Debugf("Eth_GetTransactionReceipt: transaction with ID=%q or ethID=%q cannot be found",
txID, ethTxID,
)
Expand Down Expand Up @@ -509,7 +510,7 @@ type GetTransactionByHashResponse struct {
func (s RPCService) Eth_GetTransactionByHash(ethTxID proto.EthereumHash) (*GetTransactionByHashResponse, error) {
txID := crypto.Digest(ethTxID)
tx, err := s.nodeRPCApp.State.TransactionByID(txID.Bytes())
if state.IsNotFound(err) {
if stateerr.IsNotFound(err) {
zap.S().Debugf("Eth_GetTransactionByHash: transaction with ID=%q or ethID=%q cannot be found",
txID, ethTxID,
)
Expand Down
21 changes: 11 additions & 10 deletions pkg/api/node_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/wavesplatform/gowaves/pkg/errs"
"github.com/wavesplatform/gowaves/pkg/proto"
"github.com/wavesplatform/gowaves/pkg/state"
"github.com/wavesplatform/gowaves/pkg/state/stateerr"
"github.com/wavesplatform/gowaves/pkg/util/limit_listener"
)

Expand Down Expand Up @@ -92,7 +93,7 @@ func (a *NodeApi) TransactionInfo(w http.ResponseWriter, r *http.Request) error
tx, err := a.state.TransactionByID(id.Bytes())
if err != nil {
origErr := errors.Cause(err)
if state.IsNotFound(origErr) {
if stateerr.IsNotFound(origErr) {
return apiErrs.TransactionDoesNotExist
}
return errors.Wrapf(err,
Expand Down Expand Up @@ -150,7 +151,7 @@ func (a *NodeApi) BlocksHeadersAt(w http.ResponseWriter, r *http.Request) error
}
header, err := a.app.BlocksHeadersAt(h)
if err != nil {
if state.IsInvalidInput(err) || state.IsNotFound(err) {
if stateerr.IsInvalidInput(err) || stateerr.IsNotFound(err) {
return apiErrs.BlockDoesNotExist
}
return errors.Wrapf(err, "BlocksHeadersAt: failed to get block header at height %d", h)
Expand All @@ -174,7 +175,7 @@ func (a *NodeApi) BlockHeadersID(w http.ResponseWriter, r *http.Request) error {
}
header, err := a.app.BlocksHeadersByID(id)
if err != nil {
if state.IsNotFound(err) {
if stateerr.IsNotFound(err) {
return apiErrs.BlockDoesNotExist
}
return errors.Wrapf(err, "BlockHeadersID: failed to get block header by ID=%q", s)
Expand Down Expand Up @@ -320,7 +321,7 @@ func (a *NodeApi) BlocksSnapshotAt(w http.ResponseWriter, r *http.Request) error
}
blockSnapshot, err := a.state.SnapshotsAtHeight(height)
if err != nil {
if state.IsNotFound(err) {
if stateerr.IsNotFound(err) {
return apiErrs.BlockDoesNotExist
}
return errors.Wrapf(err, "BlocksSnapshotAt: failed to get block snapshot at height %d", height)
Expand Down Expand Up @@ -523,7 +524,7 @@ func (a *NodeApi) AddrByAlias(w http.ResponseWriter, r *http.Request) error {
addr, err := a.app.AddrByAlias(*alias)
if err != nil {
origErr := errors.Cause(err)
if state.IsNotFound(origErr) {
if stateerr.IsNotFound(origErr) {
return apiErrs.NewAliasDoesNotExistError(alias.String())
}
return errors.Wrapf(err, "failed to find addr by short alias %q", aliasShort)
Expand All @@ -546,7 +547,7 @@ func (a *NodeApi) AliasesByAddr(w http.ResponseWriter, r *http.Request) error {

aliases, err := a.app.AliasesByAddr(addr)
if err != nil {
if state.IsNotFound(err) {
if stateerr.IsNotFound(err) {
aliases = nil
} else {
return errors.Wrapf(err, "failed to find aliases by addr")
Expand Down Expand Up @@ -696,7 +697,7 @@ func (a *NodeApi) RollbackToHeight(w http.ResponseWriter, r *http.Request) error
err := a.state.RollbackToHeight(rollbackReq.Height)
if err != nil {
origErr := errors.Cause(err)
if state.IsNotFound(origErr) {
if stateerr.IsNotFound(origErr) {
return apiErrs.BlockDoesNotExist
}
return errors.Wrapf(err, "failed to rollback to height %d", rollbackReq.Height)
Expand Down Expand Up @@ -811,7 +812,7 @@ func (a *NodeApi) stateHash(w http.ResponseWriter, r *http.Request) error {
}
stateHashDebug, err := a.stateHashDebug(height)
if err != nil {
if state.IsNotFound(err) {
if stateerr.IsNotFound(err) {
return apiErrs.BlockDoesNotExist
}
return errors.Wrap(err, "failed to get state hash debug")
Expand All @@ -834,7 +835,7 @@ func (a *NodeApi) stateHashLast(w http.ResponseWriter, _ *http.Request) error {
}
stateHashDebug, err := a.stateHashDebug(h)
if err != nil {
if state.IsNotFound(err) {
if stateerr.IsNotFound(err) {
return apiErrs.BlockDoesNotExist
}
return errors.Wrap(err, "failed to get last state hash")
Expand All @@ -857,7 +858,7 @@ func (a *NodeApi) snapshotStateHash(w http.ResponseWriter, r *http.Request) erro
}
sh, err := a.state.SnapshotStateHashAtHeight(height)
if err != nil {
if state.IsNotFound(err) {
if stateerr.IsNotFound(err) {
return apiErrs.BlockDoesNotExist
}
return errors.Wrapf(err, "failed to get snapshot state hash at height %d", height)
Expand Down
5 changes: 3 additions & 2 deletions pkg/grpc/server/accounts_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
pb "github.com/wavesplatform/gowaves/pkg/grpc/generated/waves"
g "github.com/wavesplatform/gowaves/pkg/grpc/generated/waves/node/grpc"
"github.com/wavesplatform/gowaves/pkg/proto"
"github.com/wavesplatform/gowaves/pkg/state/stateerr"
)

func (s *Server) GetBalances(req *g.BalancesRequest, srv g.AccountsApi_GetBalancesServer) error {
Expand Down Expand Up @@ -123,7 +124,7 @@ func (s *Server) GetDataEntries(req *g.DataRequest, srv g.AccountsApi_GetDataEnt
if req.Key != "" {
entry, err := s.state.RetrieveEntry(rcp, req.Key)
if err != nil {
if err.Error() == "not found" { // TODO: fix this error message comparison, use errors.Is instead
if stateerr.IsNotFound(err) {
return nil
}
return status.Error(codes.NotFound, err.Error())
Expand All @@ -139,7 +140,7 @@ func (s *Server) GetDataEntries(req *g.DataRequest, srv g.AccountsApi_GetDataEnt
}
entries, err := s.state.RetrieveEntries(rcp)
if err != nil {
if err.Error() == "not found" { // TODO: fix this error message comparison, use errors.Is instead
if stateerr.IsNotFound(err) {
return nil
}
return status.Error(codes.Internal, err.Error())
Expand Down
3 changes: 2 additions & 1 deletion pkg/miner/micro_miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/wavesplatform/gowaves/pkg/proto"
"github.com/wavesplatform/gowaves/pkg/services"
"github.com/wavesplatform/gowaves/pkg/state"
"github.com/wavesplatform/gowaves/pkg/state/stateerr"
"github.com/wavesplatform/gowaves/pkg/types"
)

Expand Down Expand Up @@ -88,7 +89,7 @@ func (a *MicroMiner) Micro(minedBlock *proto.Block, rest proto.MiningLimits, key
// In the miner we pack transactions from UTX into new block.
// We should accept failed transactions here.
snapshot, errVal := s.ValidateNextTx(t.T, minedBlock.Timestamp, parentTimestamp, minedBlock.Version, true)
if state.IsTxCommitmentError(errVal) {
if stateerr.IsTxCommitmentError(errVal) {
// This should not happen in practice.
// Reset state, tx count, return applied transactions to UTX.
s.ResetValidationList()
Expand Down
6 changes: 4 additions & 2 deletions pkg/miner/utxpool/transaction_bulk_validator.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package utxpool

import (
"go.uber.org/zap"

"github.com/wavesplatform/gowaves/pkg/proto"
"github.com/wavesplatform/gowaves/pkg/state"
"github.com/wavesplatform/gowaves/pkg/state/stateerr"
"github.com/wavesplatform/gowaves/pkg/types"
"go.uber.org/zap"
)

type BulkValidator interface {
Expand Down Expand Up @@ -53,7 +55,7 @@ func (a bulkValidator) validate() ([]*types.TransactionWithBytes, error) {
break
}
_, err := s.ValidateNextTx(t.T, currentTimestamp, lastKnownBlock.Timestamp, lastKnownBlock.Version, false)
if state.IsTxCommitmentError(err) {
if stateerr.IsTxCommitmentError(err) {
// This should not happen in practice.
// Reset state, return applied transactions to UTX.
s.ResetValidationList()
Expand Down
9 changes: 5 additions & 4 deletions pkg/node/blocks_applier/blocks_applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/wavesplatform/gowaves/pkg/proto"
"github.com/wavesplatform/gowaves/pkg/state"
"github.com/wavesplatform/gowaves/pkg/state/stateerr"
)

const maxRollbackDeltaHeight = 100
Expand All @@ -32,7 +33,7 @@ func (a *innerBlocksApplier) exists(storage innerState, block *proto.Block) (boo
if err == nil {
return true, nil
}
if state.IsNotFound(err) {
if stateerr.IsNotFound(err) {
return false, nil
}
return false, err
Expand Down Expand Up @@ -102,7 +103,7 @@ func (a *innerBlocksApplier) getParentAndCurrentHeight(
if err == nil {
return 0, 0, proto.NewInfoMsg(errors.Errorf("first block %s exists", firstBlock.BlockID().String()))
}
if !state.IsNotFound(err) {
if !stateerr.IsNotFound(err) {
return 0, 0, errors.Wrap(err, "unknown error")
}
currentHeight, err := storage.Height()
Expand Down Expand Up @@ -240,7 +241,7 @@ func (a *innerBlocksApplier) applyMicro(
if err == nil {
return 0, errors.Errorf("block '%s' already exist", block.BlockID().String())
}
if !state.IsNotFound(err) {
if !stateerr.IsNotFound(err) {
return 0, errors.Wrap(err, "unexpected error")
}

Expand Down Expand Up @@ -290,7 +291,7 @@ func (a *innerBlocksApplier) applyMicroWithSnapshot(
if err == nil {
return 0, errors.Errorf("block '%s' already exist", block.BlockID().String())
}
if !state.IsNotFound(err) {
if !stateerr.IsNotFound(err) {
return 0, errors.Wrap(err, "unexpected error")
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/node/blocks_applier/node_mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"github.com/wavesplatform/gowaves/pkg/proto"
"github.com/wavesplatform/gowaves/pkg/settings"
"github.com/wavesplatform/gowaves/pkg/state"
"github.com/wavesplatform/gowaves/pkg/state/stateerr"
)

func notFound() state.StateError {
return state.NewStateError(state.NotFoundError, proto.ErrNotFound)
func notFound() stateerr.StateError {
return stateerr.NewStateError(stateerr.NotFoundError, proto.ErrNotFound)
}

type MockStateManager struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/proto/transactions_with_proofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3312,7 +3312,7 @@ func (tx *DataWithProofs) bodyUnmarshalBinary(data []byte) error {
var e DataEntry
t, err := extractValueType(data)
if err != nil {
return errors.Errorf("failed to extract type of data entry")
return errors.Wrap(err, "failed to extract type of data entry")
}
switch t {
case DataInteger:
Expand Down
2 changes: 1 addition & 1 deletion pkg/ride/compiler/ast_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1682,7 +1682,7 @@ func (p *astParser) ruleGenericTypeHandler(node *node32) s.Type {
curNode = skipToNextRule(curNode.next)
T := p.ruleTypesHandler(curNode)
if T == nil {
return T
return nil
}
return s.ListType{Type: T}
}
Expand Down
Loading
Loading