@@ -1358,30 +1358,42 @@ int msPostGISParseData(layerObj *layer)
13581358 };
13591359
13601360 /*
1361- ** What clause appear after 2nd 'using'?
1361+ ** What clause appear after 2nd 'using', if set ?
13621362 */
1363- for ( tmp = pos_use_2nd + 5 ; * tmp == ' ' ; tmp ++ );
1364- if ( strncmp ( tmp , "unique " , 7 ) == 0 )
1365- {
1366- for ( pos_uid = tmp + 7 ; * pos_uid == ' ' ; pos_uid ++ );
1367- }
1368- else
1363+ if ( pos_use_2nd )
13691364 {
1365+ for ( tmp = pos_use_2nd + 5 ; * tmp == ' ' ; tmp ++ );
1366+ if ( strncmp ( tmp , "unique " , 7 ) == 0 )
1367+ for ( pos_uid = tmp + 7 ; * pos_uid == ' ' ; pos_uid ++ );
13701368 if ( strncmp ( tmp , "srid=" , 5 ) == 0 ) pos_srid = tmp + 5 ;
13711369 };
13721370
13731371 /*
1374- ** What clause appear after 1st 'using'?
1372+ ** What clause appear after 1st 'using', if set ?
13751373 */
1376- if ( ! pos_uid )
1374+ if ( pos_use_1st )
13771375 {
13781376 for ( tmp = pos_use_1st + 5 ; * tmp == ' ' ; tmp ++ );
13791377 if ( strncmp ( tmp , "unique " , 7 ) == 0 )
1378+ {
1379+ if ( pos_uid )
1380+ {
1381+ free ( data );
1382+ msSetError (MS_QUERYERR , "Error parsing PostGIS DATA variable. Too many 'USING UNIQUE' found! %s" , "msPostGISParseData()" , layer -> data );
1383+ return MS_FAILURE ;
1384+ };
13801385 for ( pos_uid = tmp + 7 ; * pos_uid == ' ' ; pos_uid ++ );
1381- };
1382- if ( !pos_srid )
1383- {
1384- if ( strncmp ( tmp , "srid=" , 5 ) == 0 ) pos_srid = tmp + 5 ;
1386+ };
1387+ if ( strncmp ( tmp , "srid=" , 5 ) == 0 )
1388+ {
1389+ if ( pos_srid )
1390+ {
1391+ free ( data );
1392+ msSetError (MS_QUERYERR , "Error parsing PostGIS DATA variable. Too many 'USING SRID' found! %s" , "msPostGISParseData()" , layer -> data );
1393+ return MS_FAILURE ;
1394+ };
1395+ pos_srid = tmp + 5 ;
1396+ };
13851397 };
13861398
13871399 /*
@@ -1395,7 +1407,7 @@ int msPostGISParseData(layerObj *layer)
13951407 tmp = pos_uid + strlen (pos_uid );
13961408 }
13971409 layerinfo -> uid = (char * ) msSmallMalloc ((tmp - pos_uid ) + 1 );
1398- strlcpy (layerinfo -> uid , pos_uid , (tmp - pos_uid ) + 1 );
1410+ strlcpy (layerinfo -> uid , pos_uid , (tmp - pos_uid ) + 1 );
13991411 msStringTrim (layerinfo -> uid );
14001412 }
14011413
@@ -1409,7 +1421,7 @@ int msPostGISParseData(layerObj *layer)
14091421 slength = strspn (pos_srid , "-0123456789" );
14101422 if (!slength ) {
14111423 free ( data );
1412- msSetError (MS_QUERYERR , "Error parsing PostGIS DATA variable. You specified 'USING SRID' but didnt have any numbers! %s" , "msPostGISParseData()" , layer -> data );
1424+ msSetError (MS_QUERYERR , "Error parsing PostGIS DATA variable. You specified 'USING SRID' but didn't have any numbers! %s" , "msPostGISParseData()" , layer -> data );
14131425 return MS_FAILURE ;
14141426 } else {
14151427 layerinfo -> srid = (char * ) msSmallMalloc (slength + 1 );
@@ -1423,8 +1435,6 @@ int msPostGISParseData(layerObj *layer)
14231435 ** pos_opt should point to the start of the optional blocks.
14241436 **
14251437 ** If they are both set, return the smaller one.
1426- */
1427- /*
14281438 ** If pos_use_1st set, then it smaller.
14291439 */
14301440 if (pos_use_1st ) {
@@ -1438,17 +1448,15 @@ int msPostGISParseData(layerObj *layer)
14381448 if (!pos_opt ) {
14391449 pos_opt = data + strlen (data );
14401450 }
1441- /* Back the last non-space character. */
1442- for ( -- pos_opt ; * pos_opt != ' ' ; pos_opt -- );
1451+ /* Back after the last non-space character. */
1452+ for ( pos_opt ; ( pos_opt > data ) && ( * ( pos_opt - 1 ) == ' ' ) ; pos_opt -- );
14431453
14441454 /*
14451455 ** Scan for the 'geometry from table' or 'geometry from () as foo' clause.
14461456 */
14471457
14481458 /* Find the first non-white character to start from */
1449- pos_geom = data ;
1450- while ( * pos_geom == ' ' || * pos_geom == '\t' || * pos_geom == '\n' || * pos_geom == '\r' )
1451- pos_geom ++ ;
1459+ for ( pos_geom = data ; * pos_geom == ' ' ; pos_geom ++ );
14521460
14531461 /* Find the end of the geom column name */
14541462 pos_scn = strcasestr (data , " from " );
@@ -1460,13 +1468,18 @@ int msPostGISParseData(layerObj *layer)
14601468
14611469 /* Copy the geometry column name */
14621470 layerinfo -> geomcolumn = (char * ) msSmallMalloc ((pos_scn - pos_geom ) + 1 );
1463- strlcpy (layerinfo -> geomcolumn , pos_geom , pos_scn - pos_geom + 1 );
1471+ strlcpy (layerinfo -> geomcolumn , pos_geom , ( pos_scn - pos_geom ) + 1 );
14641472 msStringTrim (layerinfo -> geomcolumn );
14651473
14661474 /* Copy the table name or sub-select clause */
1467- pos_scn += 6 ;
1468- while ( * pos_scn == ' ' ) pos_scn ++ ;
1469- layerinfo -> fromsource = (char * ) msSmallMalloc ((pos_opt + 1 ) - pos_scn );
1475+ for ( pos_scn += 6 ; * pos_scn == ' ' ; pos_scn ++ );
1476+ if ( pos_opt - pos_scn < 1 )
1477+ {
1478+ free ( data );
1479+ msSetError (MS_QUERYERR , "Error parsing PostGIS DATA variable. Must contain 'geometry from table' or 'geometry from (subselect) as foo'. %s" , "msPostGISParseData()" , layer -> data );
1480+ return MS_FAILURE ;
1481+ };
1482+ layerinfo -> fromsource = (char * ) msSmallMalloc (pos_opt - pos_scn + 1 );
14701483 strlcpy (layerinfo -> fromsource , ( layer -> data - data ) + pos_scn , pos_opt - pos_scn + 1 );
14711484 msStringTrim (layerinfo -> fromsource );
14721485
0 commit comments