]> granicus.if.org Git - postgis/commitdiff
Fix for #1088: Too many columns in select crashes pgsql2shp.
authorMark Cave-Ayland <mark.cave-ayland@siriusit.co.uk>
Sun, 3 Jul 2011 21:47:09 +0000 (21:47 +0000)
committerMark Cave-Ayland <mark.cave-ayland@siriusit.co.uk>
Sun, 3 Jul 2011 21:47:09 +0000 (21:47 +0000)
Instead of a fixed length query string, dynamically allocate the memory based
upon the sum of the lengths of all of the column names.

git-svn-id: http://svn.osgeo.org/postgis/trunk@7566 b70326c6-7e19-0410-871a-916f4a2858ee

loader/pgsql2shp-core.c

index 7f0a69e2e9526aac231d945bf04204d2758dc376..e7ef7dfd0cbbf2ae33d7efdbd23ebbf20eb7dfcf 100644 (file)
@@ -51,9 +51,6 @@
 /* Maximum DBF field width (according to ARCGIS) */
 #define MAX_DBF_FIELD_SIZE 254
 
-/* Maximum length of the main scan query */
-#define MAX_QUERY_LEN  2048
-
 
 /* Prototypes */
 static int reverse_points(int num_points, double *x, double *y, double *z, double *m);
@@ -1884,8 +1881,14 @@ ShpDumperOpenTable(SHPDUMPERSTATE *state)
        }
        
 
-       /* Now we have the complete list of fieldnames, let's generate the SQL query */
-       state->main_scan_query = malloc(MAX_QUERY_LEN);
+       /* Now we have the complete list of fieldnames, let's generate the SQL query. First let's make sure
+          we reserve enough space for tables with lots of columns */
+       j = 0;
+       for (i = 0; i < state->fieldcount; i++)
+               j += strlen(state->pgfieldnames[i] + 2);        /* Add 2 for leading and trailing quotes */
+       
+       state->main_scan_query = malloc(1024 + j);
+       
        sprintf(state->main_scan_query, "DECLARE cur ");
        if (state->config->binary)
                strcat(state->main_scan_query, "BINARY ");
@@ -1991,7 +1994,7 @@ ShpDumperOpenTable(SHPDUMPERSTATE *state)
        state->fetchres = NULL;
 
        /* Generate the fetch query */
-       state->fetch_query = malloc(MAX_QUERY_LEN);
+       state->fetch_query = malloc(256);
        sprintf(state->fetch_query, "FETCH %d FROM cur", state->config->fetchsize);
 
        return SHPDUMPEROK;