/ Check-in [7952c322]
Login

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

Overview
Comment:Cleanup of the sqlite3StrAccumInit() function. No functionality changes.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 7952c32268aa650d9ee946d5bfe190f712e3bbe6
User & Date: drh 2015-05-02 11:45:53
Context
2015-05-02
12:40
Reorganize some multi-threaded code in vdbesort.c so that full MC/DC test coverage does not depend on the outcome of a race condition. check-in: 78c7ec95 user: dan tags: trunk
11:45
Cleanup of the sqlite3StrAccumInit() function. No functionality changes. check-in: 7952c322 user: drh tags: trunk
2015-05-01
20:34
Enhance the fuzzershell --uniquecases option to output results in order of increasing runtime and to include the runtime in the comment separator of the output. check-in: 04630b98 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/btree.c.

  8895   8895     if( !sCheck.aPgRef ){
  8896   8896       *pnErr = 1;
  8897   8897       sqlite3BtreeLeave(p);
  8898   8898       return 0;
  8899   8899     }
  8900   8900     i = PENDING_BYTE_PAGE(pBt);
  8901   8901     if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
  8902         -  sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
  8903         -  sCheck.errMsg.useMalloc = 2;
         8902  +  sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
  8904   8903   
  8905   8904     /* Check the integrity of the freelist
  8906   8905     */
  8907   8906     sCheck.zPfx = "Main freelist: ";
  8908   8907     checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
  8909   8908               get4byte(&pBt->pPage1->aData[36]));
  8910   8909     sCheck.zPfx = 0;

Changes to src/build.c.

  4020   4020     Index *pIdx       /* The index that triggers the constraint */
  4021   4021   ){
  4022   4022     char *zErr;
  4023   4023     int j;
  4024   4024     StrAccum errMsg;
  4025   4025     Table *pTab = pIdx->pTable;
  4026   4026   
  4027         -  sqlite3StrAccumInit(&errMsg, 0, 0, 200);
  4028         -  errMsg.db = pParse->db;
         4027  +  sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200);
  4029   4028     for(j=0; j<pIdx->nKeyCol; j++){
  4030   4029       char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
  4031   4030       if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2);
  4032   4031       sqlite3StrAccumAppendAll(&errMsg, pTab->zName);
  4033   4032       sqlite3StrAccumAppend(&errMsg, ".", 1);
  4034   4033       sqlite3StrAccumAppendAll(&errMsg, zCol);
  4035   4034     }

Changes to src/func.c.

   228    228     int argc,
   229    229     sqlite3_value **argv
   230    230   ){
   231    231     PrintfArguments x;
   232    232     StrAccum str;
   233    233     const char *zFormat;
   234    234     int n;
          235  +  sqlite3 *db = sqlite3_context_db_handle(context);
   235    236   
   236    237     if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){
   237    238       x.nArg = argc-1;
   238    239       x.nUsed = 0;
   239    240       x.apArg = argv+1;
   240         -    sqlite3StrAccumInit(&str, 0, 0, SQLITE_MAX_LENGTH);
   241         -    str.db = sqlite3_context_db_handle(context);
          241  +    sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
   242    242       sqlite3XPrintf(&str, SQLITE_PRINTF_SQLFUNC, zFormat, &x);
   243    243       n = str.nChar;
   244    244       sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n,
   245    245                           SQLITE_DYNAMIC);
   246    246     }
   247    247   }
   248    248   
