Skip to content

Commit df079b1

Browse files
authored
Merge pull request #821 from Icinga/golangci-lint-init-and-fix
Introduce golangci-lint and fix findings
2 parents a6d134c + 14d4070 commit df079b1

File tree

8 files changed

+43
-29
lines changed

8 files changed

+43
-29
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: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/icinga/icingadb/pkg/icingadb/v1/history"
1212
"github.com/jmoiron/sqlx"
1313
"github.com/pkg/errors"
14+
"math"
1415
"strconv"
1516
"strings"
1617
"time"
@@ -282,6 +283,14 @@ func convertDowntimeRows(
282283
cancelTime = actualEnd
283284
}
284285

286+
// The IDO duration is of type bigint, representing seconds, while Icinga DB's FlexibleDuration is an unsigned
287+
// bigint, representing milliseconds. In theory, there should be no negative value in the IDO and multiplied
288+
// with factor 1,000 should not overflow anything. In theory, at least. To make sure, invalid values are capped.
289+
var flexibleDuration uint64
290+
if durationSec := row.Duration; durationSec >= 0 && durationSec < math.MaxUint64/1_000 {
291+
flexibleDuration = uint64(durationSec) * 1_000
292+
}
293+
285294
downtimeHistory = append(downtimeHistory, &history.DowntimeHistory{
286295
DowntimeHistoryEntity: history.DowntimeHistoryEntity{DowntimeId: id},
287296
HistoryTableMeta: history.HistoryTableMeta{
@@ -299,7 +308,7 @@ func convertDowntimeRows(
299308
Author: row.AuthorName,
300309
Comment: row.CommentData,
301310
IsFlexible: types.Bool{Bool: row.IsFixed == 0, Valid: true},
302-
FlexibleDuration: uint64(row.Duration) * 1000,
311+
FlexibleDuration: flexibleDuration,
303312
ScheduledStartTime: scheduledStart,
304313
ScheduledEndTime: scheduledEnd,
305314
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 -- used as a non-cryptographic hash function to hash IDs
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 -- used as a non-cryptographic hash function to hash IDs
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 -- used as a non-cryptographic hash function to hash IDs
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: 7 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,10 @@ func (stmt *CleanupStmt) CleanupOlderThan(
4545
return database.CantPerformQuery(err, q)
4646
}
4747

48-
rowsDeleted, err = rs.RowsAffected()
48+
i, err := rs.RowsAffected()
49+
if err == nil && i >= 0 {
50+
rowsDeleted = uint64(i)
51+
}
4952

5053
return err
5154
},
@@ -57,15 +60,15 @@ func (stmt *CleanupStmt) CleanupOlderThan(
5760
return 0, err
5861
}
5962

60-
counter.Add(uint64(rowsDeleted))
63+
counter.Add(rowsDeleted)
6164

6265
for _, onSuccess := range onSuccess {
6366
if err := onSuccess(ctx, make([]struct{}, rowsDeleted)); err != nil {
6467
return 0, err
6568
}
6669
}
6770

68-
if rowsDeleted < int64(count) {
71+
if rowsDeleted < count {
6972
break
7073
}
7174
}

pkg/icingadb/delta_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,14 @@ func testDeltaVerifyResult(t *testing.T, name string, expected map[uint64]uint64
208208
}
209209

210210
func BenchmarkDelta(b *testing.B) {
211-
for n := 1 << 10; n <= 1<<20; n <<= 1 {
212-
b.Run(strconv.Itoa(n), func(b *testing.B) {
211+
for n := uint64(1 << 10); n <= 1<<20; n <<= 1 {
212+
b.Run(strconv.FormatUint(n, 10), func(b *testing.B) {
213213
benchmarkDelta(b, n)
214214
})
215215
}
216216
}
217217

218-
func benchmarkDelta(b *testing.B, numEntities int) {
218+
func benchmarkDelta(b *testing.B, numEntities uint64) {
219219
chActual := make([]chan database.Entity, b.N)
220220
chDesired := make([]chan database.Entity, b.N)
221221
for i := 0; i < b.N; i++ {
@@ -231,20 +231,20 @@ func benchmarkDelta(b *testing.B, numEntities int) {
231231
binary.BigEndian.PutUint64(e.PropertiesChecksum, checksum)
232232
return e
233233
}
234-
for i := 0; i < numEntities; i++ {
234+
for i := uint64(0); i < numEntities; i++ {
235235
// each iteration writes exactly one entity to each channel
236236
var eActual, eDesired database.Entity
237237
switch i % 3 {
238238
case 0: // distinct IDs
239-
eActual = makeEndpoint(1, uint64(i), uint64(i))
240-
eDesired = makeEndpoint(2, uint64(i), uint64(i))
239+
eActual = makeEndpoint(1, i, i)
240+
eDesired = makeEndpoint(2, i, i)
241241
case 1: // same ID, same checksum
242-
e := makeEndpoint(3, uint64(i), uint64(i))
242+
e := makeEndpoint(3, i, i)
243243
eActual = e
244244
eDesired = e
245245
case 2: // same ID, different checksum
246-
eActual = makeEndpoint(4, uint64(i), uint64(i))
247-
eDesired = makeEndpoint(4, uint64(i), uint64(i+1))
246+
eActual = makeEndpoint(4, i, i)
247+
eDesired = makeEndpoint(4, i, i+1)
248248
}
249249
for _, ch := range chActual {
250250
ch <- eActual

pkg/icingadb/types/notification_type.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package types
33
import (
44
"database/sql/driver"
55
"encoding"
6-
"github.com/icinga/icinga-go-library/types"
76
"github.com/pkg/errors"
87
"strconv"
98
)
@@ -15,17 +14,12 @@ type NotificationType uint16
1514
func (nt *NotificationType) UnmarshalText(text []byte) error {
1615
s := string(text)
1716

18-
i, err := strconv.ParseUint(s, 10, 64)
17+
i, err := strconv.ParseUint(s, 10, 16)
1918
if err != nil {
20-
return types.CantParseUint64(err, s)
19+
return errors.Wrapf(err, "can't parse %q into uint16", s)
2120
}
2221

2322
n := NotificationType(i)
24-
if uint64(n) != i {
25-
// Truncated due to above cast, obviously too high
26-
return badNotificationType(s)
27-
}
28-
2923
if _, ok := notificationTypes[n]; !ok {
3024
return badNotificationType(s)
3125
}

0 commit comments

Comments
 (0)