From 977236cb5f337e3bee32c6bcea13619da777d3e5 Mon Sep 17 00:00:00 2001 From: Richard Gomez <32133502+rgmz@users.noreply.github.com> Date: Wed, 15 Jan 2025 07:45:37 -0500 Subject: [PATCH] fix: unaligned 64-bit atomic operation panic (#1696) --- cmd/root.go | 3 +-- detect/detect.go | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 49ebc4b0..7e9d8574 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -6,7 +6,6 @@ import ( "os" "path/filepath" "strings" - "sync/atomic" "time" "github.com/rs/zerolog" @@ -362,7 +361,7 @@ func bytesConvert(bytes uint64) string { } func findingSummaryAndExit(detector *detect.Detector, findings []report.Finding, exitCode int, start time.Time, err error) { - totalBytes := atomic.LoadUint64(&detector.TotalBytes) + totalBytes := detector.TotalBytes.Load() bytesMsg := fmt.Sprintf("scanned ~%d bytes (%s)", totalBytes, bytesConvert(totalBytes)) if err == nil { logging.Info().Msgf("%s in %s", bytesMsg, FormatDuration(time.Since(start))) diff --git a/detect/detect.go b/detect/detect.go index 35e66170..669d19ae 100644 --- a/detect/detect.go +++ b/detect/detect.go @@ -89,7 +89,7 @@ type Detector struct { ReportPath string Reporter report.Reporter - TotalBytes uint64 + TotalBytes atomic.Uint64 } // Fragment contains the data to be scanned @@ -184,9 +184,9 @@ func (d *Detector) DetectString(content string) []report.Finding { // Detect scans the given fragment and returns a list of findings func (d *Detector) Detect(fragment Fragment) []report.Finding { if fragment.Bytes == nil { - atomic.AddUint64(&d.TotalBytes, uint64(len(fragment.Raw))) + d.TotalBytes.Add(uint64(len(fragment.Raw))) } - atomic.AddUint64(&d.TotalBytes, uint64(len(fragment.Bytes))) + d.TotalBytes.Add(uint64(len(fragment.Bytes))) var findings []report.Finding