Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions autotest/ogr/ogr_pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -6429,3 +6429,24 @@ def test_ogr_pg_geometry_intersection_spatial_filter(
assert f.GetGeometryRef().ExportToWkt() == "POINT (0.5 0.5)"
f = sql_lyr.GetNextFeature()
assert f is None


###############################################################################
# Test backslash characters are properly read when reading string lists


@only_without_postgis
def test_ogr_pg_string_list_escaping(pg_ds):

paths = [r"c:\data", r'c:\"Program Files"']

lyr = pg_ds.CreateLayer("paths")
lyr.CreateField(ogr.FieldDefn("txt", ogr.OFTStringList))
f = ogr.Feature(lyr.GetLayerDefn())
f["txt"] = paths
lyr.CreateFeature(f)

lyr.ResetReading()

f_out = lyr.GetNextFeature()
assert f_out["txt"] == paths
29 changes: 29 additions & 0 deletions autotest/ogr/ogr_pgdump.py
Original file line number Diff line number Diff line change
Expand Up @@ -1673,3 +1673,32 @@ def check_and_remove(needle):
sql = sql[sql.find(needle) + len(needle) :]

check_and_remove(") ON CONFLICT DO NOTHING;")


###############################################################################
# Test that " and \ in string list items are escaped in both COPY and INSERT mode


@pytest.mark.parametrize("pg_use_copy", ["YES", "NO"])
def test_ogr_pgdump_stringlist_escaping(tmp_vsimem, pg_use_copy):

with gdaltest.config_option("PG_USE_COPY", pg_use_copy):
ds = ogr.GetDriverByName("PGDump").CreateDataSource(
tmp_vsimem / "ogr_pgdump_stringlist_escape.sql"
)
lyr = ds.CreateLayer("testlayer", geom_type=ogr.wkbNone)
lyr.CreateField(ogr.FieldDefn("stringlist", ogr.OFTStringList))

feat = ogr.Feature(lyr.GetLayerDefn())
feat.SetFieldStringList(0, ["a\\b", 'c"d'])
lyr.CreateFeature(feat)
ds = None

f = gdal.VSIFOpenL(tmp_vsimem / "ogr_pgdump_stringlist_escape.sql", "rb")
sql = gdal.VSIFReadL(1, 10000, f).decode("utf8")
gdal.VSIFCloseL(f)

if pg_use_copy == "YES":
assert r'{"a\\\\b","c\\"d"}' in sql, sql
else:
assert "ARRAY['a\\b','c\"d']" in sql, sql
9 changes: 6 additions & 3 deletions ogr/ogrsf_frmts/pg/ogrpglayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,11 +404,14 @@ static void OGRPGTokenizeStringListUnescapeToken(char *pszToken)
}

int iSrc = 0, iDst = 0;
for (iSrc = 0; pszToken[iSrc] != '\0'; iSrc++)
while (pszToken[iSrc] != '\0')
{
pszToken[iDst] = pszToken[iSrc];
if (pszToken[iSrc] != '\\')
iDst++;
if (pszToken[iSrc] == '\\' && pszToken[iSrc + 1] != '\0')
{
iSrc++;
}
pszToken[iDst++] = pszToken[iSrc++];
}
pszToken[iDst] = '\0';
}
Expand Down
2 changes: 1 addition & 1 deletion ogr/ogrsf_frmts/pgdump/ogrpgdumplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ OGRPGDumpEscapeStringList(char **papszItems, bool bForInsertOrUpdate,

while (*pszStr)
{
if (*pszStr == '"')
if (*pszStr == '"' || *pszStr == '\\')
osStr += "\\";
osStr += *pszStr;
pszStr++;
Expand Down
Loading