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: fix segfault in expandedSQL by tniessen · Pull Request #54687 · nodejs/node

Conversation

@tniessen

@tniessen tniessen commented Sep 1, 2024

Copy link
Copy Markdown
Member

The call to sqlite3_expanded_sql() may return NULL depending on various factors. Handle this case instead of running into a segmentation fault.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bricss and TheOneTheOnlyJJ reacted with rocket emoji
All reactions
  • 🚀 2 reactions
@tniessen
sqlite: fix segfault in expandedSQL
d4685f0
The call to sqlite3_expanded_sql() may return NULL depending on various
factors. Handle this case instead of running into a segmentation fault.
@tniessen tniessen added c++ Issues and PRs that require attention from people who are familiar with C++. sqlite Issues and PRs related to the SQLite subsystem. labels Sep 1, 2024
@nodejs-github-bot nodejs-github-bot added the needs-ci PRs that need a full CI run. label Sep 1, 2024
@tniessen
tniessen requested a review from cjihrig September 1, 2024 13:35
cjihrig
cjihrig approved these changes Sep 1, 2024
View reviewed changes

@cjihrig cjihrig left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks. I've had this on my TODO list since coverity reported it a while back.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

tniessen reacted with heart emoji
All reactions
  • ❤️ 1 reaction
@targos

targos commented Sep 1, 2024

Copy link
Copy Markdown
Member

Isn't there a way to test it?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tniessen

tniessen commented Sep 1, 2024

Copy link
Copy Markdown
Member Author

@targos It's easiest with different compile-time options, which is what I did locally. I'm not sure if the current JavaScript API allows configuring SQLite in a way that doesn't require a lot of memory for triggering the segmentation fault. I can try that later.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@codecov

codecov Bot commented Sep 1, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 57.14286% with 6 lines in your changes missing coverage. Please review.

Project coverage is 87.60%. Comparing base (4c844a2) to head (d4685f0).
Report is 259 commits behind head on main.

Files with missing lines Patch % Lines
src/node_sqlite.cc 57.14% 5 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #54687      +/-   ##
==========================================
+ Coverage   87.33%   87.60%   +0.27%     
==========================================
  Files         650      650              
  Lines      182832   182843      +11     
  Branches    35067    35384     +317     
==========================================
+ Hits       159670   160184     +514     
+ Misses      16421    15937     -484     
+ Partials     6741     6722      -19     
Files with missing lines Coverage Δ
src/node_sqlite.cc 83.05% <57.14%> (-0.95%) ⬇️

... and 67 files with indirect coverage changes

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

targos
targos approved these changes Sep 1, 2024
View reviewed changes
@tniessen

tniessen commented Sep 2, 2024

Copy link
Copy Markdown
Member Author

@targos Here is a demonstration using the default options and the official Node.js 22.7.0 binary:

const { DatabaseSync } = require('node:sqlite');

const db = new DatabaseSync('tmp.sqlite');
db.exec('CREATE TABLE a (t TEXT NOT NULL) STRICT;');

const targetStringSize = 1024 * 1024 * 1024;

const nRows = 10_000;
const valueLength = targetStringSize / nRows;

const stmt = db.prepare(`INSERT INTO a (t) VALUES ${[...Array(nRows).keys()].map(() => '(?)').join(',')};`);
const value = 'A'.repeat(valueLength);
const allValues = [...Array(nRows).keys()].map(() => value);
stmt.run(...allValues);
console.log(stmt.expandedSQL());

The segmentation fault occurs after a few seconds:

$ node -v
v22.7.0
$ time node --experimental-sqlite trigger-it.js 
Segmentation fault (core dumped)

real    0m3.872s
user    0m1.444s
sys     0m1.590s

With this PR, instead, this happens:

$ ./node --experimental-sqlite trigger-it.js 
/home/tniessen/dev/node/trigger-it.js:15
console.log(stmt.expandedSQL());
                 ^

Error: Expanded SQL text would exceed configured limits
    at Object.<anonymous> (/home/tniessen/dev/node/trigger-it.js:15:18)
    at Module._compile (node:internal/modules/cjs/loader:1546:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1691:10)
    at Module.load (node:internal/modules/cjs/loader:1317:32)
    at Module._load (node:internal/modules/cjs/loader:1127:12)
    at TracingChannel.traceSync (node:diagnostics_channel:315:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:217:24)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:166:5)
    at node:internal/main/run_main_module:30:49 {
  code: 'ERR_SQLITE_ERROR'
}

Node.js v23.0.0-pre

However, this test is quite slow and takes a lot of memory, so I am not sure if I should add it to our test suite.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@targos

targos commented Sep 3, 2024

Copy link
Copy Markdown
Member

Thanks for demonstrating it. Let's not add the test.

