From 6ed13f71e9f80d63f4593a09ba2c9c1b03814ad9 Mon Sep 17 00:00:00 2001 From: "D. Richard Hipp" Date: Wed, 9 May 2018 14:29:40 +0000 Subject: [PATCH] Fix minor problems with the sqlite3_str interface. --- src/printf.c | 13 ++++++++---- src/sqlite.h.in | 63 +++++++++++++++++++++++++++------------------------------ 2 files changed, 39 insertions(+), 37 deletions(-) diff --git a/src/printf.c b/src/printf.c index ee4c089c33..3fc4aca55e 100644 --- a/src/printf.c +++ b/src/printf.c @@ -973,7 +973,7 @@ char *sqlite3_str_finish(sqlite3_str *p){ char *z; if( p ){ z = sqlite3StrAccumFinish(p); - sqlite3DbFree(p->db, p); + sqlite3_free(p); }else{ z = 0; } @@ -992,7 +992,9 @@ int sqlite3_str_length(sqlite3_str *p){ /* Return the current value for p */ char *sqlite3_str_value(sqlite3_str *p){ - return p ? p->zText : 0; + if( p==0 || p->nChar==0 ) return 0; + p->zText[p->nChar] = 0; + return p->zText; } /* @@ -1003,6 +1005,8 @@ void sqlite3_str_reset(StrAccum *p){ sqlite3DbFree(p->db, p->zText); p->printfFlags &= ~SQLITE_PRINTF_MALLOCED; } + p->nAlloc = 0; + p->nChar = 0; p->zText = 0; } @@ -1032,9 +1036,10 @@ void sqlite3StrAccumInit(StrAccum *p, sqlite3 *db, char *zBase, int n, int mx){ /* Allocate and initialize a new dynamic string object */ sqlite3_str *sqlite3_str_new(sqlite3 *db){ - sqlite3_str *p = sqlite3DbMallocRaw(db, sizeof(*p)); + sqlite3_str *p = sqlite3_malloc64(sizeof(*p)); if( p ){ - sqlite3StrAccumInit(p, db, 0, 0, SQLITE_MAX_LENGTH); + sqlite3StrAccumInit(p, 0, 0, 0, + db ? db->aLimit[SQLITE_LIMIT_LENGTH] : SQLITE_MAX_LENGTH); } return p; } diff --git a/src/sqlite.h.in b/src/sqlite.h.in index 2ed277e234..e2413d4d30 100644 --- a/src/sqlite.h.in +++ b/src/sqlite.h.in @@ -7141,10 +7141,10 @@ int sqlite3_keyword_check(const char*,int); ** ** The lifecycle of an sqlite3_str object is as follows: **
    -**
  1. The sqlite3_str object is created using [sqlite3_str_new()]. -**
  2. Text is appended to the sqlite3_str object using various +**
  3. ^The sqlite3_str object is created using [sqlite3_str_new()]. +**
  4. ^Text is appended to the sqlite3_str object using various ** methods, such as [sqlite3_str_appendf()]. -**
  5. The sqlite3_str object is destroyed and the string it created +**
  6. ^The sqlite3_str object is destroyed and the string it created ** is returned using the [sqlite3_str_finish()] interface. **
*/ @@ -7154,21 +7154,18 @@ typedef struct sqlite3_str sqlite3_str; ** CAPI3REF: Create A New Dynamic String Object ** CONSTRUCTOR: sqlite3_str ** -** The [sqlite3_str_new(D)] allocates and initializes a new [sqlite3_str] -** object. The [sqlite3_str_new(D)] interface returns NULL on an out-of-memory +** ^The [sqlite3_str_new(D)] interface allocates and initializes +** a new [sqlite3_str] +** object. ^The [sqlite3_str_new()] interface returns NULL on an out-of-memory ** condition. To avoid memory leaks, the object returned by -** [sqlite3_str_new(D)] must be freed by a subsequent call to -** [sqlite3_str_finish(S)]. -** -** If the D argument to [sqlite3_str_new(D)] is NULL then memory used to -** construct the string is always taken from the global memory pool used -** by [sqlite3_malloc64()]. If D is not NULL, then a private memory pool -** used by connection D might also be used. This private memory pool is -** faster, but is limited in space, and should only be used for transient -** allocations. The final string returned by [sqlite3_str_finish(X)] is -** always stored in space obtained from [sqlite3_malloc64()] regardless -** of whether or not the private per-connection memory pool is used during -** its construction. +** [sqlite3_str_new()] must be freed by a subsequent call to +** [sqlite3_str_finish(X)]. +** +** The D parameter to [sqlite3_str_new(D)] may be NULL. If the +** D parameter in [sqlite3_str_new(D)] is not NULL, then the maximum +** length of the string contained in the [sqlite3_str] object will be +** the value set for [sqlite3_limit](D,[SQLITE_LIMIT_LENGTH]) instead +** of [SQLITE_MAX_LENGTH]. */ sqlite3_str *sqlite3_str_new(sqlite3*); @@ -7176,12 +7173,12 @@ sqlite3_str *sqlite3_str_new(sqlite3*); ** CAPI3REF: Finalize A Dynamic String ** DESTRUCTOR: sqlite3_str ** -** The [sqlite3_str_finish(X)] interface destroys the sqlite3_str object X +** ^The [sqlite3_str_finish(X)] interface destroys the sqlite3_str object X ** and returns a pointer to a memory buffer obtained from [sqlite3_malloc64()] ** that contains the constructed string. The calling application should ** pass the returned value to [sqlite3_free()] to avoid a memory leak. -** The [sqlite3_str_finish(X)] interface may return a NULL pointer if any -** errors were encountered during construction of the string. The +** ^The [sqlite3_str_finish(X)] interface may return a NULL pointer if any +** errors were encountered during construction of the string. ^The ** [sqlite3_str_finish(X)] interface will also return a NULL pointer if the ** string in [sqlite3_str] object X is zero bytes long. */ @@ -7194,28 +7191,28 @@ char *sqlite3_str_finish(sqlite3_str*); ** These interfaces add content to an sqlite3_str object previously obtained ** from [sqlite3_str_new()]. ** -** The [sqlite3_str_appendf(X,F,...)] and +** ^The [sqlite3_str_appendf(X,F,...)] and ** [sqlite3_str_vappendf(X,F,V)] interfaces uses the [built-in printf] ** functionality of SQLite to append formatted text onto the end of ** [sqlite3_str] object X. ** -** The [sqlite3_str_append(X,S,N)] method appends exactly N bytes from string S +** ^The [sqlite3_str_append(X,S,N)] method appends exactly N bytes from string S ** onto the end of the [sqlite3_str] object X. N must be non-negative. ** S must contain at least N non-zero bytes of content. To append a ** zero-terminated string in its entirety, use the [sqlite3_str_appendall()] ** method instead. ** -** The [sqlite3_str_appendall(X,S)] method the complete content of +** ^The [sqlite3_str_appendall(X,S)] method appends the complete content of ** zero-terminated string S onto the end of [sqlite3_str] object X. ** -** The [sqlite3_str_appendchar(X,N,C)] method appends N copies of the +** ^The [sqlite3_str_appendchar(X,N,C)] method appends N copies of the ** single-byte character C onto the end of [sqlite3_str] object X. -** This method can be used, for example, to add whitespace indentation. +** ^This method can be used, for example, to add whitespace indentation. ** -** The [sqlite3_str_reset(X)] method resets the string under construction +** ^The [sqlite3_str_reset(X)] method resets the string under construction ** inside [sqlite3_str] object X back to zero bytes in length. ** -** These methods do not return a result code. If an error occurs, that fact +** These methods do not return a result code. ^If an error occurs, that fact ** is recorded in the [sqlite3_str] object and can be recovered by a ** subsequent call to [sqlite3_str_errcode(X)]. */ @@ -7232,25 +7229,25 @@ void sqlite3_str_reset(sqlite3_str*); ** ** These interfaces return the current status of an [sqlite3_str] object. ** -** If any prior errors have occurred while constructing the dynamic string +** ^If any prior errors have occurred while constructing the dynamic string ** in sqlite3_str X, then the [sqlite3_str_errcode(X)] method will return -** an appropriate error code. The [sqlite3_str_errcode(X)] method returns +** an appropriate error code. ^The [sqlite3_str_errcode(X)] method returns ** [SQLITE_NOMEM] following any out-of-memory error, or ** [SQLITE_TOOBIG] if the size of the dynamic string exceeds ** [SQLITE_MAX_LENGTH], or [SQLITE_OK] if there have been no errors. ** -** The [sqlite3_str_length(X)] method returns the current length, in bytes, +** ^The [sqlite3_str_length(X)] method returns the current length, in bytes, ** of the dynamic string under construction in [sqlite3_str] object X. -** The length returned by [sqlite3_str_length(X)] does not include the +** ^The length returned by [sqlite3_str_length(X)] does not include the ** zero-termination byte. ** -** The [sqlite3_str_value(X)] method returns a pointer to the current +** ^The [sqlite3_str_value(X)] method returns a pointer to the current ** content of the dynamic string under construction in X. The value ** returned by [sqlite3_str_value(X)] is managed by the sqlite3_str object X ** and might be freed or altered by any subsequent method on the same ** [sqlite3_str] object. Applications must not used the pointer returned ** [sqlite3_str_value(X)] after any subsequent method call on the same -** object. Applications may change the content of the string returned +** object. ^Applications may change the content of the string returned ** by [sqlite3_str_value(X)] as long as they do not write into any bytes ** outside the range of 0 to [sqlite3_str_length(X)] and do not read or ** write any byte after any subsequent sqlite3_str method call. -- 2.11.4.GIT