ContainerBlock CopyTo IndexOutOfRangeException #529

Closed
opened 2026-01-29 14:38:50 +00:00 by claunia · 2 comments
Owner

Originally created by @ladeak on GitHub (Apr 21, 2022).

When I call ToList() on a ContainerBlock (ListItemBlock) with a single item in it, I get a IndexOutOfRangeException.

Stacktrace:

   at System.Runtime.CompilerServices.CastHelpers.StelemRef(Array array, Int32 index, Object obj)
   at Markdig.Syntax.ContainerBlock.CopyTo(Block[] array, Int32 arrayIndex)
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

This seems to be a regression since v28.1. I am still further looking into the detailts.

(.NET6, Blazor app)

Originally created by @ladeak on GitHub (Apr 21, 2022). When I call ToList() on a ContainerBlock (ListItemBlock) with a single item in it, I get a IndexOutOfRangeException. Stacktrace: ``` at System.Runtime.CompilerServices.CastHelpers.StelemRef(Array array, Int32 index, Object obj) at Markdig.Syntax.ContainerBlock.CopyTo(Block[] array, Int32 arrayIndex) at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) ``` This seems to be a regression since v28.1. I am still further looking into the detailts. (.NET6, Blazor app)
claunia added the bug label 2026-01-29 14:38:50 +00:00
Author
Owner

@MihaZupan commented on GitHub (Apr 21, 2022):

Caused by a typo here:
a4a1a177bc/src/Markdig/Syntax/ContainerBlock.cs (L133)

Should be arrayIndex + i instead of arrayIndex + 1.

As a temporary workaround, you can avoid the CopyTo.

var list = new List<Block>(container.Count);
for (int i = 0; i < list.Count; i++)
    list[i] = container[i];
@MihaZupan commented on GitHub (Apr 21, 2022): Caused by a typo here: https://github.com/xoofx/markdig/blob/a4a1a177bc06163452842d016e6c965b9f632b8a/src/Markdig/Syntax/ContainerBlock.cs#L133 Should be `arrayIndex + i` instead of `arrayIndex + 1`. As a temporary workaround, you can avoid the `CopyTo`. ```c# var list = new List<Block>(container.Count); for (int i = 0; i < list.Count; i++) list[i] = container[i]; ```
Author
Owner

@ladeak commented on GitHub (Apr 21, 2022):

I was wondering why I did not find this piece of code... GitHub search has taken me to a different version of the file. 😡

It seems calling ToList() on MarkdownDocument results a weird list of, probably also caused by the same typo.

@ladeak commented on GitHub (Apr 21, 2022): I was wondering why I did not find this piece of code... GitHub search has taken me to a different version of the file. 😡 It seems calling ToList() on MarkdownDocument results a weird list of, probably also caused by the same typo.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#529