From 82c3f9acdcf8d0d1987ba20cb3cde942435a0f5a Mon Sep 17 00:00:00 2001 From: Bastien Riviere Date: Thu, 5 Dec 2024 10:30:24 +0100 Subject: [PATCH] fix(tmplexec): handle rare case where lastMatcherEvent address change after lock --- pkg/tmplexec/exec.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pkg/tmplexec/exec.go b/pkg/tmplexec/exec.go index 0caefe6024..dc1add5573 100644 --- a/pkg/tmplexec/exec.go +++ b/pkg/tmplexec/exec.go @@ -214,10 +214,12 @@ func (e *TemplateExecuter) Execute(ctx *scan.ScanContext) (bool, error) { ctx.LogError(errx) if lastMatcherEvent != nil { - lastMatcherEvent.Lock() - lastMatcherEvent.InternalEvent["error"] = getErrorCause(ctx.GenerateErrorMessage()) - lastMatcherEvent.Unlock() - writeFailureCallback(lastMatcherEvent, e.options.Options.MatcherStatus) + // copy lastMatcherEvent pointer to avoid race condition where lastMatcherEvent is changed after the lock. + event := lastMatcherEvent + event.Lock() + event.InternalEvent["error"] = getErrorCause(ctx.GenerateErrorMessage()) + event.Unlock() + writeFailureCallback(event, e.options.Options.MatcherStatus) } //TODO: this is a hacky way to handle the case where the callback is not called and matcher-status is true.