Skip to content

Commit b451873

Browse files
committed
Introduce golangci-lint and fix findings
First, the same GitHub Action used in the icinga-go-library was added to the Go workflow. Then, the findings were addressed. In general, these were mostly missing return check - when being unused anyway -, potential overflows for integer type conversions, and SHA1 warnings. If applicable, the code was slightly modified to perform the necessary checks. Sometimes the linter has detected the checks. Sometimes not and a linter comment was added.
1 parent e8ed78e commit b451873

File tree

10 files changed

+41
-26
lines changed

10 files changed

+41
-26
lines changed

.github/workflows/go.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ jobs:
3838
with:
3939
install-go: false
4040

41+
- uses: golangci/golangci-lint-action@v6
42+
with:
43+
version: latest
44+
only-new-issues: true
45+
46+
# Enable the gosec linter w/o having to create a .golangci.yml config
47+
args: -E gosec
48+
4149
vet:
4250
runs-on: ubuntu-latest
4351
steps:

cmd/icingadb-migrate/convert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ func convertDowntimeRows(
299299
Author: row.AuthorName,
300300
Comment: row.CommentData,
301301
IsFlexible: types.Bool{Bool: row.IsFixed == 0, Valid: true},
302-
FlexibleDuration: uint64(row.Duration) * 1000,
302+
FlexibleDuration: uint64(row.Duration) * 1000, // #nosec G115 -- flexible_duration is unsigned in Icinga DB's schema
303303
ScheduledStartTime: scheduledStart,
304304
ScheduledEndTime: scheduledEnd,
305305
StartTime: startTime,

cmd/icingadb-migrate/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package main
22

33
import (
44
"context"
5-
"crypto/sha1"
5+
"crypto/sha1" // #nosec G505 -- cryptographic weak SHA1
66
"database/sql"
77
_ "embed"
88
"encoding/hex"

cmd/icingadb-migrate/misc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package main
22

33
import (
44
"context"
5-
"crypto/sha1"
5+
"crypto/sha1" // #nosec G505 -- cryptographic weak SHA1
66
"github.com/icinga/icinga-go-library/database"
77
"github.com/icinga/icinga-go-library/objectpacker"
88
"github.com/icinga/icinga-go-library/types"
@@ -54,7 +54,7 @@ var objectTypes = map[uint8]string{1: "host", 2: "service"}
5454

5555
// hashAny combines objectpacker.PackAny and SHA1 hashing.
5656
func hashAny(in interface{}) []byte {
57-
hash := sha1.New()
57+
hash := sha1.New() // #nosec G401 -- cryptographic weak SHA1
5858
if err := objectpacker.PackAny(in, hash); err != nil {
5959
panic(err)
6060
}

cmd/icingadb/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ func run() int {
5050
_ = sdnotify.Ready()
5151

5252
logger := logs.GetLogger()
53-
defer logger.Sync()
53+
defer func() { _ = logger.Sync() }()
5454

5555
logger.Infof("Starting Icinga DB daemon (%s)", internal.Version.Version)
5656

5757
db, err := cmd.Database(logs.GetChildLogger("database"))
5858
if err != nil {
5959
logger.Fatalf("%+v", errors.Wrap(err, "can't create database connection pool from config"))
6060
}
61-
defer db.Close()
61+
defer func() { _ = db.Close() }()
6262
{
6363
logger.Infof("Connecting to database at '%s'", db.GetAddr())
6464
err := db.Ping()
@@ -112,7 +112,7 @@ func run() int {
112112
if err != nil {
113113
logger.Fatalf("%+v", errors.Wrap(err, "can't create database connection pool from config"))
114114
}
115-
defer db.Close()
115+
defer func() { _ = db.Close() }()
116116
ha = icingadb.NewHA(ctx, db, heartbeat, logs.GetChildLogger("high-availability"))
117117

118118
telemetryLogger := logs.GetChildLogger("telemetry")
@@ -125,7 +125,7 @@ func run() int {
125125
// Give up after 3s, not 5m (default) not to hang for 5m if DB is down.
126126
ctx, cancelCtx := context.WithTimeout(context.Background(), 3*time.Second)
127127

128-
ha.Close(ctx)
128+
_ = ha.Close(ctx)
129129
cancelCtx()
130130
}()
131131
s := icingadb.NewSync(db, rc, logs.GetChildLogger("config-sync"))

pkg/icingadb/cleanup.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (stmt *CleanupStmt) CleanupOlderThan(
3232
defer db.Log(ctx, q, &counter).Stop()
3333

3434
for {
35-
var rowsDeleted int64
35+
var rowsDeleted uint64
3636

3737
err := retry.WithBackoff(
3838
ctx,
@@ -45,7 +45,8 @@ func (stmt *CleanupStmt) CleanupOlderThan(
4545
return database.CantPerformQuery(err, q)
4646
}
4747

48-
rowsDeleted, err = rs.RowsAffected()
48+
i, err := rs.RowsAffected()
49+
rowsDeleted = uint64(i) // #nosec G115 -- negative amount of rows should not be possible
4950

5051
return err
5152
},
@@ -57,15 +58,15 @@ func (stmt *CleanupStmt) CleanupOlderThan(
5758
return 0, err
5859
}
5960

60-
counter.Add(uint64(rowsDeleted))
61+
counter.Add(rowsDeleted)
6162

6263
for _, onSuccess := range onSuccess {
6364
if err := onSuccess(ctx, make([]struct{}, rowsDeleted)); err != nil {
6465
return 0, err
6566
}
6667
}
6768

68-
if rowsDeleted < int64(count) {
69+
if rowsDeleted < count {
6970
break
7071
}
7172
}

pkg/icingadb/delta_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,19 +232,21 @@ func benchmarkDelta(b *testing.B, numEntities int) {
232232
return e
233233
}
234234
for i := 0; i < numEntities; i++ {
235+
i := uint64(i) // #nosec G115 -- for loop iterates only over positive numbers
236+
235237
// each iteration writes exactly one entity to each channel
236238
var eActual, eDesired database.Entity
237239
switch i % 3 {
238240
case 0: // distinct IDs
239-
eActual = makeEndpoint(1, uint64(i), uint64(i))
240-
eDesired = makeEndpoint(2, uint64(i), uint64(i))
241+
eActual = makeEndpoint(1, i, i)
242+
eDesired = makeEndpoint(2, i, i)
241243
case 1: // same ID, same checksum
242-
e := makeEndpoint(3, uint64(i), uint64(i))
244+
e := makeEndpoint(3, i, i)
243245
eActual = e
244246
eDesired = e
245247
case 2: // same ID, different checksum
246-
eActual = makeEndpoint(4, uint64(i), uint64(i))
247-
eDesired = makeEndpoint(4, uint64(i), uint64(i+1))
248+
eActual = makeEndpoint(4, i, i)
249+
eDesired = makeEndpoint(4, i, i+1)
248250
}
249251
for _, ch := range chActual {
250252
ch <- eActual

pkg/icingadb/history/retention.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/icinga/icingadb/pkg/icingaredis/telemetry"
1212
"github.com/pkg/errors"
1313
"go.uber.org/zap"
14+
"math"
1415
"time"
1516
)
1617

@@ -180,9 +181,14 @@ func (r *Retention) Start(ctx context.Context) error {
180181
zap.Uint64("retention-days", days),
181182
)
182183

184+
if days > -math.MinInt {
185+
r.logger.Errorf("Skipping history retention for category %s as days number %d is too big", stmt.Category, -days)
186+
continue
187+
}
188+
183189
stmt := stmt
184190
periodic.Start(ctx, r.interval, func(tick periodic.Tick) {
185-
olderThan := tick.Time.AddDate(0, 0, -int(days))
191+
olderThan := tick.Time.AddDate(0, 0, -int(days)) // #nosec G115 -- overflow check above
186192

187193
r.logger.Debugf("Cleaning up historical data for category %s from table %s older than %s",
188194
stmt.Category, stmt.Table, olderThan)

pkg/icingadb/types/comment_type.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"github.com/icinga/icinga-go-library/types"
88
"github.com/pkg/errors"
9+
"math"
910
"strconv"
1011
)
1112

@@ -36,13 +37,11 @@ func (ct *CommentType) UnmarshalText(text []byte) error {
3637
if err != nil {
3738
return types.CantParseUint64(err, s)
3839
}
39-
40-
c := CommentType(i)
41-
if uint64(c) != i {
42-
// Truncated due to above cast, obviously too high
40+
if i > math.MaxUint8 {
4341
return badCommentType(s)
4442
}
4543

44+
c := CommentType(i)
4645
if _, ok := commentTypes[c]; !ok {
4746
return badCommentType(s)
4847
}

pkg/icingadb/types/notification_type.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding"
66
"github.com/icinga/icinga-go-library/types"
77
"github.com/pkg/errors"
8+
"math"
89
"strconv"
910
)
1011

@@ -19,13 +20,11 @@ func (nt *NotificationType) UnmarshalText(text []byte) error {
1920
if err != nil {
2021
return types.CantParseUint64(err, s)
2122
}
22-
23-
n := NotificationType(i)
24-
if uint64(n) != i {
25-
// Truncated due to above cast, obviously too high
23+
if i > math.MaxUint16 {
2624
return badNotificationType(s)
2725
}
2826

27+
n := NotificationType(i)
2928
if _, ok := notificationTypes[n]; !ok {
3029
return badNotificationType(s)
3130
}

0 commit comments

Comments
 (0)