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

Mergen Imeev imeevma at tarantool.org
Sat Jun 15 13:01:54 MSK 2019


On Fri, Jun 14, 2019 at 12:24:49AM +0200, Vladislav Shpilevoy wrote:
> Thanks for the patch!
> 
> Consider my review fixes below and on the branch
> in a separate commit.
> 
Thank you! New patch:

>From ba2dd4105b288a6ede3c947a1ee6f425ad41e19c Mon Sep 17 00:00:00 2001
Date: Fri, 24 May 2019 17:20:30 +0300
Subject: [PATCH] sql: remove SQL_NOMEM errcode

Removing this error code is part of getting rid of the SQL error
system.

diff --git a/src/box/sql/fault.c b/src/box/sql/fault.c
index 26eeaee..62e9924 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 error
+ * 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 7b3c20e..761a3ab 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -1772,14 +1772,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;
 }
 
 /*
@@ -1790,11 +1787,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 2947abe..7335266 100644
--- a/src/box/sql/legacy.c
+++ b/src/box/sql/legacy.c
@@ -161,7 +161,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 d4129a5..495a87e 100644
--- a/src/box/sql/main.c
+++ b/src/box/sql/main.c
@@ -442,16 +442,16 @@ int
 sql_init_db(sql **out_db)
 {
 	sql *db;
-	int rc;			/* Return code */
 
-	rc = sql_initialize();
-	if (rc)
-		return rc;
+	if (sql_initialize() != 0)
+		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;
 
@@ -465,7 +465,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
@@ -474,16 +476,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 4e10380..c97d371 100644
--- a/src/box/sql/malloc.c
+++ b/src/box/sql/malloc.c
@@ -607,7 +607,7 @@ static SQL_NOINLINE int
 apiOomError(sql * db)
 {
 	sqlOomClear(db);
-	return SQL_NOMEM;
+	return -1;
 }
 
 /*
@@ -616,10 +616,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 10b68c8..b7d6b2a 100644
--- a/src/box/sql/os.c
+++ b/src/box/sql/os.c
@@ -164,7 +164,7 @@ sqlOsOpenMalloc(sql_vfs * pVfs,
 			*ppFile = pFile;
 		}
 	} else {
-		rc = SQL_NOMEM;
+		rc = -1;
 	}
 	return rc;
 }
diff --git a/src/box/sql/os_unix.c b/src/box/sql/os_unix.c
index 4bf1463..5170dd2 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 236b077..736c6ab 100644
--- a/src/box/sql/select.c
+++ b/src/box/sql/select.c
@@ -1813,7 +1813,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,
@@ -1916,17 +1916,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;
 
 }
 
@@ -4614,7 +4613,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 5b93b7d..b7b85cf 100644
--- a/src/box/sql/sqlInt.h
+++ b/src/box/sql/sqlInt.h
@@ -325,10 +325,8 @@ struct sql_vfs {
 #define SQL_LIMIT_TRIGGER_DEPTH             9
 
 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 9dc2659..c617540 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;
@@ -2618,8 +2612,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) &&
@@ -2652,8 +2645,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 a0465e6..69036ee 100644
--- a/src/box/sql/vdbeapi.c
+++ b/src/box/sql/vdbeapi.c
@@ -418,8 +418,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) {
@@ -450,9 +450,6 @@ sqlStep(Vdbe * p)
 	if (rc != SQL_ROW)
 		checkProfileCallback(db, p);
 
-	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
@@ -678,38 +675,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.
@@ -719,60 +684,43 @@ 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;
 }
 
 int
 sql_column_bytes(sql_stmt * pStmt, int i)
 {
-	int val = sql_value_bytes(columnMem(pStmt, i));
-	columnMallocFailure(pStmt);
-	return val;
+	return sql_value_bytes(columnMem(pStmt, i));
 }
 
 double
 sql_column_double(sql_stmt * pStmt, int i)
 {
-	double val = sql_value_double(columnMem(pStmt, i));
-	columnMallocFailure(pStmt);
-	return val;
+	return sql_value_double(columnMem(pStmt, i));
 }
 
 int
 sql_column_int(sql_stmt * pStmt, int i)
 {
-	int val = sql_value_int(columnMem(pStmt, i));
-	columnMallocFailure(pStmt);
-	return val;
+	return sql_value_int(columnMem(pStmt, i));
 }
 
 bool
 sql_column_boolean(struct sql_stmt *stmt, int i)
 {
-	bool val = sql_value_boolean(columnMem(stmt, i));
-	columnMallocFailure(stmt);
-	return val;
+	return sql_value_boolean(columnMem(stmt, i));
 }
 
 sql_int64
 sql_column_int64(sql_stmt * pStmt, int i)
 {
-	sql_int64 val = sql_value_int64(columnMem(pStmt, i));
-	columnMallocFailure(pStmt);
-	return val;
+	return sql_value_int64(columnMem(pStmt, i));
 }
 
 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;
+	return sql_value_text(columnMem(pStmt, i));
 }
 
 sql_value *
@@ -783,16 +731,13 @@ sql_column_value(sql_stmt * pStmt, int i)
 		pOut->flags &= ~MEM_Static;
 		pOut->flags |= MEM_Ephem;
 	}
-	columnMallocFailure(pStmt);
 	return (sql_value *) pOut;
 }
 
 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;
+	return sql_value_type(columnMem(pStmt, i));
 }
 
 enum sql_subtype
diff --git a/src/box/sql/vdbeaux.c b/src/box/sql/vdbeaux.c
index e4ddeda..a592f2e 100644
--- a/src/box/sql/vdbeaux.c
+++ b/src/box/sql/vdbeaux.c
@@ -159,7 +159,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).
  */
