diff --git a/src/box/sql/build.c b/src/box/sql/build.c index f2f30c6a208617d40237b75a69c156be3032b8a0..5fd154c2c67e42f20fefe40c8ada705fa0fff003 100644 --- a/src/box/sql/build.c +++ b/src/box/sql/build.c @@ -2658,64 +2658,26 @@ sql_src_list_new(struct sql *db) return src_list; } -/* - * Append a new table name to the given SrcList. Create a new SrcList if - * need be. A new entry is created in the SrcList even if pTable is NULL. - * - * A SrcList is returned, or NULL if there is an OOM error. The returned - * SrcList might be the same as the SrcList that was input or it might be - * a new one. If an OOM error does occurs, then the prior value of pList - * that is input to this routine is automatically freed. - * - * If pDatabase is not null, it means that the table has an optional - * database name prefix. Like this: "database.table". The pDatabase - * points to the table name and the pTable points to the database name. - * The SrcList.a[].zName field is filled with the table name which might - * come from pTable (if pDatabase is NULL) or from pDatabase. - * SrcList.a[].zDatabase is filled with the database name from pTable, - * or with NULL if no database is specified. - * - * In other words, if call like this: - * - * sqlSrcListAppend(D,A,B,0); - * - * Then B is a table name and the database name is unspecified. If called - * like this: - * - * sqlSrcListAppend(D,A,B,C); - * - * Then C is the table name and B is the database name. If C is defined - * then so is B. In other words, we never have a case where: - * - * sqlSrcListAppend(D,A,0,C); - * - * Both pTable and pDatabase are assumed to be quoted. They are dequoted - * before being added to the SrcList. - */ -SrcList * -sqlSrcListAppend(sql * db, /* Connection to notify of malloc failures */ - SrcList * pList, /* Append to this SrcList. NULL creates a new SrcList */ - Token * pTable /* Table to append */ - ) +struct SrcList * +sql_src_list_append(struct sql *db, struct SrcList *list, + struct Token *name_token) { - struct SrcList_item *pItem; - assert(db != 0); - if (pList == 0) { - pList = sql_src_list_new(db); - if (pList == 0) - return 0; + if (list == NULL) { + list = sql_src_list_new(db); + if (list == NULL) + return NULL; } else { struct SrcList *new_list = - sql_src_list_enlarge(db, pList, 1, pList->nSrc); + sql_src_list_enlarge(db, list, 1, list->nSrc); if (new_list == NULL) { - sqlSrcListDelete(db, pList); + sqlSrcListDelete(db, list); return NULL; } - pList = new_list; + list = new_list; } - pItem = &pList->a[pList->nSrc - 1]; - pItem->zName = sqlNameFromToken(db, pTable); - return pList; + struct SrcList_item *item = &list->a[list->nSrc - 1]; + item->zName = sqlNameFromToken(db, name_token); + return list; } /* @@ -2807,10 +2769,12 @@ sqlSrcListAppendFromTerm(Parse * pParse, /* Parsing context */ pParse->is_aborted = true; goto append_from_error; } - p = sqlSrcListAppend(db, p, pTable); - if (p == 0 || NEVER(p->nSrc == 0)) { + p = sql_src_list_append(db, p, pTable); + if (p == NULL) { + pParse->is_aborted = true; goto append_from_error; } + assert(p->nSrc != 0); pItem = &p->a[p->nSrc - 1]; assert(pAlias != 0); if (pAlias->n) { diff --git a/src/box/sql/delete.c b/src/box/sql/delete.c index 8d9d0ff32e4884cc285ba0679b777185aee82a50..5a3d7cfadb1b29605307dc13e56ba1267d51a102 100644 --- a/src/box/sql/delete.c +++ b/src/box/sql/delete.c @@ -70,14 +70,16 @@ sql_materialize_view(struct Parse *parse, const char *name, struct Expr *where, int cursor) { struct sql *db = parse->db; - where = sqlExprDup(db, where, 0); - struct SrcList *from = sqlSrcListAppend(db, NULL, NULL); - if (from != NULL) { - assert(from->nSrc == 1); - from->a[0].zName = sqlDbStrDup(db, name); - assert(from->a[0].pOn == NULL); - assert(from->a[0].pUsing == NULL); + struct SrcList *from = sql_src_list_append(db, NULL, NULL); + if (from == NULL) { + parse->is_aborted = true; + return; } + where = sqlExprDup(db, where, 0); + assert(from->nSrc == 1); + from->a[0].zName = sqlDbStrDup(db, name); + assert(from->a[0].pOn == NULL); + assert(from->a[0].pUsing == NULL); struct Select *select = sqlSelectNew(parse, NULL, from, where, NULL, NULL, NULL, 0, NULL, NULL); struct SelectDest dest; diff --git a/src/box/sql/fk_constraint.c b/src/box/sql/fk_constraint.c index 4066b1cf1cb3fe01122e2c60cc96dcb00518a502..cc4dc6448298b663c463af6075e39f9d86e7870d 100644 --- a/src/box/sql/fk_constraint.c +++ b/src/box/sql/fk_constraint.c @@ -618,9 +618,11 @@ fk_constraint_emit_check(struct Parse *parser, struct space *space, int reg_old, * table. We need the child table as a SrcList for * sqlWhereBegin(). */ - struct SrcList *src = sqlSrcListAppend(db, NULL, NULL); - if (src == NULL) - continue; + struct SrcList *src = sql_src_list_append(db, NULL, NULL); + if (src == NULL) { + parser->is_aborted = true; + return; + } struct SrcList_item *item = src->a; struct space *child = space_by_id(fk->def->child_id); assert(child != NULL); @@ -866,11 +868,12 @@ fk_constraint_action_trigger(struct Parse *pParse, struct space_def *def, "constraint failed"); if (r != NULL) r->on_conflict_action = ON_CONFLICT_ACTION_ABORT; - select = sqlSelectNew(pParse, - sql_expr_list_append(db, NULL, r), - sqlSrcListAppend(db, NULL, &err), - where, NULL, NULL, NULL, 0, NULL, - NULL); + struct SrcList *src_list = sql_src_list_append(db, NULL, &err); + if (src_list == NULL) + pParse->is_aborted = true; + select = sqlSelectNew(pParse, sql_expr_list_append(db, NULL, r), + src_list, where, NULL, NULL, NULL, 0, + NULL, NULL); where = NULL; } diff --git a/src/box/sql/parse.y b/src/box/sql/parse.y index d2614d9b0add58a63ed81e4fd601b5f0f6ae8c46..2245a100d8231b44f58536b79121ce6371ed4f24 100644 --- a/src/box/sql/parse.y +++ b/src/box/sql/parse.y @@ -617,8 +617,14 @@ seltablist(A) ::= stl_prefix(A) LP seltablist(F) RP %type fullname {SrcList*} %destructor fullname {sqlSrcListDelete(pParse->db, $$);} -fullname(A) ::= nm(X). - {A = sqlSrcListAppend(pParse->db,0,&X); /*A-overwrites-X*/} +fullname(A) ::= nm(X). { + /* A-overwrites-X. */ + A = sql_src_list_append(pParse->db,0,&X); + if (A == NULL) { + pParse->is_aborted = true; + return; + } +} %type joinop {int} join_nm(A) ::= id(A). @@ -1175,7 +1181,11 @@ expr(A) ::= expr(A) in_op(N) LP select(Y) RP(E). [IN] { A.zEnd = &E.z[E.n]; } expr(A) ::= expr(A) in_op(N) nm(Y) paren_exprlist(E). [IN] { - SrcList *pSrc = sqlSrcListAppend(pParse->db, 0,&Y); + struct SrcList *pSrc = sql_src_list_append(pParse->db, 0,&Y); + if (pSrc == NULL) { + pParse->is_aborted = true; + return; + } Select *pSelect = sqlSelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0); if( E ) sqlSrcListFuncArgs(pParse, pSelect ? pSrc : 0, E); A.pExpr = sqlPExpr(pParse, TK_IN, A.pExpr, 0); @@ -1245,8 +1255,12 @@ paren_exprlist(A) ::= LP exprlist(X) RP. {A = X;} // cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) ON nm(Y) LP sortlist(Z) RP. { - sql_create_index(pParse, &X, sqlSrcListAppend(pParse->db,0,&Y), Z, &S, - SORT_ORDER_ASC, NE, U); + struct SrcList *src_list = sql_src_list_append(pParse->db,0,&Y); + if (src_list == NULL) { + pParse->is_aborted = true; + return; + } + sql_create_index(pParse, &X, src_list, Z, &S, SORT_ORDER_ASC, NE, U); } %type uniqueflag {int} diff --git a/src/box/sql/select.c b/src/box/sql/select.c index af1aded6fa546db0c6b9ed78c004b83ad5385b4c..8c87f30ec33569c985d9df8d639eb49b2ccb5ae4 100644 --- a/src/box/sql/select.c +++ b/src/box/sql/select.c @@ -256,7 +256,7 @@ findRightmost(Select * p) /** - * Work the same as sqlSrcListAppend(), but before adding to + * Work the same as sql_src_list_append(), but before adding to * list provide check on name duplicates: only values with unique * names are appended. Moreover, names of tables are not * normalized: it is parser's business and in struct Select they @@ -4124,9 +4124,9 @@ flattenSubquery(Parse * pParse, /* Parsing context */ } else { assert(pParent != p); /* 2nd and subsequent times through the loop */ pSrc = pParent->pSrc = - sqlSrcListAppend(db, 0, 0); - if (pSrc == 0) { - assert(db->mallocFailed); + sql_src_list_append(db, 0, 0); + if (pSrc == NULL) { + pParse->is_aborted = true; break; } } diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h index 6c5ddfb0d03bccaad86acd552519aacb2fee0cef..b10ad37620348a6db3b9ff1b08e6e9414c91a85d 100644 --- a/src/box/sql/sqlInt.h +++ b/src/box/sql/sqlInt.h @@ -3456,7 +3456,21 @@ sql_src_list_enlarge(struct sql *db, struct SrcList *src_list, int new_slots, struct SrcList * sql_src_list_new(struct sql *db); -SrcList *sqlSrcListAppend(sql *, SrcList *, Token *); +/** + * Append a new table name to the given list. Create a new + * SrcList if need be. A new entry is created in the list even + * if name_token is NULL. + * + * @param db The database connection. + * @param list Append to this SrcList. NULL creates a new SrcList. + * @param name_token Token representing table name. + * @retval Not NULL A new SrcList or updated @a list. + * @retval NULL Error. A diag message is set. @A list is deleted. + */ +struct SrcList * +sql_src_list_append(struct sql *db, struct SrcList *list, + struct Token *name_token); + SrcList *sqlSrcListAppendFromTerm(Parse *, SrcList *, Token *, Token *, Select *, Expr *, IdList *); void sqlSrcListIndexedBy(Parse *, SrcList *, Token *); diff --git a/src/box/sql/trigger.c b/src/box/sql/trigger.c index 519e3305a7817a5ac8793fbfb92f54a1173c6924..2c9785fe972c1a72009eb3470daa38774a296b97 100644 --- a/src/box/sql/trigger.c +++ b/src/box/sql/trigger.c @@ -587,12 +587,13 @@ targetSrcList(Parse * pParse, /* The parsing context */ sql *db = pParse->db; SrcList *pSrc; /* SrcList to be returned */ - pSrc = sqlSrcListAppend(db, 0, 0); - if (pSrc) { - assert(pSrc->nSrc > 0); - pSrc->a[pSrc->nSrc - 1].zName = - sqlDbStrDup(db, pStep->zTarget); + pSrc = sql_src_list_append(db, 0, 0); + if (pSrc == NULL) { + pParse->is_aborted = true; + return NULL; } + assert(pSrc->nSrc > 0); + pSrc->a[pSrc->nSrc - 1].zName = sqlDbStrDup(db, pStep->zTarget); return pSrc; }