Skip to content

Commit b66ac95

Browse files
committed
Satisfy linter
This mostly relates to integer conversions. In the most cases, the conversions are safe and thus disabled. Signed-off-by: Matej Pavlovic <matopavlovic@gmail.com>
1 parent ea51d81 commit b66ac95

17 files changed

Lines changed: 79 additions & 48 deletions

File tree

cmd/mircat/debug.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func debug(args *arguments) error {
111111
for _, event := range entry.Events {
112112

113113
// Set the index of the event in the event log.
114-
metadata.index = uint64(index)
114+
metadata.index = uint64(index) //nolint:gosec
115115

116116
// If the event was selected by the user for inspection, pause before submitting it to the node.
117117
// The processing continues after the user's interactive confirmation.
@@ -200,9 +200,6 @@ func debuggerNode(id stdtypes.NodeID, membership *trantorpbtypes.Membership) (*m
200200
"iss": protocol,
201201
"timer": timer.New(),
202202
}
203-
if err != nil {
204-
panic(fmt.Errorf("error initializing the Mir modules: %w", err))
205-
}
206203

207204
node, err := mir.NewNode(id, mir.DefaultNodeConfig().WithLogger(logger), nodeModules, nil)
208205
if err != nil {

cmd/mircat/display.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func displayEvents(args *arguments) error { //nolint:gocognit
6969
}
7070
// getting events from entry
7171
for _, event := range entry.Events {
72-
metadata.index = uint64(index)
72+
metadata.index = uint64(index) //nolint:gosec
7373

7474
_, validEvent := args.selectedEventNames[eventName(event)]
7575
_, validDest := args.selectedEventDests[event.DestModule]

node_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package mir
33
import (
44
"context"
55
"fmt"
6+
"math"
67
"sync"
78
"sync/atomic"
89
"testing"
@@ -140,7 +141,7 @@ func TestNode_Backpressure(t *testing.T) {
140141
nodeConfig.Stats.Period = 100 * time.Millisecond
141142

142143
// Set an input event rate that would fill the node's event buffers in one second in 10 batches.
143-
blabberModule := newBlabber(uint64(nodeConfig.PauseInputThreshold/10), 100*time.Millisecond)
144+
blabberModule := newBlabber(uint64(nodeConfig.PauseInputThreshold/10), 100*time.Millisecond) //nolint:gosec
144145

145146
// Set the event consumption rate to 1/2 of the input rate (i.e., draining the buffer in 2 seconds)
146147
// and create the consumer module.
@@ -181,8 +182,8 @@ func TestNode_Backpressure(t *testing.T) {
181182
fmt.Printf("Total submitted events: %d\n", atomic.LoadUint64(&blabberModule.totalSubmitted))
182183
totalSubmitted := atomic.LoadUint64(&blabberModule.totalSubmitted)
183184
expectSubmitted := atomic.LoadUint64(&consumerModule.numProcessed) +
184-
uint64(nodeConfig.PauseInputThreshold) + // Events left in the buffer
185-
uint64(nodeConfig.MaxEventBatchSize) + // Events in the consumer's processing queue
185+
uint64(nodeConfig.PauseInputThreshold) + //nolint:gosec // Events left in the buffer
186+
uint64(nodeConfig.MaxEventBatchSize) + //nolint:gosec // Events in the consumer's processing queue
186187
2*blabberModule.batchSize // one batch of overshooting, one batch waiting in the babbler's output channel.
187188
assert.LessOrEqual(t, totalSubmitted, expectSubmitted, "too many events submitted (node event buffer overflow)")
188189
}
@@ -223,9 +224,12 @@ func (b *blabber) Go() {
223224
return
224225
default:
225226
}
227+
if b.batchSize > math.MaxInt {
228+
panic("batch size too big for int")
229+
}
226230
evts := stdtypes.ListOf(sliceutil.Repeat(
227231
stdtypes.Event(stdevents.NewTestUint64("consumer", 0)),
228-
int(b.batchSize),
232+
int(b.batchSize), //nolint:gosec
229233
)...)
230234
select {
231235
case <-b.stop:

pkg/availability/multisigcollector/multisigcollector.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package multisigcollector
22

33
import (
4+
"fmt"
45
"math"
56

67
"google.golang.org/protobuf/proto"
@@ -69,12 +70,17 @@ func NewReconfigurableModule(mc ModuleConfig, paramsTemplate ModuleParams, logge
6970
submc := mc
7071
submc.Self = mscID
7172

73+
// Check for integer overflow
74+
if mscParams.MaxRequests > math.MaxInt {
75+
return nil, fmt.Errorf("max requests too high for int type: %d", mscParams.MaxRequests)
76+
}
77+
7278
// Fill in instance-specific parameters.
7379
moduleParams := paramsTemplate
7480
moduleParams.InstanceUID = []byte(mscID)
7581
moduleParams.EpochNr = mscParams.Epoch
7682
moduleParams.Membership = mscParams.Membership
77-
moduleParams.MaxRequests = int(mscParams.MaxRequests)
83+
moduleParams.MaxRequests = int(mscParams.MaxRequests) //nolint:gosec
7884
// TODO: Use InstanceUIDs properly.
7985
// (E.g., concatenate this with the instantiating protocol's InstanceUID when introduced.)
8086

pkg/checkpoint/chkpvalidator/conservativecv.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,19 @@ func (ccv *ConservativeCV) Verify(
5656
}
5757

5858
// Check how far the received stable checkpoint is ahead of the local node's state.
59-
chkpMembershipOffset := int(sc.Epoch()) - 1 - int(epochNr)
59+
chkpMembershipOffset := sc.Epoch() - tt.EpochNr(1) - epochNr
6060
if chkpMembershipOffset <= 0 {
6161
// Ignore stable checkpoints that are not far enough
6262
// ahead of the current state of the local node.
6363
return es.Errorf("checkpoint not far ahead enough")
6464
}
6565

66-
if chkpMembershipOffset > ccv.configOffset {
66+
// Make sure ccv.configOffset is non-negative before conversion
67+
if ccv.configOffset < 0 {
68+
return es.Errorf("configOffset cannot be negative")
69+
}
70+
71+
if chkpMembershipOffset > tt.EpochNr(ccv.configOffset) { //nolint:gosec
6772
// cannot verify checkpoint signatures, too far ahead
6873
return es.Errorf("checkpoint too far ahead")
6974
}

pkg/checkpoint/chkpvalidator/permissivecv.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ func (pcv *PermissiveCV) Verify(chkp *checkpointpbtypes.StableCheckpoint, epochN
5656
// simply by receiving a maliciously crafted checkpoint.
5757
// Thus, the permissive checker is a form of a stub and should not be used in production.
5858
chkpMembership := sc.PreviousMembership()
59-
chkpMembershipOffset := int(sc.Epoch()) - 1 - int(epochNr)
59+
chkpMembershipOffset := sc.Epoch() - tt.EpochNr(1) - epochNr
6060

61-
if chkpMembershipOffset > pcv.configOffset {
61+
if chkpMembershipOffset > tt.EpochNr(pcv.configOffset) { //nolint:gosec
6262
// cannot verify checkpoint signatures, too far ahead
6363
pcv.logger.Log(logging.LevelWarn, "-----------------------------------------------------\n",
6464
"ATTENTION: cannot verify membership of checkpoint, too far ahead, proceed with caution\n",

pkg/deploytest/deployment.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ func (d *Deployment) Run(ctx context.Context) (nodeErrors []error, heapObjects i
179179
<-ctx.Done()
180180
runtime.GC()
181181
runtime.ReadMemStats(&m2)
182-
heapObjects = int64(m2.HeapObjects - m1.HeapObjects)
183-
heapAlloc = int64(m2.HeapAlloc - m1.HeapAlloc)
182+
heapObjects = int64(m2.HeapObjects - m1.HeapObjects) //nolint:gosec
183+
heapAlloc = int64(m2.HeapAlloc - m1.HeapAlloc) //nolint:gosec
184184
cancel()
185185
}()
186186

pkg/deploytest/testreplica.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func (tr *TestReplica) submitFakeTransactions(ctx context.Context, node *mir.Nod
183183
destModule,
184184
[]*trantorpbtypes.Transaction{{
185185
ClientId: tt.NewClientIDFromInt(0),
186-
TxNo: tt.TxNo(i),
186+
TxNo: tt.TxNo(i), //nolint:gosec
187187
Data: []byte(fmt.Sprintf("Transaction %d", i)),
188188
}},
189189
).Pb())

pkg/dsl/test/dslmodule_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func newContextTestingModule(mc *contextTestingModuleModuleConfig) dsl.Module {
278278

279279
// NB: avoid using primitive types as the context in the actual implementation, prefer named structs,
280280
// remember that the context type is used to match requests with responses.
281-
cryptopbdsl.VerifySigs(m, mc.Crypto, sliceutil.Repeat(msg, int(u)), signatures, nodeIDs, &u)
281+
cryptopbdsl.VerifySigs(m, mc.Crypto, sliceutil.Repeat(msg, int(u)), signatures, nodeIDs, &u) //nolint:gosec
282282
}
283283
return nil
284284
})

pkg/iss/iss.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ func New(
342342

343343
// Choose a leader for the new orderer instance.
344344
// TODO: Use the corresponding epoch's leader set to pick a leader, instead of just selecting one from all nodes.
345-
leader := maputil.GetSortedKeys(membership.Nodes)[int(epoch)%len(membership.Nodes)]
345+
leader := maputil.GetSortedKeys(membership.Nodes)[int(epoch)%len(membership.Nodes)] //nolint:gosec
346346

347347
// Serialize checkpoint, so it can be proposed as a value.
348348
stableCheckpoint := checkpointpbtypes.StableCheckpoint{
@@ -408,7 +408,7 @@ func New(
408408
// that are not yet part of the system for those checkpoints.
409409
var delayed []stdtypes.NodeID
410410
for n := range membership.Nodes {
411-
if epoch > iss.nodeEpochMap[n]+tt.EpochNr(iss.Params.RetainedEpochs) {
411+
if epoch > iss.nodeEpochMap[n]+tt.EpochNr(iss.Params.RetainedEpochs) { //nolint:gosec
412412
delayed = append(delayed, n)
413413
}
414414
}
@@ -438,7 +438,7 @@ func New(
438438

439439
sc := checkpoint.StableCheckpointFromPb(chkp.Pb())
440440
// Check how far the received stable checkpoint is ahead of the local node's state.
441-
chkpMembershipOffset := int(sc.Epoch()) - 1 - int(iss.epoch.Nr())
441+
chkpMembershipOffset := sc.Epoch() - tt.EpochNr(1) - iss.epoch.Nr()
442442
if chkpMembershipOffset <= 0 {
443443
// Ignore stable checkpoints that are not far enough
444444
// ahead of the current state of the local node.
@@ -465,7 +465,7 @@ func New(
465465
}
466466

467467
chkp := checkpoint.StableCheckpointFromPb(c.checkpoint.Pb())
468-
chkpMembershipOffset := int(chkp.Epoch()) - 1 - int(iss.epoch.Nr())
468+
chkpMembershipOffset := chkp.Epoch() - tt.EpochNr(1) - iss.epoch.Nr()
469469
if chkpMembershipOffset <= 0 {
470470
// Ignore stable checkpoints that have been lagged behind
471471
// during validation
@@ -564,7 +564,7 @@ func InitialStateSnapshot(
564564
return nil, err
565565
}
566566

567-
firstEpochLength := uint64(params.SegmentLength * len(params.InitialMembership.Nodes))
567+
firstEpochLength := uint64(params.SegmentLength * len(params.InitialMembership.Nodes)) //nolint:gosec
568568
return &trantorpbtypes.StateSnapshot{
569569
AppData: appState,
570570
EpochData: &trantorpbtypes.EpochData{
@@ -624,7 +624,7 @@ func (iss *ISS) initAvailability() {
624624
(*multisigcollector.InstanceParams)(&mscpbtypes.InstanceParams{
625625
Epoch: iss.epoch.Nr(),
626626
Membership: iss.memberships[0],
627-
MaxRequests: uint64(iss.Params.SegmentLength),
627+
MaxRequests: uint64(iss.Params.SegmentLength), //nolint:gosec
628628
}),
629629
stdtypes.RetentionIndex(iss.epoch.Nr()),
630630
)
@@ -640,12 +640,12 @@ func (iss *ISS) initOrderers() error {
640640

641641
// Create segment.
642642
// The sequence proposals are all set to nil, so that the orderer proposes new availability certificates.
643-
proposals := freeProposals(iss.nextDeliveredSN+tt.SeqNr(i), tt.SeqNr(len(leaders)), iss.Params.SegmentLength)
643+
proposals := freeProposals(iss.nextDeliveredSN+tt.SeqNr(i), tt.SeqNr(len(leaders)), iss.Params.SegmentLength) //nolint:gosec
644644
seg, err := common.NewSegment(leader, iss.epoch.Membership, proposals)
645645
if err != nil {
646646
return es.Errorf("error creating new segment: %w", err)
647647
}
648-
iss.newEpochSN += tt.SeqNr(seg.Len())
648+
iss.newEpochSN += tt.SeqNr(seg.Len()) //nolint:gosec
649649

650650
// Instantiate a new PBFT orderer.
651651
stddsl.NewSubmodule(iss.m, iss.moduleConfig.Ordering,
@@ -792,7 +792,7 @@ func (iss *ISS) advanceEpoch() error {
792792
EpochConfig: &trantorpbtypes.EpochConfig{ // nolint:govet
793793
iss.epoch.Nr(),
794794
iss.epoch.FirstSN(),
795-
uint64(iss.epoch.Len()),
795+
uint64(iss.epoch.Len()), //nolint:gosec
796796
iss.memberships,
797797
},
798798
},
@@ -904,8 +904,8 @@ func (iss *ISS) deliverCommonCheckpoint(chkpData []byte) error {
904904
// The state to prune is determined according to the retention index
905905
// which is derived from the epoch number the new
906906
// stable checkpoint is associated with.
907-
pruneIndex := int(chkp.Epoch()) - iss.Params.RetainedEpochs
908-
if pruneIndex > 0 { // "> 0" and not ">= 0", since only entries strictly smaller than the index are pruned.
907+
pruneIndex := chkp.Epoch() - tt.EpochNr(iss.Params.RetainedEpochs) //nolint:gosec
908+
if pruneIndex > 0 { // "> 0" and not ">= 0", since only entries strictly smaller than the index are pruned.
909909

910910
// Prune timer, checkpointing, availability, orderers, and other modules.
911911
stddsl.GarbageCollect(iss.m, iss.moduleConfig.Timer, stdtypes.RetentionIndex(pruneIndex))
@@ -917,7 +917,7 @@ func (iss *ISS) deliverCommonCheckpoint(chkpData []byte) error {
917917

918918
// Prune epoch state.
919919
for epoch := range iss.epochs {
920-
if epoch < tt.EpochNr(pruneIndex) {
920+
if epoch < pruneIndex {
921921
delete(iss.epochs, epoch)
922922
}
923923
}
@@ -931,7 +931,7 @@ func (iss *ISS) deliverCommonCheckpoint(chkpData []byte) error {
931931
// Note that we are not using the current epoch number here, because it is not relevant for checkpoints.
932932
// Using pruneIndex makes sure that the re-transmission is stopped
933933
// on every stable checkpoint (when another one is started).
934-
stdtypes.RetentionIndex(pruneIndex),
934+
stdtypes.RetentionIndex(pruneIndex), //nolint:gosec
935935
isspbevents.PushCheckpoint(iss.moduleConfig.Self).Pb(),
936936
)
937937

0 commit comments

Comments
 (0)