From: drh <> Date: Wed, 21 Dec 2022 19:11:56 +0000 (+0000) Subject: More efficient implementation of large precisions on the "%c" format for X-Git-Tag: version-3.41.0~211 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=79b9bc4452fd844a9fe9a729521631c42fd3af55;p=thirdparty%2Fsqlite.git More efficient implementation of large precisions on the "%c" format for the built-in printf(). This is an effort to avoid a reported timeout on a (ridiculous) query generated by OSSFuzz. FossilOrigin-Name: 371f9b88387a44a5f820279d79733d1deb7eafc72f320ec47a11679bbdbb49ef --- diff --git a/manifest b/manifest index 4b376c7d96..cf2db3b182 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Ensure\sthat\sthe\sexpression\sof\sa\svirtual\scolumn\sreally\sis\san\sexpression\sand\nnot\sjust\sa\sreference\sto\sanother\scolumn,\sas\sa\sreal\sexpression\sis\snecessary\sfor\nthe\sindexed\sexpression\scoverage\soptimization\sto\swork\sproperly.\n[forum:/forumpost/07b36e3899a9ae21|Forum\sthread\s07b36e3899a9ae21]. -D 2022-12-21T14:13:49.983 +C More\sefficient\simplementation\sof\slarge\sprecisions\son\sthe\s"%c"\sformat\sfor\nthe\sbuilt-in\sprintf().\s\sThis\sis\san\seffort\sto\savoid\sa\sreported\ntimeout\son\sa\s(ridiculous)\squery\sgenerated\sby\sOSSFuzz. +D 2022-12-21T19:11:56.600 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -598,7 +598,7 @@ F src/delete.c 86573edae75e3d3e9a8b590d87db8e47222103029df4f3e11fa56044459b514e F src/expr.c 204af6a83c191f5ac19ec4af6ecc546f188cc2dd1c76fc5280982f710ec4b9c4 F src/fault.c 460f3e55994363812d9d60844b2a6de88826e007 F src/fkey.c 722f20779f5342a787922deded3628d8c74b5249cab04098cf17ee2f2aaff002 -F src/func.c 7e86074afc4dc702691a29b7801f6dcc191db092b52e8bbe69dcd2f7be52194d +F src/func.c f6b7f49f4097461701311ff0126010e17239273cdcaaef9c3b85114f2e07a2d4 F src/global.c e06ff8e0acd85aec13563c9ecb44fbbf38232ccf73594998fd880b92d619594b F src/hash.c 8d7dda241d0ebdafb6ffdeda3149a412d7df75102cecfc1021c98d6219823b19 F src/hash.h 3340ab6e1d13e725571d7cee6d3e3135f0779a7d8e76a9ce0a85971fa3953c51 @@ -641,7 +641,7 @@ F src/pcache1.c dee95e3cd2b61e6512dc814c5ab76d5eb36f0bfc9441dbb4260fccc0d12bbddc F src/pragma.c 894c2621d35edd4beea9b331cfdb1b42032394420074d2294c8febe548eea8a1 F src/pragma.h e690a356c18e98414d2e870ea791c1be1545a714ba623719deb63f7f226d8bb7 F src/prepare.c 9ebd3a1b12bbd1951f0d6db850f32cf5d4547a6ab8bb9e958d75dfbe4e60d0a3 -F src/printf.c e99ee9741e79ae3873458146f59644276657340385ade4e76a5f5d1c25793764 +F src/printf.c ff4b05e38bf928ff1b80d3dda4f977b10fe39ecbfe69c018224c7e5594fb2455 F src/random.c 606b00941a1d7dd09c381d3279a058d771f406c5213c9932bbd93d5587be4b9c F src/resolve.c efea4e5fbecfd6d0a9071b0be0d952620991673391b6ffaaf4c277b0bb674633 F src/rowset.c ba9515a922af32abe1f7d39406b9d35730ed65efab9443dc5702693b60854c92 @@ -650,7 +650,7 @@ F src/shell.c.in 24e0c75947dd8a3426473d90dfc4887f42553c8b57dff02a6865f04c5efcf86 F src/sqlite.h.in e752f82b9d71f1d42b259b1900e4b1caf0965e844d756cd5cc91cc2cf45ed925 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8 F src/sqlite3ext.h c4b9fa7a7e2bcdf850cfeb4b8a91d5ec47b7a00033bc996fd2ee96cbf2741f5f -F src/sqliteInt.h 3a1e92597b2f34a770f349b80008b28647cc1ddfe72d8dba5594ebaeae3f85c3 +F src/sqliteInt.h da23a13868fc76ca8e16931c825f62b778754055d52448e46d6620e766e0af34 F src/sqliteLimit.h d7323ffea5208c6af2734574bae933ca8ed2ab728083caa117c9738581a31657 F src/status.c 160c445d7d28c984a0eae38c144f6419311ed3eace59b44ac6dafc20db4af749 F src/table.c 0f141b58a16de7e2fbe81c308379e7279f4c6b50eb08efeec5892794a0ba30d1 @@ -2067,8 +2067,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 4dc438a951bdbe27fbd5ee7cfb907e30ec0efbf8ce02109f18997cc221e0ad3b -R 03059136b81a57fc846508773f65ad38 +P 40549bacb3923e439627b0103bedd7da30258b69a46960040f7176e060f51f2f +R b8948321b04d25266ba13687cc331ab6 U drh -Z ac3f8ecd0e415b369dc994efeb14a19d +Z fb87aea17e86619f82d9de43213241b3 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index d70b0a9302..3a6089430d 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -40549bacb3923e439627b0103bedd7da30258b69a46960040f7176e060f51f2f \ No newline at end of file +371f9b88387a44a5f820279d79733d1deb7eafc72f320ec47a11679bbdbb49ef \ No newline at end of file diff --git a/src/func.c b/src/func.c index 3c5a852307..70ad3d5ca5 100644 --- a/src/func.c +++ b/src/func.c @@ -1082,7 +1082,7 @@ void sqlite3QuoteValue(StrAccum *pStr, sqlite3_value *pValue){ } case SQLITE_BLOB: { char const *zBlob = sqlite3_value_blob(pValue); - int nBlob = sqlite3_value_bytes(pValue); + i64 nBlob = sqlite3_value_bytes(pValue); assert( zBlob==sqlite3_value_blob(pValue) ); /* No encoding change */ sqlite3StrAccumEnlarge(pStr, nBlob*2 + 4); if( pStr->accError==0 ){ diff --git a/src/printf.c b/src/printf.c index 3602e1fcb9..335ad06844 100644 --- a/src/printf.c +++ b/src/printf.c @@ -736,13 +736,26 @@ void sqlite3_str_vappendf( } } if( precision>1 ){ + i64 nPrior = 1; width -= precision-1; if( width>1 && !flag_leftjustify ){ sqlite3_str_appendchar(pAccum, width-1, ' '); width = 0; } - while( precision-- > 1 ){ - sqlite3_str_append(pAccum, buf, length); + sqlite3_str_append(pAccum, buf, length); + precision--; + while( precision > 1 ){ + i64 nCopyBytes; + if( nPrior > precision-1 ) nPrior = precision - 1; + nCopyBytes = length*nPrior; + if( nCopyBytes + pAccum->nChar >= pAccum->nAlloc ){ + sqlite3StrAccumEnlarge(pAccum, nCopyBytes); + } + if( pAccum->accError ) break; + sqlite3_str_append(pAccum, + &pAccum->zText[pAccum->nChar-nCopyBytes], nCopyBytes); + precision -= nPrior; + nPrior *= 2; } } bufpt = buf; @@ -970,9 +983,9 @@ void sqlite3RecordErrorOffsetOfExpr(sqlite3 *db, const Expr *pExpr){ ** Return the number of bytes of text that StrAccum is able to accept ** after the attempted enlargement. The value returned might be zero. */ -int sqlite3StrAccumEnlarge(StrAccum *p, int N){ +int sqlite3StrAccumEnlarge(StrAccum *p, i64 N){ char *zNew; - assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */ + assert( p->nChar+N >= p->nAlloc ); /* Only called if really needed */ if( p->accError ){ testcase(p->accError==SQLITE_TOOBIG); testcase(p->accError==SQLITE_NOMEM); @@ -983,8 +996,7 @@ int sqlite3StrAccumEnlarge(StrAccum *p, int N){ return p->nAlloc - p->nChar - 1; }else{ char *zOld = isMalloced(p) ? p->zText : 0; - i64 szNew = p->nChar; - szNew += (sqlite3_int64)N + 1; + i64 szNew = p->nChar + N + 1; if( szNew+p->nChar<=p->mxAlloc ){ /* Force exponential buffer size growth as long as it does not overflow, ** to avoid having to call this routine too often */ @@ -1014,7 +1026,8 @@ int sqlite3StrAccumEnlarge(StrAccum *p, int N){ return 0; } } - return N; + assert( N>=0 && N<=0x7fffffff ); + return (int)N; } /* diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 3d92c81d70..cdf447b710 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -5214,7 +5214,7 @@ int sqlite3ApiExit(sqlite3 *db, int); int sqlite3OpenTempDatabase(Parse *); void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int); -int sqlite3StrAccumEnlarge(StrAccum*, int); +int sqlite3StrAccumEnlarge(StrAccum*, i64); char *sqlite3StrAccumFinish(StrAccum*); void sqlite3StrAccumSetError(StrAccum*, u8); void sqlite3ResultStrAccum(sqlite3_context*,StrAccum*);