Skip to content

Commit 2184aff

Browse files
committed
gosec: handle integer conversions and potential overflows
1 parent 28b06b7 commit 2184aff

File tree

5 files changed

+25
-19
lines changed

5 files changed

+25
-19
lines changed

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,

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: 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/history/retention.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ func (r *Retention) Start(ctx context.Context) error {
172172
r.logger.Debugf("Skipping history retention for category %s", stmt.Category)
173173
continue
174174
}
175+
if days > 1<<16 {
176+
r.logger.Warnf(
177+
"Skipping history retention for category %s as %d days may overflow, leading to undesired retention behavior",
178+
stmt.Category, days)
179+
continue
180+
}
175181

176182
r.logger.Debugw(
177183
fmt.Sprintf("Starting history retention for category %s", stmt.Category),
@@ -182,7 +188,7 @@ func (r *Retention) Start(ctx context.Context) error {
182188

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 -- if this overflows, AddDate will overflow as well
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/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)