................................................................................
  1563   1563     int nVal, nSep;
  1564   1564     assert( argc==1 || argc==2 );
  1565   1565     if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
  1566   1566     pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
  1567   1567   
  1568   1568     if( pAccum ){
  1569   1569       sqlite3 *db = sqlite3_context_db_handle(context);
  1570         -    int firstTerm = pAccum->useMalloc==0;
  1571         -    pAccum->useMalloc = 2;
         1570  +    int firstTerm = pAccum->mxAlloc==0;
  1572   1571       pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
  1573   1572       if( !firstTerm ){
  1574   1573         if( argc==2 ){
  1575   1574           zSep = (char*)sqlite3_value_text(argv[1]);
  1576   1575           nSep = sqlite3_value_bytes(argv[1]);
  1577   1576         }else{
  1578   1577           zSep = ",";

Changes to src/printf.c.

   134    134   }
   135    135   #endif /* SQLITE_OMIT_FLOATING_POINT */
   136    136   
   137    137   /*
   138    138   ** Set the StrAccum object to an error mode.
   139    139   */
   140    140   static void setStrAccumError(StrAccum *p, u8 eError){
          141  +  assert( eError==STRACCUM_NOMEM || eError==STRACCUM_TOOBIG );
   141    142     p->accError = eError;
   142    143     p->nAlloc = 0;
   143    144   }
   144    145   
   145    146   /*
   146    147   ** Extra argument values from a PrintfArguments object
   147    148   */
................................................................................
   751    752     char *zNew;
   752    753     assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */
   753    754     if( p->accError ){
   754    755       testcase(p->accError==STRACCUM_TOOBIG);
   755    756       testcase(p->accError==STRACCUM_NOMEM);
   756    757       return 0;
   757    758     }
   758         -  if( !p->useMalloc ){
          759  +  if( p->mxAlloc==0 ){
   759    760       N = p->nAlloc - p->nChar - 1;
   760    761       setStrAccumError(p, STRACCUM_TOOBIG);
   761    762       return N;
   762    763     }else{
   763    764       char *zOld = (p->zText==p->zBase ? 0 : p->zText);
   764    765       i64 szNew = p->nChar;
   765    766       szNew += N + 1;
................................................................................
   771    772       if( szNew > p->mxAlloc ){
   772    773         sqlite3StrAccumReset(p);
   773    774         setStrAccumError(p, STRACCUM_TOOBIG);
   774    775         return 0;
   775    776       }else{
   776    777         p->nAlloc = (int)szNew;
   777    778       }
   778         -    if( p->useMalloc==1 ){
          779  +    if( p->db ){
   779    780         zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
   780    781       }else{
   781    782         zNew = sqlite3_realloc64(zOld, p->nAlloc);
   782    783       }
   783    784       if( zNew ){
   784    785         assert( p->zText!=0 || p->nChar==0 );
   785    786         if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
................................................................................
   851    852   ** Finish off a string by making sure it is zero-terminated.
   852    853   ** Return a pointer to the resulting string.  Return a NULL
   853    854   ** pointer if any kind of error was encountered.
   854    855   */
   855    856   char *sqlite3StrAccumFinish(StrAccum *p){
   856    857     if( p->zText ){
   857    858       p->zText[p->nChar] = 0;
   858         -    if( p->useMalloc && p->zText==p->zBase ){
   859         -      if( p->useMalloc==1 ){
   860         -        p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
   861         -      }else{
   862         -        p->zText = sqlite3_malloc64(p->nChar+1);
   863         -      }
          859  +    if( p->mxAlloc>0 && p->zText==p->zBase ){
          860  +      p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
   864    861         if( p->zText ){
   865    862           memcpy(p->zText, p->zBase, p->nChar+1);
   866    863         }else{
   867    864           setStrAccumError(p, STRACCUM_NOMEM);
   868    865         }
   869    866       }
   870    867     }
................................................................................
   872    869   }
   873    870   
   874    871   /*
   875    872   ** Reset an StrAccum string.  Reclaim all malloced memory.
   876    873   */
   877    874   void sqlite3StrAccumReset(StrAccum *p){
   878    875     if( p->zText!=p->zBase ){
   879         -    if( p->useMalloc==1 ){
   880         -      sqlite3DbFree(p->db, p->zText);
   881         -    }else{
   882         -      sqlite3_free(p->zText);
   883         -    }
          876  +    sqlite3DbFree(p->db, p->zText);
   884    877     }
   885    878     p->zText = 0;
   886    879   }
   887    880   
   888    881   /*
   889         -** Initialize a string accumulator
          882  +** Initialize a string accumulator.
          883  +**
          884  +** p:     The accumulator to be initialized.
          885  +** db:    Pointer to a database connection.  May be NULL.  Lookaside
          886  +**        memory is used if not NULL. db->mallocFailed is set appropriately
          887  +**        when not NULL.
          888  +** zBase: An initial buffer.  May be NULL in which case the initial buffer
          889  +**        is malloced.
          890  +** n:     Size of zBase in bytes.  If total space requirements never exceed
          891  +**        n then no memory allocations ever occur.
          892  +** mx:    Maximum number of bytes to accumulate.  If mx==0 then no memory
          893  +**        allocations will ever occur.
   890    894   */
   891         -void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
          895  +void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){
   892    896     p->zText = p->zBase = zBase;
   893         -  p->db = 0;
          897  +  p->db = db;
   894    898     p->nChar = 0;
   895    899     p->nAlloc = n;
   896    900     p->mxAlloc = mx;
   897         -  p->useMalloc = 1;
   898    901     p->accError = 0;
   899    902   }
   900    903   
   901    904   /*
   902    905   ** Print into memory obtained from sqliteMalloc().  Use the internal
   903    906   ** %-conversion extensions.
   904    907   */
   905    908   char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
   906    909     char *z;
   907    910     char zBase[SQLITE_PRINT_BUF_SIZE];
   908    911     StrAccum acc;
   909    912     assert( db!=0 );
   910         -  sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
          913  +  sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase),
   911    914                         db->aLimit[SQLITE_LIMIT_LENGTH]);
   912         -  acc.db = db;
   913    915     sqlite3VXPrintf(&acc, SQLITE_PRINTF_INTERNAL, zFormat, ap);
   914    916     z = sqlite3StrAccumFinish(&acc);
   915    917     if( acc.accError==STRACCUM_NOMEM ){
   916    918       db->mallocFailed = 1;
   917    919     }
   918    920     return z;
   919    921   }
................................................................................
   963    965       (void)SQLITE_MISUSE_BKPT;
   964    966       return 0;
   965    967     }
   966    968   #endif
   967    969   #ifndef SQLITE_OMIT_AUTOINIT
   968    970     if( sqlite3_initialize() ) return 0;
   969    971   #endif
   970         -  sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
   971         -  acc.useMalloc = 2;
          972  +  sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
   972    973     sqlite3VXPrintf(&acc, 0, zFormat, ap);
   973    974     z = sqlite3StrAccumFinish(&acc);
   974    975     return z;
   975    976   }
   976    977   
   977    978   /*
   978    979   ** Print into memory obtained from sqlite3_malloc()().  Omit the internal
................................................................................
  1009   1010   #ifdef SQLITE_ENABLE_API_ARMOR
  1010   1011     if( zBuf==0 || zFormat==0 ) {
  1011   1012       (void)SQLITE_MISUSE_BKPT;
  1012   1013       if( zBuf ) zBuf[0] = 0;
  1013   1014       return zBuf;
  1014   1015     }
  1015   1016   #endif
  1016         -  sqlite3StrAccumInit(&acc, zBuf, n, 0);
  1017         -  acc.useMalloc = 0;
         1017  +  sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
  1018   1018     sqlite3VXPrintf(&acc, 0, zFormat, ap);
  1019   1019     return sqlite3StrAccumFinish(&acc);
  1020   1020   }
  1021   1021   char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
  1022   1022     char *z;
  1023   1023     va_list ap;
  1024   1024     va_start(ap,zFormat);
................................................................................
  1036   1036   ** allocate memory because it might be called while the memory allocator
  1037   1037   ** mutex is held.
  1038   1038   */
  1039   1039   static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
  1040   1040     StrAccum acc;                          /* String accumulator */
  1041   1041     char zMsg[SQLITE_PRINT_BUF_SIZE*3];    /* Complete log message */
  1042   1042   
  1043         -  sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0);
  1044         -  acc.useMalloc = 0;
         1043  +  sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
  1045   1044     sqlite3VXPrintf(&acc, 0, zFormat, ap);
  1046   1045     sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
  1047   1046                              sqlite3StrAccumFinish(&acc));
  1048   1047   }
  1049   1048   
  1050   1049   /*
  1051   1050   ** Format and write a message to the log if logging is enabled.
................................................................................
  1065   1064   ** The printf() built into some versions of windows does not understand %lld
  1066   1065   ** and segfaults if you give it a long long int.
  1067   1066   */
  1068   1067   void sqlite3DebugPrintf(const char *zFormat, ...){
  1069   1068     va_list ap;
  1070   1069     StrAccum acc;
  1071   1070     char zBuf[500];
  1072         -  sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
  1073         -  acc.useMalloc = 0;
         1071  +  sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
  1074   1072     va_start(ap,zFormat);
  1075   1073     sqlite3VXPrintf(&acc, 0, zFormat, ap);
  1076   1074     va_end(ap);
  1077   1075     sqlite3StrAccumFinish(&acc);
  1078   1076     fprintf(stdout,"%s", zBuf);
  1079   1077     fflush(stdout);
  1080   1078   }
