/ Check-in [c1814242]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add the sqlite3_value_dup() and sqlite3_value_free() interfaces. Use these interfaces to enhance R-Tree to add the sqlite3_rtree_query_info.apSqlParam field.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: c1814242e6ce035d486cfbad5a1f4b49aa8898f3
User & Date: drh 2015-05-22 21:12:59
Context
2015-05-22
23:17
Fix incorrect validation of the size of the BLOB returned by a geometry function on the RHS of a MATCH operator in RTree. The old code worked for 64-bit systems (by chance) but fails on 32-bit. check-in: 10cc44f5 user: drh tags: trunk
21:12
Add the sqlite3_value_dup() and sqlite3_value_free() interfaces. Use these interfaces to enhance R-Tree to add the sqlite3_rtree_query_info.apSqlParam field. check-in: c1814242 user: drh tags: trunk
21:04
Make sure sqlite3_value_dup() works correctly even for sqlite3_value objects that are marked SQLITE_STATIC. Closed-Leaf check-in: f332ffc3 user: drh tags: value-dup
17:29
The SRT_Table type for the SelectDest object is now just an SRT_EphemTab for which the ephemeral table has already been allocated. check-in: b9727e6b user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to ext/rtree/rtree.c.

   347    347   ** sqlite3_rtree_query_callback() create, and is read as the right-hand
   348    348   ** operand to the MATCH operator of an R-Tree.
   349    349   */
   350    350   struct RtreeMatchArg {
   351    351     u32 magic;                  /* Always RTREE_GEOMETRY_MAGIC */
   352    352     RtreeGeomCallback cb;       /* Info about the callback functions */
   353    353     int nParam;                 /* Number of parameters to the SQL function */
          354  +  sqlite3_value **apSqlParam; /* Original SQL parameter values */
   354    355     RtreeDValue aParam[1];      /* Values for parameters to the SQL function */
   355    356   };
   356    357   
   357    358   #ifndef MAX
   358    359   # define MAX(x,y) ((x) < (y) ? (y) : (x))
   359    360   #endif
   360    361   #ifndef MIN
................................................................................
  1491   1492     pInfo = (sqlite3_rtree_query_info*)sqlite3_malloc( sizeof(*pInfo)+nBlob );
  1492   1493     if( !pInfo ) return SQLITE_NOMEM;
  1493   1494     memset(pInfo, 0, sizeof(*pInfo));
  1494   1495     pBlob = (RtreeMatchArg*)&pInfo[1];
  1495   1496   
  1496   1497     memcpy(pBlob, sqlite3_value_blob(pValue), nBlob);
  1497   1498     nExpected = (int)(sizeof(RtreeMatchArg) +
         1499  +                    pBlob->nParam*sizeof(sqlite3_value*) +
  1498   1500                       (pBlob->nParam-1)*sizeof(RtreeDValue));
  1499   1501     if( pBlob->magic!=RTREE_GEOMETRY_MAGIC || nBlob!=nExpected ){
  1500   1502       sqlite3_free(pInfo);
  1501   1503       return SQLITE_ERROR;
  1502   1504     }
  1503   1505     pInfo->pContext = pBlob->cb.pContext;
  1504   1506     pInfo->nParam = pBlob->nParam;
  1505   1507     pInfo->aParam = pBlob->aParam;
         1508  +  pInfo->apSqlParam = pBlob->apSqlParam;
  1506   1509   
  1507   1510     if( pBlob->cb.xGeom ){
  1508   1511       pCons->u.xGeom = pBlob->cb.xGeom;
  1509   1512     }else{
  1510   1513       pCons->op = RTREE_QUERY;
  1511   1514       pCons->u.xQueryFunc = pBlob->cb.xQueryFunc;
  1512   1515     }
