Skip to content

Commit

Permalink
Fix issues reported by static analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Sep 3, 2024
1 parent 49eac2c commit 2510f23
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 8 deletions.
3 changes: 2 additions & 1 deletion device-provisioning-service/service/cloudConfiguration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions device-provisioning-service/service/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions internal/math/cast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package math

import "golang.org/x/exp/constraints"

func CastTo[T, F constraints.Integer](from F) T {
return T(from)
}
3 changes: 2 additions & 1 deletion pkg/net/grpc/server/makeDefaultOptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion resource-aggregate/commands/resouces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion resource-aggregate/commands/resourceconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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()),
}
}

Expand Down
3 changes: 2 additions & 1 deletion tools/mongodb/standby-tool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2510f23

Please sign in to comment.