From: drh Date: Sat, 21 Feb 2004 19:02:30 +0000 (+0000) Subject: Cleanup the printf code to make it smaller and more modular. X-Git-Tag: version-3.6.10~4810 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5f968436e6b8b467cf3ccb293eae012711da8005;p=thirdparty%2Fsqlite.git Cleanup the printf code to make it smaller and more modular. Fix a memory leak in the new OP_ContextPush opcode. (CVS 1258) FossilOrigin-Name: 2756f7af3382fa9d186ab99cf76f469fb891a3c3 --- diff --git a/manifest b/manifest index 19c980e982..902096e674 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Flag\spragmas\slike\svdbe_trace\snow\sreturn\stheir\scurrent\ssetting\sif\sthey\nare\scalled\swith\sno\sarguments.\s(CVS\s1257) -D 2004-02-21T14:00:29 +C Cleanup\sthe\sprintf\scode\sto\smake\sit\ssmaller\sand\smore\smodular.\r\nFix\sa\smemory\sleak\sin\sthe\snew\sOP_ContextPush\sopcode.\s(CVS\s1258) +D 2004-02-21T19:02:30 F Makefile.in cfd75c46b335881999333a9e4b982fa8491f200b F Makefile.linux-gcc b86a99c493a5bfb402d1d9178dcdc4bd4b32f906 F README f1de682fbbd94899d50aca13d387d1b3fd3be2dd @@ -45,27 +45,27 @@ F src/pager.c 29ddad4dd454f0aaa98e2bcd327710ab9f02f833 F src/pager.h 82332878799280145639a48d88cdb4058925e3f6 F src/parse.y 226bbdba2dee362d4b1cacc424bd82f7740071ee F src/pragma.c a8d43661193ba3114da787f43969d0a34f0ed07c -F src/printf.c ef1838bd06246d5d323600dc592d337b1c0762b0 +F src/printf.c 2a8bf0fa0dcc1cab62067da3b7e984d56527fe40 F src/random.c 775913e0b7fbd6295d21f12a7bd35b46387c44b2 F src/select.c 9a41dace754f0dab5e991e402c05fa3c24d04f19 F src/shell.c c3d3404fa82bb0808444fda9884d1bb572fd18b9 F src/sqlite.h.in 64f016cd5ce190643a0f47760188fdf4e0b2227e -F src/sqliteInt.h c647413de0ab8f6e70a1972126da6ed2a84fb5cc +F src/sqliteInt.h de32ca5481b9ffc30c3ec0d9ff97b505eaa2f016 F src/table.c d845cb101b5afc1f7fea083c99e3d2fa7998d895 F src/tclsqlite.c b84dafe3a8532ff534c36e96bd38880e4b9cedf3 -F src/test1.c 56e9a156df3ad5e4e98df776776e963effc727f7 +F src/test1.c ec7b13dec8faf33b47551b9e9f4e2e0e1a8b122f F src/test2.c 75819b0f2c63c6a0fd6995445881f2eb94036996 F src/test3.c 30985ebdfaf3ee1462a9b0652d3efbdc8d9798f5 F src/test4.c dcbbbb382626fd466a7c46907f74db35fc8bad64 F src/tokenize.c 6676b946fd8825b67ab52140af4fdc57a70bda48 F src/trigger.c a9927b57c865b6f3df3fb5e40c9824d722660ded F src/update.c e6eed1a4a429cc28f57533365c72293794c904cf -F src/util.c 64995b5949a5d377629ffd2598747bc771cade1e +F src/util.c 8fbed02d3d5d29d4b236772f779778599c911e21 F src/vacuum.c d9e80c2b36ee1f623dbf1bdf3cedad24a23f87ac F src/vdbe.c f14e8e2ef82cb8480394697c40644d70195598e5 F src/vdbe.h b1b22ffca48f593d375fd846c583679d49c2e5c9 F src/vdbeInt.h b40ff02ce39fd076e6ff3369e19c1bbfe1986682 -F src/vdbeaux.c 2268af6441a55461ec80e5d8f57d77267cd46423 +F src/vdbeaux.c 88ebe3aa963478f3cee9fb6e3d8ca9db1bafacb2 F src/where.c 1302d728bd338c237e6a8282e4e3eadbbdf11e45 F test/all.test 569a92a8ee88f5300c057cc4a8f50fbbc69a3242 F test/attach.test ba8261d38da6b6a7d4f78ec543c548c4418582ef @@ -189,7 +189,7 @@ F www/sqlite.tcl 3c83b08cf9f18aa2d69453ff441a36c40e431604 F www/tclsqlite.tcl b9271d44dcf147a93c98f8ecf28c927307abd6da F www/vdbe.tcl 9b9095d4495f37697fd1935d10e14c6015e80aa1 F www/whentouse.tcl a8335bce47cc2fddb07f19052cb0cb4d9129a8e4 -P 8e3eda2a909bd80b2b14ace36ab44303750a4409 -R a43c4de14dbe7097b5ca3dff718096d1 +P 6a5fb5b89a98307060bb4a92a499b5d3dba74553 +R 23ba520dcd63d47bd6ccb825a335f506 U drh -Z 74241342cf04355015de5f00644a1b7e +Z af3bc531c9f40f1ba9bd7423a0477add diff --git a/manifest.uuid b/manifest.uuid index cacabcf658..7c8520c195 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -6a5fb5b89a98307060bb4a92a499b5d3dba74553 \ No newline at end of file +2756f7af3382fa9d186ab99cf76f469fb891a3c3 \ No newline at end of file diff --git a/src/printf.c b/src/printf.c index cf00c5b2f0..d0785d1d77 100644 --- a/src/printf.c +++ b/src/printf.c @@ -52,13 +52,6 @@ */ #include "sqliteInt.h" -/* -** Undefine COMPATIBILITY to make some slight changes in the way things -** work. I think the changes are an improvement, but they are not -** backwards compatible. -*/ -/* #define COMPATIBILITY / * Compatible with SUN OS 4.1 */ - /* ** Conversion types fall into various categories as defined by the ** following enumeration. @@ -78,6 +71,8 @@ #define etSQLESCAPE 12 /* Strings with '\'' doubled. %q */ #define etSQLESCAPE2 13 /* Strings with '\'' doubled and enclosed in '', NULL pointers replaced by SQL NULL. %Q */ +#define etTOKEN 14 /* a pointer to a Token structure */ +#define etSRCLIST 15 /* a pointer to a SrcList */ /* @@ -129,6 +124,8 @@ static et_info fmtinfo[] = { { 'n', 0, 0, etSIZE, 0, 0 }, { '%', 0, 0, etPERCENT, 0, 0 }, { 'p', 10, 0, etRADIX, "0123456789", 0 }, + { 'T', 0, 2, etTOKEN, 0, 0 }, + { 'S', 0, 2, etSRCLIST, 0, 0 }, }; #define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0])) @@ -192,10 +189,11 @@ static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){ ** will run. */ static int vxprintf( - void (*func)(void*,char*,int), - void *arg, - const char *fmt, - va_list ap + void (*func)(void*,const char*,int), /* Consumer of text */ + void *arg, /* First argument to the consumer */ + int useExtended, /* Allow extended %-conversions */ + const char *fmt, /* Format string */ + va_list ap /* arguments */ ){ int c; /* Next character in the format string */ char *bufpt; /* Pointer to the conversion buffer */ @@ -284,10 +282,7 @@ static int vxprintf( c = *++fmt; if( c=='*' ){ precision = va_arg(ap,int); -#ifndef etCOMPATIBILITY - /* This is sensible, but SUN OS 4.1 doesn't do it. */ if( precision<0 ) precision = -precision; -#endif c = *++fmt; }else{ while( c>='0' && c<='9' ){ @@ -313,7 +308,9 @@ static int vxprintf( for(idx=0; idxtype; + if( useExtended || (infop->flags & FLAG_INTERN)==0 ){ + xtype = infop->type; + } break; } } @@ -341,7 +338,7 @@ static int vxprintf( case etRADIX: if( flag_long ) longvalue = va_arg(ap,long); else longvalue = va_arg(ap,int); -#ifdef etCOMPATIBILITY +#if 1 /* For the format %#x, the value zero is printed "0" not "0x0". ** I think this is stupid. */ if( longvalue==0 ) flag_alternateform = 0; @@ -403,7 +400,7 @@ static int vxprintf( } if( infop->type==etGENERIC && precision>0 ) precision--; rounder = 0.0; -#ifdef COMPATIBILITY +#if 0 /* Rounding works like BSD when the constant 0.4999 is used. Wierd! */ for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1); #else @@ -571,6 +568,25 @@ static int vxprintf( if( precision>=0 && precisionz, pToken->n); + length = width = 0; + break; + } + case etSRCLIST: { + SrcList *pSrc = va_arg(ap, SrcList*); + int k = va_arg(ap, int); + struct SrcList_item *pItem = &pSrc->a[k]; + assert( k>=0 && knSrc ); + if( pItem->zDatabase && pItem->zDatabase[0] ){ + (*func)(arg, pItem->zDatabase, strlen(pItem->zDatabase)); + (*func)(arg, ".", 1); + } + (*func)(arg, pItem->zName, strlen(pItem->zName)); + length = width = 0; + break; + } case etERROR: buf[0] = '%'; buf[1] = c; @@ -615,11 +631,7 @@ static int vxprintf( } } if( zExtra ){ - if( xtype==etDYNSTRING ){ - free(zExtra); - }else{ - sqliteFree(zExtra); - } + sqliteFree(zExtra); } }/* End for loop over the format string */ return errorflag ? -1 : count; @@ -633,7 +645,9 @@ struct sgMprintf { char *zBase; /* A base allocation */ char *zText; /* The string collected so far */ int nChar; /* Length of the string so far */ + int nTotal; /* Output size if unconstrained */ int nAlloc; /* Amount of space allocated in zText */ + void *(*xRealloc)(void*,int); /* Function used to realloc memory */ }; /* @@ -642,24 +656,25 @@ struct sgMprintf { ** This routine add nNewChar characters of text in zNewText to ** the sgMprintf structure pointed to by "arg". */ -static void mout(void *arg, char *zNewText, int nNewChar){ +static void mout(void *arg, const char *zNewText, int nNewChar){ struct sgMprintf *pM = (struct sgMprintf*)arg; + pM->nTotal += nNewChar; if( pM->nChar + nNewChar + 1 > pM->nAlloc ){ - pM->nAlloc = pM->nChar + nNewChar*2 + 1; - if( pM->zText==pM->zBase ){ - pM->zText = sqliteMalloc(pM->nAlloc); - if( pM->zText && pM->nChar ) memcpy(pM->zText,pM->zBase,pM->nChar); + if( pM->xRealloc==0 ){ + nNewChar = pM->nAlloc - pM->nChar - 1; }else{ - char *z = sqliteRealloc(pM->zText, pM->nAlloc); - if( z==0 ){ - sqliteFree(pM->zText); - pM->nChar = 0; - pM->nAlloc = 0; + pM->nAlloc = pM->nChar + nNewChar*2 + 1; + if( pM->zText==pM->zBase ){ + pM->zText = pM->xRealloc(0, pM->nAlloc); + if( pM->zText && pM->nChar ){ + memcpy(pM->zText, pM->zBase, pM->nChar); + } + }else{ + pM->zText = pM->xRealloc(pM->zText, pM->nAlloc); } - pM->zText = z; } } - if( pM->zText ){ + if( pM->zText && nNewChar>0 ){ memcpy(&pM->zText[pM->nChar], zNewText, nNewChar); pM->nChar += nNewChar; pM->zText[pM->nChar] = 0; @@ -667,114 +682,104 @@ static void mout(void *arg, char *zNewText, int nNewChar){ } /* -** sqlite_mprintf() works like printf(), but allocations memory to hold the -** resulting string and returns a pointer to the allocated memory. Use -** sqliteFree() to release the memory allocated. +** This routine is a wrapper around xprintf() that invokes mout() as +** the consumer. +*/ +static char *base_vprintf( + void *(*xRealloc)(void*,int), /* Routine to realloc memory. May be NULL */ + int useInternal, /* Use internal %-conversions if true */ + char *zInitBuf, /* Initially write here, before mallocing */ + int nInitBuf, /* Size of zInitBuf[] */ + const char *zFormat, /* format string */ + va_list ap /* arguments */ +){ + struct sgMprintf sM; + sM.zBase = sM.zText = zInitBuf; + sM.nChar = sM.nTotal = 0; + sM.nAlloc = nInitBuf; + sM.xRealloc = xRealloc; + vxprintf(mout, &sM, useInternal, zFormat, ap); + if( xRealloc ){ + if( sM.zText==sM.zBase ){ + sM.zText = xRealloc(0, sM.nChar+1); + memcpy(sM.zText, sM.zBase, sM.nChar+1); + }else if( sM.nAlloc>sM.nChar+10 ){ + sM.zText = xRealloc(sM.zText, sM.nChar+1); + } + } + return sM.zText; +} + +/* +** Realloc that is a real function, not a macro. +*/ +static void *printf_realloc(void *old, int size){ + return sqliteRealloc(old,size); +} + +/* +** Print into memory obtained from sqliteMalloc(). Use the internal +** %-conversion extensions. +*/ +char *sqliteVMPrintf(const char *zFormat, va_list ap){ + char zBase[1000]; + return base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap); +} + +/* +** Print into memory obtained from sqliteMalloc(). Use the internal +** %-conversion extensions. */ char *sqliteMPrintf(const char *zFormat, ...){ va_list ap; - struct sgMprintf sMprintf; - char zBuf[200]; - - sMprintf.nChar = 0; - sMprintf.nAlloc = sizeof(zBuf); - sMprintf.zText = zBuf; - sMprintf.zBase = zBuf; - va_start(ap,zFormat); - vxprintf(mout,&sMprintf,zFormat,ap); + char *z; + char zBase[1000]; + va_start(ap, zFormat); + z = base_vprintf(printf_realloc, 1, zBase, sizeof(zBase), zFormat, ap); va_end(ap); - sMprintf.zText[sMprintf.nChar] = 0; - return sqliteRealloc(sMprintf.zText, sMprintf.nChar+1); + return z; } /* -** sqlite_mprintf() works like printf(), but allocations memory to hold the -** resulting string and returns a pointer to the allocated memory. Use -** sqliteFree() to release the memory allocated. +** Print into memory obtained from malloc(). Do not use the internal +** %-conversion extensions. This routine is for use by external users. */ char *sqlite_mprintf(const char *zFormat, ...){ va_list ap; - struct sgMprintf sMprintf; - char *zNew; + char *z; char zBuf[200]; - sMprintf.nChar = 0; - sMprintf.nAlloc = sizeof(zBuf); - sMprintf.zText = zBuf; - sMprintf.zBase = zBuf; va_start(ap,zFormat); - vxprintf(mout,&sMprintf,zFormat,ap); + z = base_vprintf((void*(*)(void*,int))realloc, 0, + zBuf, sizeof(zBuf), zFormat, ap); va_end(ap); - sMprintf.zText[sMprintf.nChar] = 0; - zNew = malloc( sMprintf.nChar+1 ); - if( zNew ) strcpy(zNew,sMprintf.zText); - if( sMprintf.zText!=sMprintf.zBase ){ - sqliteFree(sMprintf.zText); - } - return zNew; + return z; } /* This is the varargs version of sqlite_mprintf. */ char *sqlite_vmprintf(const char *zFormat, va_list ap){ - struct sgMprintf sMprintf; - char *zNew; char zBuf[200]; - sMprintf.nChar = 0; - sMprintf.zText = zBuf; - sMprintf.nAlloc = sizeof(zBuf); - sMprintf.zBase = zBuf; - vxprintf(mout,&sMprintf,zFormat,ap); - sMprintf.zText[sMprintf.nChar] = 0; - zNew = malloc( sMprintf.nChar+1 ); - if( zNew ) strcpy(zNew,sMprintf.zText); - if( sMprintf.zText!=sMprintf.zBase ){ - sqliteFree(sMprintf.zText); - } - return zNew; -} - -/* -** This function implements the callback from vxprintf. -** -** This routine add nNewChar characters of text in zNewText to -** the sgMprintf structure pointed to by "arg". Unlike mout() above, -** this routine does not allocate new space when the buffer fills. -** It just truncates. -*/ -static void sout(void *arg, char *zNewText, int nNewChar){ - struct sgMprintf *pM = (struct sgMprintf*)arg; - if( pM->nChar + nNewChar + 1 > pM->nAlloc ){ - nNewChar = pM->nAlloc - pM->nChar - 1; - if( nNewChar<=0 ) return; - } - memcpy(&pM->zText[pM->nChar], zNewText, nNewChar); - pM->nChar += nNewChar; - pM->zText[pM->nChar] = 0; + return base_vprintf((void*(*)(void*,int))realloc, 0, + zBuf, sizeof(zBuf), zFormat, ap); } /* -** sqlite_sprintf() works like sprintf() except that it ignores the +** sqlite_snprintf() works like snprintf() except that it ignores the ** current locale settings. This is important for SQLite because we ** are not able to use a "," as the decimal point in place of "." as ** specified by some locales. */ -int sqlite_snprintf(int n, char *zBuf, const char *zFormat, ...){ +char *sqlite_snprintf(int n, char *zBuf, const char *zFormat, ...){ + char *z; va_list ap; - struct sgMprintf sMprintf; - sMprintf.nChar = 0; - sMprintf.nAlloc = n; - sMprintf.zText = zBuf; - sMprintf.zBase = zBuf; va_start(ap,zFormat); - vxprintf(sout,&sMprintf,zFormat,ap); + z = base_vprintf(0, 0, zBuf, n, zFormat, ap); va_end(ap); - return sMprintf.nChar; + return z; } - - /* ** The following four routines implement the varargs versions of the ** sqlite_exec() and sqlite_get_table() interfaces. See the sqlite.h diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 0fa6d29147..2baeab5e97 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -11,7 +11,7 @@ ************************************************************************* ** Internal interface definitions for SQLite. ** -** @(#) $Id: sqliteInt.h,v 1.216 2004/02/21 13:31:10 drh Exp $ +** @(#) $Id: sqliteInt.h,v 1.217 2004/02/21 19:02:30 drh Exp $ */ #include "config.h" #include "sqlite.h" @@ -182,6 +182,7 @@ typedef unsigned INTPTR_TYPE uptr; /* Big enough to hold a pointer */ # define sqliteStrNDup(X,Y) sqliteStrNDup_(X,Y,__FILE__,__LINE__) void sqliteStrRealloc(char**); #else +# define sqliteRealloc_(X,Y) sqliteRealloc(X,Y) # define sqliteStrRealloc(X) #endif @@ -1111,7 +1112,8 @@ void sqliteRealToSortable(double r, char *); char *sqliteStrNDup(const char*, int); # define sqliteCheckMemory(a,b) #endif -char *sqliteMPrintf(const char *,...); +char *sqliteMPrintf(const char*, ...); +char *sqliteVMPrintf(const char*, va_list); void sqliteSetString(char **, const char *, ...); void sqliteSetNString(char **, ...); void sqliteErrorMsg(Parse*, const char*, ...); @@ -1256,5 +1258,5 @@ int sqliteFixExpr(DbFixer*, Expr*); int sqliteFixExprList(DbFixer*, ExprList*); int sqliteFixTriggerStep(DbFixer*, TriggerStep*); double sqliteAtoF(const char *z); -int sqlite_snprintf(int,char*,const char*,...); +char *sqlite_snprintf(int,char*,const char*,...); int sqliteFitsIn32Bits(const char *); diff --git a/src/test1.c b/src/test1.c index f0ebf8cdf1..99d1a994f7 100644 --- a/src/test1.c +++ b/src/test1.c @@ -13,7 +13,7 @@ ** is not included in the SQLite library. It is used for automated ** testing of the SQLite library. ** -** $Id: test1.c,v 1.33 2004/02/08 18:07:35 drh Exp $ +** $Id: test1.c,v 1.34 2004/02/21 19:02:30 drh Exp $ */ #include "sqliteInt.h" #include "tcl.h" @@ -180,10 +180,10 @@ static int test_mprintf_z( int i; for(i=2; i @@ -417,120 +417,11 @@ void sqliteSetNString(char **pz, ...){ */ void sqliteErrorMsg(Parse *pParse, const char *zFormat, ...){ va_list ap; - int nByte; - int i, j; - char *z; - static char zNull[] = "NULL"; - pParse->nErr++; - nByte = 1 + strlen(zFormat); - va_start(ap, zFormat); - for(i=0; zFormat[i]; i++){ - if( zFormat[i]!='%' || zFormat[i+1]==0 ) continue; - i++; - switch( zFormat[i] ){ - case 'd': { - (void)va_arg(ap, int); - nByte += 20; - break; - } - case 'z': - case 's': { - char *z2 = va_arg(ap, char*); - if( z2==0 ) z2 = zNull; - nByte += strlen(z2); - break; - } - case 'T': { - Token *p = va_arg(ap, Token*); - nByte += p->n; - break; - } - case 'S': { - SrcList *p = va_arg(ap, SrcList*); - int k = va_arg(ap, int); - assert( p->nSrc>k && k>=0 ); - nByte += strlen(p->a[k].zName); - if( p->a[k].zDatabase && p->a[k].zDatabase[0] ){ - nByte += strlen(p->a[k].zDatabase)+1; - } - break; - } - default: { - nByte++; - break; - } - } - } - va_end(ap); - z = sqliteMalloc( nByte ); - if( z==0 ) return; sqliteFree(pParse->zErrMsg); - pParse->zErrMsg = z; va_start(ap, zFormat); - for(i=j=0; zFormat[i]; i++){ - if( zFormat[i]!='%' || zFormat[i+1]==0 ) continue; - if( i>j ){ - memcpy(z, &zFormat[j], i-j); - z += i-j; - } - j = i+2; - i++; - switch( zFormat[i] ){ - case 'd': { - int x = va_arg(ap, int); - sprintf(z, "%d", x); - z += strlen(z); - break; - } - case 'z': - case 's': { - int len; - char *z2 = va_arg(ap, char*); - if( z2==0 ) z2 = zNull; - len = strlen(z2); - memcpy(z, z2, len); - z += len; - if( zFormat[i]=='z' && z2!=zNull ){ - sqliteFree(z2); - } - break; - } - case 'T': { - Token *p = va_arg(ap, Token*); - memcpy(z, p->z, p->n); - z += p->n; - break; - } - case 'S': { - int len; - SrcList *p = va_arg(ap, SrcList*); - int k = va_arg(ap, int); - assert( p->nSrc>k && k>=0 ); - if( p->a[k].zDatabase && p->a[k].zDatabase[0] ){ - len = strlen(p->a[k].zDatabase); - memcpy(z, p->a[k].zDatabase, len); - z += len; - *(z++) = '.'; - } - len = strlen(p->a[k].zName); - memcpy(z, p->a[k].zName, len); - z += len; - break; - } - default: { - *(z++) = zFormat[i]; - break; - } - } - } + pParse->zErrMsg = sqliteVMPrintf(zFormat, ap); va_end(ap); - if( i>j ){ - memcpy(z, &zFormat[j], i-j); - z += i-j; - } - assert( (z - pParse->zErrMsg) < nByte ); - *z = 0; } /* diff --git a/src/vdbeaux.c b/src/vdbeaux.c index ea1e8cd4a2..f7ba9a77ab 100644 --- a/src/vdbeaux.c +++ b/src/vdbeaux.c @@ -787,6 +787,8 @@ static void Cleanup(Vdbe *p){ p->keylistStackDepth = 0; p->keylistStack = 0; } + sqliteFree(p->contextStack); + p->contextStack = 0; sqliteFree(p->zErrMsg); p->zErrMsg = 0; }