Skip to content

Commit fe98081

Browse files
author
drh
committed
Enhance sqlite3_randomness(N,P) such that it resets the internal PRNG
if N is less than 1. Subsequent calls to sqlite3_randomness() will reinitialize the internal PRNG by calling the xRandomness() method of the default VFS. FossilOrigin-Name: a221aa82bb5496885fd0bf76e4601443799511de
1 parent 3312348 commit fe98081

6 files changed

Lines changed: 26 additions & 21 deletions

File tree

manifest

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
C Add\s-I.\sto\sthe\smain.mk\smakefile\sfor\sbuilding\ssqlite3.o\sand\sspeedtest1.
2-
D 2013-12-24T12:04:24.811
1+
C Enhance\ssqlite3_randomness(N,P)\ssuch\sthat\sit\sresets\sthe\sinternal\sPRNG\nif\sN\sis\sless\sthan\s1.\s\sSubsequent\scalls\sto\ssqlite3_randomness()\swill\sreinitialize\nthe\sinternal\sPRNG\sby\scalling\sthe\sxRandomness()\smethod\sof\sthe\sdefault\sVFS.
2+
D 2014-01-01T14:00:13.905
33
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
44
F Makefile.in 2ef13430cd359f7b361bb863504e227b25cc7f81
55
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -188,7 +188,7 @@ F src/journal.c b4124532212b6952f42eb2c12fa3c25701d8ba8d
188188
F src/legacy.c 0df0b1550b9cc1f58229644735e317ac89131f12
189189
F src/lempar.c cdf0a000315332fc9b50b62f3b5e22e080a0952b
190190
F src/loadext.c 867c7b330b740c6c917af9956b13b81d0a048303
191-
F src/main.c 5307601c58b6e08153a15846098399a59c166d8d
191+
F src/main.c 37f55de2000f6c882414ad3e1bcdb751c531fddd
192192
F src/malloc.c 0203ebce9152c6a0e5de520140b8ba65187350be
193193
F src/mem0.c 6a55ebe57c46ca1a7d98da93aaa07f99f1059645
194194
F src/mem1.c c0c990fcaddff810ea277b4fb5d9138603dd5d4b
@@ -216,15 +216,15 @@ F src/pcache1.c 57fee9a9a617218f5037afbbe49b09da65bde56b
216216
F src/pragma.c 5ab7279d132143feb77f773688a24ab05da75fd7
217217
F src/prepare.c 677521ab7132615a8a26107a1d1c3132f44ae337
218218
F src/printf.c 85d07756e45d7496d19439dcae3e6e9e0090f269
219-
F src/random.c 0b2dbc37fdfbfa6bd455b091dfcef5bdb32dba68
219+
F src/random.c d10c1f85b6709ca97278428fd5db5bbb9c74eece
220220
F src/resolve.c 7eda9097b29fcf3d2b42fdc17d1de672134e09b6
221221
F src/rowset.c 64655f1a627c9c212d9ab497899e7424a34222e0
222222
F src/select.c 819bb090c9a348d17f69f136cad2bfa9ee9cbb41
223223
F src/shell.c a3541193d5fce37e91dad8ef46a9505aa7c9b344
224-
F src/sqlite.h.in 4ef56464aeaa3785a2c5ca37fb3a0fb229d68b2e
224+
F src/sqlite.h.in d94a8b89522f526ba711182ee161e06f8669bcc9
225225
F src/sqlite3.rc 11094cc6a157a028b301a9f06b3d03089ea37c3e
226226
F src/sqlite3ext.h 886f5a34de171002ad46fae8c36a7d8051c190fc
227-
F src/sqliteInt.h fdb8c1e4bc5424ad82a9394a845781abf0d7d849
227+
F src/sqliteInt.h 62664868458b53a815380733f059ff9d92cb08be
228228
F src/sqliteLimit.h 164b0e6749d31e0daa1a4589a169d31c0dec7b3d
229229
F src/status.c 7ac05a5c7017d0b9f0b4bcd701228b784f987158
230230
F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e
@@ -1147,7 +1147,7 @@ F tool/vdbe-compress.tcl 0cf56e9263a152b84da86e75a5c0cdcdb7a47891
11471147
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
11481148
F tool/warnings.sh d1a6de74685f360ab718efda6265994b99bbea01
11491149
F tool/win/sqlite.vsix 030f3eeaf2cb811a3692ab9c14d021a75ce41fff
1150-
P c289a253c0c053ac8fc344efe138262c327d8096
1151-
R 2bb3d3295c5bbe58e528a8069b6e67b9
1150+
P cc72c5aec7fe93d4a1368517aab949dc98d33003
1151+
R 4dbb82094f79fe324b8594c5661b9532
11521152
U drh
1153-
Z 2be15cf525011fea6a70c12bcaf2ac37
1153+
Z 7f33812905741a2d8942dba59b43dd66