................................................................................
  3380   3383   ** the corresponding SQL function is deleted.
  3381   3384   */
  3382   3385   static void rtreeFreeCallback(void *p){
  3383   3386     RtreeGeomCallback *pInfo = (RtreeGeomCallback*)p;
  3384   3387     if( pInfo->xDestructor ) pInfo->xDestructor(pInfo->pContext);
  3385   3388     sqlite3_free(p);
  3386   3389   }
         3390  +
         3391  +/*
         3392  +** This routine frees the BLOB that is returned by geomCallback().
         3393  +*/
         3394  +static void rtreeMatchArgFree(void *pArg){
         3395  +  int i;
         3396  +  RtreeMatchArg *p = (RtreeMatchArg*)pArg;
         3397  +  for(i=0; i<p->nParam; i++){
         3398  +    sqlite3_value_free(p->apSqlParam[i]);
         3399  +  }
         3400  +  sqlite3_free(p);
         3401  +}
  3387   3402   
  3388   3403   /*
  3389   3404   ** Each call to sqlite3_rtree_geometry_callback() or
  3390   3405   ** sqlite3_rtree_query_callback() creates an ordinary SQLite
  3391   3406   ** scalar function that is implemented by this routine.
  3392   3407   **
  3393   3408   ** All this function does is construct an RtreeMatchArg object that
................................................................................
  3399   3414   ** the RtreeMatchArg object, and use the RtreeMatchArg object to figure
  3400   3415   ** out which elements of the R-Tree should be returned by the query.
  3401   3416   */
  3402   3417   static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
  3403   3418     RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
  3404   3419     RtreeMatchArg *pBlob;
  3405   3420     int nBlob;
         3421  +  int memErr = 0;
  3406   3422   
  3407         -  nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue);
         3423  +  nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue)
         3424  +           + nArg*sizeof(sqlite3_value*);
  3408   3425     pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
  3409   3426     if( !pBlob ){
  3410   3427       sqlite3_result_error_nomem(ctx);
  3411   3428     }else{
  3412   3429       int i;
  3413   3430       pBlob->magic = RTREE_GEOMETRY_MAGIC;
  3414   3431       pBlob->cb = pGeomCtx[0];
         3432  +    pBlob->apSqlParam = (sqlite3_value**)&pBlob->aParam[nArg];
  3415   3433       pBlob->nParam = nArg;
  3416   3434       for(i=0; i<nArg; i++){
  3417         -      if( sqlite3_value_type(aArg[i])==SQLITE_BLOB
  3418         -        && sqlite3_value_bytes(aArg[i])==sizeof(sqlite3_rtree_dbl)
  3419         -      ){
  3420         -        memcpy(&pBlob->aParam[i], sqlite3_value_blob(aArg[i]),
  3421         -               sizeof(sqlite3_rtree_dbl));
  3422         -      }else{
         3435  +      pBlob->apSqlParam[i] = sqlite3_value_dup(aArg[i]);
         3436  +      if( pBlob->apSqlParam[i]==0 ) memErr = 1;
  3423   3437   #ifdef SQLITE_RTREE_INT_ONLY
  3424         -        pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
         3438  +      pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
  3425   3439   #else
  3426         -        pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
         3440  +      pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
  3427   3441   #endif
  3428         -      }
  3429   3442       }
  3430         -    sqlite3_result_blob(ctx, pBlob, nBlob, sqlite3_free);
         3443  +    if( memErr ){
         3444  +      sqlite3_result_error_nomem(ctx);
         3445  +      rtreeMatchArgFree(pBlob);
         3446  +    }else{
         3447  +      sqlite3_result_blob(ctx, pBlob, nBlob, rtreeMatchArgFree);
         3448  +    }
  3431   3449     }
  3432   3450   }
  3433   3451   
  3434   3452   /*
  3435   3453   ** Register a new geometry function for use with the r-tree MATCH operator.
  3436   3454   */
  3437   3455   int sqlite3_rtree_geometry_callback(

Changes to ext/rtree/rtreeE.test.

    48     48     INSERT INTO rt1 SELECT 200+x+5*y, x*7, x*7+15, y*7+200, y*7+215 FROM x, y;
    49     49   } {}
    50     50   
    51     51   # Queries against each of the three clusters */
    52     52   do_execsql_test rtreeE-1.1 {
    53     53     SELECT id FROM rt1 WHERE id MATCH Qcircle(0.0, 0.0, 50.0, 3) ORDER BY id;
    54     54   } {0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24}
           55  +do_execsql_test rtreeE-1.1x {
           56  +  SELECT id FROM rt1 WHERE id MATCH Qcircle('x:0 y:0 r:50.0 e:3') ORDER BY id;
           57  +} {0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24}
    55     58   do_execsql_test rtreeE-1.2 {
    56     59     SELECT id FROM rt1 WHERE id MATCH Qcircle(100.0, 0.0, 50.0, 3) ORDER BY id;
    57     60   } {100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124}
    58     61   do_execsql_test rtreeE-1.3 {
    59     62     SELECT id FROM rt1 WHERE id MATCH Qcircle(0.0, 200.0, 50.0, 3) ORDER BY id;
    60     63   } {200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224}
    61     64   
    62     65   # The Qcircle geometry function gives a lower score to larger leaf-nodes.
    63     66   # This causes the 200s to sort before the 100s and the 0s to sort before
    64     67   # last.
    65     68   #
    66     69   do_execsql_test rtreeE-1.4 {
    67         -  SELECT id FROM rt1 WHERE id MATCH Qcircle(0,0,1000,3) AND id%100==0
           70  +  SELECT id FROM rt1 WHERE id MATCH Qcircle('r:1000 e:3') AND id%100==0
    68     71   } {200 100 0}
    69     72   
    70     73   # Exclude odd rowids on a depth-first search
    71     74   do_execsql_test rtreeE-1.5 {
    72         -  SELECT id FROM rt1 WHERE id MATCH Qcircle(0,0,1000,4) ORDER BY +id
           75  +  SELECT id FROM rt1 WHERE id MATCH Qcircle('r:1000 e:4') ORDER BY +id
    73     76   } {0 2 4 6 8 10 12 14 16 18 20 22 24 100 102 104 106 108 110 112 114 116 118 120 122 124 200 202 204 206 208 210 212 214 216 218 220 222 224}
    74     77   
    75     78   # Exclude odd rowids on a breadth-first search.
    76     79   do_execsql_test rtreeE-1.6 {
    77     80     SELECT id FROM rt1 WHERE id MATCH Qcircle(0,0,1000,5) ORDER BY +id
    78     81   } {0 2 4 6 8 10 12 14 16 18 20 22 24 100 102 104 106 108 110 112 114 116 118 120 122 124 200 202 204 206 208 210 212 214 216 218 220 222 224}
    79     82   

