From: drh <> Date: Fri, 10 Apr 2026 16:06:09 +0000 (+0000) Subject: Fix the printf() optimization added on 2026-03-29 so that X-Git-Tag: version-3.53.1~23 X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=916f74e48522426875b4b17cb9f9c9ba01a6e557;p=thirdparty%2Fsqlite.git Fix the printf() optimization added on 2026-03-29 so that sqlite3_snprintf() does not incorrectly truncate floating-point conversions. FossilOrigin-Name: 7c6883ecd3b284f38bfa93b03653fb48eaa42f223ae7ab12e1ad03082f3db44b --- diff --git a/manifest b/manifest index 0306a91612..f0513d46fa 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Correct\sshell\sresult\scode\spropagation\sregression\sintroduced\sin\s[ff084ae341eab5c4]\sand\sreported\sin\s[forum:6fa3247e9724844c|forum\spost\s6fa3247e9724844c]. -D 2026-04-10T08:39:51.122 +C Fix\sthe\sprintf()\soptimization\sadded\son\s2026-03-29\sso\sthat\nsqlite3_snprintf()\sdoes\snot\sincorrectly\struncate\sfloating-point\nconversions. +D 2026-04-10T16:06:09.834 F .fossil-settings/binary-glob 61195414528fb3ea9693577e1980230d78a1f8b0a54c78cf1b9b24d0a409ed6a x F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea @@ -730,7 +730,7 @@ F src/pcache.h 092b758d2c5e4dabb30eae46d8dfad77c0f70b16bf3ff1943f7a232b0fe0d4ba F src/pcache1.c 131ca0daf4e66b4608d2945ae76d6ed90de3f60539afbd5ef9ec65667a5f2fcd F src/pragma.c 789ef67117b74b5be0a2db6681f7f0c55e6913791b9da309aefd280de2c8a74d F src/prepare.c f6a6e28a281bd1d1da12f47d370a81af46159b40f73bf7fa0b276b664f9c8b7d -F src/printf.c 41fb76fcb5ed7e16aaddc659d3b23891abebea45549fe125fc2e6ec380cc7175 +F src/printf.c 1a36bbae9b90883a2fb0538d507a0ad442bd9ad5d09f70611497f4545fcb9c18 F src/random.c 606b00941a1d7dd09c381d3279a058d771f406c5213c9932bbd93d5587be4b9c F src/resolve.c 928ff887f2a7c64275182060d94d06fdddbe32226c569781cf7e7edc6f58d7fd F src/rowset.c 8432130e6c344b3401a8874c3cb49fefe6873fec593294de077afea2dce5ec97 @@ -2197,12 +2197,9 @@ F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee F tool/warnings.sh a554d13f6e5cf3760f041b87939e3d616ec6961859c3245e8ef701d1eafc2ca2 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f F tool/winmain.c 00c8fb88e365c9017db14c73d3c78af62194d9644feaf60e220ab0f411f3604c -P 4525003a53a7fc63ca75c59b22c79608659ca12f0131f52c18637f829977f20b -Q +a76682a3b1d6c5e8ca16caf8afbe545c9ae39849fe21ba68d4cddae683a409b5 -R d0079594d8c2ed9694e8861b1becf746 -T *branch * branch-3.53 -T *sym-branch-3.53 * -T -sym-trunk * -U stephan -Z 690258fba0f78f9dd35af2160ed308f1 +P 5306da4231efcaf1bd7afa3cdcfcb87edf70958e3dd2edaed765a0d844e3be03 +Q +a50521a16068e555aa08ee25726b081bb4cd33e3ea388b82dcbaa691c2576284 +R 3f255d0b50c5242ee76a6c73d61196c9 +U drh +Z b69208482818fba880a973c6690f8ab0 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 6899ac93d6..edd83ead3e 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -5306da4231efcaf1bd7afa3cdcfcb87edf70958e3dd2edaed765a0d844e3be03 +7c6883ecd3b284f38bfa93b03653fb48eaa42f223ae7ab12e1ad03082f3db44b diff --git a/src/printf.c b/src/printf.c index c9fc1a72c0..42952723d4 100644 --- a/src/printf.c +++ b/src/printf.c @@ -623,11 +623,27 @@ void sqlite3_str_vappendf( szBufNeeded = MAX(e2,0)+(i64)precision+(i64)width+10; if( cThousand && e2>0 ) szBufNeeded += (e2+2)/3; - if( sqlite3StrAccumEnlargeIfNeeded(pAccum, szBufNeeded) ){ - width = length = 0; - break; + if( szBufNeeded + pAccum->nChar >= pAccum->nAlloc ){ + if( pAccum->mxAlloc==0 && pAccum->accError==0 ){ + /* Unable to allocate space in pAccum, perhaps because it + ** is coming from sqlite3_snprintf() or similar. We'll have + ** to render into temporary space and the memcpy() it over. */ + bufpt = sqlite3DbMallocRaw(pAccum->db, szBufNeeded); + if( bufpt==0 ){ + sqlite3StrAccumSetError(pAccum, SQLITE_NOMEM); + return; + } + zExtra = bufpt; + }else if( sqlite3StrAccumEnlarge(pAccum, szBufNeeded)zText + pAccum->nChar; + } + }else{ + bufpt = pAccum->zText + pAccum->nChar; } - bufpt = zOut = pAccum->zText + pAccum->nChar; + zOut = bufpt; flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2; /* The sign in front of the number */ @@ -728,14 +744,22 @@ void sqlite3_str_vappendf( } length = width; } - pAccum->nChar += length; - zOut[length] = 0; - - /* Floating point conversions render directly into the output - ** buffer. Hence, don't just break out of the switch(). Bypass the - ** output buffer writing that occurs after the switch() by continuing - ** to the next character in the format string. */ - continue; + + if( zExtra==0 ){ + /* The result is being rendered directory into pAccum. This + ** is the command and fast case */ + pAccum->nChar += length; + zOut[length] = 0; + continue; + }else{ + /* We were unable to render directly into pAccum because we + ** couldn't allocate sufficient memory. We need to memcpy() + ** the rendering (or some prefix thereof) into the output + ** buffer. */ + bufpt[0] = 0; + bufpt = zExtra; + break; + } } case etSIZE: if( !bArgList ){ @@ -782,7 +806,7 @@ void sqlite3_str_vappendf( if( sqlite3StrAccumEnlargeIfNeeded(pAccum, nCopyBytes) ){ break; } - sqlite3_str_append(pAccum, + sqlite3_str_append(pAccum, &pAccum->zText[pAccum->nChar-nCopyBytes], nCopyBytes); precision -= nPrior; nPrior *= 2;