Skip to content

Commit 4181d9e

Browse files
fix overflow integer conversion lint warning from gosec (#399)
* fix overflow integer conversion lint warning from gosec * fix integer conversion for linux and windows builds * fix type in tests * use binary shift to compute power of 2
1 parent 3612b51 commit 4181d9e

File tree

17 files changed

+107
-40
lines changed

17 files changed

+107
-40
lines changed

Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,12 @@ generate-config-reference: build
278278
LAYOUT_UPLINK="[go to top](#reference)" \
279279
$(abspath $(BINARY)) generate --config-reference --to $(CONFIG_REFERENCE_DIR)
280280

281+
.PHONY: documentation
281282
documentation: generate-jsonschema generate-config-reference
282283
@echo "[*] $@"
283284
cd docs && hugo --minify
284285

286+
.PHONY: syslog-ng
285287
syslog-ng:
286288
@echo "[*] $@"
287289
docker run -d \
@@ -300,6 +302,23 @@ checkdoc:
300302
@echo "[*] $@"
301303
$(GOCMD) run ./config/checkdoc -r docs/content
302304

305+
.PHONY: checklinks
303306
checklinks:
304307
@echo "[*] $@"
305308
muffet -b 8192 --exclude="(linux.die.net|stackoverflow.com)" http://localhost:1313/resticprofile/
309+
310+
.PHONY: lint
311+
lint:
312+
@echo "[*] $@"
313+
GOOS=darwin golangci-lint run
314+
GOOS=linux golangci-lint run
315+
GOOS=windows golangci-lint run
316+
317+
.PHONY: fix
318+
fix:
319+
@echo "[*] $@"
320+
$(GOCMD) mod tidy
321+
$(GOCMD) fix ./...
322+
GOOS=darwin golangci-lint run --fix
323+
GOOS=linux golangci-lint run --fix
324+
GOOS=windows golangci-lint run --fix

filesearch/filesearch_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ func TestShellExpand(t *testing.T) {
314314
func TestFindConfigurationIncludes(t *testing.T) {
315315
t.Parallel()
316316

317-
testID := fmt.Sprintf("%d", uint32(time.Now().UnixNano()))
317+
testID := fmt.Sprintf("%x", time.Now().UnixNano())
318318
tempDir := os.TempDir()
319319
files := []string{
320320
filepath.Join(tempDir, "base."+testID+".conf"),

lock/lock.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
// SetPID is a callback that writes the PID in the lockfile
16-
type SetPID func(pid int)
16+
type SetPID func(pid int32)
1717

1818
// Lock prevents code to run at the same time by using a lockfile
1919
type Lock struct {
@@ -83,7 +83,7 @@ func (l *Lock) Who() (string, error) {
8383

8484
// SetPID writes down the PID in the lock file.
8585
// You can run the method as many times as you want when the PID changes
86-
func (l *Lock) SetPID(pid int) {
86+
func (l *Lock) SetPID(pid int32) {
8787
if !l.locked {
8888
return
8989
}
@@ -109,7 +109,7 @@ func (l *Lock) LastPID() (int32, error) {
109109
if contents[i] != "" {
110110
pid, err := strconv.ParseInt(contents[i], 10, 32)
111111
if err == nil {
112-
return int32(pid), nil
112+
return int32(pid), nil //nolint:gosec
113113
}
114114
}
115115
}

lock/lock_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,24 +130,24 @@ func TestSetMorePID(t *testing.T) {
130130
func TestProcessPID(t *testing.T) {
131131
t.Parallel()
132132

133-
childPID := 0
133+
var childPID int32
134134
buffer := &bytes.Buffer{}
135135

136136
// use the lock helper binary (we only need to wait for some time, we don't need the locking part)
137137
cmd := shell.NewCommand(helperBinary, []string{"-wait", "200", "-lock", filepath.Join(t.TempDir(), t.Name())})
138138
cmd.Stdout = buffer
139139
// SetPID method is called right after we forked and have a PID available
140-
cmd.SetPID = func(pid int) {
140+
cmd.SetPID = func(pid int32) {
141141
childPID = pid
142-
running, err := process.PidExists(int32(childPID))
142+
running, err := process.PidExists(childPID)
143143
assert.NoError(t, err)
144144
assert.True(t, running)
145145
}
146146
_, _, err := cmd.Run()
147147
require.NoError(t, err)
148148

149149
// at that point, the child process should be finished
150-
running, err := process.PidExists(int32(childPID))
150+
running, err := process.PidExists(childPID)
151151
assert.NoError(t, err)
152152
assert.False(t, running)
153153
}
@@ -220,7 +220,7 @@ func TestForceLockWithRunningPID(t *testing.T) {
220220

221221
// user the lock helper binary (we only need to wait for some time, we don't need the locking part)
222222
cmd := shell.NewCommand(helperBinary, []string{"-wait", "100", "-lock", filepath.Join(t.TempDir(), t.Name())})
223-
cmd.SetPID = func(pid int) {
223+
cmd.SetPID = func(pid int32) {
224224
lock.SetPID(pid)
225225
// make sure we cannot break the lock right now
226226
other := NewLock(tempfile)

priority/ioprio_linux.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ func getIOPrio(who IOPrioWho) (IOPrioClass, int, error) {
7171
if errno != 0 {
7272
return 0, 0, errnoToError(errno)
7373
}
74-
class := r1 >> IOPrioClassShift
75-
value := r1 & IOPrioMask
76-
return IOPrioClass(class), int(value), nil
74+
class := IOPrioClass(r1 >> IOPrioClassShift) //nolint:gosec
75+
value := int(r1 & IOPrioMask) //nolint:gosec
76+
return class, value, nil
7777
}
7878

7979
func setIOPrio(who IOPrioWho, class IOPrioClass, value int) error {

schedule/tree_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//+build darwin
1+
//go:build darwin
22

33
package schedule
44

schedule/tree_darwin_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//go:build darwin
2-
// +build darwin
32

43
package schedule
54

schtasks/taskscheduler.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package schtasks
55
import (
66
"errors"
77
"fmt"
8-
"math"
98
"os/user"
109
"strings"
1110
"text/tabwriter"
@@ -589,18 +588,18 @@ func compileDifferences(recurrences []time.Time) ([]time.Duration, []time.Durati
589588
return differences, compactDifferences
590589
}
591590

592-
func convertWeekdaysToBitmap(weekdays []int) int {
591+
func convertWeekdaysToBitmap(weekdays []int) uint16 {
593592
if len(weekdays) == 0 {
594593
return 0
595594
}
596-
bitmap := 0
595+
var bitmap uint16
597596
for _, weekday := range weekdays {
598597
bitmap |= getWeekdayBit(weekday)
599598
}
600599
return bitmap
601600
}
602601

603-
func getWeekdayBit(weekday int) int {
602+
func getWeekdayBit(weekday int) uint16 {
604603
switch weekday {
605604
case 0:
606605
return 1
@@ -623,32 +622,36 @@ func getWeekdayBit(weekday int) int {
623622
return 0
624623
}
625624

626-
func convertMonthsToBitmap(months []int) int {
625+
func convertMonthsToBitmap(months []int) uint16 {
627626
if months == nil {
628627
return 0
629628
}
630629
if len(months) == 0 {
631630
// all values
632-
return int(math.Exp2(12)) - 1
631+
return (1 << 12) - 1
633632
}
634-
bitmap := 0
633+
var bitmap uint16
635634
for _, month := range months {
636-
bitmap |= int(math.Exp2(float64(month - 1)))
635+
if month > 0 && month <= 12 {
636+
bitmap |= 1 << (month - 1)
637+
}
637638
}
638639
return bitmap
639640
}
640641

641-
func convertDaysToBitmap(days []int) int {
642+
func convertDaysToBitmap(days []int) uint32 {
642643
if days == nil {
643644
return 0
644645
}
645646
if len(days) == 0 {
646647
// every day
647-
return int(math.Exp2(31)) - 1
648+
return (1 << 31) - 1
648649
}
649-
bitmap := 0
650+
var bitmap uint32
650651
for _, day := range days {
651-
bitmap |= int(math.Exp2(float64(day - 1)))
652+
if day > 0 && day <= 31 {
653+
bitmap |= 1 << (day - 1)
654+
}
652655
}
653656
return bitmap
654657
}

schtasks/taskscheduler_test.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package schtasks
44

55
import (
66
"bytes"
7+
"math"
78
"os/exec"
89
"regexp"
910
"strconv"
@@ -22,7 +23,7 @@ import (
2223
func TestConversionWeekdaysToBitmap(t *testing.T) {
2324
testData := []struct {
2425
weekdays []int
25-
bitmap int
26+
bitmap uint16
2627
}{
2728
{nil, 0},
2829
{[]int{}, 0},
@@ -40,6 +41,48 @@ func TestConversionWeekdaysToBitmap(t *testing.T) {
4041
}
4142
}
4243

44+
func TestConversionMonthsToBitmap(t *testing.T) {
45+
testData := []struct {
46+
months []int
47+
bitmap uint16
48+
}{
49+
{nil, 0},
50+
{[]int{}, 4095}, // every month
51+
{[]int{0}, 0},
52+
{[]int{1}, 1},
53+
{[]int{2}, 2},
54+
{[]int{7}, 64},
55+
{[]int{1, 2, 3, 4, 5, 6, 7}, 127},
56+
{[]int{0, 1, 2, 3, 4, 5, 6, 7}, 127},
57+
{[]int{1, 2, 3, 4, 5, 6}, 63},
58+
}
59+
60+
for _, testItem := range testData {
61+
assert.Equal(t, testItem.bitmap, convertMonthsToBitmap(testItem.months))
62+
}
63+
}
64+
65+
func TestConversionDaysToBitmap(t *testing.T) {
66+
testData := []struct {
67+
days []int
68+
bitmap uint32
69+
}{
70+
{nil, 0},
71+
{[]int{}, math.MaxInt32}, // every day
72+
{[]int{0}, 0},
73+
{[]int{1}, 1},
74+
{[]int{2}, 2},
75+
{[]int{7}, 64},
76+
{[]int{1, 2, 3, 4, 5, 6, 7}, 127},
77+
{[]int{0, 1, 2, 3, 4, 5, 6, 7}, 127},
78+
{[]int{1, 2, 3, 4, 5, 6}, 63},
79+
}
80+
81+
for _, testItem := range testData {
82+
assert.Equal(t, testItem.bitmap, convertDaysToBitmap(testItem.days))
83+
}
84+
}
85+
4386
func TestCompileDifferences(t *testing.T) {
4487
testData := []struct {
4588
input string
@@ -111,7 +154,7 @@ func TestTaskSchedulerConversion(t *testing.T) {
111154
// 3rd task will be a weekly recurring
112155
weeklyEvent, ok := task.Triggers[2].(taskmaster.WeeklyTrigger)
113156
require.True(t, ok)
114-
assert.Equal(t, getWeekdayBit(int(time.Saturday))+getWeekdayBit(int(time.Sunday)), int(weeklyEvent.DaysOfWeek))
157+
assert.Equal(t, getWeekdayBit(int(time.Saturday))+getWeekdayBit(int(time.Sunday)), uint16(weeklyEvent.DaysOfWeek))
115158

116159
// 4th task will be a monthly recurring
117160
monthlyEvent, ok := task.Triggers[3].(taskmaster.MonthlyTrigger)

shell/command.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const (
2727
)
2828

2929
// SetPID is a callback to send the PID of the current child process
30-
type SetPID func(pid int)
30+
type SetPID func(pid int32)
3131

3232
// ScanOutput is a callback to scan the default output of the command
3333
// The implementation is expected to send everything read from the reader back to the writer
@@ -124,7 +124,7 @@ func (c *Command) Run() (monitor.Summary, string, error) {
124124
}
125125
if c.SetPID != nil {
126126
// send the PID back (to write down in a lockfile)
127-
c.SetPID(cmd.Process.Pid)
127+
c.SetPID(int32(cmd.Process.Pid)) //nolint:gosec
128128
}
129129
// setup the OS signalling if we need it (typically used for unixes but not windows)
130130
if c.sigChan != nil {

0 commit comments

Comments
 (0)