From 2510f239c35a9916a13d70986ead383e67bd4d03 Mon Sep 17 00:00:00 2001 From: Daniel Adam Date: Tue, 3 Sep 2024 17:00:09 +0200 Subject: [PATCH] Fix issues reported by static analysis --- device-provisioning-service/service/cloudConfiguration.go | 3 ++- device-provisioning-service/service/config.go | 7 ++++--- internal/math/cast.go | 7 +++++++ pkg/net/grpc/server/makeDefaultOptions.go | 3 ++- resource-aggregate/commands/resouces.go | 2 +- resource-aggregate/commands/resourceconv.go | 3 ++- tools/mongodb/standby-tool/main.go | 3 ++- 7 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 internal/math/cast.go diff --git a/device-provisioning-service/service/cloudConfiguration.go b/device-provisioning-service/service/cloudConfiguration.go index 466d127de..3dd1c6587 100644 --- a/device-provisioning-service/service/cloudConfiguration.go +++ b/device-provisioning-service/service/cloudConfiguration.go @@ -12,6 +12,7 @@ import ( "github.com/plgd-dev/go-coap/v3/mux" "github.com/plgd-dev/hub/v2/device-provisioning-service/pb" "github.com/plgd-dev/hub/v2/device-provisioning-service/store" + "github.com/plgd-dev/hub/v2/internal/math" "github.com/plgd-dev/kit/v2/codec/cbor" ) @@ -40,7 +41,7 @@ func (RequestHandle) ProcessCloudConfiguration(ctx context.Context, req *mux.Mes }, Gateways: coapGateways, ProviderName: cloudCfg.AuthorizationProvider, - SelectedGateway: int32(selectedGateway), //nolint:gosec + SelectedGateway: math.CastTo[int32](selectedGateway), }, }) return msg, err diff --git a/device-provisioning-service/service/config.go b/device-provisioning-service/service/config.go index e95cf4ad1..fa840de27 100644 --- a/device-provisioning-service/service/config.go +++ b/device-provisioning-service/service/config.go @@ -9,6 +9,7 @@ import ( "github.com/plgd-dev/hub/v2/device-provisioning-service/security/oauth/clientcredentials" "github.com/plgd-dev/hub/v2/device-provisioning-service/service/http" "github.com/plgd-dev/hub/v2/device-provisioning-service/store/mongodb" + "github.com/plgd-dev/hub/v2/internal/math" "github.com/plgd-dev/hub/v2/pkg/config" "github.com/plgd-dev/hub/v2/pkg/config/property/urischeme" "github.com/plgd-dev/hub/v2/pkg/log" @@ -259,9 +260,9 @@ func HTTPConfigToProto(cfg pkgHttpClient.Config) (*pb.HttpConfig, error) { } return &pb.HttpConfig{ - MaxIdleConns: uint32(cfg.MaxIdleConns), //nolint:gosec - MaxConnsPerHost: uint32(cfg.MaxConnsPerHost), //nolint:gosec - MaxIdleConnsPerHost: uint32(cfg.MaxIdleConnsPerHost), //nolint:gosec + MaxIdleConns: math.CastTo[uint32](cfg.MaxIdleConns), + MaxConnsPerHost: math.CastTo[uint32](cfg.MaxConnsPerHost), + MaxIdleConnsPerHost: math.CastTo[uint32](cfg.MaxIdleConnsPerHost), IdleConnTimeout: cfg.IdleConnTimeout.Nanoseconds(), Timeout: cfg.Timeout.Nanoseconds(), Tls: tls, diff --git a/internal/math/cast.go b/internal/math/cast.go new file mode 100644 index 000000000..f959958e2 --- /dev/null +++ b/internal/math/cast.go @@ -0,0 +1,7 @@ +package math + +import "golang.org/x/exp/constraints" + +func CastTo[T, F constraints.Integer](from F) T { + return T(from) +} diff --git a/pkg/net/grpc/server/makeDefaultOptions.go b/pkg/net/grpc/server/makeDefaultOptions.go index 417b8e3ab..d5c8cbdb3 100644 --- a/pkg/net/grpc/server/makeDefaultOptions.go +++ b/pkg/net/grpc/server/makeDefaultOptions.go @@ -12,6 +12,7 @@ import ( grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags" "github.com/plgd-dev/hub/v2/grpc-gateway/pb" "github.com/plgd-dev/hub/v2/http-gateway/serverMux" + pkgMath "github.com/plgd-dev/hub/v2/internal/math" "github.com/plgd-dev/hub/v2/pkg/log" pkgGrpc "github.com/plgd-dev/hub/v2/pkg/net/grpc" pkgJwt "github.com/plgd-dev/hub/v2/pkg/security/jwt" @@ -157,7 +158,7 @@ func defaultMessageProducer(ctx context.Context, ctxLogger context.Context, msg } tags := grpc_ctxtags.Extract(ctx) newTags := grpc_ctxtags.NewTags() - newTags.Set(log.DurationMSKey, math.Float32frombits(uint32(duration.Integer))) //nolint:gosec + newTags.Set(log.DurationMSKey, math.Float32frombits(pkgMath.CastTo[uint32](duration.Integer))) newTags.Set(log.ProtocolKey, "GRPC") for k, v := range tags.Values() { if strings.EqualFold(k, grpcPrefixKey+"."+requestKey+"."+log.StartTimeKey) { diff --git a/resource-aggregate/commands/resouces.go b/resource-aggregate/commands/resouces.go index 9e37114f6..6dcf1f499 100644 --- a/resource-aggregate/commands/resouces.go +++ b/resource-aggregate/commands/resouces.go @@ -78,5 +78,5 @@ func CloneResourcesMap(resources map[string]*Resource) map[string]*Resource { } func ToPolicyBitFlags(bm schema.BitMask) int32 { - return int32(bm) //nolint:gosec + return int32(bm) } diff --git a/resource-aggregate/commands/resourceconv.go b/resource-aggregate/commands/resourceconv.go index 3f44a808c..ac32f71ed 100644 --- a/resource-aggregate/commands/resourceconv.go +++ b/resource-aggregate/commands/resourceconv.go @@ -4,6 +4,7 @@ import ( "time" "github.com/plgd-dev/device/v2/schema" + "github.com/plgd-dev/hub/v2/internal/math" pkgTime "github.com/plgd-dev/hub/v2/pkg/time" ) @@ -32,7 +33,7 @@ func (p *Policy) ToSchema() *schema.Policy { return nil } return &schema.Policy{ - BitMask: schema.BitMask(p.GetBitFlags()), + BitMask: math.CastTo[schema.BitMask](p.GetBitFlags()), } } diff --git a/tools/mongodb/standby-tool/main.go b/tools/mongodb/standby-tool/main.go index c01e79cae..56d52776b 100644 --- a/tools/mongodb/standby-tool/main.go +++ b/tools/mongodb/standby-tool/main.go @@ -6,6 +6,7 @@ import ( "fmt" "time" + "github.com/plgd-dev/hub/v2/internal/math" "github.com/plgd-dev/hub/v2/pkg/build" "github.com/plgd-dev/hub/v2/pkg/config" "github.com/plgd-dev/hub/v2/pkg/fsnotify" @@ -502,7 +503,7 @@ func (app *App) updateSecondaryMemberConfig(member string, config primitive.M) ( if memberMap["host"] == member { memberMap["hidden"] = false memberMap["priority"] = float64(app.Config.ReplicaSet.Secondary.Priority) - memberMap["votes"] = int32(app.Config.ReplicaSet.Secondary.Votes) //nolint:gosec + memberMap["votes"] = math.CastTo[int32](app.Config.ReplicaSet.Secondary.Votes) memberMap["secondaryDelaySecs"] = int32(0) } newMembers = append(newMembers, memberMap)