776ca3
From eca47c8481b0c2f09a7818ed2bce0ad27b1dae27 Mon Sep 17 00:00:00 2001
776ca3
From: Ondrej Dubaj <odubaj@redhat.com>
776ca3
Date: Wed, 26 Jun 2019 12:25:10 +0200
776ca3
Subject: [PATCH] Fixed out of bounds heap read in function rtreenode()
776ca3
776ca3
    Enhance the rtreenode() function of rtree (used for
776ca3
    testing) so that it uses the newer sqlite3_str object
776ca3
    for better performance and improved error reporting.
776ca3
    Test cases added to TH3.
776ca3
776ca3
    Resolves: #1723338
776ca3
    Version: 3.26.0-4
776ca3
---
776ca3
 ext/rtree/rtree.c | 35 ++++++++++++++++-------------------
776ca3
 1 file changed, 16 insertions(+), 19 deletions(-)
776ca3
776ca3
diff --git a/ext/rtree/rtree.c b/ext/rtree/rtree.c
776ca3
index 4b044cb..87d0de0 100644
776ca3
--- a/ext/rtree/rtree.c
776ca3
+++ b/ext/rtree/rtree.c
776ca3
@@ -3711,49 +3711,46 @@ rtreeInit_fail:
776ca3
 ** <num-dimension>*2 coordinates.
776ca3
 */
776ca3
 static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
776ca3
-  char *zText = 0;
776ca3
   RtreeNode node;
776ca3
   Rtree tree;
776ca3
   int ii;
776ca3
+  int nData;
776ca3
+  int errCode;
776ca3
+  sqlite3_str *pOut;
776ca3
 
776ca3
   UNUSED_PARAMETER(nArg);
776ca3
   memset(&node, 0, sizeof(RtreeNode));
776ca3
   memset(&tree, 0, sizeof(Rtree));
776ca3
   tree.nDim = (u8)sqlite3_value_int(apArg[0]);
776ca3
+  if( tree.nDim<1 || tree.nDim>5 ) return;
776ca3
   tree.nDim2 = tree.nDim*2;
776ca3
   tree.nBytesPerCell = 8 + 8 * tree.nDim;
776ca3
   node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
776ca3
+  nData = sqlite3_value_bytes(apArg[1]);
776ca3
+  if( nData<4 ) return;
776ca3
+  if( nData
776ca3
 
776ca3
+  pOut = sqlite3_str_new(0);
776ca3
   for(ii=0; ii
776ca3
-    char zCell[512];
776ca3
-    int nCell = 0;
776ca3
     RtreeCell cell;
776ca3
     int jj;
776ca3
 
776ca3
     nodeGetCell(&tree, &node, ii, &cell);
776ca3
-    sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
776ca3
-    nCell = (int)strlen(zCell);
776ca3
+    if( ii>0 ) sqlite3_str_append(pOut, " ", 1);
776ca3
+    sqlite3_str_appendf(pOut, "{%lld", cell.iRowid);
776ca3
     for(jj=0; jj
776ca3
 #ifndef SQLITE_RTREE_INT_ONLY
776ca3
-      sqlite3_snprintf(512-nCell,&zCell[nCell], " %g",
776ca3
-                       (double)cell.aCoord[jj].f);
776ca3
+      sqlite3_str_appendf(pOut, " %g", (double)cell.aCoord[jj].f);
776ca3
 #else
776ca3
-      sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
776ca3
-                       cell.aCoord[jj].i);
776ca3
+      sqlite3_str_appendf(pOut, " %d", cell.aCoord[jj].i);
776ca3
 #endif
776ca3
-      nCell = (int)strlen(zCell);
776ca3
-    }
776ca3
-
776ca3
-    if( zText ){
776ca3
-      char *zTextNew = sqlite3_mprintf("%s {%s}", zText, zCell);
776ca3
-      sqlite3_free(zText);
776ca3
-      zText = zTextNew;
776ca3
-    }else{
776ca3
-      zText = sqlite3_mprintf("{%s}", zCell);
776ca3
     }
776ca3
+    sqlite3_str_append(pOut, "}", 1);
776ca3
   }
776ca3
   
776ca3
-  sqlite3_result_text(ctx, zText, -1, sqlite3_free);
776ca3
+  errCode = sqlite3_str_errcode(pOut);
776ca3
+  sqlite3_result_text(ctx, sqlite3_str_finish(pOut), -1, sqlite3_free);
776ca3
+  sqlite3_result_error_code(ctx, errCode);
776ca3
 }
776ca3
 
776ca3
 /* This routine implements an SQL function that returns the "depth" parameter
776ca3
-- 
776ca3
2.19.1
776ca3