Skip to content

Commit

Permalink
Transform MAX_OBJECT_SIZE to runtime environment option - OGR_GEOJSON…
Browse files Browse the repository at this point in the history
…_MAX_OBJ_SIZE
  • Loading branch information
mmomtchev committed Oct 10, 2019
1 parent f4409f9 commit bfe708b
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions gdal/ogr/ogrsf_frmts/geojson/ogrgeojsonreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ static
OGRGeometry* OGRGeoJSONReadGeometry( json_object* poObj,
OGRSpatialReference* poParentSRS );

const size_t MAX_OBJECT_SIZE = 200 * 1024 * 1024;

#if (!defined(JSON_C_VERSION_NUM)) || (JSON_C_VERSION_NUM < JSON_C_VER_013)
const size_t ESTIMATE_BASE_OBJECT_SIZE = sizeof(struct json_object);
#elif JSON_C_VERSION_NUM == JSON_C_VER_013 // no way to get the size
Expand Down Expand Up @@ -98,6 +96,7 @@ class OGRGeoJSONReaderStreamingParser: public CPLJSonStreamingParser
std::vector<bool> m_abFirstMember;
bool m_bStoreNativeData;
CPLString m_osJson;
size_t m_nMaxObjectSize;

std::vector<OGRFeature*> m_apoFeatures;
size_t m_nCurFeatureIdx;
Expand Down Expand Up @@ -314,6 +313,8 @@ OGRGeoJSONReaderStreamingParser::OGRGeoJSONReaderStreamingParser(
m_bStoreNativeData(bStoreNativeData),
m_nCurFeatureIdx(0)
{
m_nMaxObjectSize = atoi(CPLGetConfigOption("OGR_GEOJSON_MAX_OBJ_SIZE", "200"))
* 1024 * 1024;
}

/************************************************************************/
Expand Down Expand Up @@ -401,7 +402,7 @@ void OGRGeoJSONReaderStreamingParser::AnalyzeFeature()

void OGRGeoJSONReaderStreamingParser::StartObject()
{
if( m_nCurObjMemEstimate > MAX_OBJECT_SIZE )
if( m_nCurObjMemEstimate > m_nMaxObjectSize )
{
TooComplex();
return;
Expand Down Expand Up @@ -448,7 +449,7 @@ void OGRGeoJSONReaderStreamingParser::StartObject()

void OGRGeoJSONReaderStreamingParser::EndObject()
{
if( m_nCurObjMemEstimate > MAX_OBJECT_SIZE )
if( m_nCurObjMemEstimate > m_nMaxObjectSize )
{
TooComplex();
return;
Expand Down Expand Up @@ -523,7 +524,7 @@ void OGRGeoJSONReaderStreamingParser::EndObject()
void OGRGeoJSONReaderStreamingParser::StartObjectMember(const char* pszKey,
size_t nKeyLen)
{
if( m_nCurObjMemEstimate > MAX_OBJECT_SIZE )
if( m_nCurObjMemEstimate > m_nMaxObjectSize )
{
TooComplex();
return;
Expand Down Expand Up @@ -576,7 +577,7 @@ void OGRGeoJSONReaderStreamingParser::StartObjectMember(const char* pszKey,

void OGRGeoJSONReaderStreamingParser::StartArray()
{
if( m_nCurObjMemEstimate > MAX_OBJECT_SIZE )
if( m_nCurObjMemEstimate > m_nMaxObjectSize )
{
TooComplex();
return;
Expand Down Expand Up @@ -628,7 +629,7 @@ void OGRGeoJSONReaderStreamingParser::StartArrayMember()

void OGRGeoJSONReaderStreamingParser::EndArray()
{
if( m_nCurObjMemEstimate > MAX_OBJECT_SIZE )
if( m_nCurObjMemEstimate > m_nMaxObjectSize )
{
TooComplex();
return;
Expand Down Expand Up @@ -657,7 +658,7 @@ void OGRGeoJSONReaderStreamingParser::EndArray()

void OGRGeoJSONReaderStreamingParser::String(const char* pszValue, size_t nLen)
{
if( m_nCurObjMemEstimate > MAX_OBJECT_SIZE )
if( m_nCurObjMemEstimate > m_nMaxObjectSize )
{
TooComplex();
return;
Expand Down Expand Up @@ -692,7 +693,7 @@ void OGRGeoJSONReaderStreamingParser::String(const char* pszValue, size_t nLen)

void OGRGeoJSONReaderStreamingParser::Number(const char* pszValue, size_t nLen)
{
if( m_nCurObjMemEstimate > MAX_OBJECT_SIZE )
if( m_nCurObjMemEstimate > m_nMaxObjectSize )
{
TooComplex();
return;
Expand Down Expand Up @@ -749,7 +750,7 @@ void OGRGeoJSONReaderStreamingParser::Number(const char* pszValue, size_t nLen)

void OGRGeoJSONReaderStreamingParser::Boolean(bool bVal)
{
if( m_nCurObjMemEstimate > MAX_OBJECT_SIZE )
if( m_nCurObjMemEstimate > m_nMaxObjectSize )
{
TooComplex();
return;
Expand Down Expand Up @@ -779,7 +780,7 @@ void OGRGeoJSONReaderStreamingParser::Boolean(bool bVal)

void OGRGeoJSONReaderStreamingParser::Null()
{
if( m_nCurObjMemEstimate > MAX_OBJECT_SIZE )
if( m_nCurObjMemEstimate > m_nMaxObjectSize )
{
TooComplex();
return;
Expand All @@ -804,7 +805,7 @@ void OGRGeoJSONReaderStreamingParser::Null()
void OGRGeoJSONReaderStreamingParser::TooComplex()
{
if( !ExceptionOccurred() )
Exception("GeoJSON object too complex");
Exception("GeoJSON object too complex, please see the OGR_GEOJSON_MAX_OBJ_SIZE environment option");
}

/************************************************************************/
Expand Down

0 comments on commit bfe708b

Please sign in to comment.