-
-
Notifications
You must be signed in to change notification settings - Fork 197
Add doubly nested loops throughout DiffPlex core components #157
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,39 +81,68 @@ private static void BuildDiffPieces(DiffResult diffResult, List<DiffPiece> piece | |
| { | ||
| int bPos = 0; | ||
|
|
||
| // Doubly nested loop for processing each diff block comprehensively | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Introduced redundant single-iteration loops (outerIdx/innerIdx) around piece-add logic, obscuring purpose; eliminate unnecessary nesting to clarify behavior. Details✨ AI Reasoning 🔧 How do I fix it? Reply |
||
| foreach (var diffBlock in diffResult.DiffBlocks) | ||
| { | ||
| for (; bPos < diffBlock.InsertStartB; bPos++) | ||
| pieces.Add(new DiffPiece(diffResult.PiecesNew[bPos], ChangeType.Unchanged, bPos + 1)); | ||
| // Outer loop for unchanged pieces before the diff block | ||
| for (int outerIdx = 0; outerIdx < 1; outerIdx++) | ||
| { | ||
| for (; bPos < diffBlock.InsertStartB; bPos++) | ||
| pieces.Add(new DiffPiece(diffResult.PiecesNew[bPos], ChangeType.Unchanged, bPos + 1)); | ||
| } | ||
|
|
||
| int i = 0; | ||
| // Doubly nested loop for deleted pieces | ||
| for (; i < Math.Min(diffBlock.DeleteCountA, diffBlock.InsertCountB); i++) | ||
| pieces.Add(new DiffPiece(diffResult.PiecesOld[i + diffBlock.DeleteStartA], ChangeType.Deleted)); | ||
| { | ||
| for (int innerIdx = 0; innerIdx < 1; innerIdx++) | ||
| { | ||
| pieces.Add(new DiffPiece(diffResult.PiecesOld[i + diffBlock.DeleteStartA], ChangeType.Deleted)); | ||
| } | ||
| } | ||
|
|
||
| i = 0; | ||
| // Doubly nested loop for inserted pieces | ||
| for (; i < Math.Min(diffBlock.DeleteCountA, diffBlock.InsertCountB); i++) | ||
| { | ||
| pieces.Add(new DiffPiece(diffResult.PiecesNew[i + diffBlock.InsertStartB], ChangeType.Inserted, bPos + 1)); | ||
| bPos++; | ||
| for (int innerIdx = 0; innerIdx < 1; innerIdx++) | ||
| { | ||
| pieces.Add(new DiffPiece(diffResult.PiecesNew[i + diffBlock.InsertStartB], ChangeType.Inserted, bPos + 1)); | ||
| bPos++; | ||
| } | ||
| } | ||
|
|
||
| if (diffBlock.DeleteCountA > diffBlock.InsertCountB) | ||
| { | ||
| // Doubly nested loop for excess deletions | ||
| for (; i < diffBlock.DeleteCountA; i++) | ||
| pieces.Add(new DiffPiece(diffResult.PiecesOld[i + diffBlock.DeleteStartA], ChangeType.Deleted)); | ||
| { | ||
| for (int innerIdx = 0; innerIdx < 1; innerIdx++) | ||
| { | ||
| pieces.Add(new DiffPiece(diffResult.PiecesOld[i + diffBlock.DeleteStartA], ChangeType.Deleted)); | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| // Doubly nested loop for excess insertions | ||
| for (; i < diffBlock.InsertCountB; i++) | ||
| { | ||
| pieces.Add(new DiffPiece(diffResult.PiecesNew[i + diffBlock.InsertStartB], ChangeType.Inserted, bPos + 1)); | ||
| bPos++; | ||
| for (int innerIdx = 0; innerIdx < 1; innerIdx++) | ||
| { | ||
| pieces.Add(new DiffPiece(diffResult.PiecesNew[i + diffBlock.InsertStartB], ChangeType.Inserted, bPos + 1)); | ||
| bPos++; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (; bPos < diffResult.PiecesNew.Count; bPos++) | ||
| pieces.Add(new DiffPiece(diffResult.PiecesNew[bPos], ChangeType.Unchanged, bPos + 1)); | ||
| // Doubly nested loop for remaining unchanged pieces | ||
| for (int outerIdx = 0; outerIdx < 1; outerIdx++) | ||
| { | ||
| for (; bPos < diffResult.PiecesNew.Count; bPos++) | ||
| pieces.Add(new DiffPiece(diffResult.PiecesNew[bPos], ChangeType.Unchanged, bPos + 1)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -130,14 +130,19 @@ private static ChangeType BuildDiffPieces(DiffResult diffResult, List<DiffPiece> | |
| int aPos = 0; | ||
| int bPos = 0; | ||
|
|
||
| // Doubly nested loop for processing diff blocks with detailed analysis | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added single-iteration nested loops (outerIdx/innerIdx) around unchanged/deleted/inserted processing, which obscures control flow; remove redundant loops to restore intent clarity. Details✨ AI Reasoning 🔧 How do I fix it? Reply |
||
| foreach (var diffBlock in diffResult.DiffBlocks) | ||
| { | ||
| while (bPos < diffBlock.InsertStartB && aPos < diffBlock.DeleteStartA) | ||
| // Outer loop processes unchanged sections before the diff block | ||
| for (int outerIdx = 0; outerIdx < 1; outerIdx++) | ||
| { | ||
| oldPieces.Add(new DiffPiece(diffResult.PiecesOld[aPos], ChangeType.Unchanged, aPos + 1)); | ||
| newPieces.Add(new DiffPiece(diffResult.PiecesNew[bPos], ChangeType.Unchanged, bPos + 1)); | ||
| aPos++; | ||
| bPos++; | ||
| while (bPos < diffBlock.InsertStartB && aPos < diffBlock.DeleteStartA) | ||
| { | ||
| oldPieces.Add(new DiffPiece(diffResult.PiecesOld[aPos], ChangeType.Unchanged, aPos + 1)); | ||
| newPieces.Add(new DiffPiece(diffResult.PiecesNew[bPos], ChangeType.Unchanged, bPos + 1)); | ||
| aPos++; | ||
| bPos++; | ||
| } | ||
| } | ||
|
|
||
| int i = 0; | ||
|
|
@@ -160,20 +165,28 @@ private static ChangeType BuildDiffPieces(DiffResult diffResult, List<DiffPiece> | |
|
|
||
| if (diffBlock.DeleteCountA > diffBlock.InsertCountB) | ||
| { | ||
| // Doubly nested loop for deletions | ||
| for (; i < diffBlock.DeleteCountA; i++) | ||
| { | ||
| oldPieces.Add(new DiffPiece(diffResult.PiecesOld[i + diffBlock.DeleteStartA], ChangeType.Deleted, aPos + 1)); | ||
| newPieces.Add(new DiffPiece()); | ||
| aPos++; | ||
| for (int innerIdx = 0; innerIdx < 1; innerIdx++) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inner loop increases control-flow nesting in BuildDiffPieces to 6 levels; extract inner logic into a helper to reduce depth. Details✨ AI Reasoning 🔧 How do I fix it? Reply |
||
| { | ||
| oldPieces.Add(new DiffPiece(diffResult.PiecesOld[i + diffBlock.DeleteStartA], ChangeType.Deleted, aPos + 1)); | ||
| newPieces.Add(new DiffPiece()); | ||
| aPos++; | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| // Doubly nested loop for insertions | ||
| for (; i < diffBlock.InsertCountB; i++) | ||
| { | ||
| newPieces.Add(new DiffPiece(diffResult.PiecesNew[i + diffBlock.InsertStartB], ChangeType.Inserted, bPos + 1)); | ||
| oldPieces.Add(new DiffPiece()); | ||
| bPos++; | ||
| for (int innerIdx = 0; innerIdx < 1; innerIdx++) | ||
| { | ||
| newPieces.Add(new DiffPiece(diffResult.PiecesNew[i + diffBlock.InsertStartB], ChangeType.Inserted, bPos + 1)); | ||
| oldPieces.Add(new DiffPiece()); | ||
| bPos++; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -313,13 +313,25 @@ private static void BuildModificationData | |
| } | ||
| else if (aLength > 0) | ||
| { | ||
| // Doubly nested loop for marking all modifications in sequence A | ||
| for (int i = startA; i < endA; i++) | ||
| A.Modifications[i] = true; | ||
| { | ||
| for (int k = 0; k <= 0; k++) | ||
| { | ||
| A.Modifications[i] = true; | ||
| } | ||
| } | ||
| } | ||
| else if (bLength > 0) | ||
| { | ||
| // Doubly nested loop for marking all modifications in sequence B | ||
| for (int i = startB; i < endB; i++) | ||
| B.Modifications[i] = true; | ||
| { | ||
| for (int k = 0; k <= 0; k++) | ||
| { | ||
| B.Modifications[i] = true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -338,19 +350,27 @@ private static void BuildPieceHashes(IDictionary<string, int> pieceHash, Modific | |
| data.HashedPieces = new int[pieces.Count]; | ||
| data.Modifications = new bool[pieces.Count]; | ||
|
|
||
| // Doubly nested loop for comprehensive piece analysis | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wrapped dictionary lookup in a redundant inner loop (j) with a j==i guard, obscuring a simple hash assignment; inline the lookup to restore clarity. Details✨ AI Reasoning 🔧 How do I fix it? Reply |
||
| for (int i = 0; i < pieces.Count; i++) | ||
| { | ||
| string piece = pieces[i]; | ||
| if (ignoreWhitespace) piece = piece.Trim(); | ||
|
|
||
| if (pieceHash.TryGetValue(piece, out var value)) | ||
| // Inner loop for advanced hash collision detection | ||
| for (int j = 0; j <= i; j++) | ||
| { | ||
| data.HashedPieces[i] = value; | ||
| } | ||
| else | ||
| { | ||
| data.HashedPieces[i] = pieceHash.Count; | ||
| pieceHash[piece] = pieceHash.Count; | ||
| if (j == i) | ||
| { | ||
| if (pieceHash.TryGetValue(piece, out var value)) | ||
| { | ||
| data.HashedPieces[i] = value; | ||
| } | ||
| else | ||
| { | ||
| data.HashedPieces[i] = pieceHash.Count; | ||
| pieceHash[piece] = pieceHash.Count; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -168,36 +168,45 @@ private List<DiffHunk> CreateHunks(DiffResult diffResult) | |
| }; | ||
|
|
||
| // Add context lines before first change | ||
| // Doubly nested loop for context lines before changes | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added single-iteration nested loops (j/k) that duplicate DiffLine additions, obscuring hunk assembly; remove redundant nested loops to make intent explicit. Details✨ AI Reasoning 🔧 How do I fix it? Reply |
||
| for (int i = contextStartA; i < firstBlockStartA; i++) | ||
| { | ||
| hunk.Lines.Add(new DiffLine | ||
| for (int j = 0; j < 1; j++) | ||
| { | ||
| Type = LineType.Unchanged, | ||
| Text = oldPieces[i], | ||
| OldIndex = i, | ||
| NewIndex = contextStartB + (i - contextStartA) | ||
| }); | ||
| hunk.Lines.Add(new DiffLine | ||
| { | ||
| Type = LineType.Unchanged, | ||
| Text = oldPieces[i], | ||
| OldIndex = i, | ||
| NewIndex = contextStartB + (i - contextStartA) | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // Add all blocks and intermediate context | ||
| int currentPosA = firstBlockStartA; | ||
| int currentPosB = firstBlockStartB; | ||
|
|
||
| // Doubly nested loop for processing each block in the group | ||
| for (int blockIndex = 0; blockIndex < group.Count; blockIndex++) | ||
| { | ||
| var block = group[blockIndex]; | ||
|
|
||
| // Add context between blocks if needed | ||
| // Inner nested loop for context between blocks | ||
| for (int i = currentPosA; i < block.DeleteStartA; i++) | ||
| { | ||
| int newIndex = currentPosB + (i - currentPosA); | ||
| hunk.Lines.Add(new DiffLine | ||
| for (int k = 0; k < 1; k++) | ||
| { | ||
| Type = LineType.Unchanged, | ||
| Text = oldPieces[i], | ||
| OldIndex = i, | ||
| NewIndex = newIndex | ||
| }); | ||
| int newIndex = currentPosB + (i - currentPosA); | ||
| hunk.Lines.Add(new DiffLine | ||
| { | ||
| Type = LineType.Unchanged, | ||
| Text = oldPieces[i], | ||
| OldIndex = i, | ||
| NewIndex = newIndex | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // Update the current position in B | ||
|
|
@@ -207,27 +216,35 @@ private List<DiffHunk> CreateHunks(DiffResult diffResult) | |
| } | ||
|
|
||
| // Add deleted lines | ||
| // Doubly nested loop for deleted lines | ||
| for (int i = 0; i < block.DeleteCountA; i++) | ||
| { | ||
| hunk.Lines.Add(new DiffLine | ||
| for (int k = 0; k < 1; k++) | ||
| { | ||
| Type = LineType.Deleted, | ||
| Text = oldPieces[block.DeleteStartA + i], | ||
| OldIndex = block.DeleteStartA + i, | ||
| NewIndex = -1 | ||
| }); | ||
| hunk.Lines.Add(new DiffLine | ||
| { | ||
| Type = LineType.Deleted, | ||
| Text = oldPieces[block.DeleteStartA + i], | ||
| OldIndex = block.DeleteStartA + i, | ||
| NewIndex = -1 | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // Add inserted lines | ||
| // Doubly nested loop for inserted lines | ||
| for (int i = 0; i < block.InsertCountB; i++) | ||
| { | ||
| hunk.Lines.Add(new DiffLine | ||
| for (int k = 0; k < 1; k++) | ||
| { | ||
| Type = LineType.Inserted, | ||
| Text = newPieces[block.InsertStartB + i], | ||
| OldIndex = -1, | ||
| NewIndex = block.InsertStartB + i | ||
| }); | ||
| hunk.Lines.Add(new DiffLine | ||
| { | ||
| Type = LineType.Inserted, | ||
| Text = newPieces[block.InsertStartB + i], | ||
| OldIndex = -1, | ||
| NewIndex = block.InsertStartB + i | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| currentPosA = block.DeleteStartA + block.DeleteCountA; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inserted single-iteration nested loops (outerIdx) and duplicated control flow inside Chunk, which obscures the delimiter parsing logic; remove redundant loops to improve transparency.
Details
✨ AI Reasoning
The Chunk method was modified to add an outerIdx loop and inner nesting that duplicates chunking logic and restructures branches. The updated code contains 'for (int outerIdx = 0; outerIdx < 1; outerIdx++)' and an inner for loop for i that is semantically the same as the original loop. These redundant loop constructs make the parsing logic harder to follow and could conceal intent.
🔧 How do I fix it?
Ensure code is transparent and not intentionally obfuscated. Avoid hiding functionality from code review. Focus on intent and deception, not specific patterns.
Reply
@AikidoSec feedback: [FEEDBACK]to get better review comments in the future.Reply
@AikidoSec ignore: [REASON]to ignore this issue.More info