From 10a31bd84103e272f5afa599093b6dfe167f2bb5 Mon Sep 17 00:00:00 2001 From: alex <53851759+alxxjohn@users.noreply.github.com> Date: Tue, 18 Aug 2026 17:21:55 -0400 Subject: [PATCH] fix(quality): skip impossible clone windows --- .../codeguard/checks/quality/quality_clone.go | 10 ++++++++++ .../checks/quality/quality_clone_test.go | 15 +++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 internal/codeguard/checks/quality/quality_clone_test.go diff --git a/internal/codeguard/checks/quality/quality_clone.go b/internal/codeguard/checks/quality/quality_clone.go index 20b8c36b..71475fb0 100644 --- a/internal/codeguard/checks/quality/quality_clone.go +++ b/internal/codeguard/checks/quality/quality_clone.go @@ -88,6 +88,16 @@ const cloneWindowMultiplier uint64 = 6364136223846793005 // candidates are identical to the old per-window byte hashing. func cloneWindowIndex(docs []cloneDocument, threshold int) cloneIndex { index := make(cloneIndex) + hasWindow := false + for _, doc := range docs { + if len(doc.Tokens) >= threshold { + hasWindow = true + break + } + } + if !hasWindow { + return index + } // top = multiplier^(threshold-1), the weight of the token leaving the // window on each slide. top := uint64(1) diff --git a/internal/codeguard/checks/quality/quality_clone_test.go b/internal/codeguard/checks/quality/quality_clone_test.go new file mode 100644 index 00000000..06b537d5 --- /dev/null +++ b/internal/codeguard/checks/quality/quality_clone_test.go @@ -0,0 +1,15 @@ +package quality + +import "testing" + +func TestCloneWindowIndexSkipsMultiplierForOversizedThreshold(t *testing.T) { + docs := []cloneDocument{ + {Tokens: []cloneToken{{Hash: 1}}}, + {Tokens: []cloneToken{{Hash: 1}}}, + } + threshold := int(^uint(0) >> 1) + + if index := cloneWindowIndex(docs, threshold); len(index) != 0 { + t.Fatalf("cloneWindowIndex() returned %d windows, want 0", len(index)) + } +}