................................................................................
  1116   1114   /* Generate a single line of output for the tree, with a prefix that contains
  1117   1115   ** all the appropriate tree lines */
  1118   1116   void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
  1119   1117     va_list ap;
  1120   1118     int i;
  1121   1119     StrAccum acc;
  1122   1120     char zBuf[500];
  1123         -  sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
  1124         -  acc.useMalloc = 0;
         1121  +  sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
  1125   1122     if( p ){
  1126   1123       for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
  1127   1124         sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|   " : "    ", 4);
  1128   1125       }
  1129   1126       sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
  1130   1127     }
  1131   1128     va_start(ap, zFormat);

Changes to src/select.c.

  5535   5535       int i;
  5536   5536       pView = sqlite3TreeViewPush(pView, (n--)>0);
  5537   5537       sqlite3TreeViewLine(pView, "FROM");
  5538   5538       for(i=0; i<p->pSrc->nSrc; i++){
  5539   5539         struct SrcList_item *pItem = &p->pSrc->a[i];
  5540   5540         StrAccum x;
  5541   5541         char zLine[100];
  5542         -      sqlite3StrAccumInit(&x, zLine, sizeof(zLine), 0);
  5543         -      x.useMalloc = 0;
         5542  +      sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
  5544   5543         sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor);
  5545   5544         if( pItem->zDatabase ){
  5546   5545           sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName);
  5547   5546         }else if( pItem->zName ){
  5548   5547           sqlite3XPrintf(&x, 0, " %s", pItem->zName);
  5549   5548         }
  5550   5549         if( pItem->pTab ){

Changes to src/sqliteInt.h.

  2831   2831   */
  2832   2832   struct StrAccum {
  2833   2833     sqlite3 *db;         /* Optional database for lookaside.  Can be NULL */
  2834   2834     char *zBase;         /* A base allocation.  Not from malloc. */
  2835   2835     char *zText;         /* The string collected so far */
  2836   2836     int  nChar;          /* Length of the string so far */
  2837   2837     int  nAlloc;         /* Amount of space allocated in zText */
  2838         -  int  mxAlloc;        /* Maximum allowed string length */
  2839         -  u8   useMalloc;      /* 0: none,  1: sqlite3DbMalloc,  2: sqlite3_malloc */
         2838  +  int  mxAlloc;        /* Maximum allowed allocation.  0 for no malloc usage */
  2840   2839     u8   accError;       /* STRACCUM_NOMEM or STRACCUM_TOOBIG */
  2841   2840   };
  2842   2841   #define STRACCUM_NOMEM   1
  2843   2842   #define STRACCUM_TOOBIG  2
  2844   2843   
  2845   2844   /*
  2846   2845   ** A pointer to this structure is used to communicate information
................................................................................
  3590   3589     void (*)(sqlite3_context*,int,sqlite3_value **),
  3591   3590     void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
  3592   3591     FuncDestructor *pDestructor
  3593   3592   );
  3594   3593   int sqlite3ApiExit(sqlite3 *db, int);
  3595   3594   int sqlite3OpenTempDatabase(Parse *);
  3596   3595   
  3597         -void sqlite3StrAccumInit(StrAccum*, char*, int, int);
         3596  +void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
  3598   3597   void sqlite3StrAccumAppend(StrAccum*,const char*,int);
  3599   3598   void sqlite3StrAccumAppendAll(StrAccum*,const char*);
  3600   3599   void sqlite3AppendChar(StrAccum*,int,char);
  3601   3600   char *sqlite3StrAccumFinish(StrAccum*);
  3602   3601   void sqlite3StrAccumReset(StrAccum*);
  3603   3602   void sqlite3SelectDestInit(SelectDest*,int,int);
  3604   3603   Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);

Changes to src/vdbetrace.c.

    80     80     int nToken;              /* Length of the parameter token */
    81     81     int i;                   /* Loop counter */
    82     82     Mem *pVar;               /* Value of a host parameter */
    83     83     StrAccum out;            /* Accumulate the output here */
    84     84     char zBase[100];         /* Initial working space */
    85     85   
    86     86     db = p->db;
    87         -  sqlite3StrAccumInit(&out, zBase, sizeof(zBase), 
           87  +  sqlite3StrAccumInit(&out, db, zBase, sizeof(zBase), 
    88     88                         db->aLimit[SQLITE_LIMIT_LENGTH]);
    89         -  out.db = db;
    90     89     if( db->nVdbeExec>1 ){
    91     90       while( *zRawSql ){
    92     91         const char *zStart = zRawSql;
    93     92         while( *(zRawSql++)!='\n' && *zRawSql );
    94     93         sqlite3StrAccumAppend(&out, "-- ", 3);
    95     94         assert( (zRawSql - zStart) > 0 );
    96     95         sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));

Changes to src/where.c.

  3099   3099       flags = pLoop->wsFlags;
  3100   3100       if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0;
  3101   3101   
  3102   3102       isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
  3103   3103               || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
  3104   3104               || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
  3105   3105   
  3106         -    sqlite3StrAccumInit(&str, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
  3107         -    str.db = db;
         3106  +    sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
  3108   3107       sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
  3109   3108       if( pItem->pSelect ){
  3110   3109         sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId);
  3111   3110       }else{
  3112   3111         sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName);
  3113   3112       }
  3114   3113