@@ -193,7 +193,7 @@ growOpArray(Vdbe * v, int nOp)
 		v->aOp = pNew;
 		return 0;
 	}
-	return SQL_NOMEM;
+	return -1;
 }
 
 #ifdef SQL_DEBUG
@@ -1383,14 +1383,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
@@ -1399,14 +1397,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
@@ -1944,7 +1934,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);
@@ -2098,7 +2088,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
@@ -2107,7 +2096,7 @@ sqlVdbeHalt(Vdbe * p)
 	 */
 
 	if (db->mallocFailed) {
-		p->rc = SQL_NOMEM;
+		p->rc = -1;
 	}
 	closeTopFrameCursors(p);
 	if (p->magic != VDBE_MAGIC_RUN) {
@@ -2125,7 +2114,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.
@@ -2136,20 +2125,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. */
@@ -2257,7 +2240,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() ||
@@ -2889,7 +2872,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;
@@ -2906,7 +2889,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 0586b11..3275c49 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 = sqlMallocSize(pMem->zMalloc);
 		}
@@ -158,8 +158,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)
@@ -179,17 +178,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;
@@ -221,7 +220,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);
@@ -238,7 +237,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;
@@ -289,7 +288,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);
@@ -1001,7 +1000,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) {
@@ -1268,7 +1267,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++) {
@@ -1281,7 +1280,7 @@ valueFromFunction(sql * db,	/* The database connection */
 
 	pVal = valueNew(db, pCtx);
 	if (pVal == 0) {
-		rc = SQL_NOMEM;
+		rc = -1;
 		goto value_from_function_out;
 	}
 
@@ -1444,7 +1443,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 230bb0c..e87c885 100644
--- a/src/box/sql/vdbesort.c
+++ b/src/box/sql/vdbesort.c
@@ -534,7 +534,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;
 		}
@@ -658,7 +658,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 != 0) {
@@ -765,7 +765,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.
@@ -821,7 +821,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;
@@ -848,7 +848,7 @@ sqlVdbeSorterInit(sql * db,	/* Database connection (for malloc()) */
 			pSorter->list.aMemory =
 			    (u8 *) sqlMalloc(pgsz);
 			if (!pSorter->list.aMemory)
-				rc = SQL_NOMEM;
+				rc = -1;
 		}
 
 		if (pCsr->key_def->part_count < 13
@@ -1053,7 +1053,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)
@@ -1063,7 +1063,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;
 	}
@@ -1125,7 +1125,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
@@ -1146,7 +1146,7 @@ vdbeSorterSort(SortSubtask * pTask, SorterList * pList)
 	aSlot =
 	    (SorterRecord **) sqlMallocZero(64 * sizeof(SorterRecord *));
 	if (!aSlot) {
-		return SQL_NOMEM;
+		return -1;
 	}
 
 	while (p) {
@@ -1183,8 +1183,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;
 }
 
@@ -1201,7 +1199,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;
@@ -1535,7 +1533,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;
@@ -1552,7 +1550,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;
 	}
@@ -1663,7 +1661,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;
 }
@@ -1860,7 +1858,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;
@@ -1936,7 +1934,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);
@@ -1991,7 +1989,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 */
@@ -2179,7 +2177,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);
@@ -2196,7 +2194,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.
@@ -2224,7 +2222,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 6c91ffb..565e2db 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));
 
@@ -3545,7 +3545,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
@@ -3601,7 +3601,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]));





More information about the Tarantool-patches mailing list