Changes to ext/rtree/sqlite3rtree.h.

    94     94     int iLevel;                       /* Level of current node or entry */
    95     95     int mxLevel;                      /* The largest iLevel value in the tree */
    96     96     sqlite3_int64 iRowid;             /* Rowid for current entry */
    97     97     sqlite3_rtree_dbl rParentScore;   /* Score of parent node */
    98     98     int eParentWithin;                /* Visibility of parent node */
    99     99     int eWithin;                      /* OUT: Visiblity */
   100    100     sqlite3_rtree_dbl rScore;         /* OUT: Write the score here */
          101  +  /* The following fields are only available in 3.8.11 and later */
          102  +  sqlite3_value **apSqlParam;       /* Original SQL values of parameters */
   101    103   };
   102    104   
   103    105   /*
   104    106   ** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
   105    107   */
   106    108   #define NOT_WITHIN       0   /* Object completely outside of query region */
   107    109   #define PARTLY_WITHIN    1   /* Object partially overlaps query region */

Changes to src/sqlite.h.in.

  4290   4290   SQLITE_DEPRECATED int sqlite3_global_recover(void);
  4291   4291   SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
  4292   4292   SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
  4293   4293                         void*,sqlite3_int64);
  4294   4294   #endif
  4295   4295   
  4296   4296   /*
  4297         -** CAPI3REF: Obtaining SQL Function Parameter Values
         4297  +** CAPI3REF: Obtaining SQL Values
  4298   4298   ** METHOD: sqlite3_value
  4299   4299   **
  4300   4300   ** The C-language implementation of SQL functions and aggregates uses
  4301   4301   ** this set of interface routines to access the parameter values on
  4302         -** the function or aggregate.
         4302  +** the function or aggregate.  
  4303   4303   **
  4304   4304   ** The xFunc (for scalar functions) or xStep (for aggregates) parameters
  4305   4305   ** to [sqlite3_create_function()] and [sqlite3_create_function16()]
  4306   4306   ** define callbacks that implement the SQL functions and aggregates.
  4307   4307   ** The 3rd parameter to these callbacks is an array of pointers to
  4308   4308   ** [protected sqlite3_value] objects.  There is one [sqlite3_value] object for
  4309   4309   ** each parameter to the SQL function.  These routines are used to
................................................................................
  4348   4348   const unsigned char *sqlite3_value_text(sqlite3_value*);
  4349   4349   const void *sqlite3_value_text16(sqlite3_value*);
  4350   4350   const void *sqlite3_value_text16le(sqlite3_value*);
  4351   4351   const void *sqlite3_value_text16be(sqlite3_value*);
  4352   4352   int sqlite3_value_type(sqlite3_value*);
  4353   4353   int sqlite3_value_numeric_type(sqlite3_value*);
  4354   4354   
         4355  +/*
         4356  +** CAPI3REF: Copy And Free SQL Values
         4357  +** METHOD: sqlite3_value
         4358  +**
         4359  +** ^The sqlite3_value_dup(V) interface makes a copy of the [sqlite3_value]
         4360  +** object D and returns a pointer to that copy.  ^The [sqlite3_value] returned
         4361  +** is a [protected sqlite3_value] object even if the input is not.
         4362  +** ^The sqlite3_value_dup(V) interface returns NULL if V is NULL or if a
         4363  +** memory allocation fails.
         4364  +**
         4365  +** ^The sqlite3_value_free(V) interface frees an [sqlite3_value] object
         4366  +** previously obtained from [sqlite_value_dup()].  ^If V is a NULL pointer
         4367  +** then sqlite3_value_free(V) is a harmless no-op.
         4368  +*/
         4369  +SQLITE_EXPERIMENTAL sqlite3_value *sqlite3_value_dup(const sqlite3_value*);
         4370  +SQLITE_EXPERIMENTAL void sqlite3_value_free(sqlite3_value*);
         4371  +
  4355   4372   /*
  4356   4373   ** CAPI3REF: Obtain Aggregate Function Context
  4357   4374   ** METHOD: sqlite3_context
  4358   4375   **
  4359   4376   ** Implementations of aggregate SQL functions use this
  4360   4377   ** routine to allocate memory for storing their state.
  4361   4378   **
................................................................................
  5871   5888   ** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
  5872   5889   ** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
  5873   5890   ** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
  5874   5891   ** always returns zero.
  5875   5892   **
  5876   5893   ** ^This function sets the database handle error code and message.
  5877   5894   */
  5878         -SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
         5895  +int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
  5879   5896   
  5880   5897   /*
  5881   5898   ** CAPI3REF: Close A BLOB Handle
  5882   5899   ** DESTRUCTOR: sqlite3_blob
  5883   5900   **
  5884   5901   ** ^This function closes an open [BLOB handle]. ^(The BLOB handle is closed
  5885   5902   ** unconditionally.  Even if this routine returns an error code, the 
................................................................................
  7681   7698   ** ^Statistics might not be available for all loops in all statements. ^In cases
  7682   7699   ** where there exist loops with no available statistics, this function behaves
  7683   7700   ** as if the loop did not exist - it returns non-zero and leave the variable
  7684   7701   ** that pOut points to unchanged.
  7685   7702   **
  7686   7703   ** See also: [sqlite3_stmt_scanstatus_reset()]
  7687   7704   */
  7688         -SQLITE_EXPERIMENTAL int sqlite3_stmt_scanstatus(
         7705  +int sqlite3_stmt_scanstatus(
  7689   7706     sqlite3_stmt *pStmt,      /* Prepared statement for which info desired */
  7690   7707     int idx,                  /* Index of loop to report on */
  7691   7708     int iScanStatusOp,        /* Information desired.  SQLITE_SCANSTAT_* */
  7692   7709     void *pOut                /* Result written here */
  7693   7710   );     
  7694   7711   
  7695   7712   /*
................................................................................
  7697   7714   ** METHOD: sqlite3_stmt
  7698   7715   **
  7699   7716   ** ^Zero all [sqlite3_stmt_scanstatus()] related event counters.
  7700   7717   **
  7701   7718   ** This API is only available if the library is built with pre-processor
  7702   7719   ** symbol [SQLITE_ENABLE_STMT_SCANSTATUS] defined.
  7703   7720   */
  7704         -SQLITE_EXPERIMENTAL void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
         7721  +void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
  7705   7722   
  7706   7723   
  7707   7724   /*
  7708   7725   ** Undo the hack that converts floating point types to integer for
  7709   7726   ** builds on processors without floating point support.
  7710   7727   */
  7711   7728   #ifdef SQLITE_OMIT_FLOATING_POINT

Changes to src/sqlite3ext.h.

   263    263     void *(*realloc64)(void*,sqlite3_uint64);
   264    264     void (*reset_auto_extension)(void);
   265    265     void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
   266    266                           void(*)(void*));
   267    267     void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
   268    268                            void(*)(void*), unsigned char);
   269    269     int (*strglob)(const char*,const char*);
          270  +  sqlite3_value (*value_dup)(const sqlite3_value*);
          271  +  void (*value_free)(sqlite3_value*);
   270    272   };
   271    273   
   272    274   /*
   273    275   ** The following macros redefine the API routines so that they are
   274    276   ** redirected through the global sqlite3_api structure.
   275    277   **
   276    278   ** This header file is also used by the loadext.c source file
................................................................................
   493    495   #define sqlite3_malloc64               sqlite3_api->malloc64
   494    496   #define sqlite3_msize                  sqlite3_api->msize
   495    497   #define sqlite3_realloc64              sqlite3_api->realloc64
   496    498   #define sqlite3_reset_auto_extension   sqlite3_api->reset_auto_extension
   497    499   #define sqlite3_result_blob64          sqlite3_api->result_blob64
   498    500   #define sqlite3_result_text64          sqlite3_api->result_text64
   499    501   #define sqlite3_strglob                sqlite3_api->strglob
          502  +/* Version 3.8.11 and later */
          503  +#define sqlite3_value_dup              sqlite3_api->value_dup
          504  +#define sqlite3_value_free             sqlite3_api->value_free
   500    505   #endif /* SQLITE_CORE */
   501    506   
   502    507   #ifndef SQLITE_CORE
   503    508     /* This case when the file really is being compiled as a loadable 
   504    509     ** extension */
   505    510   # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
   506    511   # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;

