[tarantool-patches] [PATCH v1 20/28] sql: remove SQL_NOMEM errcode

imeevma at tarantool.org imeevma at tarantool.org
Mon Jun 10 16:56:58 MSK 2019


Removing this error code is part of getting rid of the SQL error
system.
---
 src/box/sql/fault.c    |  4 ++--
 src/box/sql/func.c     | 14 ++++----------
 src/box/sql/legacy.c   |  1 -
 src/box/sql/main.c     | 26 ++++++++++---------------
 src/box/sql/malloc.c   |  7 ++-----
 src/box/sql/os.c       |  4 ++--
 src/box/sql/os_unix.c  |  4 ++--
 src/box/sql/select.c   | 11 +++++------
 src/box/sql/sqlInt.h   |  4 +---
 src/box/sql/vdbe.c     | 14 +++-----------
 src/box/sql/vdbeapi.c  | 52 ++------------------------------------------------
 src/box/sql/vdbeaux.c  | 48 ++++++++++++++++------------------------------
 src/box/sql/vdbemem.c  | 25 ++++++++++++------------
 src/box/sql/vdbesort.c | 46 +++++++++++++++++++++-----------------------
 src/box/sql/where.c    | 12 ++++++------
 15 files changed, 89 insertions(+), 183 deletions(-)

diff --git a/src/box/sql/fault.c b/src/box/sql/fault.c
index 8ee774e..0e0978d 100644
--- a/src/box/sql/fault.c
+++ b/src/box/sql/fault.c
@@ -37,8 +37,8 @@
  * and returns 0).
  *
  * Most malloc failures are non-benign. After they occur, sql
- * abandons the current operation and returns an error code (usually
- * SQL_NOMEM) to the user. However, sometimes a fault is not necessarily
+ * abandons the current operation and returns an -1
+ * to the user. However, sometimes a fault is not necessarily
  * fatal. For example, if a malloc fails while resizing a hash table, this
  * is completely recoverable simply by not carrying out the resize. The
  * hash table will continue to function normally.  So a malloc failure
diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index 29712e0..994d97e 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -1799,14 +1799,11 @@ static inline int
 sql_overload_function(sql * db, const char *zName,
 			  enum field_type type, int nArg)
 {
-	int rc = 0;
-
 	if (sqlFindFunction(db, zName, nArg, 0) == 0) {
-		rc = sqlCreateFunc(db, zName, type, nArg, 0, 0,
-				       sqlInvalidFunction, 0, 0, 0);
+		return sqlCreateFunc(db, zName, type, nArg, 0, 0,
+				     sqlInvalidFunction, 0, 0, 0);
 	}
-	rc = sqlApiExit(db, rc);
-	return rc;
+	return 0;
 }
 
 /*
@@ -1817,11 +1814,8 @@ sql_overload_function(sql * db, const char *zName,
 void
 sqlRegisterPerConnectionBuiltinFunctions(sql * db)
 {
-	int rc = sql_overload_function(db, "MATCH", FIELD_TYPE_SCALAR, 2);
-	assert(rc == SQL_NOMEM || rc == 0);
-	if (rc == SQL_NOMEM) {
+	if (sql_overload_function(db, "MATCH", FIELD_TYPE_SCALAR, 2) != 0)
 		sqlOomFault(db);
-	}
 }
 
 /*
diff --git a/src/box/sql/legacy.c b/src/box/sql/legacy.c
index 3101428..df40032 100644
--- a/src/box/sql/legacy.c
+++ b/src/box/sql/legacy.c
@@ -162,7 +162,6 @@ sql_exec(sql * db,	/* The database on which the SQL executes */
 		sqlVdbeFinalize((Vdbe *) pStmt);
 	sqlDbFree(db, azCols);
 
-	rc = sqlApiExit(db, rc);
 	assert(rc == 0);
 	assert((rc & db->errMask) == rc);
 	return rc;
diff --git a/src/box/sql/main.c b/src/box/sql/main.c
index 8474836..e6d3a3d 100644
--- a/src/box/sql/main.c
+++ b/src/box/sql/main.c
@@ -525,16 +525,16 @@ int
 sql_init_db(sql **out_db)
 {
 	sql *db;
-	int rc;			/* Return code */
 
-	rc = sql_initialize();
-	if (rc)
-		return rc;
+	if (sql_initialize())
+		return -1;
 
 	/* Allocate the sql data structure */
 	db = sqlMallocZero(sizeof(sql));
-	if (db == 0)
-		goto opendb_out;
+	if (db == NULL) {
+		*out_db = NULL;
+		return -1;
+	}
 	db->errMask = 0xff;
 	db->magic = SQL_MAGIC_BUSY;
 
@@ -549,7 +549,9 @@ sql_init_db(sql **out_db)
 
 	db->magic = SQL_MAGIC_OPEN;
 	if (db->mallocFailed) {
-		goto opendb_out;
+		sql_free(db);
+		*out_db = NULL;
+		return -1;
 	}
 
 	/* Register all built-in functions, but do not attempt to read the
@@ -558,16 +560,8 @@ sql_init_db(sql **out_db)
 	 */
 	sqlRegisterPerConnectionBuiltinFunctions(db);
 
-opendb_out:
-	assert(db != 0 || rc == SQL_NOMEM);
-	if (rc == SQL_NOMEM)
-		db = NULL;
-	else if (rc != 0)
-		db->magic = SQL_MAGIC_SICK;
-
 	*out_db = db;
-
-	return rc;
+	return 0;
 }
 
 /*
diff --git a/src/box/sql/malloc.c b/src/box/sql/malloc.c
index 8830cc8..4b8ef8f 100644
--- a/src/box/sql/malloc.c
+++ b/src/box/sql/malloc.c
@@ -765,7 +765,7 @@ static SQL_NOINLINE int
 apiOomError(sql * db)
 {
 	sqlOomClear(db);
-	return SQL_NOMEM;
+	return -1;
 }
 
 /*
@@ -774,10 +774,7 @@ apiOomError(sql * db)
  * sql_realloc.
  *
  * The returned value is normally a copy of the second argument to this
- * function. However, if a malloc() failure has occurred since the previous
- * invocation SQL_NOMEM is returned instead.
- *
- * If an OOM as occurred, SQL_NOMEM is returned.
+ * function.
  */
 int
 sqlApiExit(sql * db, int rc)
