Skip to content

Commit

Permalink
Fix nil pointer deref if the file output lacks a path setting. (#40992
Browse files Browse the repository at this point in the history
) (#41030)

* fix nil pointer deref if the fileout lacks a path

* format

* fix tests

(cherry picked from commit bdcad10)

Co-authored-by: Alex K. <[email protected]>
  • Loading branch information
mergify[bot] and fearful-symmetry authored Sep 29, 2024
1 parent 055a124 commit 62a6e35
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions libbeat/outputs/fileout/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type fileOutConfig struct {

func defaultConfig() fileOutConfig {
return fileOutConfig{
Path: &PathFormatString{},
NumberOfFiles: 7,
RotateEveryKb: 10 * 1024,
Permissions: 0600,
Expand Down
1 change: 1 addition & 0 deletions libbeat/outputs/fileout/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestConfig(t *testing.T) {
config: config.MustNewConfigFrom([]byte(`{ }`)),
assertion: func(t *testing.T, actual *fileOutConfig, err error) {
expectedConfig := &fileOutConfig{
Path: &PathFormatString{},
NumberOfFiles: 7,
RotateEveryKb: 10 * 1024,
Permissions: 0600,
Expand Down
4 changes: 4 additions & 0 deletions libbeat/outputs/fileout/pathformatstring.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package fileout

import (
"fmt"
"os"
"strings"
"time"
Expand All @@ -44,6 +45,9 @@ func (fs *PathFormatString) Run(timestamp time.Time) (string, error) {
placeholderEvent := &beat.Event{
Timestamp: timestamp,
}
if fs.efs == nil {
return "", fmt.Errorf("path format string is nil; check if `path` option is configured correctly")
}
return fs.efs.Run(placeholderEvent)
}

Expand Down
12 changes: 12 additions & 0 deletions libbeat/outputs/fileout/pathformatstring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,20 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/elastic/elastic-agent-libs/config"
)

func TestCheckNilDoesntPanic(t *testing.T) {
newCfg := config.NewConfig()
handler, err := readConfig(newCfg)
require.NoError(t, err)
_, err = handler.Path.Run(time.Now())
require.Error(t, err)

}

func TestPathFormatString(t *testing.T) {
tests := []struct {
title string
Expand Down

0 comments on commit 62a6e35

Please sign in to comment.