Skip to content

Commit 406e215

Browse files
sqlite: validate maxSize argument in createTagStore()
Signed-off-by: anshikakalpana <anshikajain196872@gmail.com>
1 parent 1b04f16 commit 406e215

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

‎src/node_sqlite.cc‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,10 +1044,23 @@ void DatabaseSync::CreateTagStore(const FunctionCallbackInfo<Value>& args) {
10441044
return;
10451045
}
10461046
int capacity = 1000;
1047-
if (args.Length() > 0 && args[0]->IsNumber()) {
1048-
capacity = args[0].As<Number>()->Value();
1047+
if (args.Length() > 0 && !args[0]->IsUndefined()) {
1048+
if (!args[0]->IsNumber()) {
1049+
THROW_ERR_INVALID_ARG_TYPE(
1050+
env->isolate(),
1051+
"The \"maxSize\" argument must be a positive integer.");
1052+
return;
1053+
}
1054+
double val = args[0].As<Number>()->Value();
1055+
if (!std::isfinite(val) || std::floor(val) != val || val <= 0 ||
1056+
val > std::numeric_limits<int>::max()) {
1057+
THROW_ERR_OUT_OF_RANGE(
1058+
env->isolate(),
1059+
"The \"maxSize\" argument must be a positive integer.");
1060+
return;
1061+
}
1062+
capacity = static_cast<int>(val);
10491063
}
1050-
10511064
BaseObjectPtr<SQLTagStore> session =
10521065
SQLTagStore::Create(env, BaseObjectWeakPtr<DatabaseSync>(db), capacity);
10531066
if (!session) {

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,40 @@ test('TagStore capacity, size, and clear', () => {
112112
assert.strictEqual(sql.capacity, 10);
113113
});
114114

115+
test('createTagStore throws on invalid maxSize', () => {
116+
const db = new DatabaseSync(':memory:');
117+
118+
assert.throws(() => db.createTagStore(0), {
119+
code: 'ERR_OUT_OF_RANGE',
120+
message: /maxSize/,
121+
});
122+
123+
assert.throws(() => db.createTagStore(-1), {
124+
code: 'ERR_OUT_OF_RANGE',
125+
message: /maxSize/,
126+
});
127+
128+
assert.throws(() => db.createTagStore(NaN), {
129+
code: 'ERR_OUT_OF_RANGE',
130+
message: /maxSize/,
131+
});
132+
133+
assert.throws(() => db.createTagStore(1.5), {
134+
code: 'ERR_OUT_OF_RANGE',
135+
message: /maxSize/,
136+
});
137+
138+
assert.throws(() => db.createTagStore('abc'), {
139+
code: 'ERR_INVALID_ARG_TYPE',
140+
message: /maxSize/,
141+
});
142+
143+
assert.throws(() => db.createTagStore(Number.MAX_SAFE_INTEGER), {
144+
code: 'ERR_OUT_OF_RANGE',
145+
message: /maxSize/,
146+
});
147+
});
148+
115149
test('sql.db returns the associated DatabaseSync instance', () => {
116150
assert.strictEqual(sql.db, db);
117151
});

0 commit comments

Comments
 (0)