Repository: dotnet/runtime
File: src/coreclr/vm/eepolicy.cpp (C++)

Code before:
                {
                    if ((i != largestCommonStartOffset) && (pMD == m_frames[largestCommonStartOffset]))
                    {
                        // We have found a frame with the same MethodDesc as the frame at the top of the stack,
                        // possibly a new repeated sequence is starting.
                        commonStartIndex = i;
                    }
                }
            }

            if (largestCommonRepeat != 0)
            {
                // A repeated sequence of frames was identified
                break;
            }
        }

        for (int i = 0; i < largestCommonStartOffset; i++)
        {
            PrintFrame(i, pWordAt);
        }

        if (largestCommonLength != 0)
        {
            SmallStackSString repeatStr;
            repeatStr.AppendPrintf("Repeated %d times:\n", largestCommonRepeat);

            PrintToStdErrW(repeatStr.GetUnicode());

            PrintToStdErrA("--------------------------------\n");
            for (int i = largestCommonStartOffset; i < largestCommonStartOffset + largestCommonLength; i++)
            {
                PrintFrame(i, pWordAt);
            }
            PrintToStdErrA("--------------------------------\n");
        }

        for (int i = largestCommonLength * largestCommonRepeat + largestCommonStartOffset; i < m_frames.Count(); i++)
        {
            PrintFrame(i, pWordAt);
        }
    }
};

#ifdef _DEBUG
// Temporarilly added flag to enable extra verbose printing of progress of stack overflow handling
// to catch a bug that is not reproducible locally.
static bool g_LogStackOverflowExit = false;
#endif // _DEBUG

//---------------------------------------------------------------------------------------

Review comment (suggestion):
When no repetition is found in any iteration of the outer loop, `largestCommonStartOffset` will equal `m_frames.Count()`, causing none of the frames to be printed. The code should handle the case where no repetition is found by printing all frames normally. Consider initializing `largestCommonStartOffset` to 0 and only updating it when a valid repetition is found, or adding logic after the loop to handle the no-repetition case.
```suggestion

        // If no repetition was found, print all frames and return
        if (largestCommonRepeat == 0 || largestCommonLength == 0)
        {
            for (int i = 0; i < m_frames.Count(); i++)
            {
                PrintFrame(i, pWordAt);
            }
            return;
        }
```

Code after:
                            largestCommonLength = commonLength;
                            largestCommonRepeat = commonRepeat;
                        }

                        commonStartIndex = -1;
                    }
                }

                if (commonStartIndex == -1)
                {
                    if ((i != largestCommonStartOffset) && (pMD == m_frames[largestCommonStartOffset]))
                    {
                        // We have found a frame with the same MethodDesc as the frame at the start of the repetition search (index largestCommonStartOffset),
                        // possibly a new repeated sequence is starting.
                        commonStartIndex = i;
                    }
                }
            }

            if (largestCommonRepeat != 0)
            {
                // A repeated sequence of frames was identified
                break;
            }
        }

        for (int i = 0; i < largestCommonStartOffset; i++)
        {
            PrintFrame(i, pWordAt);
        }

        if (largestCommonLength != 0)
        {
            SmallStackSString repeatStr;
            repeatStr.AppendPrintf("Repeated %d times:\n", largestCommonRepeat);

            PrintToStdErrW(repeatStr.GetUnicode());

            PrintToStdErrA("--------------------------------\n");
            for (int i = largestCommonStartOffset; i < largestCommonStartOffset + largestCommonLength; i++)
            {
                PrintFrame(i, pWordAt);
            }
            PrintToStdErrA("--------------------------------\n");
        }

        for (int i = largestCommonLength * largestCommonRepeat + largestCommonStartOffset; i < m_frames.Count(); i++)
        {
            PrintFrame(i, pWordAt);
        }
    }