Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In case a csv string starts with BOM I get exception #2311

Open
Vldmrkh opened this issue Jan 6, 2025 · 0 comments
Open

In case a csv string starts with BOM I get exception #2311

Vldmrkh opened this issue Jan 6, 2025 · 0 comments
Labels

Comments

@Vldmrkh
Copy link

Vldmrkh commented Jan 6, 2025

Describe the bug
In case a csv string starts with BOM I get exception

CsvHelper.BadDataException: You can ignore bad data by setting BadDataFound to null.

CsvHelper.BadDataException
You can ignore bad data by setting BadDataFound to null.
IReader state:
   ColumnCount: 2
   CurrentIndex: -1
   HeaderRecord:

IParser state:
   ByteCount: 0
   CharCount: 19
   Row: 1
   RawRow: 1
   Count: 2
   RawRecord:
"username";"email"

To Reproduce

using System.Globalization;
using System.Text;
using CsvHelper;
using Xunit;

namespace CsvHelperBugTests;

public class ErrorThrow
{
    [Fact]
    public void Bug()
    {
        string stringContent = """
                               "username";"email"
                               """;
        
        var preamble = Encoding.UTF8.GetPreamble();
        var bom = Encoding.UTF8.GetString(preamble);

        var stringContentWithBOM = bom + stringContent;
        
        using (var reader = new StringReader(stringContentWithBOM))
        {
            using (var csv = new CsvReader(reader,
                       new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture)
                       {
                           Delimiter = ";",
                       }))
            {
                var records = csv.GetRecords<dynamic>();

                var a = records.ToList(); // exception
            }
        }
    }
}

Expected behavior
just ignore first symbol of BOM

Additional context
to fix a problem I have to do:

        var preamble = Encoding.UTF8.GetPreamble();
        var bom = Encoding.UTF8.GetString(preamble);
        string stringContent2;
        if (stringContent[0] == bom[0])
        {
            stringContent2 = stringContent.Remove(0, 1);
        }
@Vldmrkh Vldmrkh added the bug label Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant