Skip to content

Commit

Permalink
internal/log: fix potential integer conversion issue from parsed value (
Browse files Browse the repository at this point in the history
#2289)

Co-authored-by: Katie Hockman <[email protected]>
  • Loading branch information
darccio and katiehockman authored Oct 25, 2023
1 parent 5e9b0b1 commit 5f985bb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
13 changes: 11 additions & 2 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,18 @@ var (

func init() {
if v := os.Getenv("DD_LOGGING_RATE"); v != "" {
if sec, err := strconv.ParseUint(v, 10, 64); err != nil {
Warn("Invalid value for DD_LOGGING_RATE: %v", err)
setLoggingRate(v)
}
}

func setLoggingRate(v string) {
if sec, err := strconv.ParseInt(v, 10, 64); err != nil {
Warn("Invalid value for DD_LOGGING_RATE: %v", err)
} else {
if sec < 0 {
Warn("Invalid value for DD_LOGGING_RATE: negative value")
} else {
// DD_LOGGING_RATE = 0 allows to log errors immediately.
errrate = time.Duration(sec) * time.Second
}
}
Expand Down
36 changes: 36 additions & 0 deletions internal/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,42 @@ func TestRecordLoggerIgnore(t *testing.T) {
assert.Contains(t, tp.Logs()[0], "appsec")
}

func TestSetLoggingRate(t *testing.T) {
testCases := []struct {
input string
result time.Duration
}{
{
input: "",
result: time.Minute,
},
{
input: "0",
result: 0 * time.Second,
},
{
input: "10",
result: 10 * time.Second,
},
{
input: "-1",
result: time.Minute,
},
{
input: "this is not a number",
result: time.Minute,
},
}
for _, tC := range testCases {
tC := tC
errrate = time.Minute // reset global variable
t.Run(tC.input, func(t *testing.T) {
setLoggingRate(tC.input)
assert.Equal(t, tC.result, errrate)
})
}
}

func BenchmarkError(b *testing.B) {
Error("k %s", "a") // warm up cache
for i := 0; i < b.N; i++ {
Expand Down

0 comments on commit 5f985bb

Please sign in to comment.