manifest.uuid

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
cc72c5aec7fe93d4a1368517aab949dc98d33003
1+
a221aa82bb5496885fd0bf76e4601443799511de

src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3101,7 +3101,7 @@ int sqlite3_test_control(int op, ...){
31013101
** to the xRandomness method of the default VFS.
31023102
*/
31033103
case SQLITE_TESTCTRL_PRNG_RESET: {
3104-
sqlite3PrngResetState();
3104+
sqlite3_randomness(0,0);
31053105
break;
31063106
}
31073107

src/random.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ void sqlite3_randomness(int N, void *pBuf){
5252
sqlite3_mutex_enter(mutex);
5353
#endif
5454

55+
if( N<=0 ){
56+
wsdPrng.isInit = 0;
57+
sqlite3_mutex_leave(mutex);
58+
return;
59+
}
60+
5561
/* Initialize the state of the random number generator once,
5662
** the first time this routine is called. The seed value does
5763
** not need to contain a lot of randomness since we are not
@@ -79,15 +85,16 @@ void sqlite3_randomness(int N, void *pBuf){
7985
wsdPrng.isInit = 1;
8086
}
8187

82-
while( N-- ){
88+
assert( N>0 );
89+
do{
8390
wsdPrng.i++;
8491
t = wsdPrng.s[wsdPrng.i];
8592
wsdPrng.j += t;
8693
wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
8794
wsdPrng.s[wsdPrng.j] = t;
8895
t += wsdPrng.s[wsdPrng.i];
8996
*(zBuf++) = wsdPrng.s[t];
90-
}
97+
}while( --N );
9198
sqlite3_mutex_leave(mutex);
9299
}
93100

@@ -116,7 +123,4 @@ void sqlite3PrngRestoreState(void){
116123
sizeof(sqlite3Prng)
117124
);
118125
}
119-
void sqlite3PrngResetState(void){
120-
GLOBAL(struct sqlite3PrngType, sqlite3Prng).isInit = 0;
121-
}
122126
#endif /* SQLITE_OMIT_BUILTIN_TEST */

src/sqlite.h.in

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2395,11 +2395,13 @@ sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
23952395
** applications to access the same PRNG for other purposes.
23962396
**
23972397
** ^A call to this routine stores N bytes of randomness into buffer P.
2398+
** ^If N is less than one, then P can be a NULL pointer.
23982399
**
2399-
** ^The first time this routine is invoked (either internally or by
2400-
** the application) the PRNG is seeded using randomness obtained
2401-
** from the xRandomness method of the default [sqlite3_vfs] object.
2402-
** ^On all subsequent invocations, the pseudo-randomness is generated
2400+
** ^If this routine has not been previously called or if the previous
2401+
** call had N less than one, then the PRNG is seeded using randomness
2402+
** obtained from the xRandomness method of the default [sqlite3_vfs] object.
2403+
** ^If the previous call to this routine had an N of 1 or more then
2404+
** the pseudo-randomness is generated
24032405
** internally and without recourse to the [sqlite3_vfs] xRandomness
24042406
** method.
24052407
*/

src/sqliteInt.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2972,7 +2972,6 @@ int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
29722972
Vdbe *sqlite3GetVdbe(Parse*);
29732973
void sqlite3PrngSaveState(void);
29742974
void sqlite3PrngRestoreState(void);
2975-
void sqlite3PrngResetState(void);
29762975
void sqlite3RollbackAll(sqlite3*,int);
29772976
void sqlite3CodeVerifySchema(Parse*, int);
29782977
void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);

0 commit comments

Comments
 (0)