Skip to content

Commit 5ba19c1

Browse files
tecosaurKristofferC
authored andcommitted
Fix potential underrun with annotation merging (#54917)
Fixes #54860, see the commit message for more details. The added test serves as a MWE of the original bug report. (cherry picked from commit e621c74)
1 parent 93e086f commit 5ba19c1

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

base/strings/annotated.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ function _insert_annotations!(io::AnnotatedIOBuffer, annotations::Vector{Tuple{U
593593
for i in reverse(axes(annotations, 1))
594594
annot = annotations[i]
595595
first(first(annot)) == 1 || continue
596+
i <= length(io.annotations) || continue
596597
if last(annot) == last(last(io.annotations))
597598
valid_run = true
598599
for runlen in 1:i

test/strings/annotated.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,12 @@ end
215215
@test write(aio2, Base.AnnotatedChar('c', [:b => 2, :c => 3, :d => 4])) == 1
216216
@test Base.annotations(aio2) == [(1:2, :a => 1), (1:3, :b => 2), (3:3, :c => 3), (3:3, :d => 4)]
217217
end
218+
let aio2 = Base.AnnotatedIOBuffer()
219+
@test write(aio2, Base.AnnotatedChar('a', [:b => 1])) == 1
220+
@test write(aio2, Base.AnnotatedChar('b', [:a => 1, :b => 1])) == 1
221+
@test read(seekstart(aio2), Base.AnnotatedString) ==
222+
Base.AnnotatedString("ab", [(1:1, :b => 1), (2:2, :a => 1), (2:2, :b => 1)])
223+
end
218224
# Working through an IOContext
219225
aio = Base.AnnotatedIOBuffer()
220226
wrapio = IOContext(aio)

0 commit comments

Comments
 (0)