fix potential endless loop

This commit is contained in:
meld-cp 2024-07-13 11:58:13 +12:00
parent bb86a9e8ab
commit 04866cf46c
2 changed files with 34 additions and 4 deletions

View File

@ -39,7 +39,11 @@ namespace MiniExcelLibs.Csv
{
while (finalRow.Count(c => c == '"') % 2 != 0)
{
finalRow += string.Concat( _config.NewLine, reader.ReadLine());
var nextPart = reader.ReadLine();
if (nextPart == null ) {
break;
}
finalRow = string.Concat( finalRow, _config.NewLine, nextPart );
}
}
read = Split(finalRow);

View File

@ -3556,7 +3556,8 @@ MyProperty4,MyProperty1,MyProperty5,MyProperty2,MyProperty6,,MyProperty3
new() { A = "Microsoft\"\" \r\nTest\n3", B = DateTime.Parse("2021-02-01"), C = "a\"\"\nb\n\nc", D = 123 },
};
var config = new CsvConfiguration() {
var config = new CsvConfiguration()
{
//AlwaysQuote = true,
ReadLineBreaksWithinQuotes = true,
};
@ -3599,13 +3600,15 @@ MyProperty4,MyProperty1,MyProperty5,MyProperty2,MyProperty6,,MyProperty3
new() { B = DateTime.Parse("2021-02-01"), D = 123 },
};
var config = new CsvConfiguration() {
var config = new CsvConfiguration()
{
//AlwaysQuote = true,
ReadLineBreaksWithinQuotes = true,
};
// create
using (var stream = File.Create(path)) {
using (var stream = File.Create(path))
{
stream.SaveAs(values, excelType: ExcelType.CSV, configuration: config);
}
@ -3615,5 +3618,28 @@ MyProperty4,MyProperty1,MyProperty5,MyProperty2,MyProperty6,,MyProperty3
Assert.Equal(values.Length, getRowsInfo.Count());
}
[Fact]
public void Issue507_3_MismatchedQuoteCsv() {
//Problem with multi-line when using Query func
//https://github.com/mini-software/MiniExcel/issues/507
var config = new CsvConfiguration()
{
//AlwaysQuote = true,
ReadLineBreaksWithinQuotes = true,
};
var badCsv = "A,B,C\n\"r1a: no end quote,r1b,r1c";
// create
using var stream = new MemoryStream( Encoding.UTF8.GetBytes( badCsv ) );
// read
var getRowsInfo = MiniExcel.Query( stream, excelType: ExcelType.CSV, configuration: config ).ToArray();
Assert.Equal(2, getRowsInfo.Length );
}
}
}