Skip to content

Commit

Permalink
GPKG: CreateField(): check we are not reaching the max number of fields
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Oct 20, 2024
1 parent 3d06180 commit 35d3cb9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
18 changes: 18 additions & 0 deletions autotest/ogr/ogr_gpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -10692,3 +10692,21 @@ def test_gpkg_create_duplicate_field_names(tmp_vsimem):
):
lyr.CreateField(ogr.FieldDefn("geom"))
assert lyr.GetLayerDefn().GetFieldCount() == 1


###############################################################################
# Test creating more than 2000 fields


@gdaltest.enable_exceptions()
def test_gpkg_create_more_than_2000_fields(tmp_vsimem):

filename = str(tmp_vsimem / "test_gpkg_create_more_than_2000_fields.gpkg")
with ogr.GetDriverByName("GPKG").CreateDataSource(filename) as ds:
lyr = ds.CreateLayer("test")

for i in range(2000 - 2):
lyr.CreateField(ogr.FieldDefn(f"foo{i}"))
with pytest.raises(Exception, match="Limit of 2000 columns reached"):
lyr.CreateField(ogr.FieldDefn("foo"))
assert lyr.GetLayerDefn().GetFieldCount() == 2000 - 2
13 changes: 13 additions & 0 deletions ogr/ogrsf_frmts/gpkg/ogrgeopackagetablelayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1761,6 +1761,19 @@ OGRErr OGRGeoPackageTableLayer::CreateField(const OGRFieldDefn *poField,
return OGRERR_FAILURE;
}

const int nMaxColumns =
sqlite3_limit(m_poDS->GetDB(), SQLITE_LIMIT_COLUMN, -1);
// + 1 for the FID column
if (m_poFeatureDefn->GetFieldCount() +
m_poFeatureDefn->GetGeomFieldCount() + 1 >=
nMaxColumns)
{
CPLError(CE_Failure, CPLE_AppDefined,
"Cannot add field %s. Limit of %d columns reached",
oFieldDefn.GetNameRef(), nMaxColumns);
return OGRERR_FAILURE;
}

if (!m_bDeferredCreation)
{
CPLString osCommand;
Expand Down

0 comments on commit 35d3cb9

Please sign in to comment.