[postgis-tickets] r17848 - getSRIDbySRS: Fix crash
Raul
raul at rmr.ninja
Wed Oct 2 07:24:45 PDT 2019
Author: algunenano
Date: 2019-10-02 07:24:45 -0700 (Wed, 02 Oct 2019)
New Revision: 17848
Modified:
branches/2.4/NEWS
branches/2.4/postgis/lwgeom_export.c
Log:
getSRIDbySRS: Fix crash
- Prevents stack overflow when the srs is long (query + srs > 256 chars).
- Prevents sql injection.
References #4519
Modified: branches/2.4/NEWS
===================================================================
--- branches/2.4/NEWS 2019-10-02 14:23:26 UTC (rev 17847)
+++ branches/2.4/NEWS 2019-10-02 14:24:45 UTC (rev 17848)
@@ -12,6 +12,7 @@
- #4495, Fix ST_SnapToGrid output having an outdated bbox (Raúl Marín)
- #4506, Remove tolerance in point-in-ring stabline tests (Paul Ramsey)
- #4518, Backport system views to make upgrade to PostGIS3 cleaner (Paul Ramsey)
+ - #4519, Fix getSRIDbySRS crash (Raúl Marín)
PostGIS 2.4.8
Modified: branches/2.4/postgis/lwgeom_export.c
===================================================================
--- branches/2.4/postgis/lwgeom_export.c 2019-10-02 14:23:26 UTC (rev 17847)
+++ branches/2.4/postgis/lwgeom_export.c 2019-10-02 14:24:45 UTC (rev 17848)
@@ -29,7 +29,9 @@
*/
#include "float.h" /* for DBL_DIG */
+
#include "postgres.h"
+#include "catalog/pg_type.h" /* for CSTRINGOID */
#include "executor/spi.h"
#include "../postgis_config.h"
@@ -116,26 +118,26 @@
*/
int getSRIDbySRS(const char* srs)
{
- char query[256];
- int srid, err;
+ char *query =
+ "SELECT srid "
+ "FROM spatial_ref_sys, "
+ "regexp_matches($1::text, E'([a-z]+):([0-9]+)', 'gi') AS re "
+ "WHERE re[1] ILIKE auth_name AND int4(re[2]) = auth_srid";
+ Oid argtypes[] = {CSTRINGOID};
+ Datum values[] = {CStringGetDatum(srs)};
+ int32_t srid, err;
if (srs == NULL)
return 0;
- if (SPI_OK_CONNECT != SPI_connect ())
+ if (SPI_OK_CONNECT != SPI_connect())
{
elog(NOTICE, "getSRIDbySRS: could not connect to SPI manager");
- SPI_finish();
return 0;
}
- sprintf(query,
- "SELECT srid "
- "FROM spatial_ref_sys, "
- "regexp_matches('%s', E'([a-z]+):([0-9]+)', 'gi') AS re "
- "WHERE re[1] ILIKE auth_name AND int4(re[2]) = auth_srid", srs);
- err = SPI_exec(query, 1);
- if ( err < 0 )
+ err = SPI_execute_with_args(query, 1, argtypes, values, NULL, true, 1);
+ if (err < 0)
{
elog(NOTICE, "getSRIDbySRS: error executing query %d", err);
SPI_finish();
@@ -145,14 +147,14 @@
/* no entry in spatial_ref_sys */
if (SPI_processed <= 0)
{
- sprintf(query,
- "SELECT srid "
- "FROM spatial_ref_sys, "
- "regexp_matches('%s', E'urn:ogc:def:crs:([a-z]+):.*:([0-9]+)', 'gi') AS re "
- "WHERE re[1] ILIKE auth_name AND int4(re[2]) = auth_srid", srs);
+ query =
+ "SELECT srid "
+ "FROM spatial_ref_sys, "
+ "regexp_matches($1::text, E'urn:ogc:def:crs:([a-z]+):.*:([0-9]+)', 'gi') AS re "
+ "WHERE re[1] ILIKE auth_name AND int4(re[2]) = auth_srid";
- err = SPI_exec(query, 1);
- if ( err < 0 )
+ err = SPI_execute_with_args(query, 1, argtypes, values, NULL, true, 1);
+ if (err < 0)
{
elog(NOTICE, "getSRIDbySRS: error executing query %d", err);
SPI_finish();
@@ -159,7 +161,8 @@
return 0;
}
- if (SPI_processed <= 0) {
+ if (SPI_processed <= 0)
+ {
SPI_finish();
return 0;
}
More information about the postgis-tickets
mailing list