AryaWu/sqlite
0
1/*2** 2004 May 263**4** The author disclaims copyright to this source code. In place of5** 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**13** This file contains code use to manipulate "Mem" structure. A "Mem"14** stores a single value in the VDBE. Mem is an opaque structure visible15** only within the VDBE. Interface routines refer to a Mem using the16** name sqlite_value17*/18#include "sqliteInt.h"19#include "vdbeInt.h"20 21/* True if X is a power of two. 0 is considered a power of two here.22** In other words, return true if X has at most one bit set.23*/24#define ISPOWEROF2(X) (((X)&((X)-1))==0)25 26#ifdef SQLITE_DEBUG27/*28** Check invariants on a Mem object.29**30** This routine is intended for use inside of assert() statements, like31** this: assert( sqlite3VdbeCheckMemInvariants(pMem) );32*/33int sqlite3VdbeCheckMemInvariants(Mem *p){34 /* If MEM_Dyn is set then Mem.xDel!=0. 35 ** Mem.xDel might not be initialized if MEM_Dyn is clear.36 */37 assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );38 39 /* MEM_Dyn may only be set if Mem.szMalloc==0. In this way we40 ** ensure that if Mem.szMalloc>0 then it is safe to do41 ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn.42 ** That saves a few cycles in inner loops. */43 assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );44 45 /* Cannot have more than one of MEM_Int, MEM_Real, or MEM_IntReal */46 assert( ISPOWEROF2(p->flags & (MEM_Int|MEM_Real|MEM_IntReal)) );47 48 if( p->flags & MEM_Null ){49 /* Cannot be both MEM_Null and some other type */50 assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 );51 52 /* If MEM_Null is set, then either the value is a pure NULL (the usual53 ** case) or it is a pointer set using sqlite3_bind_pointer() or54 ** sqlite3_result_pointer(). If a pointer, then MEM_Term must also be55 ** set.56 */57 if( (p->flags & (MEM_Term|MEM_Subtype))==(MEM_Term|MEM_Subtype) ){58 /* This is a pointer type. There may be a flag to indicate what to59 ** do with the pointer. */60 assert( ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +61 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +62 ((p->flags&MEM_Static)!=0 ? 1 : 0) <= 1 );63 64 /* No other bits set */65 assert( (p->flags & ~(MEM_Null|MEM_Term|MEM_Subtype|MEM_FromBind66 |MEM_Dyn|MEM_Ephem|MEM_Static))==0 );67 }else{68 /* A pure NULL might have other flags, such as MEM_Static, MEM_Dyn,69 ** MEM_Ephem, MEM_Cleared, or MEM_Subtype */70 }71 }else{72 /* The MEM_Cleared bit is only allowed on NULLs */73 assert( (p->flags & MEM_Cleared)==0 );74 }75 76 /* The szMalloc field holds the correct memory allocation size */77 assert( p->szMalloc==078 || (p->flags==MEM_Undefined 79 && p->szMalloc<=sqlite3DbMallocSize(p->db,p->zMalloc))80 || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc));81 82 /* If p holds a string or blob, the Mem.z must point to exactly83 ** one of the following:84 **85 ** (1) Memory in Mem.zMalloc and managed by the Mem object86 ** (2) Memory to be freed using Mem.xDel87 ** (3) An ephemeral string or blob88 ** (4) A static string or blob89 */90 if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){91 assert( 92 ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +93 ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +94 ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +95 ((p->flags&MEM_Static)!=0 ? 1 : 0) == 196 );97 }98 return 1;99}100#endif101 102/*103** Render a Mem object which is one of MEM_Int, MEM_Real, or MEM_IntReal104** into a buffer.105*/106static void vdbeMemRenderNum(int sz, char *zBuf, Mem *p){107 StrAccum acc;108 assert( p->flags & (MEM_Int|MEM_Real|MEM_IntReal) );109 assert( sz>22 );110 if( p->flags & MEM_Int ){111#if GCC_VERSION>=7000000112 /* Work-around for GCC bug113 ** https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96270 */114 i64 x;115 assert( (p->flags&MEM_Int)*2==sizeof(x) );116 memcpy(&x, (char*)&p->u, (p->flags&MEM_Int)*2);117 p->n = sqlite3Int64ToText(x, zBuf);118#else119 p->n = sqlite3Int64ToText(p->u.i, zBuf);120#endif121 }else{122 sqlite3StrAccumInit(&acc, 0, zBuf, sz, 0);123 sqlite3_str_appendf(&acc, "%!.15g", 124 (p->flags & MEM_IntReal)!=0 ? (double)p->u.i : p->u.r);125 assert( acc.zText==zBuf && acc.mxAlloc<=0 );126 zBuf[acc.nChar] = 0; /* Fast version of sqlite3StrAccumFinish(&acc) */127 p->n = acc.nChar;128 }129}130 131#ifdef SQLITE_DEBUG132/*133** Validity checks on pMem. pMem holds a string.134**135** (1) Check that string value of pMem agrees with its integer or real value.136** (2) Check that the string is correctly zero terminated137**138** A single int or real value always converts to the same strings. But139** many different strings can be converted into the same int or real.140** If a table contains a numeric value and an index is based on the141** corresponding string value, then it is important that the string be142** derived from the numeric value, not the other way around, to ensure143** that the index and table are consistent. See ticket144** https://sqlite.org/src/info/343634942dd54ab (2018-01-31) for145** an example.146**147** This routine looks at pMem to verify that if it has both a numeric148** representation and a string representation then the string rep has149** been derived from the numeric and not the other way around. It returns150** true if everything is ok and false if there is a problem.151**152** This routine is for use inside of assert() statements only.153*/154int sqlite3VdbeMemValidStrRep(Mem *p){155 Mem tmp;156 char zBuf[100];157 char *z;158 int i, j, incr;159 if( (p->flags & MEM_Str)==0 ) return 1;160 if( p->db && p->db->mallocFailed ) return 1;161 if( p->flags & MEM_Term ){162 /* Insure that the string is properly zero-terminated. Pay particular163 ** attention to the case where p->n is odd */164 if( p->szMalloc>0 && p->z==p->zMalloc ){165 assert( p->enc==SQLITE_UTF8 || p->szMalloc >= ((p->n+1)&~1)+2 );166 assert( p->enc!=SQLITE_UTF8 || p->szMalloc >= p->n+1 );167 }168 assert( p->z[p->n]==0 );169 assert( p->enc==SQLITE_UTF8 || p->z[(p->n+1)&~1]==0 );170 assert( p->enc==SQLITE_UTF8 || p->z[((p->n+1)&~1)+1]==0 );171 }172 if( (p->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ) return 1;173 memcpy(&tmp, p, sizeof(tmp));174 vdbeMemRenderNum(sizeof(zBuf), zBuf, &tmp);175 z = p->z;176 i = j = 0;177 incr = 1;178 if( p->enc!=SQLITE_UTF8 ){179 incr = 2;180 if( p->enc==SQLITE_UTF16BE ) z++;181 }182 while( zBuf[j] ){183 if( zBuf[j++]!=z[i] ) return 0;184 i += incr;185 }186 return 1;187}188#endif /* SQLITE_DEBUG */189 190/*191** If pMem is an object with a valid string representation, this routine192** ensures the internal encoding for the string representation is193** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.194**195** If pMem is not a string object, or the encoding of the string196** representation is already stored using the requested encoding, then this197** routine is a no-op.198**199** SQLITE_OK is returned if the conversion is successful (or not required).200** SQLITE_NOMEM may be returned if a malloc() fails during conversion201** between formats.202*/203int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){204#ifndef SQLITE_OMIT_UTF16205 int rc;206#endif207 assert( pMem!=0 );208 assert( !sqlite3VdbeMemIsRowSet(pMem) );209 assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE210 || desiredEnc==SQLITE_UTF16BE );211 if( !(pMem->flags&MEM_Str) ){212 pMem->enc = desiredEnc;213 return SQLITE_OK;214 }215 if( pMem->enc==desiredEnc ){216 return SQLITE_OK;217 }218 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );219#ifdef SQLITE_OMIT_UTF16220 return SQLITE_ERROR;221#else222 223 /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,224 ** then the encoding of the value may not have changed.225 */226 rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);227 assert(rc==SQLITE_OK || rc==SQLITE_NOMEM);228 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);229 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);230 return rc;231#endif232}233 234/*235** Make sure pMem->z points to a writable allocation of at least n bytes.236**237** If the bPreserve argument is true, then copy of the content of238** pMem->z into the new allocation. pMem must be either a string or239** blob if bPreserve is true. If bPreserve is false, any prior content240** in pMem->z is discarded.241*/242SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){243 assert( sqlite3VdbeCheckMemInvariants(pMem) );244 assert( !sqlite3VdbeMemIsRowSet(pMem) );245 testcase( pMem->db==0 );246 247 /* If the bPreserve flag is set to true, then the memory cell must already248 ** contain a valid string or blob value. */249 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );250 testcase( bPreserve && pMem->z==0 );251 252 assert( pMem->szMalloc==0253 || (pMem->flags==MEM_Undefined 254 && pMem->szMalloc<=sqlite3DbMallocSize(pMem->db,pMem->zMalloc))255 || pMem->szMalloc==sqlite3DbMallocSize(pMem->db,pMem->zMalloc));256 if( pMem->szMalloc>0 && bPreserve && pMem->z==pMem->zMalloc ){257 if( pMem->db ){258 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);259 }else{260 pMem->zMalloc = sqlite3Realloc(pMem->z, n);261 if( pMem->zMalloc==0 ) sqlite3_free(pMem->z);262 pMem->z = pMem->zMalloc;263 }264 bPreserve = 0;265 }else{266 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);267 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);268 }269 if( pMem->zMalloc==0 ){270 sqlite3VdbeMemSetNull(pMem);271 pMem->z = 0;272 pMem->szMalloc = 0;273 return SQLITE_NOMEM_BKPT;274 }else{275 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);276 }277 278 if( bPreserve && pMem->z ){279 assert( pMem->z!=pMem->zMalloc );280 memcpy(pMem->zMalloc, pMem->z, pMem->n);281 }282 if( (pMem->flags&MEM_Dyn)!=0 ){283 assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );284 pMem->xDel((void *)(pMem->z));285 }286 287 pMem->z = pMem->zMalloc;288 pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);289 return SQLITE_OK;290}291 292/*293** Change the pMem->zMalloc allocation to be at least szNew bytes.294** If pMem->zMalloc already meets or exceeds the requested size, this295** routine is a no-op.296**297** Any prior string or blob content in the pMem object may be discarded.298** The pMem->xDel destructor is called, if it exists. Though MEM_Str299** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, MEM_IntReal,300** and MEM_Null values are preserved.301**302** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM)303** if unable to complete the resizing.304*/305int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){306 assert( CORRUPT_DB || szNew>0 );307 assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );308 if( pMem->szMalloc<szNew ){309 return sqlite3VdbeMemGrow(pMem, szNew, 0);310 }311 assert( (pMem->flags & MEM_Dyn)==0 );312 pMem->z = pMem->zMalloc;313 pMem->flags &= (MEM_Null|MEM_Int|MEM_Real|MEM_IntReal);314 return SQLITE_OK;315}316 317/*318** If pMem is already a string, detect if it is a zero-terminated319** string, or make it into one if possible, and mark it as such.320**321** This is an optimization. Correct operation continues even if322** this routine is a no-op.323*/324void sqlite3VdbeMemZeroTerminateIfAble(Mem *pMem){325 if( (pMem->flags & (MEM_Str|MEM_Term|MEM_Ephem|MEM_Static))!=MEM_Str ){326 /* pMem must be a string, and it cannot be an ephemeral or static string */327 return;328 }329 if( pMem->enc!=SQLITE_UTF8 ) return;330 assert( pMem->z!=0 );331 if( pMem->flags & MEM_Dyn ){332 if( pMem->xDel==sqlite3_free333 && sqlite3_msize(pMem->z) >= (u64)(pMem->n+1)334 ){335 pMem->z[pMem->n] = 0;336 pMem->flags |= MEM_Term;337 return;338 }339 if( pMem->xDel==sqlite3RCStrUnref ){340 /* Blindly assume that all RCStr objects are zero-terminated */341 pMem->flags |= MEM_Term;342 return;343 }344 }else if( pMem->szMalloc >= pMem->n+1 ){345 pMem->z[pMem->n] = 0;346 pMem->flags |= MEM_Term;347 return;348 }349}350 351/*352** It is already known that pMem contains an unterminated string.353** Add the zero terminator.354**355** Three bytes of zero are added. In this way, there is guaranteed356** to be a double-zero byte at an even byte boundary in order to357** terminate a UTF16 string, even if the initial size of the buffer358** is an odd number of bytes.359*/360static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){361 if( sqlite3VdbeMemGrow(pMem, pMem->n+3, 1) ){362 return SQLITE_NOMEM_BKPT;363 }364 pMem->z[pMem->n] = 0;365 pMem->z[pMem->n+1] = 0;366 pMem->z[pMem->n+2] = 0;367 pMem->flags |= MEM_Term;368 return SQLITE_OK;369}370 371/*372** Change pMem so that its MEM_Str or MEM_Blob value is stored in373** MEM.zMalloc, where it can be safely written.374**375** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.376*/377int sqlite3VdbeMemMakeWriteable(Mem *pMem){378 assert( pMem!=0 );379 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );380 assert( !sqlite3VdbeMemIsRowSet(pMem) );381 if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){382 if( ExpandBlob(pMem) ) return SQLITE_NOMEM;383 if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){384 int rc = vdbeMemAddTerminator(pMem);385 if( rc ) return rc;386 }387 }388 pMem->flags &= ~MEM_Ephem;389#ifdef SQLITE_DEBUG390 pMem->pScopyFrom = 0;391#endif392 393 return SQLITE_OK;394}395 396/*397** If the given Mem* has a zero-filled tail, turn it into an ordinary398** blob stored in dynamically allocated space.399*/400#ifndef SQLITE_OMIT_INCRBLOB401int sqlite3VdbeMemExpandBlob(Mem *pMem){402 int nByte;403 assert( pMem!=0 );404 assert( pMem->flags & MEM_Zero );405 assert( (pMem->flags&MEM_Blob)!=0 || MemNullNochng(pMem) );406 testcase( sqlite3_value_nochange(pMem) );407 assert( !sqlite3VdbeMemIsRowSet(pMem) );408 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );409 410 /* Set nByte to the number of bytes required to store the expanded blob. */411 nByte = pMem->n + pMem->u.nZero;412 if( nByte<=0 ){413 if( (pMem->flags & MEM_Blob)==0 ) return SQLITE_OK;414 nByte = 1;415 }416 if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){417 return SQLITE_NOMEM_BKPT;418 }419 assert( pMem->z!=0 );420 assert( sqlite3DbMallocSize(pMem->db,pMem->z) >= nByte );421 422 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);423 pMem->n += pMem->u.nZero;424 pMem->flags &= ~(MEM_Zero|MEM_Term);425 return SQLITE_OK;426}427#endif428 429/*430** Make sure the given Mem is \u0000 terminated.431*/432int sqlite3VdbeMemNulTerminate(Mem *pMem){433 assert( pMem!=0 );434 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );435 testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );436 testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );437 if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){438 return SQLITE_OK; /* Nothing to do */439 }else{440 return vdbeMemAddTerminator(pMem);441 }442}443 444/*445** Add MEM_Str to the set of representations for the given Mem. This446** routine is only called if pMem is a number of some kind, not a NULL447** or a BLOB.448**449** Existing representations MEM_Int, MEM_Real, or MEM_IntReal are invalidated450** if bForce is true but are retained if bForce is false.451**452** A MEM_Null value will never be passed to this function. This function is453** used for converting values to text for returning to the user (i.e. via454** sqlite3_value_text()), or for ensuring that values to be used as btree455** keys are strings. In the former case a NULL pointer is returned the456** user and the latter is an internal programming error.457*/458int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){459 const int nByte = 32;460 461 assert( pMem!=0 );462 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );463 assert( !(pMem->flags&MEM_Zero) );464 assert( !(pMem->flags&(MEM_Str|MEM_Blob)) );465 assert( pMem->flags&(MEM_Int|MEM_Real|MEM_IntReal) );466 assert( !sqlite3VdbeMemIsRowSet(pMem) );467 assert( EIGHT_BYTE_ALIGNMENT(pMem) );468 469 470 if( sqlite3VdbeMemClearAndResize(pMem, nByte) ){471 pMem->enc = 0;472 return SQLITE_NOMEM_BKPT;473 }474 475 vdbeMemRenderNum(nByte, pMem->z, pMem);476 assert( pMem->z!=0 );477 assert( pMem->n==(int)sqlite3Strlen30NN(pMem->z) );478 pMem->enc = SQLITE_UTF8;479 pMem->flags |= MEM_Str|MEM_Term;480 if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal);481 sqlite3VdbeChangeEncoding(pMem, enc);482 return SQLITE_OK;483}484 485/*486** Memory cell pMem contains the context of an aggregate function.487** This routine calls the finalize method for that function. The488** result of the aggregate is stored back into pMem.489**490** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK491** otherwise.492*/493int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){494 sqlite3_context ctx;495 Mem t;496 assert( pFunc!=0 );497 assert( pMem!=0 );498 assert( pMem->db!=0 );499 assert( pFunc->xFinalize!=0 );500 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );501 assert( sqlite3_mutex_held(pMem->db->mutex) );502 memset(&ctx, 0, sizeof(ctx));503 memset(&t, 0, sizeof(t));504 t.flags = MEM_Null;505 t.db = pMem->db;506 ctx.pOut = &t;507 ctx.pMem = pMem;508 ctx.pFunc = pFunc;509 ctx.enc = ENC(t.db);510 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */511 assert( (pMem->flags & MEM_Dyn)==0 );512 if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);513 memcpy(pMem, &t, sizeof(t));514 return ctx.isError;515}516 517/*518** Memory cell pAccum contains the context of an aggregate function.519** This routine calls the xValue method for that function and stores520** the results in memory cell pMem.521**522** SQLITE_ERROR is returned if xValue() reports an error. SQLITE_OK 523** otherwise.524*/525#ifndef SQLITE_OMIT_WINDOWFUNC526int sqlite3VdbeMemAggValue(Mem *pAccum, Mem *pOut, FuncDef *pFunc){527 sqlite3_context ctx;528 assert( pFunc!=0 );529 assert( pFunc->xValue!=0 );530 assert( (pAccum->flags & MEM_Null)!=0 || pFunc==pAccum->u.pDef );531 assert( pAccum->db!=0 );532 assert( sqlite3_mutex_held(pAccum->db->mutex) );533 memset(&ctx, 0, sizeof(ctx));534 sqlite3VdbeMemSetNull(pOut);535 ctx.pOut = pOut;536 ctx.pMem = pAccum;537 ctx.pFunc = pFunc;538 ctx.enc = ENC(pAccum->db);539 pFunc->xValue(&ctx);540 return ctx.isError;541}542#endif /* SQLITE_OMIT_WINDOWFUNC */543 544/*545** If the memory cell contains a value that must be freed by546** invoking the external callback in Mem.xDel, then this routine547** will free that value. It also sets Mem.flags to MEM_Null.548**549** This is a helper routine for sqlite3VdbeMemSetNull() and550** for sqlite3VdbeMemRelease(). Use those other routines as the551** entry point for releasing Mem resources.552*/553static SQLITE_NOINLINE void vdbeMemClearExternAndSetNull(Mem *p){554 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );555 assert( VdbeMemDynamic(p) );556 if( p->flags&MEM_Agg ){557 sqlite3VdbeMemFinalize(p, p->u.pDef);558 assert( (p->flags & MEM_Agg)==0 );559 testcase( p->flags & MEM_Dyn );560 }561 if( p->flags&MEM_Dyn ){562 assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );563 p->xDel((void *)p->z);564 }565 p->flags = MEM_Null;566}567 568/*569** Release memory held by the Mem p, both external memory cleared570** by p->xDel and memory in p->zMalloc.571**572** This is a helper routine invoked by sqlite3VdbeMemRelease() in573** the unusual case where there really is memory in p that needs574** to be freed.575*/576static SQLITE_NOINLINE void vdbeMemClear(Mem *p){577 if( VdbeMemDynamic(p) ){578 vdbeMemClearExternAndSetNull(p);579 }580 if( p->szMalloc ){581 sqlite3DbFreeNN(p->db, p->zMalloc);582 p->szMalloc = 0;583 }584 p->z = 0;585}586 587/*588** Release any memory resources held by the Mem. Both the memory that is589** free by Mem.xDel and the Mem.zMalloc allocation are freed.590**591** Use this routine prior to clean up prior to abandoning a Mem, or to592** reset a Mem back to its minimum memory utilization.593**594** Use sqlite3VdbeMemSetNull() to release just the Mem.xDel space595** prior to inserting new content into the Mem.596*/597void sqlite3VdbeMemRelease(Mem *p){598 assert( sqlite3VdbeCheckMemInvariants(p) );599 if( VdbeMemDynamic(p) || p->szMalloc ){600 vdbeMemClear(p);601 }602}603 604/* Like sqlite3VdbeMemRelease() but faster for cases where we605** know in advance that the Mem is not MEM_Dyn or MEM_Agg.606*/607void sqlite3VdbeMemReleaseMalloc(Mem *p){608 assert( !VdbeMemDynamic(p) );609 if( p->szMalloc ) vdbeMemClear(p);610}611 612/*613** Return some kind of integer value which is the best we can do614** at representing the value that *pMem describes as an integer.615** If pMem is an integer, then the value is exact. If pMem is616** a floating-point then the value returned is the integer part.617** If pMem is a string or blob, then we make an attempt to convert618** it into an integer and return that. If pMem represents an619** an SQL-NULL value, return 0.620**621** If pMem represents a string value, its encoding might be changed.622*/623static SQLITE_NOINLINE i64 memIntValue(const Mem *pMem){624 i64 value = 0;625 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);626 return value;627}628i64 sqlite3VdbeIntValue(const Mem *pMem){629 int flags;630 assert( pMem!=0 );631 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );632 assert( EIGHT_BYTE_ALIGNMENT(pMem) );633 flags = pMem->flags;634 if( flags & (MEM_Int|MEM_IntReal) ){635 testcase( flags & MEM_IntReal );636 return pMem->u.i;637 }else if( flags & MEM_Real ){638 return sqlite3RealToI64(pMem->u.r);639 }else if( (flags & (MEM_Str|MEM_Blob))!=0 && pMem->z!=0 ){640 return memIntValue(pMem);641 }else{642 return 0;643 }644}645 646/*647** Return the best representation of pMem that we can get into a648** double. If pMem is already a double or an integer, return its649** value. If it is a string or blob, try to convert it to a double.650** If it is a NULL, return 0.0.651*/652static SQLITE_NOINLINE double memRealValue(Mem *pMem){653 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */654 double val = (double)0;655 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);656 return val;657}658double sqlite3VdbeRealValue(Mem *pMem){659 assert( pMem!=0 );660 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );661 assert( EIGHT_BYTE_ALIGNMENT(pMem) );662 if( pMem->flags & MEM_Real ){663 return pMem->u.r;664 }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){665 testcase( pMem->flags & MEM_IntReal );666 return (double)pMem->u.i;667 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){668 return memRealValue(pMem);669 }else{670 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */671 return (double)0;672 }673}674 675/*676** Return 1 if pMem represents true, and return 0 if pMem represents false.677** Return the value ifNull if pMem is NULL. 678*/679int sqlite3VdbeBooleanValue(Mem *pMem, int ifNull){680 testcase( pMem->flags & MEM_IntReal );681 if( pMem->flags & (MEM_Int|MEM_IntReal) ) return pMem->u.i!=0;682 if( pMem->flags & MEM_Null ) return ifNull;683 return sqlite3VdbeRealValue(pMem)!=0.0;684}685 686/*687** The MEM structure is already a MEM_Real or MEM_IntReal. Try to 688** make it a MEM_Int if we can.689*/690void sqlite3VdbeIntegerAffinity(Mem *pMem){691 assert( pMem!=0 );692 assert( pMem->flags & (MEM_Real|MEM_IntReal) );693 assert( !sqlite3VdbeMemIsRowSet(pMem) );694 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );695 assert( EIGHT_BYTE_ALIGNMENT(pMem) );696 697 if( pMem->flags & MEM_IntReal ){698 MemSetTypeFlag(pMem, MEM_Int);699 }else{700 i64 ix = sqlite3RealToI64(pMem->u.r);701 702 /* Only mark the value as an integer if703 **704 ** (1) the round-trip conversion real->int->real is a no-op, and705 ** (2) The integer is neither the largest nor the smallest706 ** possible integer (ticket #3922)707 **708 ** The second and third terms in the following conditional enforces709 ** the second condition under the assumption that addition overflow causes710 ** values to wrap around.711 */712 if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){713 pMem->u.i = ix;714 MemSetTypeFlag(pMem, MEM_Int);715 }716 }717}718 719/*720** Convert pMem to type integer. Invalidate any prior representations.721*/722int sqlite3VdbeMemIntegerify(Mem *pMem){723 assert( pMem!=0 );724 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );725 assert( !sqlite3VdbeMemIsRowSet(pMem) );726 assert( EIGHT_BYTE_ALIGNMENT(pMem) );727 728 pMem->u.i = sqlite3VdbeIntValue(pMem);729 MemSetTypeFlag(pMem, MEM_Int);730 return SQLITE_OK;731}732 733/*734** Convert pMem so that it is of type MEM_Real.735** Invalidate any prior representations.736*/737int sqlite3VdbeMemRealify(Mem *pMem){738 assert( pMem!=0 );739 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );740 assert( EIGHT_BYTE_ALIGNMENT(pMem) );741 742 pMem->u.r = sqlite3VdbeRealValue(pMem);743 MemSetTypeFlag(pMem, MEM_Real);744 return SQLITE_OK;745}746 747/* Compare a floating point value to an integer. Return true if the two748** values are the same within the precision of the floating point value.749**750** This function assumes that i was obtained by assignment from r1.751**752** For some versions of GCC on 32-bit machines, if you do the more obvious753** comparison of "r1==(double)i" you sometimes get an answer of false even754** though the r1 and (double)i values are bit-for-bit the same.755*/756int sqlite3RealSameAsInt(double r1, sqlite3_int64 i){757 double r2 = (double)i;758 return r1==0.0759 || (memcmp(&r1, &r2, sizeof(r1))==0760 && i >= -2251799813685248LL && i < 2251799813685248LL);761}762 763/* Convert a floating point value to its closest integer. Do so in764** a way that avoids 'outside the range of representable values' warnings765** from UBSAN.766*/767i64 sqlite3RealToI64(double r){768 if( r<-9223372036854774784.0 ) return SMALLEST_INT64;769 if( r>+9223372036854774784.0 ) return LARGEST_INT64;770 return (i64)r;771}772 773/*774** Convert pMem so that it has type MEM_Real or MEM_Int.775** Invalidate any prior representations.776**777** Every effort is made to force the conversion, even if the input778** is a string that does not look completely like a number. Convert779** as much of the string as we can and ignore the rest.780*/781int sqlite3VdbeMemNumerify(Mem *pMem){782 assert( pMem!=0 );783 testcase( pMem->flags & MEM_Int );784 testcase( pMem->flags & MEM_Real );785 testcase( pMem->flags & MEM_IntReal );786 testcase( pMem->flags & MEM_Null );787 if( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))==0 ){788 int rc;789 sqlite3_int64 ix;790 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );791 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );792 rc = sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc);793 if( ((rc==0 || rc==1) && sqlite3Atoi64(pMem->z, &ix, pMem->n, pMem->enc)<=1)794 || sqlite3RealSameAsInt(pMem->u.r, (ix = sqlite3RealToI64(pMem->u.r)))795 ){796 pMem->u.i = ix;797 MemSetTypeFlag(pMem, MEM_Int);798 }else{799 MemSetTypeFlag(pMem, MEM_Real);800 }801 }802 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))!=0 );803 pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);804 return SQLITE_OK;805}806 807/*808** Cast the datatype of the value in pMem according to the affinity809** "aff". Casting is different from applying affinity in that a cast810** is forced. In other words, the value is converted into the desired811** affinity even if that results in loss of data. This routine is812** used (for example) to implement the SQL "cast()" operator.813*/814int sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){815 if( pMem->flags & MEM_Null ) return SQLITE_OK;816 switch( aff ){817 case SQLITE_AFF_BLOB: { /* Really a cast to BLOB */818 if( (pMem->flags & MEM_Blob)==0 ){819 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);820 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );821 if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob);822 }else{823 pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);824 }825 break;826 }827 case SQLITE_AFF_NUMERIC: {828 sqlite3VdbeMemNumerify(pMem);829 break;830 }831 case SQLITE_AFF_INTEGER: {832 sqlite3VdbeMemIntegerify(pMem);833 break;834 }835 case SQLITE_AFF_REAL: {836 sqlite3VdbeMemRealify(pMem);837 break;838 }839 default: {840 int rc;841 assert( aff==SQLITE_AFF_TEXT );842 assert( MEM_Str==(MEM_Blob>>3) );843 pMem->flags |= (pMem->flags&MEM_Blob)>>3;844 sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);845 assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );846 pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal|MEM_Blob|MEM_Zero);847 if( encoding!=SQLITE_UTF8 ) pMem->n &= ~1;848 rc = sqlite3VdbeChangeEncoding(pMem, encoding);849 if( rc ) return rc;850 sqlite3VdbeMemZeroTerminateIfAble(pMem);851 }852 }853 return SQLITE_OK;854}855 856/*857** Initialize bulk memory to be a consistent Mem object.858**859** The minimum amount of initialization feasible is performed.860*/861void sqlite3VdbeMemInit(Mem *pMem, sqlite3 *db, u16 flags){862 assert( (flags & ~MEM_TypeMask)==0 );863 pMem->flags = flags;864 pMem->db = db;865 pMem->szMalloc = 0;866}867 868 869/*870** Delete any previous value and set the value stored in *pMem to NULL.871**872** This routine calls the Mem.xDel destructor to dispose of values that873** require the destructor. But it preserves the Mem.zMalloc memory allocation.874** To free all resources, use sqlite3VdbeMemRelease(), which both calls this875** routine to invoke the destructor and deallocates Mem.zMalloc.876**877** Use this routine to reset the Mem prior to insert a new value.878**879** Use sqlite3VdbeMemRelease() to complete erase the Mem prior to abandoning it.880*/881void sqlite3VdbeMemSetNull(Mem *pMem){882 if( VdbeMemDynamic(pMem) ){883 vdbeMemClearExternAndSetNull(pMem);884 }else{885 pMem->flags = MEM_Null;886 }887}888void sqlite3ValueSetNull(sqlite3_value *p){889 sqlite3VdbeMemSetNull((Mem*)p); 890}891 892/*893** Delete any previous value and set the value to be a BLOB of length894** n containing all zeros.895*/896#ifndef SQLITE_OMIT_INCRBLOB897void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){898 sqlite3VdbeMemRelease(pMem);899 pMem->flags = MEM_Blob|MEM_Zero;900 pMem->n = 0;901 if( n<0 ) n = 0;902 pMem->u.nZero = n;903 pMem->enc = SQLITE_UTF8;904 pMem->z = 0;905}906#else907int sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){908 int nByte = n>0?n:1;909 if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){910 return SQLITE_NOMEM_BKPT;911 }912 assert( pMem->z!=0 );913 assert( sqlite3DbMallocSize(pMem->db, pMem->z)>=nByte );914 memset(pMem->z, 0, nByte);915 pMem->n = n>0?n:0;916 pMem->flags = MEM_Blob;917 pMem->enc = SQLITE_UTF8;918 return SQLITE_OK;919}920#endif921 922/*923** The pMem is known to contain content that needs to be destroyed prior924** to a value change. So invoke the destructor, then set the value to925** a 64-bit integer.926*/927static SQLITE_NOINLINE void vdbeReleaseAndSetInt64(Mem *pMem, i64 val){928 sqlite3VdbeMemSetNull(pMem);929 pMem->u.i = val;930 pMem->flags = MEM_Int;931}932 933/*934** Delete any previous value and set the value stored in *pMem to val,935** manifest type INTEGER.936*/937void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){938 if( VdbeMemDynamic(pMem) ){939 vdbeReleaseAndSetInt64(pMem, val);940 }else{941 pMem->u.i = val;942 pMem->flags = MEM_Int;943 }944}945 946/*947** Set the iIdx'th entry of array aMem[] to contain integer value val.948*/949void sqlite3MemSetArrayInt64(sqlite3_value *aMem, int iIdx, i64 val){950 sqlite3VdbeMemSetInt64(&aMem[iIdx], val);951}952 953/* A no-op destructor */954void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p); }955 956/*957** Set the value stored in *pMem should already be a NULL.958** Also store a pointer to go with it.959*/960void sqlite3VdbeMemSetPointer(961 Mem *pMem,962 void *pPtr,963 const char *zPType,964 void (*xDestructor)(void*)965){966 assert( pMem->flags==MEM_Null );967 vdbeMemClear(pMem);968 pMem->u.zPType = zPType ? zPType : "";969 pMem->z = pPtr;970 pMem->flags = MEM_Null|MEM_Dyn|MEM_Subtype|MEM_Term;971 pMem->eSubtype = 'p';972 pMem->xDel = xDestructor ? xDestructor : sqlite3NoopDestructor;973}974 975#ifndef SQLITE_OMIT_FLOATING_POINT976/*977** Delete any previous value and set the value stored in *pMem to val,978** manifest type REAL.979*/980void sqlite3VdbeMemSetDouble(Mem *pMem, double val){981 sqlite3VdbeMemSetNull(pMem);982 if( !sqlite3IsNaN(val) ){983 pMem->u.r = val;984 pMem->flags = MEM_Real;985 }986}987#endif988 989#ifdef SQLITE_DEBUG990/*991** Return true if the Mem holds a RowSet object. This routine is intended992** for use inside of assert() statements.993*/994int sqlite3VdbeMemIsRowSet(const Mem *pMem){995 return (pMem->flags&(MEM_Blob|MEM_Dyn))==(MEM_Blob|MEM_Dyn)996 && pMem->xDel==sqlite3RowSetDelete;997}998#endif999 1000/*1001** Delete any previous value and set the value of pMem to be an1002** empty boolean index.1003**1004** Return SQLITE_OK on success and SQLITE_NOMEM if a memory allocation1005** error occurs.1006*/1007int sqlite3VdbeMemSetRowSet(Mem *pMem){1008 sqlite3 *db = pMem->db;1009 RowSet *p;1010 assert( db!=0 );1011 assert( !sqlite3VdbeMemIsRowSet(pMem) );1012 sqlite3VdbeMemRelease(pMem);1013 p = sqlite3RowSetInit(db);1014 if( p==0 ) return SQLITE_NOMEM;1015 pMem->z = (char*)p;1016 pMem->flags = MEM_Blob|MEM_Dyn;1017 pMem->xDel = sqlite3RowSetDelete;1018 return SQLITE_OK;1019}1020 1021/*1022** Return true if the Mem object contains a TEXT or BLOB that is1023** too large - whose size exceeds SQLITE_MAX_LENGTH.1024*/1025int sqlite3VdbeMemTooBig(Mem *p){1026 assert( p->db!=0 );1027 if( p->flags & (MEM_Str|MEM_Blob) ){1028 int n = p->n;1029 if( p->flags & MEM_Zero ){1030 n += p->u.nZero;1031 }1032 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];1033 }1034 return 0; 1035}1036 1037#ifdef SQLITE_DEBUG1038/*1039** This routine prepares a memory cell for modification by breaking1040** its link to a shallow copy and by marking any current shallow1041** copies of this cell as invalid.1042**1043** This is used for testing and debugging only - to help ensure that shallow1044** copies (created by OP_SCopy) are not misused.1045*/1046void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){1047 int i;1048 Mem *pX;1049 if( pMem->bScopy ){1050 for(i=1, pX=pVdbe->aMem+1; i<pVdbe->nMem; i++, pX++){1051 if( pX->pScopyFrom==pMem ){1052 u16 mFlags;1053 if( pVdbe->db->flags & SQLITE_VdbeTrace ){1054 sqlite3DebugPrintf("Invalidate R[%d] due to change in R[%d]\n",1055 (int)(pX - pVdbe->aMem), (int)(pMem - pVdbe->aMem));1056 }1057 /* If pX is marked as a shallow copy of pMem, then try to verify that1058 ** no significant changes have been made to pX since the OP_SCopy.1059 ** A significant change would indicated a missed call to this1060 ** function for pX. Minor changes, such as adding or removing a1061 ** dual type, are allowed, as long as the underlying value is the1062 ** same. */1063 mFlags = pMem->flags & pX->flags & pX->mScopyFlags;1064 assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i );1065 1066 /* pMem is the register that is changing. But also mark pX as1067 ** undefined so that we can quickly detect the shallow-copy error */1068 pX->flags = MEM_Undefined;1069 pX->pScopyFrom = 0;1070 }1071 }1072 pMem->bScopy = 0;1073 }1074 pMem->pScopyFrom = 0;1075}1076#endif /* SQLITE_DEBUG */1077 1078/*1079** Make an shallow copy of pFrom into pTo. Prior contents of1080** pTo are freed. The pFrom->z field is not duplicated. If1081** pFrom->z is used, then pTo->z points to the same thing as pFrom->z1082** and flags gets srcType (either MEM_Ephem or MEM_Static).1083*/1084static SQLITE_NOINLINE void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){1085 vdbeMemClearExternAndSetNull(pTo);1086 assert( !VdbeMemDynamic(pTo) );1087 sqlite3VdbeMemShallowCopy(pTo, pFrom, eType);1088}1089void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){1090 assert( !sqlite3VdbeMemIsRowSet(pFrom) );1091 assert( pTo->db==pFrom->db );1092 if( VdbeMemDynamic(pTo) ){ vdbeClrCopy(pTo,pFrom,srcType); return; }1093 memcpy(pTo, pFrom, MEMCELLSIZE);1094 if( (pFrom->flags&MEM_Static)==0 ){1095 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);1096 assert( srcType==MEM_Ephem || srcType==MEM_Static );1097 pTo->flags |= srcType;1098 }1099}1100 1101/*1102** Make a full copy of pFrom into pTo. Prior contents of pTo are1103** freed before the copy is made.1104*/1105int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){1106 int rc = SQLITE_OK;1107 1108 assert( !sqlite3VdbeMemIsRowSet(pFrom) );1109 if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);1110 memcpy(pTo, pFrom, MEMCELLSIZE);1111 pTo->flags &= ~MEM_Dyn;1112 if( pTo->flags&(MEM_Str|MEM_Blob) ){1113 if( 0==(pFrom->flags&MEM_Static) ){1114 pTo->flags |= MEM_Ephem;1115 rc = sqlite3VdbeMemMakeWriteable(pTo);1116 }1117 }1118 1119 return rc;1120}1121 1122/*1123** Transfer the contents of pFrom to pTo. Any existing value in pTo is1124** freed. If pFrom contains ephemeral data, a copy is made.1125**1126** pFrom contains an SQL NULL when this routine returns.1127*/1128void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){1129 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );1130 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );1131 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );1132 1133 sqlite3VdbeMemRelease(pTo);1134 memcpy(pTo, pFrom, sizeof(Mem));1135 pFrom->flags = MEM_Null;1136 pFrom->szMalloc = 0;1137}1138 1139/*1140** Change the value of a Mem to be a string or a BLOB.1141**1142** The memory management strategy depends on the value of the xDel1143** parameter. If the value passed is SQLITE_TRANSIENT, then the 1144** string is copied into a (possibly existing) buffer managed by the 1145** Mem structure. Otherwise, any existing buffer is freed and the1146** pointer copied.1147**1148** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH1149** size limit) then no memory allocation occurs. If the string can be1150** stored without allocating memory, then it is. If a memory allocation1151** is required to store the string, then value of pMem is unchanged. In1152** either case, SQLITE_TOOBIG is returned.1153**1154** The "enc" parameter is the text encoding for the string, or zero1155** to store a blob.1156**1157** If n is negative, then the string consists of all bytes up to but1158** excluding the first zero character. The n parameter must be1159** non-negative for blobs.1160*/1161int sqlite3VdbeMemSetStr(1162 Mem *pMem, /* Memory cell to set to string value */1163 const char *z, /* String pointer */1164 i64 n, /* Bytes in string, or negative */1165 u8 enc, /* Encoding of z. 0 for BLOBs */1166 void (*xDel)(void*) /* Destructor function */1167){1168 i64 nByte = n; /* New value for pMem->n */1169 int iLimit; /* Maximum allowed string or blob size */1170 u16 flags; /* New value for pMem->flags */1171 1172 assert( pMem!=0 );1173 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );1174 assert( !sqlite3VdbeMemIsRowSet(pMem) );1175 assert( enc!=0 || n>=0 );1176 1177 /* If z is a NULL pointer, set pMem to contain an SQL NULL. */1178 if( !z ){1179 sqlite3VdbeMemSetNull(pMem);1180 return SQLITE_OK;1181 }1182 1183 if( pMem->db ){1184 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];1185 }else{1186 iLimit = SQLITE_MAX_LENGTH;1187 }1188 if( nByte<0 ){1189 assert( enc!=0 );1190 if( enc==SQLITE_UTF8 ){1191 nByte = strlen(z);1192 }else{1193 for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}1194 }1195 flags= MEM_Str|MEM_Term;1196 }else if( enc==0 ){1197 flags = MEM_Blob;1198 enc = SQLITE_UTF8;1199 }else{1200 flags = MEM_Str;