]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Improved detection and reporting of errors in the readfile() extension SQL
authordrh <drh@noemail.net>
Fri, 11 Jan 2019 17:20:25 +0000 (17:20 +0000)
committerdrh <drh@noemail.net>
Fri, 11 Jan 2019 17:20:25 +0000 (17:20 +0000)
function.

FossilOrigin-Name: d2f0b5a483869d39f5c5814c9d6df3d3b8a46d582b4dcf0cf11e23b707c4213e

ext/misc/fileio.c
manifest
manifest.uuid

index 2219aafa0d3db7bd0f0e35846082ad5ae78225b2..34fa0b92c8de5f178e929aa45d7e628a2d2bfca3 100644 (file)
@@ -121,22 +121,47 @@ SQLITE_EXTENSION_INIT1
 
 /*
 ** Set the result stored by context ctx to a blob containing the 
-** contents of file zName.
+** contents of file zName.  Or, leave the result unchanged (NULL)
+** if the file does not exist or is unreadable.
+**
+** If the file exceeds the SQLite blob size limit, through an
+** SQLITE_TOOBIG error.
+**
+** Throw an SQLITE_IOERR if there are difficulties pulling the file
+** off of disk.
 */
 static void readFileContents(sqlite3_context *ctx, const char *zName){
   FILE *in;
-  long nIn;
+  sqlite3_int64 nIn;
   void *pBuf;
+  sqlite3 *db;
+  int mxBlob;
 
   in = fopen(zName, "rb");
-  if( in==0 ) return;
+  if( in==0 ){
+    /* File does not exist or is unreadable. Leave the result set to NULL. */
+    return;
+  }
   fseek(in, 0, SEEK_END);
   nIn = ftell(in);
   rewind(in);
+  db = sqlite3_context_db_handle(ctx);
+  mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
+  if( nIn>mxBlob ){
+    sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
+    fclose(in);
+    return;
+  }
   pBuf = sqlite3_malloc( nIn );
-  if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
+  if( pBuf==0 ){
+    sqlite3_result_error_nomem(ctx);
+    fclose(in);
+    return;
+  }
+  if( 1==fread(pBuf, nIn, 1, in) ){
     sqlite3_result_blob(ctx, pBuf, nIn, sqlite3_free);
   }else{
+    sqlite3_result_error_code(ctx, SQLITE_IOERR);
     sqlite3_free(pBuf);
   }
   fclose(in);
index ed09660150c259871842ff809f31794decae783d..5ba485eeb97cdd0d51ee897add41d1cb47378204 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Omit\serrors\sabout\smissing\sSAVEPOINTs\swhen\saborting\sthe\s.archive\scommand\nin\sthe\sCLI.
-D 2019-01-11T17:19:59.217
+C Improved\sdetection\sand\sreporting\sof\serrors\sin\sthe\sreadfile()\sextension\sSQL\nfunction.
+D 2019-01-11T17:20:25.808
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F Makefile.in 45a3fef4d325ac0220c2172aeec4e4321da351f073f3b8e8ddea655f49ef6f2b
@@ -285,7 +285,7 @@ F ext/misc/csv.c 7f047aeb68f5802e7ce6639292095d622a488bb43526ed04810e0649faa71ce
 F ext/misc/dbdump.c baf6e37447c9d6968417b1cd34cbedb0b0ab3f91b5329501d8a8d5be3287c336
 F ext/misc/eval.c 4b4757592d00fd32e44c7a067e6a0e4839c81a4d57abc4131ee7806d1be3104e
 F ext/misc/explain.c d5c12962d79913ef774b297006872af1fccda388f61a11d37758f9179a09551f
-F ext/misc/fileio.c 03ba86d5b3d5c88977a63907de2941ea7723f1930f5f547056f0aff1bf228a25
+F ext/misc/fileio.c 801d2ac9faff0d7d59c1f595dc26d32920a793f4291d81322b1837d0b3e160f0
 F ext/misc/fuzzer.c 9e79c337faffdd4c5fe4485467537438359b43e0858a40038d4300b894ff553f
 F ext/misc/ieee754.c f190d0cc5182529acb15babd177781be1ac1718c
 F ext/misc/json1.c 8af4672f43634257dbcfdb4515b4070325463d67c6968b4be1bd414de28d4d58
@@ -1797,7 +1797,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P fa47f4c6589c431cf678560ac33dea6b695052012bea2096b2c92869ed51c688
-R 254a1d7a527962cc4514f37b0edd1520
+P 2a47387ba6aa3c294607b7641aa1c4cf70a7b27a861e1098c2f79a38e5b7036a
+R 129c4eddb5f6e683d9d87be4a27619ca
 U drh
-Z 38fa272dba858ffd290fec63f7de6047
+Z 79baa7f42e4c59f65e60be610fef78d9
index 3dc0f9fce63d1cc405f0bbfb25b5661000de91df..5f136bcdf5745590ee8a2c2a86515b3b5c21f9c4 100644 (file)
@@ -1 +1 @@
-2a47387ba6aa3c294607b7641aa1c4cf70a7b27a861e1098c2f79a38e5b7036a
\ No newline at end of file
+d2f0b5a483869d39f5c5814c9d6df3d3b8a46d582b4dcf0cf11e23b707c4213e
\ No newline at end of file