Index: src/shell.c ================================================================== --- src/shell.c +++ src/shell.c @@ -2278,18 +2278,26 @@ /* Forward reference */ static int process_input(ShellState *p, FILE *in); - /* -** Read the content of a file into memory obtained from sqlite3_malloc64(). -** The caller is responsible for freeing the memory. +** Read the content of file zName into memory obtained from sqlite3_malloc64() +** and return a pointer to the buffer. The caller is responsible for freeing +** the memory. +** +** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes +** read. +** +** For convenience, a nul-terminator byte is always appended to the data read +** from the file before the buffer is returned. This byte is not included in +** the final value of (*pnByte), if applicable. ** -** NULL is returned if any error is encountered. +** NULL is returned if any error is encountered. The final value of *pnByte +** is undefined in this case. */ -static char *readFile(const char *zName){ +static char *readFile(const char *zName, int *pnByte){ FILE *in = fopen(zName, "rb"); long nIn; size_t nRead; char *pBuf; if( in==0 ) return 0; @@ -2303,10 +2311,11 @@ if( nRead!=1 ){ sqlite3_free(pBuf); return 0; } pBuf[nIn] = 0; + if( pnByte ) *pnByte = nIn; return pBuf; } /* ** Implementation of the "readfile(X)" SQL function. The entire content @@ -2318,16 +2327,17 @@ int argc, sqlite3_value **argv ){ const char *zName; void *pBuf; + int nBuf; UNUSED_PARAMETER(argc); zName = (const char*)sqlite3_value_text(argv[0]); if( zName==0 ) return; - pBuf = readFile(zName); - if( pBuf ) sqlite3_result_blob(context, pBuf, -1, sqlite3_free); + pBuf = readFile(zName, &nBuf); + if( pBuf ) sqlite3_result_blob(context, pBuf, nBuf, sqlite3_free); } /* ** Implementation of the "writefile(X,Y)" SQL function. The argument Y ** is written into file X. The number of bytes written is returned. Or @@ -3663,11 +3673,11 @@ char *zRes = 0; output_reset(p); if( nArg!=2 ){ raw_printf(stderr, "Usage: .check GLOB-PATTERN\n"); rc = 2; - }else if( (zRes = readFile("testcase-out.txt"))==0 ){ + }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){ raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n"); rc = 2; }else if( testcase_glob(azArg[1],zRes)==0 ){ utf8_printf(stderr, "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n", Index: test/shell6.test ================================================================== --- test/shell6.test +++ test/shell6.test @@ -6,10 +6,12 @@ # May you do good and not evil. # May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #*********************************************************************** +# +# Test the shell tool ".lint fkey-indexes" command. # set testdir [file dirname $argv0] source $testdir/tester.tcl set testprefix shell6 ADDED test/shell7.test Index: test/shell7.test ================================================================== --- /dev/null +++ test/shell7.test @@ -0,0 +1,54 @@ +# 2016 December 17 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# +# Test the readfile() function built into the shell tool. Specifically, +# that it does not truncate the blob read at the first embedded 0x00 +# byte. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set testprefix shell7 +set CLI [test_find_cli] + + +do_execsql_test 1.0 { + CREATE TABLE f1(tn INTEGER PRIMARY KEY, x BLOB); + CREATE TABLE f2(tn INTEGER PRIMARY KEY, x BLOB); + + INSERT INTO f1 VALUES(1, X'01020304'); + INSERT INTO f1 VALUES(2, X'01000304'); + INSERT INTO f1 VALUES(3, randomblob(200)); +} + +foreach {tn l x} [db eval { SELECT tn, length(x) AS l, x FROM f1 }] { + forcedelete shell7_test.bin + set fd [open shell7_test.bin w] + fconfigure $fd -encoding binary + fconfigure $fd -translation binary + puts -nonewline $fd $x + close $fd + + do_test 1.$tn.1 { file size shell7_test.bin } $l + do_test 1.$tn.2 { + catchcmd test.db "INSERT INTO f2 VALUES($tn, readfile('shell7_test.bin'));" + } {0 {}} + + do_execsql_test 1.$tn.3 { + SELECT (SELECT x FROM f1 WHERE tn=1)==(SELECT x FROM f2 WHERE tn=1) + } {1} +} + + + +finish_test + +