Changes to src/test_rtree.c.

   151    151     *pRes = 0;
   152    152     return SQLITE_OK;
   153    153   }
   154    154   
   155    155   /*
   156    156   ** Implementation of "circle" r-tree geometry callback using the 
   157    157   ** 2nd-generation interface that allows scoring.
          158  +**
          159  +** Two calling forms:
          160  +**
          161  +**          Qcircle(X,Y,Radius,eType)        -- All values are doubles
          162  +**          Qcircle('x:X y:Y r:R e:ETYPE')   -- Single string parameter
   158    163   */
   159    164   static int circle_query_func(sqlite3_rtree_query_info *p){
   160    165     int i;                          /* Iterator variable */
   161    166     Circle *pCircle;                /* Structure defining circular region */
   162    167     double xmin, xmax;              /* X dimensions of box being tested */
   163    168     double ymin, ymax;              /* X dimensions of box being tested */
   164    169     int nWithin = 0;                /* Number of corners inside the circle */
................................................................................
   172    177       /* If pUser is still 0, then the parameter values have not been tested
   173    178       ** for correctness or stored into a Circle structure yet. Do this now. */
   174    179   
   175    180       /* This geometry callback is for use with a 2-dimensional r-tree table.
   176    181       ** Return an error if the table does not have exactly 2 dimensions. */
   177    182       if( p->nCoord!=4 ) return SQLITE_ERROR;
   178    183   
   179         -    /* Test that the correct number of parameters (4) have been supplied,
   180         -    ** and that the parameters are in range (that the radius of the circle 
   181         -    ** radius is greater than zero). */
   182         -    if( p->nParam!=4 || p->aParam[2]<0.0 ) return SQLITE_ERROR;
          184  +    /* Test that the correct number of parameters (1 or 4) have been supplied.
          185  +    */
          186  +    if( p->nParam!=4 && p->nParam!=1 ) return SQLITE_ERROR;
   183    187   
   184    188       /* Allocate a structure to cache parameter data in. Return SQLITE_NOMEM
   185    189       ** if the allocation fails. */
   186    190       pCircle = (Circle *)(p->pUser = sqlite3_malloc(sizeof(Circle)));
   187    191       if( !pCircle ) return SQLITE_NOMEM;
   188    192       p->xDelUser = circle_del;
   189    193   
   190    194       /* Record the center and radius of the circular region. One way that
   191    195       ** tested bounding boxes that intersect the circular region are detected
   192    196       ** is by testing if each corner of the bounding box lies within radius
   193    197       ** units of the center of the circle. */
   194         -    pCircle->centerx = p->aParam[0];
   195         -    pCircle->centery = p->aParam[1];
   196         -    pCircle->radius = p->aParam[2];
   197         -    pCircle->eScoreType = (int)p->aParam[3];
          198  +    if( p->nParam==4 ){
          199  +      pCircle->centerx = p->aParam[0];
          200  +      pCircle->centery = p->aParam[1];
          201  +      pCircle->radius = p->aParam[2];
          202  +      pCircle->eScoreType = (int)p->aParam[3];
          203  +    }else{
          204  +      const char *z = (const char*)sqlite3_value_text(p->apSqlParam[0]);
          205  +      pCircle->centerx = 0.0;
          206  +      pCircle->centery = 0.0;
          207  +      pCircle->radius = 0.0;
          208  +      pCircle->eScoreType = 0;
          209  +      while( z && z[0] ){
          210  +        if( z[0]=='r' && z[1]==':' ){
          211  +          pCircle->radius = atof(&z[2]);
          212  +        }else if( z[0]=='x' && z[1]==':' ){
          213  +          pCircle->centerx = atof(&z[2]);
          214  +        }else if( z[0]=='y' && z[1]==':' ){
          215  +          pCircle->centery = atof(&z[2]);
          216  +        }else if( z[0]=='e' && z[1]==':' ){
          217  +          pCircle->eScoreType = (int)atof(&z[2]);
          218  +        }else if( z[0]==' ' ){
          219  +          z++;
          220  +          continue;
          221  +        }
          222  +        while( z[0]!=0 && z[0]!=' ' ) z++;
          223  +        while( z[0]==' ' ) z++;
          224  +      }
          225  +    }
          226  +    if( pCircle->radius<0.0 ){
          227  +      sqlite3_free(pCircle);
          228  +      return SQLITE_NOMEM;
          229  +    }
   198    230   
   199    231       /* Define two bounding box regions. The first, aBox[0], extends to
   200    232       ** infinity in the X dimension. It covers the same range of the Y dimension
   201    233       ** as the circular region. The second, aBox[1], extends to infinity in
   202    234       ** the Y dimension and is constrained to the range of the circle in the
   203    235       ** X dimension.
   204    236       **

Changes to src/vdbeInt.h.

   182    182     void (*xDel)(void*);/* Destructor for Mem.z - only valid if MEM_Dyn */
   183    183   #ifdef SQLITE_DEBUG
   184    184     Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
   185    185     void *pFiller;      /* So that sizeof(Mem) is a multiple of 8 */
   186    186   #endif
   187    187   };
   188    188   
          189  +/*
          190  +** Size of struct Mem not including the Mem.zMalloc member or anything that
          191  +** follows.
          192  +*/
          193  +#define MEMCELLSIZE offsetof(Mem,zMalloc)
          194  +
   189    195   /* One or more of the following flags are set to indicate the validOK
   190    196   ** representations of the value stored in the Mem struct.
   191    197   **
   192    198   ** If the MEM_Null flag is set, then the value is an SQL NULL value.
   193    199   ** No other flags may be set in this case.
   194    200   **
   195    201   ** If the MEM_Str flag is set then Mem.z points at a string representation.

Changes to src/vdbeapi.c.

   207    207        SQLITE_INTEGER,  /* 0x1c */
   208    208        SQLITE_NULL,     /* 0x1d */
   209    209        SQLITE_INTEGER,  /* 0x1e */
   210    210        SQLITE_NULL,     /* 0x1f */
   211    211     };
   212    212     return aType[pVal->flags&MEM_AffMask];
   213    213   }
          214  +
          215  +/* Make a copy of an sqlite3_value object
          216  +*/
          217  +sqlite3_value *sqlite3_value_dup(const sqlite3_value *pOrig){
          218  +  sqlite3_value *pNew;
          219  +  if( pOrig==0 ) return 0;
          220  +  pNew = sqlite3_malloc( sizeof(*pNew) );
          221  +  if( pNew==0 ) return 0;
          222  +  memset(pNew, 0, sizeof(*pNew));
          223  +  memcpy(pNew, pOrig, MEMCELLSIZE);
          224  +  pNew->flags &= ~MEM_Dyn;
          225  +  pNew->db = 0;
          226  +  if( pNew->flags&(MEM_Str|MEM_Blob) ){
          227  +    pNew->flags &= ~(MEM_Static|MEM_Dyn);
          228  +    pNew->flags |= MEM_Ephem;
          229  +    if( sqlite3VdbeMemMakeWriteable(pNew)!=SQLITE_OK ){
          230  +      sqlite3ValueFree(pNew);
          231  +      pNew = 0;
          232  +    }
          233  +  }
          234  +  return pNew;
          235  +}
          236  +
          237  +/* Destroy an sqlite3_value object previously obtained from
          238  +** sqlite3_value_dup().
          239  +*/
          240  +void sqlite3_value_free(sqlite3_value *pOld){
          241  +  sqlite3ValueFree(pOld);
          242  +}
          243  +  
   214    244   
   215    245   /**************************** sqlite3_result_  *******************************
   216    246   ** The following routines are used by user-defined functions to specify
   217    247   ** the function result.
   218    248   **
   219    249   ** The setStrOrError() function calls sqlite3VdbeMemSetStr() to store the
   220    250   ** result as a string or blob but if the string or blob is too large, it

Changes to src/vdbemem.c.

   766    766         pX->pScopyFrom = 0;
   767    767       }
   768    768     }
   769    769     pMem->pScopyFrom = 0;
   770    770   }
   771    771   #endif /* SQLITE_DEBUG */
   772    772   
   773         -/*
   774         -** Size of struct Mem not including the Mem.zMalloc member.
   775         -*/
   776         -#define MEMCELLSIZE offsetof(Mem,zMalloc)
   777    773   
   778    774   /*
   779    775   ** Make an shallow copy of pFrom into pTo.  Prior contents of
   780    776   ** pTo are freed.  The pFrom->z field is not duplicated.  If
   781    777   ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
   782    778   ** and flags gets srcType (either MEM_Ephem or MEM_Static).
   783    779   */
................................................................................
   796    792   /*
   797    793   ** Make a full copy of pFrom into pTo.  Prior contents of pTo are
   798    794   ** freed before the copy is made.
   799    795   */
   800    796   int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
   801    797     int rc = SQLITE_OK;
   802    798   
   803         -  assert( pTo->db==pFrom->db );
          799  +  /* The pFrom==0 case in the following assert() is when an sqlite3_value
          800  +  ** from sqlite3_value_dup() is used as the argument
          801  +  ** to sqlite3_result_value(). */
          802  +  assert( pTo->db==pFrom->db || pFrom->db==0 );
   804    803     assert( (pFrom->flags & MEM_RowSet)==0 );
   805    804     if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
   806    805     memcpy(pTo, pFrom, MEMCELLSIZE);
   807    806     pTo->flags &= ~MEM_Dyn;
   808    807     if( pTo->flags&(MEM_Str|MEM_Blob) ){
   809    808       if( 0==(pFrom->flags&MEM_Static) ){
   810    809         pTo->flags |= MEM_Ephem;