tniessen reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tniessen tniessen added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Sep 3, 2024
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Sep 3, 2024
@nodejs-github-bot

This comment has been minimized.

Sign in to view
@tniessen tniessen added the author ready PRs with CI started, the required approvals, and no outstanding review comments. label Sep 3, 2024
@nodejs-github-bot

This comment was marked as outdated.

Sign in to view
@nodejs-github-bot

This comment was marked as outdated.

Sign in to view
@nodejs-github-bot

This comment was marked as outdated.

Sign in to view
@nodejs-github-bot

This comment was marked as outdated.

Sign in to view
jasnell
jasnell reviewed Sep 5, 2024
View reviewed changes
Comment thread src/node_sqlite.cc
Local<Object> e = Exception::Error(js_msg)
->ToObject(isolate->GetCurrentContext())
.ToLocalChecked();
e->Set(isolate->GetCurrentContext(),

@jasnell jasnell Sep 5, 2024

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's not new to this PR but this should likely return MaybeLocal<Object> and not depend on ToLocalChecked() and Check(), in order to be most consistent.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@nodejs-github-bot

This comment was marked as outdated.

Sign in to view
@nodejs-github-bot

This comment was marked as outdated.

Sign in to view
@nodejs-github-bot

nodejs-github-bot commented Sep 6, 2024

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/62067/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tniessen tniessen added the commit-queue PRs queued for automated landing through the Commit Queue. label Sep 7, 2024
@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Sep 7, 2024
@nodejs-github-bot
nodejs-github-bot merged commit 17b49bd into nodejs:main Sep 7, 2024
@nodejs-github-bot

nodejs-github-bot commented Sep 7, 2024

Copy link
Copy Markdown
Collaborator

Landed in 17b49bd

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

aduh95 pushed a commit that referenced this pull request Sep 12, 2024
@tniessen @aduh95
sqlite: fix segfault in expandedSQL
7766349
The call to sqlite3_expanded_sql() may return NULL depending on various
factors. Handle this case instead of running into a segmentation fault.

PR-URL: #54687
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
@RafaelGSS RafaelGSS mentioned this pull request Sep 16, 2024
v22.9.0 proposal #54966
Merged
@targos targos added the dont-land-on-v20.x label Sep 22, 2024
tniessen added a commit to tniessen/node that referenced this pull request Oct 28, 2024
@tniessen
sqlite: improve error handling using MaybeLocal
e20695a
As per James' suggestion, consistently use MaybeLocal and avoid Check()
and ToLocalChecked() entirely.

Refs: nodejs#54687
@tniessen tniessen mentioned this pull request Oct 28, 2024
sqlite: improve error handling using MaybeLocal #55571
Merged
nodejs-github-bot pushed a commit that referenced this pull request Nov 4, 2024
@tniessen
sqlite: improve error handling using MaybeLocal
bdc2662
As per James' suggestion, consistently use MaybeLocal and avoid Check()
and ToLocalChecked() entirely.

Refs: #54687
PR-URL: #55571
Reviewed-By: Colin Ihrig <[email protected]>
aduh95 pushed a commit that referenced this pull request Nov 5, 2024
@tniessen @aduh95
sqlite: improve error handling using MaybeLocal
151bb16
As per James' suggestion, consistently use MaybeLocal and avoid Check()
and ToLocalChecked() entirely.

Refs: #54687
PR-URL: #55571
Reviewed-By: Colin Ihrig <[email protected]>
aduh95 pushed a commit that referenced this pull request Nov 6, 2024
@tniessen @aduh95
sqlite: improve error handling using MaybeLocal
7aa250a
As per James' suggestion, consistently use MaybeLocal and avoid Check()
and ToLocalChecked() entirely.

Refs: #54687
PR-URL: #55571
Reviewed-By: Colin Ihrig <[email protected]>
ruyadorno pushed a commit that referenced this pull request Nov 27, 2024
@tniessen @ruyadorno
sqlite: improve error handling using MaybeLocal
be98d32
As per James' suggestion, consistently use MaybeLocal and avoid Check()
and ToLocalChecked() entirely.

Refs: #54687
PR-URL: #55571
Reviewed-By: Colin Ihrig <[email protected]>
ruyadorno pushed a commit that referenced this pull request Nov 27, 2024
@tniessen @ruyadorno
sqlite: improve error handling using MaybeLocal
428701a
As per James' suggestion, consistently use MaybeLocal and avoid Check()
and ToLocalChecked() entirely.

Refs: #54687
PR-URL: #55571
Reviewed-By: Colin Ihrig <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@jasnell jasnell jasnell left review comments
@cjihrig cjihrig cjihrig approved these changes
+1 more reviewer
@targos targos targos approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. sqlite Issues and PRs related to the SQLite subsystem.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@tniessen @targos @nodejs-github-bot @jasnell @cjihrig
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — 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.