Skip to content

Commit 1d858bd

Browse files
authored
Fix panic when Hearbeat monitor initialization fails twice (elastic#25073)
Initialization was only tried once. Second time a nil object and a nil error are returned, producing nil pointer dereferences later.
1 parent 2b11cf8 commit 1d858bd

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

CHANGELOG.next.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
400400

401401
- Fixed excessive memory usage introduced in 7.5 due to over-allocating memory for HTTP checks. {pull}15639[15639]
402402
- Fixed TCP TLS checks to properly validate hostnames, this broke in 7.x and only worked for IP SANs. {pull}17549[17549]
403+
- Fix panic when initialization of ICMP monitors fail twice. {pull}25073[25073]
403404

404405
*Journalbeat*
405406

heartbeat/monitors/active/icmp/stdloop.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,25 @@ type requestResult struct {
8787
// These vars should not be used directly, but rather getStdLoop
8888
// should be invoked to initialize and return stdLoop.
8989
var (
90-
stdICMPLoopInit sync.Once
90+
stdICMPLoopInit sync.Mutex
9191
stdICMPLoopSingleton *stdICMPLoop
9292
)
9393

9494
func getStdLoop() (*stdICMPLoop, error) {
95-
var loopErr error
96-
stdICMPLoopInit.Do(func() {
95+
stdICMPLoopInit.Lock()
96+
defer stdICMPLoopInit.Unlock()
97+
98+
if stdICMPLoopSingleton == nil {
9799
debugf("initializing ICMP loop")
98-
stdICMPLoopSingleton, loopErr = newICMPLoop()
99-
if loopErr == nil {
100-
debugf("ICMP loop successfully initialized")
100+
singleton, err := newICMPLoop()
101+
if err != nil {
102+
return nil, err
101103
}
102-
})
103-
return stdICMPLoopSingleton, loopErr
104+
stdICMPLoopSingleton = singleton
105+
debugf("ICMP loop successfully initialized")
106+
}
107+
108+
return stdICMPLoopSingleton, nil
104109
}
105110

106111
func noPingCapabilityError(message string) error {

0 commit comments

Comments
 (0)