GeezerVison is showing github.com — sqlite: guard re-entrant reset of a virtual table cursor · nodejs/node@1673bbd
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/node_sqlite.cc +24Lines changed: 24 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1298,6 +1298,7 @@ int VirtualTableModule::xOpen(sqlite3_vtab* pVTab, | 1298 | 1298 | cursor->hidden_values.assign(vtab->module->num_columns_, nullptr); | 1299 | 1299 | cursor->rowid = 0; | 1300 | 1300 | cursor->done = true; | | 1301 | + cursor->closing = false; | 1301 | 1302 | *ppCursor = &cursor->base; | 1302 | 1303 | return SQLITE_OK; | 1303 | 1304 | } | @@ -1306,6 +1307,15 @@ int VirtualTableModule::xClose(sqlite3_vtab_cursor* pCursor) { | 1306 | 1307 | NodeVTabCursor* cursor = reinterpret_cast<NodeVTabCursor*>(pCursor); | 1307 | 1308 | VirtualTableModule* mod = cursor->module; | 1308 | 1309 |
| | 1310 | + // sqlite3_reset() from the iterator's return() can halt this VM again | | 1311 | + // before the cursor slot is cleared, so xClose would run twice and free | | 1312 | + // the cursor twice. The stepping guard rejects that re-entry; ignore a | | 1313 | + // second close here as well. | | 1314 | + if (cursor->closing) { | | 1315 | + return SQLITE_OK; | | 1316 | + } | | 1317 | + cursor->closing = true; | | 1318 | + | 1309 | 1319 | // Close the iterator so generator `finally` blocks still run when SQLite | 1310 | 1320 | // stops stepping early, as it does for LIMIT or a `break` out of a for...of | 1311 | 1321 | // loop. SQLite discards xClose's return value, so a throwing cleanup is | @@ -4467,6 +4477,10 @@ bool SQLTagStore::ResetAndBindStatement( | 4467 | 4477 | Statement* stmt, | 4468 | 4478 | const FunctionCallbackInfo<Value>& args) { | 4469 | 4479 | Isolate* isolate = env->isolate(); | | 4480 | + // sqlite3_reset() closes an open virtual-table cursor, and xClose runs the | | 4481 | + // generator's return() in JavaScript. Hold the stepping guard so a nested | | 4482 | + // reset of this same statement fails instead of freeing that cursor twice. | | 4483 | + SteppingStatementGuard stepping(stmt->db_.get(), stmt->statement_.get()); | 4470 | 4484 | int r = stmt->ResetStatement(); | 4471 | 4485 | CHECK_ERROR_OR_THROW(isolate, stmt->db_.get(), r, SQLITE_OK, false); | 4472 | 4486 |
| @@ -4567,6 +4581,11 @@ void SQLTagStore::All(const FunctionCallbackInfo<Value>& args) { | 4567 | 4581 | } | 4568 | 4582 | Environment* env = Environment::GetCurrent(args); | 4569 | 4583 |
| | 4584 | + // PrepareForExecution() holds the guard for its own reset, but this one | | 4585 | + // also spans the reset below, which runs on the error path and can close | | 4586 | + // a virtual-table cursor as well. | | 4587 | + SteppingStatementGuard stepping(stmt->db_.get(), stmt->statement_.get()); | | 4588 | + | 4570 | 4589 | Isolate* isolate = env->isolate(); | 4571 | 4590 | bool needs_reset = true; | 4572 | 4591 | auto reset = OnScopeLeave([&]() { | @@ -4889,6 +4908,11 @@ void StatementIterator::Return(const FunctionCallbackInfo<Value>& args) { | 4889 | 4908 | // is invoked by the language during abrupt completion (e.g. a `throw` | 4890 | 4909 | // inside a `for...of` body), and throwing on a deferred SQLite error | 4891 | 4910 | // would discard the caller's already-pending exception. | | 4911 | + // sqlite3_reset() closes an open virtual-table cursor and runs its | | 4912 | + // generator's return() in JavaScript. The guard makes a nested reset of | | 4913 | + // this same statement fail instead of freeing that cursor twice. | | 4914 | + SteppingStatementGuard stepping(iter->stmt_->db_.get(), | | 4915 | + iter->stmt_->statement_.get()); | 4892 | 4916 | sqlite3_reset(iter->stmt_->statement_.get()); | 4893 | 4917 | } | 4894 | 4918 | iter->done_ = true; | |
Collapse file Copy file name to clipboardExpand all lines: src/node_sqlite.h +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -696,6 +696,9 @@ struct NodeVTabCursor { | 696 | 696 | std::vector<sqlite3_value*> hidden_values; | 697 | 697 | sqlite3_int64 rowid; | 698 | 698 | bool done; | | 699 | + // Set while xClose is running. A nested sqlite3_reset() can otherwise call | | 700 | + // xClose on this same cursor again and free it twice. | | 701 | + bool closing = false; | 699 | 702 | }; | 700 | 703 |
| 701 | 704 | class VirtualTableModule { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-sqlite-virtual-table.js +112Lines changed: 112 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1090,4 +1090,116 @@ suite('Database.prototype.createModule()', () => { | 1090 | 1090 | db.close(); | 1091 | 1091 | }); | 1092 | 1092 | }); | | 1093 | + | | 1094 | + suite('re-entrant reset of a virtual table cursor', () => { | | 1095 | + // xClose runs the generator's return(). Resetting that same statement | | 1096 | + // from `finally` must fail instead of closing the cursor twice. | | 1097 | + function moduleWithFinally(db, name, onFinally) { | | 1098 | + db.createModule(name, { | | 1099 | + columns: [{ name: 'v', type: 'INTEGER' }], | | 1100 | + *rows() { | | 1101 | + try { | | 1102 | + yield [1]; | | 1103 | + yield [2]; | | 1104 | + } finally { | | 1105 | + onFinally(); | | 1106 | + } | | 1107 | + }, | | 1108 | + }); | | 1109 | + } | | 1110 | + | | 1111 | + test('iterator.return() from finally throws instead of crashing', () => { | | 1112 | + const db = new Database(':memory:'); | | 1113 | + let it; | | 1114 | + let caught; | | 1115 | + moduleWithFinally(db, 'gen_return', () => { | | 1116 | + try { | | 1117 | + it.return(); | | 1118 | + } catch (err) { | | 1119 | + caught = err; | | 1120 | + } | | 1121 | + }); | | 1122 | + it = db.prepare('SELECT v FROM gen_return').iterate(); | | 1123 | + assert.strictEqual(it.next().value.v, 1); | | 1124 | + assert.strictEqual(it.return().done, true); | | 1125 | + assert.strictEqual(caught.code, 'ERR_INVALID_STATE'); | | 1126 | + assert.strictEqual( | | 1127 | + caught.message, | | 1128 | + 'statement is already being executed', | | 1129 | + ); | | 1130 | + db.close(); | | 1131 | + }); | | 1132 | + | | 1133 | + test('statement.get() from finally throws instead of crashing', () => { | | 1134 | + const db = new Database(':memory:'); | | 1135 | + let stmt; | | 1136 | + let caught; | | 1137 | + moduleWithFinally(db, 'gen_get', () => { | | 1138 | + try { | | 1139 | + stmt.get(); | | 1140 | + } catch (err) { | | 1141 | + caught = err; | | 1142 | + } | | 1143 | + }); | | 1144 | + stmt = db.prepare('SELECT v FROM gen_get'); | | 1145 | + const it = stmt.iterate(); | | 1146 | + assert.strictEqual(it.next().value.v, 1); | | 1147 | + assert.strictEqual(it.return().done, true); | | 1148 | + assert.strictEqual(caught.code, 'ERR_INVALID_STATE'); | | 1149 | + assert.strictEqual( | | 1150 | + caught.message, | | 1151 | + 'statement is already being executed', | | 1152 | + ); | | 1153 | + db.close(); | | 1154 | + }); | | 1155 | + | | 1156 | + test('tagged get() from finally throws instead of crashing', () => { | | 1157 | + const db = new Database(':memory:'); | | 1158 | + const sql = db.createTagStore(); | | 1159 | + let caught; | | 1160 | + moduleWithFinally(db, 'gen_tag', () => { | | 1161 | + try { | | 1162 | + sql.get`SELECT v FROM gen_tag`; // eslint-disable-line no-unused-expressions | | 1163 | + } catch (err) { | | 1164 | + caught = err; | | 1165 | + } | | 1166 | + }); | | 1167 | + const it = sql.iterate`SELECT v FROM gen_tag`; | | 1168 | + assert.strictEqual(it.next().value.v, 1); | | 1169 | + assert.strictEqual(sql.get`SELECT v FROM gen_tag`.v, 1); | | 1170 | + assert.strictEqual(caught.code, 'ERR_INVALID_STATE'); | | 1171 | + assert.strictEqual( | | 1172 | + caught.message, | | 1173 | + 'statement is already being executed', | | 1174 | + ); | | 1175 | + db.close(); | | 1176 | + }); | | 1177 | + | | 1178 | + test('for...of break that queries again throws instead of crashing', () => { | | 1179 | + const db = new Database(':memory:'); | | 1180 | + let stmt; | | 1181 | + let caught; | | 1182 | + moduleWithFinally(db, 'gen_break', () => { | | 1183 | + try { | | 1184 | + stmt.get(); | | 1185 | + } catch (err) { | | 1186 | + caught = err; | | 1187 | + } | | 1188 | + }); | | 1189 | + stmt = db.prepare('SELECT v FROM gen_break'); | | 1190 | + let seen = 0; | | 1191 | + for (const row of stmt.iterate()) { | | 1192 | + assert.strictEqual(row.v, 1); | | 1193 | + seen++; | | 1194 | + break; | | 1195 | + } | | 1196 | + assert.strictEqual(seen, 1); | | 1197 | + assert.strictEqual(caught.code, 'ERR_INVALID_STATE'); | | 1198 | + assert.strictEqual( | | 1199 | + caught.message, | | 1200 | + 'statement is already being executed', | | 1201 | + ); | | 1202 | + db.close(); | | 1203 | + }); | | 1204 | + }); | 1093 | 1205 | }); |
|