GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — sqlite: reject statement-less SQL in SQLTagStore · nodejs/node@043dfe4

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • node_sqlite.cc
  • test/parallel
    • test-sqlite-template-tag.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/node_sqlite.cc‎

Copy file name to clipboardExpand all lines: src/node_sqlite.cc
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3619,6 +3619,14 @@ BaseObjectPtr<StatementSync> SQLTagStore::PrepareStatement(
36193619
return BaseObjectPtr<StatementSync>();
36203620
}
36213621

3622+
// sqlite3_prepare_v2() reports success without producing a statement when
3623+
// the input holds no SQL, such as a comment. Such a statement cannot be
3624+
// bound or executed, so reject it instead of caching it.
3625+
if (s == nullptr) {
3626+
THROW_ERR_INVALID_ARG_VALUE(env, "The SQL query contains no statements.");
3627+
return BaseObjectPtr<StatementSync>();
3628+
}
3629+
36223630
BaseObjectPtr<StatementSync> stmt_obj = StatementSync::Create(
36233631
env, BaseObjectPtr<DatabaseSync>(session->database_), s);
36243632

Collapse file

‎test/parallel/test-sqlite-template-tag.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-sqlite-template-tag.js
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,29 @@ test('sql error messages are descriptive', () => {
317317
});
318318
});
319319

320+
test('rejects SQL that contains no statements', () => {
321+
const expectedError = {
322+
code: 'ERR_INVALID_ARG_VALUE',
323+
message: /contains no statements/,
324+
};
325+
326+
for (const method of ['run', 'get', 'all', 'iterate']) {
327+
assert.throws(() => {
328+
// eslint-disable-next-line no-unused-expressions
329+
sql[method]`-- comment`;
330+
}, expectedError);
331+
332+
assert.throws(() => {
333+
// eslint-disable-next-line no-unused-expressions
334+
sql[method]``;
335+
}, expectedError);
336+
}
337+
338+
// A rejected statement must not be cached, so a later valid query with the
339+
// same tag store still works.
340+
assert.strictEqual(sql.run`INSERT INTO foo (text) VALUES (${'bob'})`.changes, 1);
341+
});
342+
320343
test('a tag store keeps the database alive by itself', () => {
321344
const sql = new DatabaseSync(':memory:').createTagStore();
322345

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.