2

I'm getting a System.IndexOutOfRangeException while trying to serialize a dataset of dynamic objects. It's not any single row since I've tested it with each individual row (there are only four in my example). It's happening in the CsvWriter.HasAnyEscapeChars function. The exact stack trace is:

   at ServiceStack.Text.CsvWriter.HasAnyEscapeChars(String value)
   at ServiceStack.TextExtensions.ToCsvField(Object text)
   at ServiceStack.Text.CsvDictionaryWriter.WriteObjectRow(TextWriter writer, IEnumerable`1 row)
   at ServiceStack.Text.CsvDictionaryWriter.Write(TextWriter writer, IEnumerable`1 records)
   at ServiceStack.Text.CsvWriter`1.Write(TextWriter writer, IEnumerable`1 records)
   at ServiceStack.Text.CsvSerializer`1.WriteObject(TextWriter writer, Object value)
   at ServiceStack.Text.CsvSerializer.SerializeToStream[T](T value, Stream stream)
   at <my code>

It looks (from the source code) that HasAnyEscapeChars needs to add a guard against an empty string since the reference to value[0] will fail there, though I'm not seeing how that would actually happen in actual usage.

public static bool HasAnyEscapeChars(string value)
{
  return CsvConfig.EscapeStrings.Any(value.Contains)
     || value[0] == JsWriter.ListStartChar
     || value[0] == JsWriter.MapStartChar;
}

Is there something I need to do to sanitize my data or is it a bug?

Arian Kulp
  • 831
  • 8
  • 31
  • Well, do you think it is meaningful that CsvWriter throws an IndexOutOfRangeException when checking for escape characters? If you think it is meaningful -> no bug. If you think it is not meaningful -> quite likely a bug. –  Jan 23 '19 at 22:50

1 Answers1

4

The IndexOutOfRangeException should now be resolved from this commit.

This change is available from v5.4.1 that's now available on MyGet.

mythz
  • 141,670
  • 29
  • 246
  • 390