diff --git a/src/box/sql/os.c b/src/box/sql/os.c
index 273aa6b..9f45eba 100644
--- a/src/box/sql/os.c
+++ b/src/box/sql/os.c
@@ -170,7 +170,7 @@ sqlOsOpenMalloc(sql_vfs * pVfs,
 			*ppFile = pFile;
 		}
 	} else {
-		rc = SQL_NOMEM;
+		rc = -1;
 	}
 	return rc;
 }
@@ -194,7 +194,7 @@ sqlOsInit(void)
 {
 	void *p = sql_malloc(10);
 	if (p == 0)
-		return SQL_NOMEM;
+		return -1;
 	sql_free(p);
 	return sql_os_init();
 }
diff --git a/src/box/sql/os_unix.c b/src/box/sql/os_unix.c
index 8b3aec1..e2bda51 100644
--- a/src/box/sql/os_unix.c
+++ b/src/box/sql/os_unix.c
@@ -409,7 +409,7 @@ findInodeInfo(unixFile * pFile,	/* Unix file with file desc used in the key */
 	if (pInode == 0) {
 		pInode = sql_malloc64(sizeof(*pInode));
 		if (pInode == 0) {
-			return SQL_NOMEM;
+			return -1;
 		}
 		memset(pInode, 0, sizeof(*pInode));
 		memcpy(&pInode->fileId, &fileId, sizeof(fileId));
@@ -1697,7 +1697,7 @@ unixOpen(sql_vfs * pVfs,	/* The VFS for which this is the xOpen method */
 		} else {
 			pUnused = sql_malloc64(sizeof(*pUnused));
 			if (!pUnused) {
-				return SQL_NOMEM;
+				return -1;
 			}
 		}
 		p->pUnused = pUnused;
diff --git a/src/box/sql/select.c b/src/box/sql/select.c
index a1bed97..2ecad78 100644
--- a/src/box/sql/select.c
+++ b/src/box/sql/select.c
@@ -1822,7 +1822,7 @@ generateColumnNames(Parse * pParse,	/* Parser context */
  * and other fields of Column are zeroed.
  *
  * Return 0 on success.  If a memory allocation error occurs,
- * store NULL in *paCol and 0 in *pnCol and return SQL_NOMEM.
+ * store NULL in *paCol and 0 in *pnCol and return -1.
  */
 int
 sqlColumnsFromExprList(Parse * parse, ExprList * expr_list,
@@ -1925,17 +1925,16 @@ sqlColumnsFromExprList(Parse * parse, ExprList * expr_list,
 	}
 cleanup:
 	sqlHashClear(&ht);
-	int rc = db->mallocFailed ? SQL_NOMEM : 0;
-	if (rc != 0) {
+	if (db->mallocFailed) {
 		/*
 		 * pTable->def could be not temporal in
 		 * sqlViewGetColumnNames so we need clean-up.
 		 */
 		space_def->fields = NULL;
 		space_def->field_count = 0;
-		rc = SQL_NOMEM;
+		return -1;
 	}
-	return rc;
+	return 0;
 
 }
 
@@ -4636,7 +4635,7 @@ withExpand(Walker * pWalker, struct SrcList_item *pFrom)
 			return WRC_Abort;
 		pFrom->pSelect = sqlSelectDup(db, pCte->pSelect, 0);
 		if (db->mallocFailed)
-			return SQL_NOMEM;
+			return -1;
 		assert(pFrom->pSelect);
 
 		/* Check if this is a recursive CTE. */
diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h
index 3aabcb2..0743952 100644
--- a/src/box/sql/sqlInt.h
+++ b/src/box/sql/sqlInt.h
@@ -356,10 +356,8 @@ struct sql_vfs {
 #define SQL_LIMIT_WORKER_THREADS           10
 
 enum sql_ret_code {
-	/** A malloc() failed. */
-	SQL_NOMEM = 2,
 	/** Some kind of disk I/O error occurred. */
-	SQL_IOERR,
+	SQL_IOERR = 3,
 	/** Abort due to constraint violation. */
 	SQL_TARANTOOL_ERROR,
 	/** sql_step() has another row ready. */
diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c
index c8ca8c4..c592518 100644
--- a/src/box/sql/vdbe.c
+++ b/src/box/sql/vdbe.c
@@ -703,7 +703,7 @@ vdbe_field_ref_fetch(struct vdbe_field_ref *field_ref, uint32_t fieldno,
 		int len = dest_mem->n;
 		if (dest_mem->szMalloc < len + 1) {
 			if (sqlVdbeMemGrow(dest_mem, len + 1, 1) != 0)
-				return SQL_NOMEM;
+				return SQL_TARANTOOL_ERROR;
 		} else {
 			dest_mem->z =
 				memcpy(dest_mem->zMalloc, dest_mem->z, len);
@@ -746,12 +746,6 @@ int sqlVdbeExec(Vdbe *p)
 	/*** INSERT STACK UNION HERE ***/
 
 	assert(p->magic==VDBE_MAGIC_RUN);  /* sql_step() verifies this */
-	if (p->rc==SQL_NOMEM) {
-		/* This happens if a malloc() inside a call to sql_column_text() or
-		 * sql_column_text16() failed.
-		 */
-		goto no_mem;
-	}
 	assert(p->rc == 0);
 	p->rc = 0;
 	p->iCurrentTime = 0;
@@ -2624,8 +2618,7 @@ case OP_Column: {
 	}
 	struct Mem *default_val_mem =
 		pOp->p4type == P4_MEM ? pOp->p4.pMem : NULL;
-	rc = vdbe_field_ref_fetch(&pC->field_ref, p2, pDest);
-	if (rc != 0)
+	if (vdbe_field_ref_fetch(&pC->field_ref, p2, pDest) != 0)
 		goto abort_due_to_error;
 
 	if ((pDest->flags & MEM_Null) &&
@@ -2658,8 +2651,7 @@ case OP_Fetch: {
 	uint32_t field_idx = pOp->p2;
 	struct Mem *dest_mem = &aMem[pOp->p3];
 	memAboutToChange(p, dest_mem);
-	rc = vdbe_field_ref_fetch(field_ref, field_idx, dest_mem);
-	if (rc != 0)
+	if (vdbe_field_ref_fetch(field_ref, field_idx, dest_mem) != 0)
 		goto abort_due_to_error;
 	REGISTER_TRACE(p, pOp->p3, dest_mem);
 	break;
diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c
index db8687b..336dc3b 100644
--- a/src/box/sql/vdbeapi.c
+++ b/src/box/sql/vdbeapi.c
@@ -422,8 +422,8 @@ sqlStep(Vdbe * p)
 	/* Check that malloc() has not failed. If it has, return early. */
 	db = p->db;
 	if (db->mallocFailed) {
-		p->rc = SQL_NOMEM;
-		return SQL_NOMEM;
+		p->rc = -1;
+		return -1;
 	}
 
 	if (p->pc <= 0 && p->expired) {
@@ -458,9 +458,6 @@ sqlStep(Vdbe * p)
 		checkProfileCallback(db, p);
 #endif
 
-	if (SQL_NOMEM == sqlApiExit(p->db, p->rc)) {
-		p->rc = SQL_NOMEM;
-	}
 	if (p->isPrepareV2 && rc != SQL_ROW && rc != SQL_DONE) {
 		/* If this statement was prepared using sql_prepare_v2(), and an
 		 * error has occurred, then return the error code in p->rc to the
@@ -686,38 +683,6 @@ columnMem(sql_stmt * pStmt, int i)
 	return pOut;
 }
 
-/*
- * This function is called after invoking an sql_value_XXX function on a
- * column value (i.e. a value returned by evaluating an SQL expression in the
- * select list of a SELECT statement) that may cause a malloc() failure. If
- * malloc() has failed, the threads mallocFailed flag is cleared and the result
- * code of statement pStmt set to SQL_NOMEM.
- *
- * Specifically, this is called from within:
- *
- *     sql_column_int()
- *     sql_column_int64()
- *     sql_column_text()
- *     sql_column_real()
- *     sql_column_bytes()
- *     sql_column_bytes16()
- *     sqiite3_column_blob()
- */
-static void
-columnMallocFailure(sql_stmt * pStmt)
-{
-	/* If malloc() failed during an encoding conversion within an
-	 * sql_column_XXX API, then set the return code of the statement to
-	 * SQL_NOMEM. The next call to _step() (if any) will return -1
-	 * and _finalize() will return NOMEM.
-	 */
-	Vdbe *p = (Vdbe *) pStmt;
-	if (p) {
-		assert(p->db != 0);
-		p->rc = sqlApiExit(p->db, p->rc);
-	}
-}
-
 /**************************** sql_column_  ******************************
  * The following routines are used to access elements of the current row
  * in the result set.
@@ -727,11 +692,6 @@ sql_column_blob(sql_stmt * pStmt, int i)
 {
 	const void *val;
 	val = sql_value_blob(columnMem(pStmt, i));
-	/* Even though there is no encoding conversion, value_blob() might
-	 * need to call malloc() to expand the result of a zeroblob()
-	 * expression.
-	 */
-	columnMallocFailure(pStmt);
 	return val;
 }
 
@@ -739,7 +699,6 @@ int
 sql_column_bytes(sql_stmt * pStmt, int i)
 {
 	int val = sql_value_bytes(columnMem(pStmt, i));
-	columnMallocFailure(pStmt);
 	return val;
 }
 
@@ -747,7 +706,6 @@ double
 sql_column_double(sql_stmt * pStmt, int i)
 {
 	double val = sql_value_double(columnMem(pStmt, i));
-	columnMallocFailure(pStmt);
 	return val;
 }
 
@@ -755,7 +713,6 @@ int
 sql_column_int(sql_stmt * pStmt, int i)
 {
 	int val = sql_value_int(columnMem(pStmt, i));
-	columnMallocFailure(pStmt);
 	return val;
 }
 
@@ -763,7 +720,6 @@ bool
 sql_column_boolean(struct sql_stmt *stmt, int i)
 {
 	bool val = sql_value_boolean(columnMem(stmt, i));
-	columnMallocFailure(stmt);
 	return val;
 }
 
@@ -771,7 +727,6 @@ sql_int64
 sql_column_int64(sql_stmt * pStmt, int i)
 {
 	sql_int64 val = sql_value_int64(columnMem(pStmt, i));
-	columnMallocFailure(pStmt);
 	return val;
 }
 
@@ -779,7 +734,6 @@ const unsigned char *
 sql_column_text(sql_stmt * pStmt, int i)
 {
 	const unsigned char *val = sql_value_text(columnMem(pStmt, i));
-	columnMallocFailure(pStmt);
 	return val;
 }
 
@@ -791,7 +745,6 @@ sql_column_value(sql_stmt * pStmt, int i)
 		pOut->flags &= ~MEM_Static;
 		pOut->flags |= MEM_Ephem;
 	}
-	columnMallocFailure(pStmt);
 	return (sql_value *) pOut;
 }
 
@@ -799,7 +752,6 @@ enum mp_type
 sql_column_type(sql_stmt * pStmt, int i)
 {
 	enum mp_type type = sql_value_type(columnMem(pStmt, i));
-	columnMallocFailure(pStmt);
 	return type;
 }
 
diff --git a/src/box/sql/vdbeaux.c b/src/box/sql/vdbeaux.c
index e056c40..7408b9f 100644
--- a/src/box/sql/vdbeaux.c
+++ b/src/box/sql/vdbeaux.c
@@ -163,7 +163,7 @@ sqlVdbeSwap(Vdbe * pA, Vdbe * pB)
  * to 1024/sizeof(Op).
  *
  * If an out-of-memory error occurs while resizing the array, return
- * SQL_NOMEM. In this case Vdbe.aOp and Parse.nOpAlloc remain
+ * -1. In this case Vdbe.aOp and Parse.nOpAlloc remain
  * unchanged (this is so that any opcodes already allocated can be
  * correctly deallocated along with the rest of the Vdbe).
  */
@@ -195,8 +195,9 @@ growOpArray(Vdbe * v, int nOp)
 		p->szOpAlloc = sqlDbMallocSize(pNew);
 		p->nOpAlloc = p->szOpAlloc / sizeof(Op);
 		v->aOp = pNew;
+		return 0;
 	}
-	return (pNew ? 0 : SQL_NOMEM);
+	return -1;
 }
 
 #ifdef SQL_DEBUG
@@ -1413,14 +1414,12 @@ sqlVdbeList(Vdbe * p)
 	int nSub = 0;		/* Number of sub-vdbes seen so far */
 	SubProgram **apSub = 0;	/* Array of sub-vdbes */
 	Mem *pSub = 0;		/* Memory cell hold array of subprogs */
-	sql *db = p->db;	/* The database connection */
 	int i;			/* Loop counter */
 	int rc = 0;	/* Return code */
 	Mem *pMem = &p->aMem[1];	/* First Mem of result set */
 
 	assert(p->explain);
 	assert(p->magic == VDBE_MAGIC_RUN);
-	assert(p->rc == 0 || p->rc == SQL_NOMEM);
 
 	/* Even though this opcode does not use dynamic strings for
 	 * the result, result columns may become dynamic if the user calls
@@ -1429,14 +1428,6 @@ sqlVdbeList(Vdbe * p)
 	releaseMemArray(pMem, 8);
 	p->pResultSet = 0;
 
-	if (p->rc == SQL_NOMEM) {
-		/* This happens if a malloc() inside a call to sql_column_text() or
-		 * sql_column_text16() failed.
-		 */
-		sqlOomFault(db);
-		return -1;
-	}
-
 	/* When the number of output rows reaches nRow, that means the
 	 * listing has finished and sql_step() should return SQL_DONE.
 	 * nRow is the sum of the number of rows in the main program, plus
@@ -2019,7 +2010,7 @@ sqlVdbeSetColName(Vdbe * p,			/* Vdbe being configured */
 	assert(var < COLNAME_N);
 	if (p->db->mallocFailed) {
 		assert(!zName || xDel != SQL_DYNAMIC);
-		return SQL_NOMEM;
+		return -1;
 	}
 	assert(p->aColName != 0);
 	assert(var == COLNAME_NAME || var == COLNAME_DECLTYPE);
@@ -2173,7 +2164,6 @@ sqlVdbeHalt(Vdbe * p)
 	 *
 	 * If any of the following errors occur:
 	 *
-	 *     SQL_NOMEM
 	 *     SQL_IOERR
 	 *
 	 * Then the internal cache might have been left in an inconsistent
@@ -2182,7 +2172,7 @@ sqlVdbeHalt(Vdbe * p)
 	 */
 
 	if (db->mallocFailed) {
-		p->rc = SQL_NOMEM;
+		p->rc = -1;
 	}
 	closeTopFrameCursors(p);
 	if (p->magic != VDBE_MAGIC_RUN) {
@@ -2200,7 +2190,7 @@ sqlVdbeHalt(Vdbe * p)
 
 		/* Check for one of the special errors */
 		mrc = p->rc & 0xff;
-		isSpecialError = mrc == SQL_NOMEM || mrc == SQL_IOERR;
+		isSpecialError = mrc == SQL_IOERR;
 		if (isSpecialError) {
 			/* At least a savepoint transaction must be rolled back
 			 * to restore the database to a consistent state.
@@ -2211,20 +2201,14 @@ sqlVdbeHalt(Vdbe * p)
 			 * file as part of an effort to free up cache space (see function
 			 * pagerStress() in pager.c), the rollback is required to restore
 			 * the pager to a consistent state.
+			 * We are forced to roll back the active transaction. Before doing
+			 * so, abort any other statements this handle currently has active.
 			 */
-			if ((mrc == SQL_NOMEM)
-			    && box_txn()) {
-				eStatementOp = SAVEPOINT_ROLLBACK;
-			} else {
-				/* We are forced to roll back the active transaction. Before doing
-				 * so, abort any other statements this handle currently has active.
-				 */
-				box_txn_rollback();
-				closeCursorsAndFree(p);
-				sqlRollbackAll(p);
-				sqlCloseSavepoints(p);
-				p->nChange = 0;
-			}
+			box_txn_rollback();
+			closeCursorsAndFree(p);
+			sqlRollbackAll(p);
+			sqlCloseSavepoints(p);
+			p->nChange = 0;
 		}
 
 		/* Check for immediate foreign key violations. */
@@ -2333,7 +2317,7 @@ sqlVdbeHalt(Vdbe * p)
 	p->magic = VDBE_MAGIC_HALT;
 	checkActiveVdbeCnt(db);
 	if (db->mallocFailed) {
-		p->rc = SQL_NOMEM;
+		p->rc = -1;
 	}
 
 	assert(db->nVdbeActive > 0 || box_txn() ||
@@ -2974,7 +2958,7 @@ sql_vdbe_mem_alloc_region(Mem *vdbe_mem, uint32_t size)
 	vdbe_mem->n = size;
 	vdbe_mem->z = region_alloc(&fiber()->gc, size);
 	if (vdbe_mem->z == NULL)
-		return SQL_NOMEM;
+		return -1;
 	vdbe_mem->flags = MEM_Ephem | MEM_Blob;
 	assert(sqlVdbeCheckMemInvariants(vdbe_mem));
 	return 0;
@@ -2991,7 +2975,7 @@ sql_vdbe_mem_alloc_region(Mem *vdbe_mem, uint32_t size)
 static int
 vdbeCompareMemString(const Mem * pMem1, const Mem * pMem2,
 		     const struct coll * pColl,
-		     u8 * prcErr)	/* If an OOM occurs, set to SQL_NOMEM */
+		     u8 * prcErr)
 {
 	(void) prcErr;
 	return pColl->cmp(pMem1->z, (size_t)pMem1->n,
diff --git a/src/box/sql/vdbemem.c b/src/box/sql/vdbemem.c
index 933ad6f..da63193 100644
--- a/src/box/sql/vdbemem.c
+++ b/src/box/sql/vdbemem.c
@@ -129,7 +129,7 @@ sqlVdbeMemGrow(Mem * pMem, int n, int bPreserve)
 			sqlVdbeMemSetNull(pMem);
 			pMem->z = 0;
 			pMem->szMalloc = 0;
-			return SQL_NOMEM;
+			return -1;
 		} else {
 			pMem->szMalloc =
 			    sqlDbMallocSize(pMem->zMalloc);
@@ -159,8 +159,7 @@ sqlVdbeMemGrow(Mem * pMem, int n, int bPreserve)
  * and MEM_Blob values may be discarded, MEM_Int, MEM_Real, and MEM_Null
  * values are preserved.
  *
- * Return 0 on success or an error code (probably SQL_NOMEM)
- * if unable to complete the resizing.
+ * Return 0 on success or -1 if unable to complete the resizing.
  */
 int
 sqlVdbeMemClearAndResize(Mem * pMem, int szNew)
@@ -180,17 +179,17 @@ sqlVdbeMemClearAndResize(Mem * pMem, int szNew)
  * Change pMem so that its MEM_Str or MEM_Blob value is stored in
  * MEM.zMalloc, where it can be safely written.
  *
- * Return 0 on success or SQL_NOMEM if malloc fails.
+ * Return 0 on success or -1 if malloc fails.
  */
 int
 sqlVdbeMemMakeWriteable(Mem * pMem)
 {
 	if ((pMem->flags & (MEM_Str | MEM_Blob)) != 0) {
 		if (ExpandBlob(pMem))
-			return SQL_NOMEM;
+			return -1;
 		if (pMem->szMalloc == 0 || pMem->z != pMem->zMalloc) {
 			if (sqlVdbeMemGrow(pMem, pMem->n + 2, 1)) {
-				return SQL_NOMEM;
+				return -1;
 			}
 			pMem->z[pMem->n] = 0;
 			pMem->z[pMem->n + 1] = 0;
@@ -222,7 +221,7 @@ sqlVdbeMemExpandBlob(Mem * pMem)
 		nByte = 1;
 	}
 	if (sqlVdbeMemGrow(pMem, nByte, 1)) {
-		return SQL_NOMEM;
+		return -1;
 	}
 
 	memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
@@ -239,7 +238,7 @@ static SQL_NOINLINE int
 vdbeMemAddTerminator(Mem * pMem)
 {
 	if (sqlVdbeMemGrow(pMem, pMem->n + 2, 1)) {
-		return SQL_NOMEM;
+		return -1;
 	}
 	pMem->z[pMem->n] = 0;
 	pMem->z[pMem->n + 1] = 0;
@@ -290,7 +289,7 @@ sqlVdbeMemStringify(Mem * pMem, u8 bForce)
 	assert(EIGHT_BYTE_ALIGNMENT(pMem));
 
 	if (sqlVdbeMemClearAndResize(pMem, nByte)) {
-		return SQL_NOMEM;
+		return -1;
 	}
 	if (fg & MEM_Int) {
 		sql_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
@@ -1002,7 +1001,7 @@ sqlVdbeMemSetStr(Mem * pMem,	/* Memory cell to set to string value */
 		testcase(nAlloc == 31);
 		testcase(nAlloc == 32);
 		if (sqlVdbeMemClearAndResize(pMem, MAX(nAlloc, 32))) {
-			return SQL_NOMEM;
+			return -1;
 		}
 		memcpy(pMem->z, z, nAlloc);
 	} else if (xDel == SQL_DYNAMIC) {
@@ -1273,7 +1272,7 @@ valueFromFunction(sql * db,	/* The database connection */
 							   sizeof(apVal[0]) *
 							   nVal);
 		if (apVal == 0) {
-			rc = SQL_NOMEM;
+			rc = -1;
 			goto value_from_function_out;
 		}
 		for (i = 0; i < nVal; i++) {
@@ -1286,7 +1285,7 @@ valueFromFunction(sql * db,	/* The database connection */
 
 	pVal = valueNew(db, pCtx);
 	if (pVal == 0) {
-		rc = SQL_NOMEM;
+		rc = -1;
 		goto value_from_function_out;
 	}
 
@@ -1450,7 +1449,7 @@ valueFromExpr(sql * db,	/* The database connection */
 	if (pCtx == 0)
 		sqlValueFree(pVal);
 
-	return SQL_NOMEM;
+	return -1;
 }
 
 /*
diff --git a/src/box/sql/vdbesort.c b/src/box/sql/vdbesort.c
index b6ae888..3ff6807 100644
--- a/src/box/sql/vdbesort.c
+++ b/src/box/sql/vdbesort.c
@@ -570,7 +570,7 @@ vdbePmaReadBlob(PmaReader * p,	/* PmaReader from which to take the blob */
 				nNew = nNew * 2;
 			aNew = sqlRealloc(p->aAlloc, nNew);
 			if (!aNew)
-				return SQL_NOMEM;
+				return -1;
 			p->nAlloc = nNew;
 			p->aAlloc = aNew;
 		}
@@ -694,7 +694,7 @@ vdbePmaReaderSeek(SortSubtask * pTask,	/* Task context */
 		if (pReadr->aBuffer == 0) {
 			pReadr->aBuffer = (u8 *) sqlMalloc(pgsz);
 			if (pReadr->aBuffer == 0)
-				rc = SQL_NOMEM;
+				rc = -1;
 			pReadr->nBuffer = pgsz;
 		}
 		if (rc == 0 && iBuf) {
@@ -803,7 +803,7 @@ vdbePmaReaderInit(SortSubtask * pTask,	/* Task context */
  * version of key2 and *pbKey2Cached set to true before returning.
  *
  * If an OOM error is encountered, (pTask->pUnpacked->error_rc) is set
- * to SQL_NOMEM.
+ * to -1.
  *
  * @param task Subtask context (for key_def).
  * @param key2_cached True if pTask->pUnpacked is key2.
@@ -879,7 +879,7 @@ sqlVdbeSorterInit(sql * db,	/* Database connection (for malloc()) */
 	pSorter = (VdbeSorter *) sqlDbMallocZero(db, sizeof(VdbeSorter));
 	pCsr->uc.pSorter = pSorter;
 	if (pSorter == 0) {
-		rc = SQL_NOMEM;
+		rc = -1;
 	} else {
 		pSorter->key_def = pCsr->key_def;
 		pSorter->pgsz = pgsz = 1024;
@@ -913,7 +913,7 @@ sqlVdbeSorterInit(sql * db,	/* Database connection (for malloc()) */
 				pSorter->list.aMemory =
 				    (u8 *) sqlMalloc(pgsz);
 				if (!pSorter->list.aMemory)
-					rc = SQL_NOMEM;
+					rc = -1;
 			}
 		}
 
@@ -1258,7 +1258,7 @@ vdbeSorterOpenTempFile(sql * db,	/* Database handle doing sort */
 /*
  * If it has not already been allocated, allocate the UnpackedRecord
  * structure at pTask->pUnpacked. Return 0 if successful (or
- * if no allocation was required), or SQL_NOMEM otherwise.
+ * if no allocation was required), or -1 otherwise.
  */
 static int
 vdbeSortAllocUnpacked(SortSubtask * pTask)
@@ -1268,7 +1268,7 @@ vdbeSortAllocUnpacked(SortSubtask * pTask)
 			sqlVdbeAllocUnpackedRecord(pTask->pSorter->db,
 						       pTask->pSorter->key_def);
 		if (pTask->pUnpacked == 0)
-			return SQL_NOMEM;
+			return -1;
 		pTask->pUnpacked->nField = pTask->pSorter->key_def->part_count;
 		pTask->pUnpacked->errCode = 0;
 	}
@@ -1330,7 +1330,7 @@ vdbeSorterGetCompare(VdbeSorter * p)
 
 /*
  * Sort the linked list of records headed at pTask->pList. Return
- * 0 if successful, or an sql error code (i.e. SQL_NOMEM) if
+ * 0 if successful, or an sql error code (i.e. -1) if
  * an error occurs.
  */
 static int
@@ -1351,7 +1351,7 @@ vdbeSorterSort(SortSubtask * pTask, SorterList * pList)
 	aSlot =
 	    (SorterRecord **) sqlMallocZero(64 * sizeof(SorterRecord *));
 	if (!aSlot) {
-		return SQL_NOMEM;
+		return -1;
 	}
 
 	while (p) {
@@ -1388,8 +1388,6 @@ vdbeSorterSort(SortSubtask * pTask, SorterList * pList)
 	pList->pList = p;
 
 	sql_free(aSlot);
-	assert(pTask->pUnpacked->errCode == 0
-	       || pTask->pUnpacked->errCode == SQL_NOMEM);
 	return pTask->pUnpacked->errCode;
 }
 
@@ -1406,7 +1404,7 @@ vdbePmaWriterInit(sql_file * pFd,	/* File handle to write to */
 	memset(p, 0, sizeof(PmaWriter));
 	p->aBuffer = (u8 *) sqlMalloc(nBuf);
 	if (!p->aBuffer) {
-		p->eFWErr = SQL_NOMEM;
+		p->eFWErr = -1;
 	} else {
 		p->iBufEnd = p->iBufStart = (iStart % nBuf);
 		p->iWriteOff = iStart - p->iBufStart;
@@ -1727,7 +1725,7 @@ vdbeSorterFlushPMA(VdbeSorter * pSorter)
 				pSorter->list.aMemory =
 				    sqlMalloc(pSorter->nMemory);
 				if (!pSorter->list.aMemory)
-					return SQL_NOMEM;
+					return -1;
 			}
 
 			rc = vdbeSorterCreateThread(pTask,
@@ -1827,7 +1825,7 @@ sqlVdbeSorterWrite(const VdbeCursor * pCsr,	/* Sorter cursor */
 
 			aNew = sqlRealloc(pSorter->list.aMemory, nNew);
 			if (!aNew)
-				return SQL_NOMEM;
+				return -1;
 			pSorter->list.pList = (SorterRecord *) & aNew[iListOff];
 			pSorter->list.aMemory = aNew;
 			pSorter->nMemory = nNew;
@@ -1844,7 +1842,7 @@ sqlVdbeSorterWrite(const VdbeCursor * pCsr,	/* Sorter cursor */
 	} else {
 		pNew = (SorterRecord *) sqlMalloc(nReq);
 		if (pNew == 0) {
-			return SQL_NOMEM;
+			return -1;
 		}
 		pNew->u.pNext = pSorter->list.pList;
 	}
@@ -2007,7 +2005,7 @@ vdbeIncrMergerNew(SortSubtask * pTask,	/* The thread that will be using the new
 		pTask->file2.iEof += pIncr->mxSz;
 	} else {
 		vdbeMergeEngineFree(pMerger);
-		rc = SQL_NOMEM;
+		rc = -1;
 	}
 	return rc;
 }
@@ -2332,7 +2330,7 @@ vdbeMergeEngineLevel0(SortSubtask * pTask,	/* Sorter task to read from */
 
 	*ppOut = pNew = vdbeMergeEngineNew(nPMA);
 	if (pNew == 0)
-		rc = SQL_NOMEM;
+		rc = -1;
 
 	for (i = 0; i < nPMA && rc == 0; i++) {
 		i64 nDummy = 0;
@@ -2408,7 +2406,7 @@ vdbeSorterAddToTree(SortSubtask * pTask,	/* Task context */
 			MergeEngine *pNew =
 			    vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT);
 			if (pNew == 0) {
-				rc = SQL_NOMEM;
+				rc = -1;
 			} else {
 				rc = vdbeIncrMergerNew(pTask, pNew,
 						       &pReadr->pIncr);
@@ -2457,7 +2455,7 @@ vdbeSorterMergeTreeBuild(VdbeSorter * pSorter,	/* The VDBE cursor that implement
 	if (pSorter->nTask > 1) {
 		pMain = vdbeMergeEngineNew(pSorter->nTask);
 		if (pMain == 0)
-			rc = SQL_NOMEM;
+			rc = -1;
 	}
 #endif
 
@@ -2478,7 +2476,7 @@ vdbeSorterMergeTreeBuild(VdbeSorter * pSorter,	/* The VDBE cursor that implement
 				pRoot =
 				    vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT);
 				if (pRoot == 0)
-					rc = SQL_NOMEM;
+					rc = -1;
 				for (i = 0; i < pTask->nPMA && rc == 0;
 				     i += SORTER_MAX_MERGE_COUNT) {
 					MergeEngine *pMerger = 0;	/* New level-0 PMA merger */
@@ -2569,7 +2567,7 @@ vdbeSorterSetupMerge(VdbeSorter * pSorter)
 								      (PmaReader));
 				pSorter->pReader = pReadr;
 				if (pReadr == 0)
-					rc = SQL_NOMEM;
+					rc = -1;
 			}
 			if (rc == 0) {
 				rc = vdbeIncrMergerNew(pLast, pMain,
@@ -2773,7 +2771,7 @@ sqlVdbeSorterRowkey(const VdbeCursor * pCsr, Mem * pOut)
 	pSorter = pCsr->uc.pSorter;
 	pKey = vdbeSorterRowkey(pSorter, &nKey);
 	if (sqlVdbeMemClearAndResize(pOut, nKey)) {
-		return SQL_NOMEM;
+		return -1;
 	}
 	pOut->n = nKey;
 	MemSetTypeFlag(pOut, MEM_Blob);
@@ -2790,7 +2788,7 @@ sqlVdbeSorterRowkey(const VdbeCursor * pCsr, Mem * pOut)
  * If the sorter cursor key contains any NULL values, consider it to be
  * less than pVal. Even if pVal also contains NULL values.
  *
- * If an error occurs, return an sql error code (i.e. SQL_NOMEM).
+ * If an error occurs, return -1.
  * Otherwise, set *pRes to a negative, zero or positive value if the
  * key in pVal is smaller than, equal to or larger than the current sorter
  * key.
@@ -2818,7 +2816,7 @@ sqlVdbeSorterCompare(const VdbeCursor * pCsr,	/* Sorter cursor */
 		r2 = pSorter->pUnpacked =
 			sqlVdbeAllocUnpackedRecord(pSorter->db,  pCsr->key_def);
 		if (r2 == 0)
-			return SQL_NOMEM;
+			return -1;
 		r2->nField = nKeyCol;
 	}
 	assert(r2->nField == nKeyCol);
diff --git a/src/box/sql/where.c b/src/box/sql/where.c
index 6781ab0..9739268 100644
--- a/src/box/sql/where.c
+++ b/src/box/sql/where.c
@@ -1741,7 +1741,7 @@ whereLoopResize(sql * db, WhereLoop * p, int n)
 	n = (n + 7) & ~7;
 	paNew = sqlDbMallocRawNN(db, sizeof(p->aLTerm[0]) * n);
 	if (paNew == 0)
-		return SQL_NOMEM;
+		return -1;
 	memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0]) * p->nLSlot);
 	if (p->aLTerm != p->aLTermSpace)
 		sqlDbFree(db, p->aLTerm);
@@ -1762,7 +1762,7 @@ whereLoopXfer(sql * db, WhereLoop * pTo, WhereLoop * pFrom)
 		pTo->nBtm = 0;
 		pTo->nTop = 0;
 		pTo->index_def = NULL;
-		return SQL_NOMEM;
+		return -1;
 	}
 	memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ);
 	memcpy(pTo->aLTerm, pFrom->aLTerm,
@@ -2079,7 +2079,7 @@ whereLoopInsert(WhereLoopBuilder * pBuilder, WhereLoop * pTemplate)
 		/* Allocate a new WhereLoop to add to the end of the list */
 		*ppPrev = p = sqlDbMallocRawNN(db, sizeof(WhereLoop));
 		if (p == 0)
-			return SQL_NOMEM;
+			return -1;
 		whereLoopInit(p);
 		p->pNextLoop = 0;
 	} else {
@@ -2308,7 +2308,7 @@ whereLoopAddBtreeIndex(WhereLoopBuilder * pBuilder,	/* The WhereLoop factory */
 
 	pNew = pBuilder->pNew;
 	if (db->mallocFailed)
-		return SQL_NOMEM;
+		return -1;
 	WHERETRACE(0x800, ("BEGIN addBtreeIdx(%s), nEq=%d\n",
 			   probe->name, pNew->nEq));
 
@@ -3546,7 +3546,7 @@ whereSortingCost(WhereInfo * pWInfo, LogEst nRow, int nOrderBy, int nSorted)
  * will be nRowEst (in the 10*log2 representation).  Or, ignore sorting
  * costs if nRowEst==0.
  *
- * Return 0 on success or SQL_NOMEM of a memory allocation
+ * Return 0 on success or -1 of a memory allocation
  * error occurs.
  */
 static int
@@ -3602,7 +3602,7 @@ wherePathSolver(WhereInfo * pWInfo, LogEst nRowEst)
 	nSpace += sizeof(LogEst) * nOrderBy;
 	pSpace = sqlDbMallocRawNN(db, nSpace);
 	if (pSpace == 0)
-		return SQL_NOMEM;
+		return -1;
 	aTo = (WherePath *) pSpace;
 	aFrom = aTo + mxChoice;
 	memset(aFrom, 0, sizeof(aFrom[0]));
-- 
2.7.4





More information about the Tarantool-patches mailing list