Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] - use DetectorKey as the key in the detectorKeysWithResults map #2366

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/engine/ahocorasick/ahocorasickcore.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ func NewAhoCorasickCore(allDetectors []detectors.Detector) *AhoCorasickCore {
}
}

// GetDetectorByKey returns the detector associated with the given key. If no detector is found, it
// returns nil.
func (ac *AhoCorasickCore) GetDetectorByKey(key DetectorKey) detectors.Detector {
return ac.detectorsByKey[key]
}

// DetectorInfo represents a detected pattern's metadata in a data chunk.
// It encapsulates the key identifying a specific detector and the detector instance itself.
type DetectorInfo struct {
Expand Down
16 changes: 8 additions & 8 deletions pkg/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ func (e *Engine) verificationOverlapWorker(ctx context.Context) {

// Reuse the same map and slice to avoid allocations.
const avgSecretsPerDetector = 8
detectorsWithResult := make(map[ahocorasick.DetectorInfo]struct{}, avgSecretsPerDetector)
detectorKeysWithResults := make(map[ahocorasick.DetectorKey]struct{}, avgSecretsPerDetector)
chunkSecrets := make(map[chunkSecretKey]struct{}, avgSecretsPerDetector)

for chunk := range e.verificationOverlapChunksChan {
Expand All @@ -658,8 +658,8 @@ func (e *Engine) verificationOverlapWorker(ctx context.Context) {
if len(results) == 0 {
continue
}
if _, ok := detectorsWithResult[detector]; !ok {
detectorsWithResult[detector] = struct{}{}
if _, ok := detectorKeysWithResults[detector.Key]; !ok {
detectorKeysWithResults[detector.Key] = struct{}{}
}

for _, res := range results {
Expand Down Expand Up @@ -694,18 +694,18 @@ func (e *Engine) verificationOverlapWorker(ctx context.Context) {
}, res)

// Remove the detector from the list of detectors with results.
delete(detectorsWithResult, detector)
delete(detectorKeysWithResults, detector.Key)
}
chunkSecrets[key] = struct{}{}
}
}

for detector := range detectorsWithResult {
for key := range detectorKeysWithResults {
wgDetect.Add(1)
chunk.chunk.Verify = e.verify
e.detectableChunksChan <- detectableChunk{
chunk: chunk.chunk,
detector: detector,
detector: e.ahoCorasickCore.GetDetectorByKey(key),
ahrav marked this conversation as resolved.
Show resolved Hide resolved
decoder: chunk.decoder,
wgDoneFn: wgDetect.Done,
}
Expand All @@ -715,8 +715,8 @@ func (e *Engine) verificationOverlapWorker(ctx context.Context) {
for k := range chunkSecrets {
delete(chunkSecrets, k)
}
for k := range detectorsWithResult {
delete(detectorsWithResult, k)
for k := range detectorKeysWithResults {
delete(detectorKeysWithResults, k)
}

chunk.verificationOverlapWgDoneFn()
Expand Down
Loading