1 : /*
2 : ** 2001 September 15
3 : **
4 : ** The author disclaims copyright to this source code. In place of
5 : ** a legal notice, here is a blessing:
6 : **
7 : ** May you do good and not evil.
8 : ** May you find forgiveness for yourself and forgive others.
9 : ** May you share freely, never taking more than you give.
10 : **
11 : *************************************************************************
12 : ** Utility functions used throughout sqlite.
13 : **
14 : ** This file contains functions for allocating memory, comparing
15 : ** strings, and stuff like that.
16 : **
17 : ** $Id$
18 : */
19 : #include "sqliteInt.h"
20 : #include "os.h"
21 : #include <stdarg.h>
22 : #include <ctype.h>
23 :
24 : /*
25 : ** MALLOC WRAPPER ARCHITECTURE
26 : **
27 : ** The sqlite code accesses dynamic memory allocation/deallocation by invoking
28 : ** the following six APIs (which may be implemented as macros).
29 : **
30 : ** sqlite3Malloc()
31 : ** sqlite3MallocRaw()
32 : ** sqlite3Realloc()
33 : ** sqlite3ReallocOrFree()
34 : ** sqlite3Free()
35 : ** sqlite3AllocSize()
36 : **
37 : ** The function sqlite3FreeX performs the same task as sqlite3Free and is
38 : ** guaranteed to be a real function. The same holds for sqlite3MallocX
39 : **
40 : ** The above APIs are implemented in terms of the functions provided in the
41 : ** operating-system interface. The OS interface is never accessed directly
42 : ** by code outside of this file.
43 : **
44 : ** sqlite3OsMalloc()
45 : ** sqlite3OsRealloc()
46 : ** sqlite3OsFree()
47 : ** sqlite3OsAllocationSize()
48 : **
49 : ** Functions sqlite3MallocRaw() and sqlite3Realloc() may invoke
50 : ** sqlite3_release_memory() if a call to sqlite3OsMalloc() or
51 : ** sqlite3OsRealloc() fails (or if the soft-heap-limit for the thread is
52 : ** exceeded). Function sqlite3Malloc() usually invokes
53 : ** sqlite3MallocRaw().
54 : **
55 : ** MALLOC TEST WRAPPER ARCHITECTURE
56 : **
57 : ** The test wrapper provides extra test facilities to ensure the library
58 : ** does not leak memory and handles the failure of the underlying OS level
59 : ** allocation system correctly. It is only present if the library is
60 : ** compiled with the SQLITE_MEMDEBUG macro set.
61 : **
62 : ** * Guardposts to detect overwrites.
63 : ** * Ability to cause a specific Malloc() or Realloc() to fail.
64 : ** * Audit outstanding memory allocations (i.e check for leaks).
65 : */
66 :
67 : #define MAX(x,y) ((x)>(y)?(x):(y))
68 :
69 : #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) && !defined(SQLITE_OMIT_DISKIO)
70 : /*
71 : ** Set the soft heap-size limit for the current thread. Passing a negative
72 : ** value indicates no limit.
73 : */
74 : void sqlite3_soft_heap_limit(int n){
75 : ThreadData *pTd = sqlite3ThreadData();
76 : if( pTd ){
77 : pTd->nSoftHeapLimit = n;
78 : }
79 : sqlite3ReleaseThreadData();
80 : }
81 :
82 : /*
83 : ** Release memory held by SQLite instances created by the current thread.
84 : */
85 : int sqlite3_release_memory(int n){
86 : return sqlite3PagerReleaseMemory(n);
87 : }
88 : #else
89 : /* If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, then define a version
90 : ** of sqlite3_release_memory() to be used by other code in this file.
91 : ** This is done for no better reason than to reduce the number of
92 : ** pre-processor #ifndef statements.
93 : */
94 : #define sqlite3_release_memory(x) 0 /* 0 == no memory freed */
95 : #endif
96 :
97 : #ifdef SQLITE_MEMDEBUG
98 : /*--------------------------------------------------------------------------
99 : ** Begin code for memory allocation system test layer.
100 : **
101 : ** Memory debugging is turned on by defining the SQLITE_MEMDEBUG macro.
102 : **
103 : ** SQLITE_MEMDEBUG==1 -> Fence-posting only (thread safe)
104 : ** SQLITE_MEMDEBUG==2 -> Fence-posting + linked list of allocations (not ts)
105 : ** SQLITE_MEMDEBUG==3 -> Above + backtraces (not thread safe, req. glibc)
106 : */
107 :
108 : /* Figure out whether or not to store backtrace() information for each malloc.
109 : ** The backtrace() function is only used if SQLITE_MEMDEBUG is set to 2 or
110 : ** greater and glibc is in use. If we don't want to use backtrace(), then just
111 : ** define it as an empty macro and set the amount of space reserved to 0.
112 : */
113 : #if defined(__GLIBC__) && SQLITE_MEMDEBUG>2
114 : extern int backtrace(void **, int);
115 : #define TESTALLOC_STACKSIZE 128
116 : #define TESTALLOC_STACKFRAMES ((TESTALLOC_STACKSIZE-8)/sizeof(void*))
117 : #else
118 : #define backtrace(x, y)
119 : #define TESTALLOC_STACKSIZE 0
120 : #define TESTALLOC_STACKFRAMES 0
121 : #endif
122 :
123 : /*
124 : ** Number of 32-bit guard words. This should probably be a multiple of
125 : ** 2 since on 64-bit machines we want the value returned by sqliteMalloc()
126 : ** to be 8-byte aligned.
127 : */
128 : #ifndef TESTALLOC_NGUARD
129 : # define TESTALLOC_NGUARD 2
130 : #endif
131 :
132 : /*
133 : ** Size reserved for storing file-name along with each malloc()ed blob.
134 : */
135 : #define TESTALLOC_FILESIZE 64
136 :
137 : /*
138 : ** Size reserved for storing the user string. Each time a Malloc() or Realloc()
139 : ** call succeeds, up to TESTALLOC_USERSIZE bytes of the string pointed to by
140 : ** sqlite3_malloc_id are stored along with the other test system metadata.
141 : */
142 : #define TESTALLOC_USERSIZE 64
143 : const char *sqlite3_malloc_id = 0;
144 :
145 : /*
146 : ** Blocks used by the test layer have the following format:
147 : **
148 : ** <sizeof(void *) pNext pointer>
149 : ** <sizeof(void *) pPrev pointer>
150 : ** <TESTALLOC_NGUARD 32-bit guard words>
151 : ** <The application level allocation>
152 : ** <TESTALLOC_NGUARD 32-bit guard words>
153 : ** <32-bit line number>
154 : ** <TESTALLOC_FILESIZE bytes containing null-terminated file name>
155 : ** <TESTALLOC_STACKSIZE bytes of backtrace() output>
156 : */
157 :
158 : #define TESTALLOC_OFFSET_GUARD1(p) (sizeof(void *) * 2)
159 : #define TESTALLOC_OFFSET_DATA(p) ( \
160 : TESTALLOC_OFFSET_GUARD1(p) + sizeof(u32) * TESTALLOC_NGUARD \
161 : )
162 : #define TESTALLOC_OFFSET_GUARD2(p) ( \
163 : TESTALLOC_OFFSET_DATA(p) + sqlite3OsAllocationSize(p) - TESTALLOC_OVERHEAD \
164 : )
165 : #define TESTALLOC_OFFSET_LINENUMBER(p) ( \
166 : TESTALLOC_OFFSET_GUARD2(p) + sizeof(u32) * TESTALLOC_NGUARD \
167 : )
168 : #define TESTALLOC_OFFSET_FILENAME(p) ( \
169 : TESTALLOC_OFFSET_LINENUMBER(p) + sizeof(u32) \
170 : )
171 : #define TESTALLOC_OFFSET_USER(p) ( \
172 : TESTALLOC_OFFSET_FILENAME(p) + TESTALLOC_FILESIZE \
173 : )
174 : #define TESTALLOC_OFFSET_STACK(p) ( \
175 : TESTALLOC_OFFSET_USER(p) + TESTALLOC_USERSIZE + 8 - \
176 : (TESTALLOC_OFFSET_USER(p) % 8) \
177 : )
178 :
179 : #define TESTALLOC_OVERHEAD ( \
180 : sizeof(void *)*2 + /* pPrev and pNext pointers */ \
181 : TESTALLOC_NGUARD*sizeof(u32)*2 + /* Guard words */ \
182 : sizeof(u32) + TESTALLOC_FILESIZE + /* File and line number */ \
183 : TESTALLOC_USERSIZE + /* User string */ \
184 : TESTALLOC_STACKSIZE /* backtrace() stack */ \
185 : )
186 :
187 :
188 : /*
189 : ** For keeping track of the number of mallocs and frees. This
190 : ** is used to check for memory leaks. The iMallocFail and iMallocReset
191 : ** values are used to simulate malloc() failures during testing in
192 : ** order to verify that the library correctly handles an out-of-memory
193 : ** condition.
194 : */
195 : int sqlite3_nMalloc; /* Number of sqliteMalloc() calls */
196 : int sqlite3_nFree; /* Number of sqliteFree() calls */
197 : int sqlite3_memUsed; /* TODO Total memory obtained from malloc */
198 : int sqlite3_memMax; /* TODO Mem usage high-water mark */
199 : int sqlite3_iMallocFail; /* Fail sqliteMalloc() after this many calls */
200 : int sqlite3_iMallocReset = -1; /* When iMallocFail reaches 0, set to this */
201 :
202 : void *sqlite3_pFirst = 0; /* Pointer to linked list of allocations */
203 : int sqlite3_nMaxAlloc = 0; /* High water mark of ThreadData.nAlloc */
204 : int sqlite3_mallocDisallowed = 0; /* assert() in sqlite3Malloc() if set */
205 : int sqlite3_isFail = 0; /* True if all malloc calls should fail */
206 : const char *sqlite3_zFile = 0; /* Filename to associate debug info with */
207 : int sqlite3_iLine = 0; /* Line number for debug info */
208 :
209 : /*
210 : ** Check for a simulated memory allocation failure. Return true if
211 : ** the failure should be simulated. Return false to proceed as normal.
212 : */
213 : int sqlite3TestMallocFail(){
214 : if( sqlite3_isFail ){
215 : return 1;
216 : }
217 : if( sqlite3_iMallocFail>=0 ){
218 : sqlite3_iMallocFail--;
219 : if( sqlite3_iMallocFail==0 ){
220 : sqlite3_iMallocFail = sqlite3_iMallocReset;
221 : sqlite3_isFail = 1;
222 : return 1;
223 : }
224 : }
225 : return 0;
226 : }
227 :
228 : /*
229 : ** The argument is a pointer returned by sqlite3OsMalloc() or xRealloc().
230 : ** assert() that the first and last (TESTALLOC_NGUARD*4) bytes are set to the
231 : ** values set by the applyGuards() function.
232 : */
233 : static void checkGuards(u32 *p)
234 : {
235 : int i;
236 : char *zAlloc = (char *)p;
237 : char *z;
238 :
239 : /* First set of guard words */
240 : z = &zAlloc[TESTALLOC_OFFSET_GUARD1(p)];
241 : for(i=0; i<TESTALLOC_NGUARD; i++){
242 : assert(((u32 *)z)[i]==0xdead1122);
243 : }
244 :
245 : /* Second set of guard words */
246 : z = &zAlloc[TESTALLOC_OFFSET_GUARD2(p)];
247 : for(i=0; i<TESTALLOC_NGUARD; i++){
248 : u32 guard = 0;
249 : memcpy(&guard, &z[i*sizeof(u32)], sizeof(u32));
250 : assert(guard==0xdead3344);
251 : }
252 : }
253 :
254 : /*
255 : ** The argument is a pointer returned by sqlite3OsMalloc() or Realloc(). The
256 : ** first and last (TESTALLOC_NGUARD*4) bytes are set to known values for use as
257 : ** guard-posts.
258 : */
259 : static void applyGuards(u32 *p)
260 : {
261 : int i;
262 : char *z;
263 : char *zAlloc = (char *)p;
264 :
265 : /* First set of guard words */
266 : z = &zAlloc[TESTALLOC_OFFSET_GUARD1(p)];
267 : for(i=0; i<TESTALLOC_NGUARD; i++){
268 : ((u32 *)z)[i] = 0xdead1122;
269 : }
270 :
271 : /* Second set of guard words */
272 : z = &zAlloc[TESTALLOC_OFFSET_GUARD2(p)];
273 : for(i=0; i<TESTALLOC_NGUARD; i++){
274 : static const int guard = 0xdead3344;
275 : memcpy(&z[i*sizeof(u32)], &guard, sizeof(u32));
276 : }
277 :
278 : /* Line number */
279 : z = &((char *)z)[TESTALLOC_NGUARD*sizeof(u32)]; /* Guard words */
280 : z = &zAlloc[TESTALLOC_OFFSET_LINENUMBER(p)];
281 : memcpy(z, &sqlite3_iLine, sizeof(u32));
282 :
283 : /* File name */
284 : z = &zAlloc[TESTALLOC_OFFSET_FILENAME(p)];
285 : strncpy(z, sqlite3_zFile, TESTALLOC_FILESIZE);
286 : z[TESTALLOC_FILESIZE - 1] = '\0';
287 :
288 : /* User string */
289 : z = &zAlloc[TESTALLOC_OFFSET_USER(p)];
290 : z[0] = 0;
291 : if( sqlite3_malloc_id ){
292 : strncpy(z, sqlite3_malloc_id, TESTALLOC_USERSIZE);
293 : z[TESTALLOC_USERSIZE-1] = 0;
294 : }
295 :
296 : /* backtrace() stack */
297 : z = &zAlloc[TESTALLOC_OFFSET_STACK(p)];
298 : backtrace((void **)z, TESTALLOC_STACKFRAMES);
299 :
300 : /* Sanity check to make sure checkGuards() is working */
301 : checkGuards(p);
302 : }
303 :
304 : /*
305 : ** The argument is a malloc()ed pointer as returned by the test-wrapper.
306 : ** Return a pointer to the Os level allocation.
307 : */
308 : static void *getOsPointer(void *p)
309 : {
310 : char *z = (char *)p;
311 : return (void *)(&z[-1 * TESTALLOC_OFFSET_DATA(p)]);
312 : }
313 :
314 :
315 : #if SQLITE_MEMDEBUG>1
316 : /*
317 : ** The argument points to an Os level allocation. Link it into the threads list
318 : ** of allocations.
319 : */
320 : static void linkAlloc(void *p){
321 : void **pp = (void **)p;
322 : pp[0] = 0;
323 : pp[1] = sqlite3_pFirst;
324 : if( sqlite3_pFirst ){
325 : ((void **)sqlite3_pFirst)[0] = p;
326 : }
327 : sqlite3_pFirst = p;
328 : }
329 :
330 : /*
331 : ** The argument points to an Os level allocation. Unlinke it from the threads
332 : ** list of allocations.
333 : */
334 : static void unlinkAlloc(void *p)
335 : {
336 : void **pp = (void **)p;
337 : if( p==sqlite3_pFirst ){
338 : assert(!pp[0]);
339 : assert(!pp[1] || ((void **)(pp[1]))[0]==p);
340 : sqlite3_pFirst = pp[1];
341 : if( sqlite3_pFirst ){
342 : ((void **)sqlite3_pFirst)[0] = 0;
343 : }
344 : }else{
345 : void **pprev = pp[0];
346 : void **pnext = pp[1];
347 : assert(pprev);
348 : assert(pprev[1]==p);
349 : pprev[1] = (void *)pnext;
350 : if( pnext ){
351 : assert(pnext[0]==p);
352 : pnext[0] = (void *)pprev;
353 : }
354 : }
355 : }
356 :
357 : /*
358 : ** Pointer p is a pointer to an OS level allocation that has just been
359 : ** realloc()ed. Set the list pointers that point to this entry to it's new
360 : ** location.
361 : */
362 : static void relinkAlloc(void *p)
363 : {
364 : void **pp = (void **)p;
365 : if( pp[0] ){
366 : ((void **)(pp[0]))[1] = p;
367 : }else{
368 : sqlite3_pFirst = p;
369 : }
370 : if( pp[1] ){
371 : ((void **)(pp[1]))[0] = p;
372 : }
373 : }
374 : #else
375 : #define linkAlloc(x)
376 : #define relinkAlloc(x)
377 : #define unlinkAlloc(x)
378 : #endif
379 :
380 : /*
381 : ** This function sets the result of the Tcl interpreter passed as an argument
382 : ** to a list containing an entry for each currently outstanding call made to
383 : ** sqliteMalloc and friends by the current thread. Each list entry is itself a
384 : ** list, consisting of the following (in order):
385 : **
386 : ** * The number of bytes allocated
387 : ** * The __FILE__ macro at the time of the sqliteMalloc() call.
388 : ** * The __LINE__ macro ...
389 : ** * The value of the sqlite3_malloc_id variable ...
390 : ** * The output of backtrace() (if available) ...
391 : **
392 : ** Todo: We could have a version of this function that outputs to stdout,
393 : ** to debug memory leaks when Tcl is not available.
394 : */
395 : #if defined(TCLSH) && defined(SQLITE_DEBUG) && SQLITE_MEMDEBUG>1
396 : #include <tcl.h>
397 : int sqlite3OutstandingMallocs(Tcl_Interp *interp){
398 : void *p;
399 : Tcl_Obj *pRes = Tcl_NewObj();
400 : Tcl_IncrRefCount(pRes);
401 :
402 :
403 : for(p=sqlite3_pFirst; p; p=((void **)p)[1]){
404 : Tcl_Obj *pEntry = Tcl_NewObj();
405 : Tcl_Obj *pStack = Tcl_NewObj();
406 : char *z;
407 : u32 iLine;
408 : int nBytes = sqlite3OsAllocationSize(p) - TESTALLOC_OVERHEAD;
409 : char *zAlloc = (char *)p;
410 : int i;
411 :
412 : Tcl_ListObjAppendElement(0, pEntry, Tcl_NewIntObj(nBytes));
413 :
414 : z = &zAlloc[TESTALLOC_OFFSET_FILENAME(p)];
415 : Tcl_ListObjAppendElement(0, pEntry, Tcl_NewStringObj(z, -1));
416 :
417 : z = &zAlloc[TESTALLOC_OFFSET_LINENUMBER(p)];
418 : memcpy(&iLine, z, sizeof(u32));
419 : Tcl_ListObjAppendElement(0, pEntry, Tcl_NewIntObj(iLine));
420 :
421 : z = &zAlloc[TESTALLOC_OFFSET_USER(p)];
422 : Tcl_ListObjAppendElement(0, pEntry, Tcl_NewStringObj(z, -1));
423 :
424 : z = &zAlloc[TESTALLOC_OFFSET_STACK(p)];
425 : for(i=0; i<TESTALLOC_STACKFRAMES; i++){
426 : char zHex[128];
427 : sprintf(zHex, "%p", ((void **)z)[i]);
428 : Tcl_ListObjAppendElement(0, pStack, Tcl_NewStringObj(zHex, -1));
429 : }
430 :
431 : Tcl_ListObjAppendElement(0, pEntry, pStack);
432 : Tcl_ListObjAppendElement(0, pRes, pEntry);
433 : }
434 :
435 : Tcl_ResetResult(interp);
436 : Tcl_SetObjResult(interp, pRes);
437 : Tcl_DecrRefCount(pRes);
438 : return TCL_OK;
439 : }
440 : #endif
441 :
442 : /*
443 : ** This is the test layer's wrapper around sqlite3OsMalloc().
444 : */
445 : static void * OSMALLOC(int n){
446 : sqlite3OsEnterMutex();
447 : #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
448 : sqlite3_nMaxAlloc =
449 : MAX(sqlite3_nMaxAlloc, sqlite3ThreadDataReadOnly()->nAlloc);
450 : #endif
451 : assert( !sqlite3_mallocDisallowed );
452 : if( !sqlite3TestMallocFail() ){
453 : u32 *p;
454 : p = (u32 *)sqlite3OsMalloc(n + TESTALLOC_OVERHEAD);
455 : assert(p);
456 : sqlite3_nMalloc++;
457 : applyGuards(p);
458 : linkAlloc(p);
459 : sqlite3OsLeaveMutex();
460 : return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]);
461 : }
462 : sqlite3OsLeaveMutex();
463 : return 0;
464 : }
465 :
466 : static int OSSIZEOF(void *p){
467 : if( p ){
468 : u32 *pOs = (u32 *)getOsPointer(p);
469 : return sqlite3OsAllocationSize(pOs) - TESTALLOC_OVERHEAD;
470 : }
471 : return 0;
472 : }
473 :
474 : /*
475 : ** This is the test layer's wrapper around sqlite3OsFree(). The argument is a
476 : ** pointer to the space allocated for the application to use.
477 : */
478 : static void OSFREE(void *pFree){
479 : u32 *p; /* Pointer to the OS-layer allocation */
480 : sqlite3OsEnterMutex();
481 : p = (u32 *)getOsPointer(pFree);
482 : checkGuards(p);
483 : unlinkAlloc(p);
484 : memset(pFree, 0x55, OSSIZEOF(pFree));
485 : sqlite3OsFree(p);
486 : sqlite3_nFree++;
487 : sqlite3OsLeaveMutex();
488 : }
489 :
490 : /*
491 : ** This is the test layer's wrapper around sqlite3OsRealloc().
492 : */
493 : static void * OSREALLOC(void *pRealloc, int n){
494 : #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
495 : sqlite3_nMaxAlloc =
496 : MAX(sqlite3_nMaxAlloc, sqlite3ThreadDataReadOnly()->nAlloc);
497 : #endif
498 : assert( !sqlite3_mallocDisallowed );
499 : if( !sqlite3TestMallocFail() ){
500 : u32 *p = (u32 *)getOsPointer(pRealloc);
501 : checkGuards(p);
502 : p = sqlite3OsRealloc(p, n + TESTALLOC_OVERHEAD);
503 : applyGuards(p);
504 : relinkAlloc(p);
505 : return (void *)(&p[TESTALLOC_NGUARD + 2*sizeof(void *)/sizeof(u32)]);
506 : }
507 : return 0;
508 : }
509 :
510 : static void OSMALLOC_FAILED(){
511 : sqlite3_isFail = 0;
512 : }
513 :
514 : #else
515 : /* Define macros to call the sqlite3OsXXX interface directly if
516 : ** the SQLITE_MEMDEBUG macro is not defined.
517 : */
518 : #define OSMALLOC(x) sqlite3OsMalloc(x)
519 : #define OSREALLOC(x,y) sqlite3OsRealloc(x,y)
520 : #define OSFREE(x) sqlite3OsFree(x)
521 : #define OSSIZEOF(x) sqlite3OsAllocationSize(x)
522 : #define OSMALLOC_FAILED()
523 :
524 : #endif /* SQLITE_MEMDEBUG */
525 : /*
526 : ** End code for memory allocation system test layer.
527 : **--------------------------------------------------------------------------*/
528 :
529 : /*
530 : ** This routine is called when we are about to allocate n additional bytes
531 : ** of memory. If the new allocation will put is over the soft allocation
532 : ** limit, then invoke sqlite3_release_memory() to try to release some
533 : ** memory before continuing with the allocation.
534 : **
535 : ** This routine also makes sure that the thread-specific-data (TSD) has
536 : ** be allocated. If it has not and can not be allocated, then return
537 : ** false. The updateMemoryUsedCount() routine below will deallocate
538 : ** the TSD if it ought to be.
539 : **
540 : ** If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, this routine is
541 : ** a no-op
542 : */
543 : #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
544 : static int enforceSoftLimit(int n){
545 : ThreadData *pTsd = sqlite3ThreadData();
546 : if( pTsd==0 ){
547 : return 0;
548 : }
549 : assert( pTsd->nAlloc>=0 );
550 : if( n>0 && pTsd->nSoftHeapLimit>0 ){
551 : while( pTsd->nAlloc+n>pTsd->nSoftHeapLimit && sqlite3_release_memory(n) ){}
552 : }
553 : return 1;
554 : }
555 : #else
556 : # define enforceSoftLimit(X) 1
557 : #endif
558 :
559 : /*
560 : ** Update the count of total outstanding memory that is held in
561 : ** thread-specific-data (TSD). If after this update the TSD is
562 : ** no longer being used, then deallocate it.
563 : **
564 : ** If SQLITE_ENABLE_MEMORY_MANAGEMENT is not defined, this routine is
565 : ** a no-op
566 : */
567 : #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
568 : static void updateMemoryUsedCount(int n){
569 : ThreadData *pTsd = sqlite3ThreadData();
570 : if( pTsd ){
571 : pTsd->nAlloc += n;
572 : assert( pTsd->nAlloc>=0 );
573 : if( pTsd->nAlloc==0 && pTsd->nSoftHeapLimit==0 ){
574 : sqlite3ReleaseThreadData();
575 : }
576 : }
577 : }
578 : #else
579 : #define updateMemoryUsedCount(x) /* no-op */
580 : #endif
581 :
582 : /*
583 : ** Allocate and return N bytes of uninitialised memory by calling
584 : ** sqlite3OsMalloc(). If the Malloc() call fails, attempt to free memory
585 : ** by calling sqlite3_release_memory().
586 : */
587 36588 : void *sqlite3MallocRaw(int n, int doMemManage){
588 36588 : void *p = 0;
589 36588 : if( n>0 && !sqlite3MallocFailed() && (!doMemManage || enforceSoftLimit(n)) ){
590 36588 : while( (p = OSMALLOC(n))==0 && sqlite3_release_memory(n) ){}
591 36588 : if( !p ){
592 0 : sqlite3FailedMalloc();
593 : OSMALLOC_FAILED();
594 : }else if( doMemManage ){
595 : updateMemoryUsedCount(OSSIZEOF(p));
596 : }
597 : }
598 36588 : return p;
599 : }
600 :
601 : /*
602 : ** Resize the allocation at p to n bytes by calling sqlite3OsRealloc(). The
603 : ** pointer to the new allocation is returned. If the Realloc() call fails,
604 : ** attempt to free memory by calling sqlite3_release_memory().
605 : */
606 3470 : void *sqlite3Realloc(void *p, int n){
607 3470 : if( sqlite3MallocFailed() ){
608 0 : return 0;
609 : }
610 :
611 3470 : if( !p ){
612 2736 : return sqlite3Malloc(n, 1);
613 : }else{
614 734 : void *np = 0;
615 : #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
616 : int origSize = OSSIZEOF(p);
617 : #endif
618 : if( enforceSoftLimit(n - origSize) ){
619 734 : while( (np = OSREALLOC(p, n))==0 && sqlite3_release_memory(n) ){}
620 734 : if( !np ){
621 0 : sqlite3FailedMalloc();
622 : OSMALLOC_FAILED();
623 : }else{
624 : updateMemoryUsedCount(OSSIZEOF(np) - origSize);
625 : }
626 : }
627 734 : return np;
628 : }
629 : }
630 :
631 : /*
632 : ** Free the memory pointed to by p. p must be either a NULL pointer or a
633 : ** value returned by a previous call to sqlite3Malloc() or sqlite3Realloc().
634 : */
635 52223 : void sqlite3FreeX(void *p){
636 52223 : if( p ){
637 : updateMemoryUsedCount(0 - OSSIZEOF(p));
638 36414 : OSFREE(p);
639 : }
640 52223 : }
641 :
642 : /*
643 : ** A version of sqliteMalloc() that is always a function, not a macro.
644 : ** Currently, this is used only to alloc to allocate the parser engine.
645 : */
646 8395 : void *sqlite3MallocX(int n){
647 8395 : return sqliteMalloc(n);
648 : }
649 :
650 : /*
651 : ** sqlite3Malloc
652 : ** sqlite3ReallocOrFree
653 : **
654 : ** These two are implemented as wrappers around sqlite3MallocRaw(),
655 : ** sqlite3Realloc() and sqlite3Free().
656 : */
657 27652 : void *sqlite3Malloc(int n, int doMemManage){
658 27652 : void *p = sqlite3MallocRaw(n, doMemManage);
659 27652 : if( p ){
660 27652 : memset(p, 0, n);
661 : }
662 27652 : return p;
663 : }
664 365 : void *sqlite3ReallocOrFree(void *p, int n){
665 : void *pNew;
666 365 : pNew = sqlite3Realloc(p, n);
667 365 : if( !pNew ){
668 0 : sqlite3FreeX(p);
669 : }
670 365 : return pNew;
671 : }
672 :
673 : /*
674 : ** sqlite3ThreadSafeMalloc() and sqlite3ThreadSafeFree() are used in those
675 : ** rare scenarios where sqlite may allocate memory in one thread and free
676 : ** it in another. They are exactly the same as sqlite3Malloc() and
677 : ** sqlite3Free() except that:
678 : **
679 : ** * The allocated memory is not included in any calculations with
680 : ** respect to the soft-heap-limit, and
681 : **
682 : ** * sqlite3ThreadSafeMalloc() must be matched with ThreadSafeFree(),
683 : ** not sqlite3Free(). Calling sqlite3Free() on memory obtained from
684 : ** ThreadSafeMalloc() will cause an error somewhere down the line.
685 : */
686 : #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
687 : void *sqlite3ThreadSafeMalloc(int n){
688 : (void)ENTER_MALLOC;
689 : return sqlite3Malloc(n, 0);
690 : }
691 : void sqlite3ThreadSafeFree(void *p){
692 : (void)ENTER_MALLOC;
693 : if( p ){
694 : OSFREE(p);
695 : }
696 : }
697 : #endif
698 :
699 :
700 : /*
701 : ** Return the number of bytes allocated at location p. p must be either
702 : ** a NULL pointer (in which case 0 is returned) or a pointer returned by
703 : ** sqlite3Malloc(), sqlite3Realloc() or sqlite3ReallocOrFree().
704 : **
705 : ** The number of bytes allocated does not include any overhead inserted by
706 : ** any malloc() wrapper functions that may be called. So the value returned
707 : ** is the number of bytes that were available to SQLite using pointer p,
708 : ** regardless of how much memory was actually allocated.
709 : */
710 : #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
711 : int sqlite3AllocSize(void *p){
712 : return OSSIZEOF(p);
713 : }
714 : #endif
715 :
716 : /*
717 : ** Make a copy of a string in memory obtained from sqliteMalloc(). These
718 : ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
719 : ** is because when memory debugging is turned on, these two functions are
720 : ** called via macros that record the current file and line number in the
721 : ** ThreadData structure.
722 : */
723 117 : char *sqlite3StrDup(const char *z){
724 : char *zNew;
725 117 : if( z==0 ) return 0;
726 117 : zNew = sqlite3MallocRaw(strlen(z)+1, 1);
727 117 : if( zNew ) strcpy(zNew, z);
728 117 : return zNew;
729 : }
730 7104 : char *sqlite3StrNDup(const char *z, int n){
731 : char *zNew;
732 7104 : if( z==0 ) return 0;
733 7104 : zNew = sqlite3MallocRaw(n+1, 1);
734 7104 : if( zNew ){
735 7104 : memcpy(zNew, z, n);
736 7104 : zNew[n] = 0;
737 : }
738 7104 : return zNew;
739 : }
740 :
741 : /*
742 : ** Create a string from the 2nd and subsequent arguments (up to the
743 : ** first NULL argument), store the string in memory obtained from
744 : ** sqliteMalloc() and make the pointer indicated by the 1st argument
745 : ** point to that string. The 1st argument must either be NULL or
746 : ** point to memory obtained from sqliteMalloc().
747 : */
748 187 : void sqlite3SetString(char **pz, ...){
749 : va_list ap;
750 : int nByte;
751 : const char *z;
752 : char *zResult;
753 :
754 : assert( pz!=0 );
755 187 : nByte = 1;
756 187 : va_start(ap, pz);
757 1005 : while( (z = va_arg(ap, const char*))!=0 ){
758 631 : nByte += strlen(z);
759 : }
760 187 : va_end(ap);
761 187 : sqliteFree(*pz);
762 187 : *pz = zResult = sqliteMallocRaw( nByte );
763 187 : if( zResult==0 ){
764 0 : return;
765 : }
766 187 : *zResult = 0;
767 187 : va_start(ap, pz);
768 1005 : while( (z = va_arg(ap, const char*))!=0 ){
769 631 : strcpy(zResult, z);
770 631 : zResult += strlen(zResult);
771 : }
772 187 : va_end(ap);
773 : }
774 :
775 : /*
776 : ** Set the most recent error code and error string for the sqlite
777 : ** handle "db". The error code is set to "err_code".
778 : **
779 : ** If it is not NULL, string zFormat specifies the format of the
780 : ** error string in the style of the printf functions: The following
781 : ** format characters are allowed:
782 : **
783 : ** %s Insert a string
784 : ** %z A string that should be freed after use
785 : ** %d Insert an integer
786 : ** %T Insert a token
787 : ** %S Insert the first element of a SrcList
788 : **
789 : ** zFormat and any string tokens that follow it are assumed to be
790 : ** encoded in UTF-8.
791 : **
792 : ** To clear the most recent error for sqlite handle "db", sqlite3Error
793 : ** should be called with err_code set to SQLITE_OK and zFormat set
794 : ** to NULL.
795 : */
796 3712 : void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
797 3712 : if( db && (db->pErr || (db->pErr = sqlite3ValueNew())!=0) ){
798 3712 : db->errCode = err_code;
799 3712 : if( zFormat ){
800 : char *z;
801 : va_list ap;
802 310 : va_start(ap, zFormat);
803 310 : z = sqlite3VMPrintf(zFormat, ap);
804 310 : va_end(ap);
805 310 : sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, sqlite3FreeX);
806 : }else{
807 3402 : sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
808 : }
809 : }
810 3712 : }
811 :
812 : /*
813 : ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
814 : ** The following formatting characters are allowed:
815 : **
816 : ** %s Insert a string
817 : ** %z A string that should be freed after use
818 : ** %d Insert an integer
819 : ** %T Insert a token
820 : ** %S Insert the first element of a SrcList
821 : **
822 : ** This function should be used to report any error that occurs whilst
823 : ** compiling an SQL statement (i.e. within sqlite3_prepare()). The
824 : ** last thing the sqlite3_prepare() function does is copy the error
825 : ** stored by this function into the database handle using sqlite3Error().
826 : ** Function sqlite3Error() should be used during statement execution
827 : ** (sqlite3_step() etc.).
828 : */
829 310 : void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
830 : va_list ap;
831 310 : pParse->nErr++;
832 310 : sqliteFree(pParse->zErrMsg);
833 310 : va_start(ap, zFormat);
834 310 : pParse->zErrMsg = sqlite3VMPrintf(zFormat, ap);
835 310 : va_end(ap);
836 310 : }
837 :
838 : /*
839 : ** Clear the error message in pParse, if any
840 : */
841 0 : void sqlite3ErrorClear(Parse *pParse){
842 0 : sqliteFree(pParse->zErrMsg);
843 0 : pParse->zErrMsg = 0;
844 0 : pParse->nErr = 0;
845 0 : }
846 :
847 : /*
848 : ** Convert an SQL-style quoted string into a normal string by removing
849 : ** the quote characters. The conversion is done in-place. If the
850 : ** input does not begin with a quote character, then this routine
851 : ** is a no-op.
852 : **
853 : ** 2002-Feb-14: This routine is extended to remove MS-Access style
854 : ** brackets from around identifers. For example: "[a-b-c]" becomes
855 : ** "a-b-c".
856 : */
857 5464 : void sqlite3Dequote(char *z){
858 : int quote;
859 : int i, j;
860 5464 : if( z==0 ) return;
861 5464 : quote = z[0];
862 5464 : switch( quote ){
863 725 : case '\'': break;
864 22 : case '"': break;
865 0 : case '`': break; /* For MySQL compatibility */
866 0 : case '[': quote = ']'; break; /* For MS SqlServer compatibility */
867 4717 : default: return;
868 : }
869 8143 : for(i=1, j=0; z[i]; i++){
870 8143 : if( z[i]==quote ){
871 748 : if( z[i+1]==quote ){
872 1 : z[j++] = quote;
873 1 : i++;
874 : }else{
875 747 : z[j++] = 0;
876 747 : break;
877 : }
878 : }else{
879 7395 : z[j++] = z[i];
880 : }
881 : }
882 : }
883 :
884 : /* An array to map all upper-case characters into their corresponding
885 : ** lower-case character.
886 : */
887 : const unsigned char sqlite3UpperToLower[] = {
888 : #ifdef SQLITE_ASCII
889 : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
890 : 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
891 : 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
892 : 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
893 : 104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
894 : 122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
895 : 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
896 : 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
897 : 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
898 : 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
899 : 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
900 : 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
901 : 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
902 : 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
903 : 252,253,254,255
904 : #endif
905 : #ifdef SQLITE_EBCDIC
906 : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 0x */
907 : 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
908 : 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
909 : 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
910 : 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
911 : 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
912 : 96, 97, 66, 67, 68, 69, 70, 71, 72, 73,106,107,108,109,110,111, /* 6x */
913 : 112, 81, 82, 83, 84, 85, 86, 87, 88, 89,122,123,124,125,126,127, /* 7x */
914 : 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
915 : 144,145,146,147,148,149,150,151,152,153,154,155,156,157,156,159, /* 9x */
916 : 160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
917 : 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
918 : 192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
919 : 208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
920 : 224,225,162,163,164,165,166,167,168,169,232,203,204,205,206,207, /* Ex */
921 : 239,240,241,242,243,244,245,246,247,248,249,219,220,221,222,255, /* Fx */
922 : #endif
923 : };
924 : #define UpperToLower sqlite3UpperToLower
925 :
926 : /*
927 : ** Some systems have stricmp(). Others have strcasecmp(). Because
928 : ** there is no consistency, we will define our own.
929 : */
930 4464 : int sqlite3StrICmp(const char *zLeft, const char *zRight){
931 : register unsigned char *a, *b;
932 4464 : a = (unsigned char *)zLeft;
933 4464 : b = (unsigned char *)zRight;
934 4464 : while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
935 4464 : return UpperToLower[*a] - UpperToLower[*b];
936 : }
937 14139 : int sqlite3StrNICmp(const char *zLeft, const char *zRight, int N){
938 : register unsigned char *a, *b;
939 14139 : a = (unsigned char *)zLeft;
940 14139 : b = (unsigned char *)zRight;
941 14139 : while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
942 14139 : return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
943 : }
944 :
945 : /*
946 : ** Return TRUE if z is a pure numeric string. Return FALSE if the
947 : ** string contains any character which is not part of a number. If
948 : ** the string is numeric and contains the '.' character, set *realnum
949 : ** to TRUE (otherwise FALSE).
950 : **
951 : ** An empty string is considered non-numeric.
952 : */
953 94 : int sqlite3IsNumber(const char *z, int *realnum, u8 enc){
954 94 : int incr = (enc==SQLITE_UTF8?1:2);
955 94 : if( enc==SQLITE_UTF16BE ) z++;
956 94 : if( *z=='-' || *z=='+' ) z += incr;
957 94 : if( !isdigit(*(u8*)z) ){
958 0 : return 0;
959 : }
960 94 : z += incr;
961 94 : if( realnum ) *realnum = 0;
962 94 : while( isdigit(*(u8*)z) ){ z += incr; }
963 94 : if( *z=='.' ){
964 0 : z += incr;
965 0 : if( !isdigit(*(u8*)z) ) return 0;
966 0 : while( isdigit(*(u8*)z) ){ z += incr; }
967 0 : if( realnum ) *realnum = 1;
968 : }
969 94 : if( *z=='e' || *z=='E' ){
970 0 : z += incr;
971 0 : if( *z=='+' || *z=='-' ) z += incr;
972 0 : if( !isdigit(*(u8*)z) ) return 0;
973 0 : while( isdigit(*(u8*)z) ){ z += incr; }
974 0 : if( realnum ) *realnum = 1;
975 : }
976 94 : return *z==0;
977 : }
978 :
979 : /*
980 : ** The string z[] is an ascii representation of a real number.
981 : ** Convert this string to a double.
982 : **
983 : ** This routine assumes that z[] really is a valid number. If it
984 : ** is not, the result is undefined.
985 : **
986 : ** This routine is used instead of the library atof() function because
987 : ** the library atof() might want to use "," as the decimal point instead
988 : ** of "." depending on how locale is set. But that would cause problems
989 : ** for SQL. So this routine always uses "." regardless of locale.
990 : */
991 0 : int sqlite3AtoF(const char *z, double *pResult){
992 : #ifndef SQLITE_OMIT_FLOATING_POINT
993 0 : int sign = 1;
994 0 : const char *zBegin = z;
995 0 : LONGDOUBLE_TYPE v1 = 0.0;
996 0 : while( isspace(*z) ) z++;
997 0 : if( *z=='-' ){
998 0 : sign = -1;
999 0 : z++;
1000 0 : }else if( *z=='+' ){
1001 0 : z++;
1002 : }
1003 0 : while( isdigit(*(u8*)z) ){
1004 0 : v1 = v1*10.0 + (*z - '0');
1005 0 : z++;
1006 : }
1007 0 : if( *z=='.' ){
1008 0 : LONGDOUBLE_TYPE divisor = 1.0;
1009 0 : z++;
1010 0 : while( isdigit(*(u8*)z) ){
1011 0 : v1 = v1*10.0 + (*z - '0');
1012 0 : divisor *= 10.0;
1013 0 : z++;
1014 : }
1015 0 : v1 /= divisor;
1016 : }
1017 0 : if( *z=='e' || *z=='E' ){
1018 0 : int esign = 1;
1019 0 : int eval = 0;
1020 0 : LONGDOUBLE_TYPE scale = 1.0;
1021 0 : z++;
1022 0 : if( *z=='-' ){
1023 0 : esign = -1;
1024 0 : z++;
1025 0 : }else if( *z=='+' ){
1026 0 : z++;
1027 : }
1028 0 : while( isdigit(*(u8*)z) ){
1029 0 : eval = eval*10 + *z - '0';
1030 0 : z++;
1031 : }
1032 0 : while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
1033 0 : while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
1034 0 : while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
1035 0 : while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
1036 0 : if( esign<0 ){
1037 0 : v1 /= scale;
1038 : }else{
1039 0 : v1 *= scale;
1040 : }
1041 : }
1042 0 : *pResult = sign<0 ? -v1 : v1;
1043 0 : return z - zBegin;
1044 : #else
1045 : return sqlite3atoi64(z, pResult);
1046 : #endif /* SQLITE_OMIT_FLOATING_POINT */
1047 : }
1048 :
1049 : /*
1050 : ** Return TRUE if zNum is a 64-bit signed integer and write
1051 : ** the value of the integer into *pNum. If zNum is not an integer
1052 : ** or is an integer that is too large to be expressed with 64 bits,
1053 : ** then return false. If n>0 and the integer is string is not
1054 : ** exactly n bytes long, return false.
1055 : **
1056 : ** When this routine was originally written it dealt with only
1057 : ** 32-bit numbers. At that time, it was much faster than the
1058 : ** atoi() library routine in RedHat 7.2.
1059 : */
1060 94 : int sqlite3atoi64(const char *zNum, i64 *pNum){
1061 94 : i64 v = 0;
1062 : int neg;
1063 : int i, c;
1064 94 : while( isspace(*zNum) ) zNum++;
1065 94 : if( *zNum=='-' ){
1066 0 : neg = 1;
1067 0 : zNum++;
1068 94 : }else if( *zNum=='+' ){
1069 0 : neg = 0;
1070 0 : zNum++;
1071 : }else{
1072 94 : neg = 0;
1073 : }
1074 250 : for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
1075 156 : v = v*10 + c - '0';
1076 : }
1077 94 : *pNum = neg ? -v : v;
1078 94 : return c==0 && i>0 &&
1079 : (i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0));
1080 : }
1081 :
1082 : /*
1083 : ** The string zNum represents an integer. There might be some other
1084 : ** information following the integer too, but that part is ignored.
1085 : ** If the integer that the prefix of zNum represents will fit in a
1086 : ** 32-bit signed integer, return TRUE. Otherwise return FALSE.
1087 : **
1088 : ** This routine returns FALSE for the string -2147483648 even that
1089 : ** that number will in fact fit in a 32-bit integer. But positive
1090 : ** 2147483648 will not fit in 32 bits. So it seems safer to return
1091 : ** false.
1092 : */
1093 106 : static int sqlite3FitsIn32Bits(const char *zNum){
1094 : int i, c;
1095 106 : if( *zNum=='-' || *zNum=='+' ) zNum++;
1096 106 : for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
1097 106 : return i<10 || (i==10 && memcmp(zNum,"2147483647",10)<=0);
1098 : }
1099 :
1100 : /*
1101 : ** If zNum represents an integer that will fit in 32-bits, then set
1102 : ** *pValue to that integer and return true. Otherwise return false.
1103 : */
1104 106 : int sqlite3GetInt32(const char *zNum, int *pValue){
1105 106 : if( sqlite3FitsIn32Bits(zNum) ){
1106 106 : *pValue = atoi(zNum);
1107 106 : return 1;
1108 : }
1109 0 : return 0;
1110 : }
1111 :
1112 : /*
1113 : ** The string zNum represents an integer. There might be some other
1114 : ** information following the integer too, but that part is ignored.
1115 : ** If the integer that the prefix of zNum represents will fit in a
1116 : ** 64-bit signed integer, return TRUE. Otherwise return FALSE.
1117 : **
1118 : ** This routine returns FALSE for the string -9223372036854775808 even that
1119 : ** that number will, in theory fit in a 64-bit integer. Positive
1120 : ** 9223373036854775808 will not fit in 64 bits. So it seems safer to return
1121 : ** false.
1122 : */
1123 0 : int sqlite3FitsIn64Bits(const char *zNum){
1124 : int i, c;
1125 0 : if( *zNum=='-' || *zNum=='+' ) zNum++;
1126 0 : for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
1127 0 : return i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0);
1128 : }
1129 :
1130 :
1131 : /*
1132 : ** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
1133 : ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
1134 : ** when this routine is called.
1135 : **
1136 : ** This routine is called when entering an SQLite API. The SQLITE_MAGIC_OPEN
1137 : ** value indicates that the database connection passed into the API is
1138 : ** open and is not being used by another thread. By changing the value
1139 : ** to SQLITE_MAGIC_BUSY we indicate that the connection is in use.
1140 : ** sqlite3SafetyOff() below will change the value back to SQLITE_MAGIC_OPEN
1141 : ** when the API exits.
1142 : **
1143 : ** This routine is a attempt to detect if two threads use the
1144 : ** same sqlite* pointer at the same time. There is a race
1145 : ** condition so it is possible that the error is not detected.
1146 : ** But usually the problem will be seen. The result will be an
1147 : ** error which can be used to debug the application that is
1148 : ** using SQLite incorrectly.
1149 : **
1150 : ** Ticket #202: If db->magic is not a valid open value, take care not
1151 : ** to modify the db structure at all. It could be that db is a stale
1152 : ** pointer. In other words, it could be that there has been a prior
1153 : ** call to sqlite3_close(db) and db has been deallocated. And we do
1154 : ** not want to write into deallocated memory.
1155 : */
1156 3697 : int sqlite3SafetyOn(sqlite3 *db){
1157 3697 : if( db->magic==SQLITE_MAGIC_OPEN ){
1158 3697 : db->magic = SQLITE_MAGIC_BUSY;
1159 3697 : return 0;
1160 0 : }else if( db->magic==SQLITE_MAGIC_BUSY ){
1161 0 : db->magic = SQLITE_MAGIC_ERROR;
1162 0 : db->u1.isInterrupted = 1;
1163 : }
1164 0 : return 1;
1165 : }
1166 :
1167 : /*
1168 : ** Change the magic from SQLITE_MAGIC_BUSY to SQLITE_MAGIC_OPEN.
1169 : ** Return an error (non-zero) if the magic was not SQLITE_MAGIC_BUSY
1170 : ** when this routine is called.
1171 : */
1172 3568 : int sqlite3SafetyOff(sqlite3 *db){
1173 3568 : if( db->magic==SQLITE_MAGIC_BUSY ){
1174 3568 : db->magic = SQLITE_MAGIC_OPEN;
1175 3568 : return 0;
1176 : }else {
1177 0 : db->magic = SQLITE_MAGIC_ERROR;
1178 0 : db->u1.isInterrupted = 1;
1179 0 : return 1;
1180 : }
1181 : }
1182 :
1183 : /*
1184 : ** Check to make sure we have a valid db pointer. This test is not
1185 : ** foolproof but it does provide some measure of protection against
1186 : ** misuse of the interface such as passing in db pointers that are
1187 : ** NULL or which have been previously closed. If this routine returns
1188 : ** TRUE it means that the db pointer is invalid and should not be
1189 : ** dereferenced for any reason. The calling function should invoke
1190 : ** SQLITE_MISUSE immediately.
1191 : */
1192 10170 : int sqlite3SafetyCheck(sqlite3 *db){
1193 : int magic;
1194 10170 : if( db==0 ) return 1;
1195 10170 : magic = db->magic;
1196 10170 : if( magic!=SQLITE_MAGIC_CLOSED &&
1197 : magic!=SQLITE_MAGIC_OPEN &&
1198 0 : magic!=SQLITE_MAGIC_BUSY ) return 1;
1199 10170 : return 0;
1200 : }
1201 :
1202 : /*
1203 : ** The variable-length integer encoding is as follows:
1204 : **
1205 : ** KEY:
1206 : ** A = 0xxxxxxx 7 bits of data and one flag bit
1207 : ** B = 1xxxxxxx 7 bits of data and one flag bit
1208 : ** C = xxxxxxxx 8 bits of data
1209 : **
1210 : ** 7 bits - A
1211 : ** 14 bits - BA
1212 : ** 21 bits - BBA
1213 : ** 28 bits - BBBA
1214 : ** 35 bits - BBBBA
1215 : ** 42 bits - BBBBBA
1216 : ** 49 bits - BBBBBBA
1217 : ** 56 bits - BBBBBBBA
1218 : ** 64 bits - BBBBBBBBC
1219 : */
1220 :
1221 : /*
1222 : ** Write a 64-bit variable-length integer to memory starting at p[0].
1223 : ** The length of data write will be between 1 and 9 bytes. The number
1224 : ** of bytes written is returned.
1225 : **
1226 : ** A variable-length integer consists of the lower 7 bits of each byte
1227 : ** for all bytes that have the 8th bit set and one byte with the 8th
1228 : ** bit clear. Except, if we get to the 9th byte, it stores the full
1229 : ** 8 bits and is the last byte.
1230 : */
1231 2265 : int sqlite3PutVarint(unsigned char *p, u64 v){
1232 : int i, j, n;
1233 : u8 buf[10];
1234 2265 : if( v & (((u64)0xff000000)<<32) ){
1235 0 : p[8] = v;
1236 0 : v >>= 8;
1237 0 : for(i=7; i>=0; i--){
1238 0 : p[i] = (v & 0x7f) | 0x80;
1239 0 : v >>= 7;
1240 : }
1241 0 : return 9;
1242 : }
1243 2265 : n = 0;
1244 : do{
1245 2310 : buf[n++] = (v & 0x7f) | 0x80;
1246 2310 : v >>= 7;
1247 2310 : }while( v!=0 );
1248 2265 : buf[0] &= 0x7f;
1249 : assert( n<=9 );
1250 4575 : for(i=0, j=n-1; j>=0; j--, i++){
1251 2310 : p[i] = buf[j];
1252 : }
1253 2265 : return n;
1254 : }
1255 :
1256 : /*
1257 : ** Read a 64-bit variable-length integer from memory starting at p[0].
1258 : ** Return the number of bytes read. The value is stored in *v.
1259 : */
1260 1431 : int sqlite3GetVarint(const unsigned char *p, u64 *v){
1261 : u32 x;
1262 : u64 x64;
1263 : int n;
1264 : unsigned char c;
1265 1431 : if( ((c = p[0]) & 0x80)==0 ){
1266 1431 : *v = c;
1267 1431 : return 1;
1268 : }
1269 0 : x = c & 0x7f;
1270 0 : if( ((c = p[1]) & 0x80)==0 ){
1271 0 : *v = (x<<7) | c;
1272 0 : return 2;
1273 : }
1274 0 : x = (x<<7) | (c&0x7f);
1275 0 : if( ((c = p[2]) & 0x80)==0 ){
1276 0 : *v = (x<<7) | c;
1277 0 : return 3;
1278 : }
1279 0 : x = (x<<7) | (c&0x7f);
1280 0 : if( ((c = p[3]) & 0x80)==0 ){
1281 0 : *v = (x<<7) | c;
1282 0 : return 4;
1283 : }
1284 0 : x64 = (x<<7) | (c&0x7f);
1285 0 : n = 4;
1286 : do{
1287 0 : c = p[n++];
1288 0 : if( n==9 ){
1289 0 : x64 = (x64<<8) | c;
1290 0 : break;
1291 : }
1292 0 : x64 = (x64<<7) | (c&0x7f);
1293 0 : }while( (c & 0x80)!=0 );
1294 0 : *v = x64;
1295 0 : return n;
1296 : }
1297 :
1298 : /*
1299 : ** Read a 32-bit variable-length integer from memory starting at p[0].
1300 : ** Return the number of bytes read. The value is stored in *v.
1301 : */
1302 560 : int sqlite3GetVarint32(const unsigned char *p, u32 *v){
1303 : u32 x;
1304 : int n;
1305 : unsigned char c;
1306 560 : if( ((signed char*)p)[0]>=0 ){
1307 496 : *v = p[0];
1308 496 : return 1;
1309 : }
1310 64 : x = p[0] & 0x7f;
1311 64 : if( ((signed char*)p)[1]>=0 ){
1312 64 : *v = (x<<7) | p[1];
1313 64 : return 2;
1314 : }
1315 0 : x = (x<<7) | (p[1] & 0x7f);
1316 0 : n = 2;
1317 : do{
1318 0 : x = (x<<7) | ((c = p[n++])&0x7f);
1319 0 : }while( (c & 0x80)!=0 && n<9 );
1320 0 : *v = x;
1321 0 : return n;
1322 : }
1323 :
1324 : /*
1325 : ** Return the number of bytes that will be needed to store the given
1326 : ** 64-bit integer.
1327 : */
1328 1950 : int sqlite3VarintLen(u64 v){
1329 1950 : int i = 0;
1330 : do{
1331 1993 : i++;
1332 1993 : v >>= 7;
1333 1993 : }while( v!=0 && i<9 );
1334 1950 : return i;
1335 : }
1336 :
1337 : #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) \
1338 : || defined(SQLITE_TEST)
1339 : /*
1340 : ** Translate a single byte of Hex into an integer.
1341 : */
1342 0 : static int hexToInt(int h){
1343 0 : if( h>='0' && h<='9' ){
1344 0 : return h - '0';
1345 0 : }else if( h>='a' && h<='f' ){
1346 0 : return h - 'a' + 10;
1347 : }else{
1348 : assert( h>='A' && h<='F' );
1349 0 : return h - 'A' + 10;
1350 : }
1351 : }
1352 : #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC || SQLITE_TEST */
1353 :
1354 : #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
1355 : /*
1356 : ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
1357 : ** value. Return a pointer to its binary value. Space to hold the
1358 : ** binary value has been obtained from malloc and must be freed by
1359 : ** the calling routine.
1360 : */
1361 0 : void *sqlite3HexToBlob(const char *z){
1362 : char *zBlob;
1363 : int i;
1364 0 : int n = strlen(z);
1365 0 : if( n%2 ) return 0;
1366 :
1367 0 : zBlob = (char *)sqliteMalloc(n/2);
1368 0 : if( zBlob ){
1369 0 : for(i=0; i<n; i+=2){
1370 0 : zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
1371 : }
1372 : }
1373 0 : return zBlob;
1374 : }
1375 : #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
1376 :
1377 : #if defined(SQLITE_TEST)
1378 : /*
1379 : ** Convert text generated by the "%p" conversion format back into
1380 : ** a pointer.
1381 : */
1382 : void *sqlite3TextToPtr(const char *z){
1383 : void *p;
1384 : u64 v;
1385 : u32 v2;
1386 : if( z[0]=='0' && z[1]=='x' ){
1387 : z += 2;
1388 : }
1389 : v = 0;
1390 : while( *z ){
1391 : v = (v<<4) + hexToInt(*z);
1392 : z++;
1393 : }
1394 : if( sizeof(p)==sizeof(v) ){
1395 : memcpy(&p, &v, sizeof(p));
1396 : }else{
1397 : assert( sizeof(p)==sizeof(v2) );
1398 : v2 = (u32)v;
1399 : memcpy(&p, &v2, sizeof(p));
1400 : }
1401 : return p;
1402 : }
1403 : #endif
1404 :
1405 : /*
1406 : ** Return a pointer to the ThreadData associated with the calling thread.
1407 : */
1408 0 : ThreadData *sqlite3ThreadData(){
1409 0 : ThreadData *p = (ThreadData*)sqlite3OsThreadSpecificData(1);
1410 0 : if( !p ){
1411 0 : sqlite3FailedMalloc();
1412 : }
1413 0 : return p;
1414 : }
1415 :
1416 : /*
1417 : ** Return a pointer to the ThreadData associated with the calling thread.
1418 : ** If no ThreadData has been allocated to this thread yet, return a pointer
1419 : ** to a substitute ThreadData structure that is all zeros.
1420 : */
1421 2692 : const ThreadData *sqlite3ThreadDataReadOnly(){
1422 : static const ThreadData zeroData = {0}; /* Initializer to silence warnings
1423 : ** from broken compilers */
1424 2692 : const ThreadData *pTd = sqlite3OsThreadSpecificData(0);
1425 2692 : return pTd ? pTd : &zeroData;
1426 : }
1427 :
1428 : /*
1429 : ** Check to see if the ThreadData for this thread is all zero. If it
1430 : ** is, then deallocate it.
1431 : */
1432 1059 : void sqlite3ReleaseThreadData(){
1433 1059 : sqlite3OsThreadSpecificData(-1);
1434 1059 : }
1435 :
1436 : /*
1437 : ** This function must be called before exiting any API function (i.e.
1438 : ** returning control to the user) that has called sqlite3Malloc or
1439 : ** sqlite3Realloc.
1440 : **
1441 : ** The returned value is normally a copy of the second argument to this
1442 : ** function. However, if a malloc() failure has occured since the previous
1443 : ** invocation SQLITE_NOMEM is returned instead.
1444 : **
1445 : ** If the first argument, db, is not NULL and a malloc() error has occured,
1446 : ** then the connection error-code (the value returned by sqlite3_errcode())
1447 : ** is set to SQLITE_NOMEM.
1448 : */
1449 : static int mallocHasFailed = 0;
1450 4793 : int sqlite3ApiExit(sqlite3* db, int rc){
1451 4793 : if( sqlite3MallocFailed() ){
1452 0 : mallocHasFailed = 0;
1453 0 : sqlite3OsLeaveMutex();
1454 0 : sqlite3Error(db, SQLITE_NOMEM, 0);
1455 0 : rc = SQLITE_NOMEM;
1456 : }
1457 4793 : return rc & (db ? db->errMask : 0xff);
1458 : }
1459 :
1460 : /*
1461 : ** Return true is a malloc has failed in this thread since the last call
1462 : ** to sqlite3ApiExit(), or false otherwise.
1463 : */
1464 93386 : int sqlite3MallocFailed(){
1465 93386 : return (mallocHasFailed && sqlite3OsInMutex(1));
1466 : }
1467 :
1468 : /*
1469 : ** Set the "malloc has failed" condition to true for this thread.
1470 : */
1471 0 : void sqlite3FailedMalloc(){
1472 0 : if( !sqlite3MallocFailed() ){
1473 0 : sqlite3OsEnterMutex();
1474 : assert( mallocHasFailed==0 );
1475 0 : mallocHasFailed = 1;
1476 : }
1477 0 : }
1478 :
1479 : #ifdef SQLITE_MEMDEBUG
1480 : /*
1481 : ** This function sets a flag in the thread-specific-data structure that will
1482 : ** cause an assert to fail if sqliteMalloc() or sqliteRealloc() is called.
1483 : */
1484 : void sqlite3MallocDisallow(){
1485 : assert( sqlite3_mallocDisallowed>=0 );
1486 : sqlite3_mallocDisallowed++;
1487 : }
1488 :
1489 : /*
1490 : ** This function clears the flag set in the thread-specific-data structure set
1491 : ** by sqlite3MallocDisallow().
1492 : */
1493 : void sqlite3MallocAllow(){
1494 : assert( sqlite3_mallocDisallowed>0 );
1495 : sqlite3_mallocDisallowed--;
1496 : }
1497 : #endif
|