a417f0
Subject: [PATCH] Limit the "precision" of floating-point to text conversions
a417f0
 in the printf() function to 100,000,000.
a417f0
a417f0
---
a417f0
 src/printf.c     | 12 ++++++++++++
a417f0
 test/printf.test | 16 +++++++++++++---
a417f0
 2 files changed, 25 insertions(+), 3 deletions(-)
a417f0
a417f0
diff --git a/src/printf.c b/src/printf.c
a417f0
index 7bce83f..260bf79 100644
a417f0
--- a/src/printf.c
a417f0
+++ b/src/printf.c
a417f0
@@ -165,6 +165,13 @@ static char *getTextArg(PrintfArguments *p){
a417f0
 #endif
a417f0
 #define etBUFSIZE SQLITE_PRINT_BUF_SIZE  /* Size of the output buffer */
a417f0
 
a417f0
+/*
a417f0
+** Hard limit on the precision of floating-point conversions.
a417f0
+*/
a417f0
+#ifndef SQLITE_PRINTF_PRECISION_LIMIT
a417f0
+# define SQLITE_FP_PRECISION_LIMIT 100000000
a417f0
+#endif
a417f0
+
a417f0
 /*
a417f0
 ** Render a string given by "fmt" into the StrAccum object.
a417f0
 */
a417f0
@@ -471,6 +478,11 @@ void sqlite3_str_vappendf(
a417f0
         length = 0;
a417f0
 #else
a417f0
         if( precision<0 ) precision = 6;         /* Set default precision */
a417f0
+#ifdef SQLITE_FP_PRECISION_LIMIT
a417f0
+        if( precision>SQLITE_FP_PRECISION_LIMIT ){
a417f0
+          precision = SQLITE_FP_PRECISION_LIMIT;
a417f0
+        }
a417f0
+#endif
a417f0
         if( realvalue<0.0 ){
a417f0
           realvalue = -realvalue;
a417f0
           prefix = '-';
a417f0
diff --git a/test/printf.test b/test/printf.test
a417f0
index d768898..a2b5e2a 100644
a417f0
--- a/test/printf.test
a417f0
+++ b/test/printf.test
a417f0
@@ -538,9 +538,11 @@ do_test printf-2.1.2.8 {
a417f0
 do_test printf-2.1.2.9 {
a417f0
   sqlite3_mprintf_double {abc: %d %d (%1.1g) :xyz} 1 1 1.0e-20
a417f0
 } {abc: 1 1 (1e-20) :xyz}
a417f0
-do_test printf-2.1.2.10 {
a417f0
-  sqlite3_mprintf_double {abc: %*.*f}  2000000000 1000000000 1.0e-20
a417f0
-} {abc: }
a417f0
+if {$SQLITE_MAX_LENGTH<=[expr 1000*1000*1000]} {
a417f0
+  do_test printf-2.1.2.10 {
a417f0
+    sqlite3_mprintf_double {abc: %*.*f}  2000000000 1000000000 1.0e-20
a417f0
+  } {}
a417f0
+}
a417f0
 do_test printf-2.1.3.1 {
a417f0
   sqlite3_mprintf_double {abc: (%*.*f) :xyz} 1 1 1.0
a417f0
 } {abc: (1.0) :xyz}
a417f0
@@ -3777,4 +3779,12 @@ foreach ::iRepeat {0 1} {
a417f0
   }
a417f0
 }
a417f0
 
a417f0
+# 2020-05-23
a417f0
+# ticket 23439ea582241138
a417f0
+#
a417f0
+do_execsql_test printf-16.1 {
a417f0
+  SELECT printf('%.*g',2147483647,0.01);
a417f0
+} {0.01}
a417f0
+
a417f0
+
a417f0
 finish_test
a417f0
-- 
a417f0
2.24.1
a417f0