Merge pull request #630 from meld-cp/master

Fix potential endless loop
This commit is contained in:
meld-cp 2024-07-13 12:07:35 +12:00 committed by GitHub
commit 4fd14f2174
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 4 deletions

View File

@ -39,7 +39,12 @@ namespace MiniExcelLibs.Csv
{ {
while (finalRow.Count(c => c == '"') % 2 != 0) 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); 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 }, 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, //AlwaysQuote = true,
ReadLineBreaksWithinQuotes = true, ReadLineBreaksWithinQuotes = true,
}; };
@ -3599,13 +3600,15 @@ MyProperty4,MyProperty1,MyProperty5,MyProperty2,MyProperty6,,MyProperty3
new() { B = DateTime.Parse("2021-02-01"), D = 123 }, new() { B = DateTime.Parse("2021-02-01"), D = 123 },
}; };
var config = new CsvConfiguration() { var config = new CsvConfiguration()
{
//AlwaysQuote = true, //AlwaysQuote = true,
ReadLineBreaksWithinQuotes = true, ReadLineBreaksWithinQuotes = true,
}; };
// create // create
using (var stream = File.Create(path)) { using (var stream = File.Create(path))
{
stream.SaveAs(values, excelType: ExcelType.CSV, configuration: config); stream.SaveAs(values, excelType: ExcelType.CSV, configuration: config);
} }
@ -3615,5 +3618,28 @@ MyProperty4,MyProperty1,MyProperty5,MyProperty2,MyProperty6,,MyProperty3
Assert.Equal(values.Length, getRowsInfo.Count()); 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 );
}
} }
} }