From 3d3d80172939f9c0672543c04c78d4a4fbf69a55 Mon Sep 17 00:00:00 2001 From: Zachary Rice Date: Fri, 17 Dec 2021 09:49:12 -0600 Subject: [PATCH] fixing eof location bug (#756) --- detect/detect_test.go | 12 ++++++++++++ detect/location.go | 14 +++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/detect/detect_test.go b/detect/detect_test.go index fa202c04..89834efb 100644 --- a/detect/detect_test.go +++ b/detect/detect_test.go @@ -34,6 +34,10 @@ func TestDetectFindings(t *testing.T) { File: "tmp.go", RuleID: "aws-access-key", Tags: []string{"key", "AWS"}, + StartLine: 1, + EndLine: 1, + StartColumn: 15, + EndColumn: 34, }, }, }, @@ -69,6 +73,10 @@ func TestDetectFindings(t *testing.T) { RuleID: "discord-api-key", Tags: []string{}, Entropy: 3.7906237, + StartLine: 1, + EndLine: 1, + StartColumn: 7, + EndColumn: 93, }, }, }, @@ -91,6 +99,10 @@ func TestDetectFindings(t *testing.T) { RuleID: "generic-api-key", Tags: []string{}, Entropy: 3.7906237, + StartLine: 1, + EndLine: 1, + StartColumn: 22, + EndColumn: 93, }, }, }, diff --git a/detect/location.go b/detect/location.go index b472eeaf..2bd5754f 100644 --- a/detect/location.go +++ b/detect/location.go @@ -14,11 +14,15 @@ func getLocation(linePairs [][]int, start int, end int) Location { var ( prevNewLine int location Location + lineSet bool + _lineNum int ) for lineNum, pair := range linePairs { + _lineNum = lineNum newLineByteIndex := pair[0] if prevNewLine <= start && start < newLineByteIndex { + lineSet = true location.startLine = lineNum location.endLine = lineNum location.startColumn = (start - prevNewLine) + 1 // +1 because counting starts at 1 @@ -30,9 +34,17 @@ func getLocation(linePairs [][]int, start int, end int) Location { location.endColumn = (end - prevNewLine) location.endLineIndex = newLineByteIndex } - prevNewLine = pair[0] } + if !lineSet { + // if lines never get set then that means the secret is most likely + // on the last line of the diff output and the diff output does not have + // a newline + location.startColumn = (start - prevNewLine) + 1 // +1 because counting starts at 1 + location.endColumn = (end - prevNewLine) + location.startLine = _lineNum + 1 + location.endLine = _lineNum + 1 + } return location }