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