Ayende @ Rahien

Refunds available at head office

Challenge: Why isn’t select broken?

Here is a crash report that I got.

System.ArgumentException: Destination array was not long enough. Check destIndex and length, and the array's lower bounds.
  at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
  at System.Collections.Generic.List`1.CopyTo(T[] array, Int32 arrayIndex)
  at System.Collections.ObjectModel.Collection`1.CopyTo(T[] array, Int32 index)
  at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
  at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)

Now, it is tempting to blame Microsoft for this, but it is actually my fault.

Care to guess why?

Comments

Jan Willem B
09/01/2009 02:13 PM by
Jan Willem B

The source is changing during evaluation.

Rob
09/01/2009 02:13 PM by
Rob

Threading issue as the IEnumerable was having elements added to it whilst the ToArray was called?

James Curran
09/01/2009 02:32 PM by
James Curran

It's taking the code path where the IEnumerable is really a ICollection, and using it's Count property to size the buffer. This property was possibly lying.

Kelly Stuard
09/01/2009 02:39 PM by
Kelly Stuard

+1 on Rob Jan's reason

Paul Batum
09/01/2009 02:41 PM by
Paul Batum

Indeed, as the others above have suggested, I would guess its a threading issue. This one was pretty easy to reproduce:

var ints = new List <int();

var thread = new Thread(delegate() { while (true) ints.Add(0); });

thread.Start();

while(true) Console.WriteLine(ints.ToArray().Length);

Paul Batum
09/01/2009 02:44 PM by
Paul Batum

Argh, I always forget to html encode my generics when posting blog comments.... :(

Um
09/01/2009 04:02 PM by
Um

@Paul,

I'm quite surprised is not being HTML encoded by the blog engine. What about tags such as and ?

dave-ilsw
09/01/2009 04:42 PM by
dave-ilsw

@Um,

I'm not at all surprised that the blog engine takes the approach of stripping everything that is enclosed by < and > rather than trying to figure out what is safe and encoding the rest. In the long run, stripping is safer.

Daniel
09/01/2009 05:03 PM by
Daniel

Threading issue came to mind first, and looks like the case here. I could also see this happening if you had some custom Collection <t implementation that didn't return the correct Count. Looking at S.L.Buffer, it looks like it reuses ICollection.Count if you're passing a collection, otherwise, it loops over the IEnumerable and counts manually.

Bertrand Le Roy
09/01/2009 08:04 PM by
Bertrand Le Roy

@dave-ilsw: how is stripping safer than Html-encoding the whole comment?? It certainly is a lot more inconvenient.

Ayende Rahien
09/02/2009 10:24 AM by
Ayende Rahien

Um, Rob, test

Removed your comments

I am aware of the issue, and it will be fixed shortly.

There is no data disclosure possible here, so I don't rate it critical

Um
09/02/2009 10:31 AM by
Um

But a simple XSS attack is possible, which puts your visitors at risk. I can't imagine that the default configuration of Subtext doesn't encode comments?!

Ayende Rahien
09/02/2009 10:35 AM by
Ayende Rahien

Um,

As I said, I contacted the SubText team and they are working on that.

What information do you think XSS can steal from visitors to this blog?

Um
09/02/2009 10:55 AM by
Um

Ayende,

I don't know, probably nothing. However, there are other risks with XSS. The worst involve exploiting browser vulnerabilities to install trojans or hijackers ( http://www.owasp.org/index.php/XSS).

Comments have been closed on this topic.