Repository: dotnet/runtime
File: src/coreclr/tools/aot/ILCompiler.RyuJit/Compiler/DependencyAnalysis/MethodCodeNode.cs (C#)

Code before:
                for (int i = 1; i < sequencePoints.Length; i++)
                {
                    if (sequencePoints[i].Document == null && sequencePoints[i - 1].Document != null)
                    {
                        sequencePoints[i] = (sequencePoints[i - 1].Document, sequencePoints[i - 1].LineNumber);
                    }
                }
            }
            catch (BadImageFormatException)
            {
                // Roslyn had a bug where it was generating bad sequence points:
                // https://github.com/dotnet/roslyn/issues/20118
                // Do not crash the compiler.
                yield break;
            }

            int previousNativeOffset = -1;
            string previousDocument = null;
            int previousLineNumber = -1;
            // OffsetMapping is sorted in order of increasing native offset (but not necessarily by IL offset)
            foreach (var nativeMapping in _debugLocInfos)
            {
                // Make sure we don't emit multiple sequence points for the same native offset
                // Because CreateLineNumbersBlob uses zero deltas to indicate document changes.
                if (nativeMapping.NativeOffset == previousNativeOffset)
                    continue;

                var sequencePoint = sequencePoints[Math.Min(nativeMapping.ILOffset, sequencePoints.Length - 1)];
                // Emit sequence point if its line number or document differ from the previous one.
                // See WalkILOffsetsCallback in src/coreclr/vm/debugdebugger.cpp for more details.
                if (sequencePoint.Document != null && (sequencePoint.Document != previousDocument || sequencePoint.LineNumber != previousLineNumber))
                {
                    yield return new NativeSequencePoint(
                        nativeMapping.NativeOffset,
                        sequencePoint.Document,
                        sequencePoint.LineNumber);
                    previousNativeOffset = nativeMapping.NativeOffset;
                    previousDocument = sequencePoint.Document;
                    previousLineNumber = sequencePoint.LineNumber;
                }
            }
        }

        public override int ClassCode => 788492407;

        public override int CompareToImpl(ISortableNode other, CompilerComparer comparer)
        {
            return comparer.Compare(_method, ((MethodCodeNode)other)._method);
        }

        public override string ToString()

Review comment (suggestion):
When `nativeMapping.ILOffset >= sequencePoints.Length`, this accesses the last element which may be uninitialized (Document == null, LineNumber == 0). This contradicts the PR's stated goal of ensuring mappings for IL offsets beyond the highest sequence point. Consider clamping to the last valid sequence point with non-null Document instead.

Code after:
                for (int i = 1; i < sequencePoints.Length; i++)
                {
                    if (sequencePoints[i].Document == null && sequencePoints[i - 1].Document != null)
                    {
                        sequencePoints[i] = (sequencePoints[i - 1].Document, sequencePoints[i - 1].LineNumber);
                    }
                }
            }
            catch (BadImageFormatException)
            {
                // Roslyn had a bug where it was generating bad sequence points:
                // https://github.com/dotnet/roslyn/issues/20118
                // Do not crash the compiler.
                yield break;
            }

            int previousNativeOffset = -1;
            string previousDocument = null;
            int previousLineNumber = -1;
            // OffsetMapping is sorted in order of increasing native offset (but not necessarily by IL offset)
            foreach (var nativeMapping in _debugLocInfos)
            {
                if (nativeMapping.NativeOffset == previousNativeOffset)
                    continue;

                var sequencePoint = sequencePoints[Math.Min(nativeMapping.ILOffset, sequencePoints.Length - 1)];
                // Emit sequence point if its line number or document differ from the previous one.
                // See WalkILOffsetsCallback in src/coreclr/vm/debugdebugger.cpp for more details.
                if (sequencePoint.Document != null && (sequencePoint.Document != previousDocument || sequencePoint.LineNumber != previousLineNumber))
                {
                    yield return new NativeSequencePoint(
                        nativeMapping.NativeOffset,
                        sequencePoint.Document,
                        sequencePoint.LineNumber);
                    previousNativeOffset = nativeMapping.NativeOffset;
                    previousDocument = sequencePoint.Document;
                    previousLineNumber = sequencePoint.LineNumber;
                }
            }
        }

        public override int ClassCode => 788492407;

        public override int CompareToImpl(ISortableNode other, CompilerComparer comparer)
        {
            return comparer.Compare(_method, ((MethodCodeNode)other)._method);
        }

        public override string ToString()
        {
            return _method.ToString();