Skip to content

Commit

Permalink
CSV: emit error when unbalanced double quotes are detected
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Feb 18, 2025
1 parent 19690a5 commit 7083ac5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions autotest/ogr/data/csv/unbalanced_double_quotes.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,txt
1,"foo""
2,bar
16 changes: 16 additions & 0 deletions autotest/ogr/ogr_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -3482,6 +3482,22 @@ def test_ogr_schema_override_wkt(tmp_vsimem):
assert f.GetGeometryRef().ExportToWkt() == "POINT (1 2)"


###############################################################################
# Test reading CSV with unbalanced double-quotes


@gdaltest.enable_exceptions()
def test_ogr_csv_unbalanced_double_quotes():

with ogr.Open("data/csv/unbalanced_double_quotes.csv") as ds:
lyr = ds.GetLayer(0)
with pytest.raises(
Exception,
match="CSV file has unbalanced number of double-quotes. Corrupted data will likely be returned",
):
lyr.GetNextFeature()


###############################################################################


Expand Down
8 changes: 8 additions & 0 deletions port/cpl_csv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,14 @@ CSVReadParseLineGeneric(void *fp, const char *(*pfnReadLine)(void *, size_t),
{
CPLError(CE_Failure, CPLE_OutOfMemory, "%s", e.what());
}

if (bInString)
{
CPLError(CE_Failure, CPLE_AppDefined,
"CSV file has unbalanced number of double-quotes. Corrupted "
"data will likely be returned");
}

return nullptr;
}

Expand Down

0 comments on commit 7083ac5

Please sign in to comment.