Skip to content

Commit

Permalink
Linter fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicletz committed Sep 2, 2024
1 parent bc0465b commit f5eeedf
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ ci_test: runtime
lint: runtime
go vet ./...
cd tools && go install honnef.co/go/tools/cmd/staticcheck@latest
$(GOPATH)/bin/staticcheck -go 1.14 ./...
$(GOPATH)/bin/staticcheck -go 1.22 ./...

# Exclude rules from security check:
# G104 (CWE-703): Errors unhandled.
Expand Down
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,7 @@ $ diode socksd

## Prerequisite

* go >= 1.14
* enable go module (see: https://blog.golang.org/using-go-modules)

```BASH
export GO111MODULE=on
```

* optional: run dev diodechain locally (see: https://github.com/diodechain/diode_server_ex)
* go <= 1.22

## Setup go environment

Expand Down
1 change: 0 additions & 1 deletion cmd/diode/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ func prepareDiode() error {
cfg.RemoteRPCAddrs = remoteRPCAddrs
}
}
rand.Seed(time.Now().Unix())
rand.Shuffle(len(cfg.RemoteRPCAddrs), func(i, j int) {
cfg.RemoteRPCAddrs[i], cfg.RemoteRPCAddrs[j] = cfg.RemoteRPCAddrs[j], cfg.RemoteRPCAddrs[i]
})
Expand Down
8 changes: 6 additions & 2 deletions crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ type ECPublicKey struct {

// ECPrivateKey reflects an ASN.1 Elliptic Curve Private Key Structure.
// References:
// RFC 5915
// SEC1 - http://www.secg.org/sec1-v2.pdf
//
// RFC 5915
// SEC1 - http://www.secg.org/sec1-v2.pdf
//
// Per RFC 5915 the NamedCurveOID is marked as ASN.1 OPTIONAL, however in
// most cases it is not.
type ECPrivateKey struct {
Expand Down Expand Up @@ -141,6 +143,7 @@ func toECDSA(d []byte, strict bool) (*ecdsa.PrivateKey, error) {

// UnmarshalPubkey converts bytes to a secp256k1 public key.
func UnmarshalPubkey(pub []byte) (*ecdsa.PublicKey, error) {
//lint:ignore SA1019 because S256() doesn't have it's own NewPublicKey method
x, y := elliptic.Unmarshal(S256(), pub)
if x == nil {
return nil, errInvalidPubkey
Expand All @@ -153,6 +156,7 @@ func MarshalPubkey(pub *ecdsa.PublicKey) []byte {
if pub == nil || pub.X == nil || pub.Y == nil {
return nil
}
//lint:ignore SA1019 because S256() doesn't have it's own Marshal method
return elliptic.Marshal(S256(), pub.X, pub.Y)
}

Expand Down
3 changes: 3 additions & 0 deletions crypto/ecies/ecies.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func ImportECDSA(prv *ecdsa.PrivateKey) *PrivateKey {
// Generate an elliptic curve public / private keypair. If params is nil,
// the recommended default parameters for the key will be chosen.
func GenerateKey(rand io.Reader, curve elliptic.Curve, params *ECIESParams) (prv *PrivateKey, err error) {
//lint:ignore SA1019 because S256() doesn't have it's own GenerateKey method
pb, x, y, err := elliptic.GenerateKey(curve, rand)
if err != nil {
return
Expand Down Expand Up @@ -283,6 +284,7 @@ func Encrypt(rand io.Reader, pub *PublicKey, m, s1, s2 []byte) (ct []byte, err e

d := messageTag(params.Hash, Km, em, s2)

//lint:ignore SA1019 because S256() doesn't have it's own Marshal method
Rb := elliptic.Marshal(pub.Curve, R.PublicKey.X, R.PublicKey.Y)
ct = make([]byte, len(Rb)+len(em)+len(d))
copy(ct, Rb)
Expand Down Expand Up @@ -329,6 +331,7 @@ func (prv *PrivateKey) Decrypt(c, s1, s2 []byte) (m []byte, err error) {

R := new(PublicKey)
R.Curve = prv.PublicKey.Curve
//lint:ignore SA1019 because S256() doesn't have it's own Unmarshal method
R.X, R.Y = elliptic.Unmarshal(R.Curve, c[:rLen])
if R.X == nil {
err = ErrInvalidPublicKey
Expand Down
1 change: 1 addition & 0 deletions crypto/secp256k1/secp256_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func generateKeyPair() (pubkey, privkey []byte) {
if err != nil {
panic(err)
}
//lint:ignore SA1019 because S256() doesn't have it's own Marshal method
pubkey = elliptic.Marshal(S256(), key.X, key.Y)

privkey = make([]byte, 32)
Expand Down
4 changes: 2 additions & 2 deletions pkg/gauge/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package cmd
import (
"crypto/tls"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"net/http/httptrace"
Expand Down Expand Up @@ -236,7 +236,7 @@ func gaugeHandler(cmd *cobra.Command, args []string) (err error) {
app.addConn(c)
defer app.delConn(c)
defer resp.Body.Close()
_, err = ioutil.ReadAll(resp.Body)
_, err = io.ReadAll(resp.Body)
if err != nil {
app.Println(row, fmt.Sprintf(errorRowTemplate, j, err.Error()))
wg.Done()
Expand Down
18 changes: 9 additions & 9 deletions rlp/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ type Decoder interface {
// rules for the field such that input values of size zero decode as a nil
// pointer. This tag can be useful when decoding recursive types.
//
// type StructWithEmptyOK struct {
// Foo *[20]byte `rlp:"nil"`
// }
// type StructWithEmptyOK struct {
// Foo *[20]byte `rlp:"nil"`
// }
//
// To decode into a slice, the input must be a list and the resulting
// slice will contain the input elements in order. For byte slices,
Expand All @@ -113,8 +113,8 @@ type Decoder interface {
// To decode into an interface value, Decode stores one of these
// in the value:
//
// []interface{}, for RLP lists
// []byte, for RLP strings
// []interface{}, for RLP lists
// []byte, for RLP strings
//
// Non-empty interface types are not supported, nor are booleans,
// signed integers, floating point numbers, maps, channels and
Expand All @@ -124,7 +124,7 @@ type Decoder interface {
// and may be vulnerable to panics cause by huge value sizes. If
// you need an input limit, use
//
// NewStream(r, limit).Decode(val)
// NewStream(r, limit).Decode(val)
func Decode(r io.Reader, val interface{}) error {
// TODO: this could use a Stream from a pool.
return NewStream(r, 0).Decode(val)
Expand Down Expand Up @@ -199,9 +199,9 @@ func makeDecoder(typ reflect.Type, tags tags) (dec decoder, err error) {
return decodeRawValue, nil
case typ.Implements(decoderInterface):
return decodeDecoder, nil
case kind != reflect.Ptr && reflect.PtrTo(typ).Implements(decoderInterface):
case kind != reflect.Ptr && reflect.PointerTo(typ).Implements(decoderInterface):
return decodeDecoderNoPtr, nil
case typ.AssignableTo(reflect.PtrTo(bigInt)):
case typ.AssignableTo(reflect.PointerTo(bigInt)):
return decodeBigInt, nil
case typ.AssignableTo(bigInt):
return decodeBigIntNoPtr, nil
Expand Down Expand Up @@ -288,7 +288,7 @@ func decodeBigInt(s *Stream, val reflect.Value) error {

func makeListDecoder(typ reflect.Type, tag tags) (decoder, error) {
etype := typ.Elem()
if etype.Kind() == reflect.Uint8 && !reflect.PtrTo(etype).Implements(decoderInterface) {
if etype.Kind() == reflect.Uint8 && !reflect.PointerTo(etype).Implements(decoderInterface) {
if typ.Kind() == reflect.Array {
return decodeByteArray, nil
}
Expand Down
4 changes: 2 additions & 2 deletions rlp/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,11 @@ func makeWriter(typ reflect.Type, ts tags) (writer, error) {
return writeRawValue, nil
case typ.Implements(encoderInterface):
return writeEncoder, nil
case kind != reflect.Ptr && reflect.PtrTo(typ).Implements(encoderInterface):
case kind != reflect.Ptr && reflect.PointerTo(typ).Implements(encoderInterface):
return writeEncoderNoPtr, nil
case kind == reflect.Interface:
return writeInterface, nil
case typ.AssignableTo(reflect.PtrTo(bigInt)):
case typ.AssignableTo(reflect.PointerTo(bigInt)):
return writeBigIntPtr, nil
case typ.AssignableTo(bigInt):
return writeBigIntNoPtr, nil
Expand Down
5 changes: 2 additions & 3 deletions rlp/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"math/big"
"sync"
"testing"
Expand Down Expand Up @@ -287,7 +286,7 @@ func TestEncodeToReader(t *testing.T) {
if err != nil {
return nil, err
}
return ioutil.ReadAll(r)
return io.ReadAll(r)
})
}

Expand Down Expand Up @@ -328,7 +327,7 @@ func TestEncodeToReaderReturnToPool(t *testing.T) {
go func() {
for i := 0; i < 1000; i++ {
_, r, _ := EncodeToReader("foo")
ioutil.ReadAll(r)
io.ReadAll(r)
r.Read(buf)
r.Read(buf)
r.Read(buf)
Expand Down
1 change: 0 additions & 1 deletion rpc/client_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ type nodeRequest struct {

// NewClientManager returns a new manager rpc client
func NewClientManager(cfg *config.Config) *ClientManager {
rand.Seed(time.Now().Unix())
cm := &ClientManager{
srv: genserver.New("ClientManager"),
clientMap: make(map[util.Address]*Client),
Expand Down
8 changes: 3 additions & 5 deletions rpc/socks.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func (socksServer *Server) doConnectDevice(requestId int64, deviceName string, p
if _, ok := err.(RPCError); ok {
return nil, HttpError{404, DeviceError{err}}
}
return nil, HttpError{500, fmt.Errorf(msg)}
return nil, HttpError{500, errors.New(msg)}
}

func (socksServer *Server) connectDevice(deviceName string, port int, protocol int, mode string, fn func(*ConnectedPort) (net.Conn, error)) (*ConnectedPort, error) {
Expand Down Expand Up @@ -682,8 +682,7 @@ func (socksServer *Server) Start() error {
return
}
// Check whether error is temporary
// See: https://golang.org/src/net/net.go?h=Temporary
if ne, ok := err.(net.Error); ok && ne.Temporary() {
if ne, ok := err.(net.Error); ok && ne.Timeout() {
delayTime := 5 * time.Millisecond
socksServer.logger.Warn("socks: Accept error %v, retry in %v", err, delayTime)
time.Sleep(delayTime)
Expand Down Expand Up @@ -918,8 +917,7 @@ func (socksServer *Server) startBind(bind *Bind) error {
return
}
// Check whether error is temporary
// See: https://golang.org/src/net/net.go?h=Temporary
if ne, ok := err.(net.Error); ok && ne.Temporary() {
if ne, ok := err.(net.Error); ok && ne.Timeout() {
delayTime := 5 * time.Millisecond
socksServer.logger.Warn("StartBind(): Accept error %v, retry in %v", err, delayTime)
time.Sleep(delayTime)
Expand Down
2 changes: 1 addition & 1 deletion rpc/wsconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (c *WSConn) Close() error {

// Read data from the connectionn
func (c *WSConn) Read(buf []byte) (n int, err error) {
if c.readBuffer != nil && len(c.readBuffer) > 0 {
if len(c.readBuffer) > 0 {
n = copy(buf, c.readBuffer)
c.readBuffer = c.readBuffer[n:]
return
Expand Down
2 changes: 1 addition & 1 deletion util/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package util
import (
"bytes"
"crypto/ecdsa"
"math/rand"
"crypto/rand"
"testing"

"github.com/diodechain/diode_client/crypto"
Expand Down

0 comments on commit f5eeedf

Please sign in to comment.