Skip to content

Commit 8923638

Browse files
authored
be more resilient against line numbers out of bounds of actual (#126)
sourcecode
1 parent e2650e8 commit 8923638

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/LineNumbers.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ end
1111
function SourceFile(data::AbstractString)
1212
offsets = UInt64[0]
1313
buf = IOBuffer(data)
14-
local line
14+
line = ""
1515
while !eof(buf)
1616
line = readuntil(buf,'\n')
1717
!eof(buf) && push!(offsets, position(buf))

src/printing.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ function print_status(io::IO, frame::Frame; force_lowered=false)
113113
read(loc.filepath, String)
114114
end
115115
breakpoint_lines = breakpoint_linenumbers(frame)
116-
print_sourcecode(outbuf, data, loc.line, loc.defline, loc.endline, breakpoint_lines)
116+
ok = print_sourcecode(outbuf, data, loc.line, loc.defline, loc.endline, breakpoint_lines)
117+
if !ok
118+
printstyled(io, "failed to lookup source code in $(repr(loc.filepath)), showing lowered code:\n"; color=Base.warn_color())
119+
print_codeinfo(outbuf, frame)
120+
end
117121
else
118122
print_codeinfo(outbuf, frame)
119123
end
@@ -214,6 +218,9 @@ function print_sourcecode(io::IO, code::String, line::Integer, defline::Integer,
214218
code = highlight_code(code; context=io)
215219
file = SourceFile(code)
216220
stopline = min(endline, line + NUM_SOURCE_LINES_UP_DOWN[])
221+
if !checkbounds(Bool, file.offsets, line)
222+
return false
223+
end
217224
startoffset, stopoffset = compute_source_offsets(code, file.offsets[line], defline, stopline; file=file)
218225

219226
if startoffset == -1
@@ -226,6 +233,7 @@ function print_sourcecode(io::IO, code::String, line::Integer, defline::Integer,
226233

227234
code = split(code[(startoffset+1):(stopoffset+1)],'\n')
228235
print_lines(io, code, line, breakpoint_lines, startline)
236+
return true
229237
end
230238

231239
function print_lines(io, code, current_line, breakpoint_lines, startline)

0 commit comments

Comments
 (0)