Skip to content

Commit 2652ebc

Browse files
committed
update sqlite limits to reset using Infinity instead of null
1 parent 771ad11 commit 2652ebc

File tree

3 files changed

+43
-16
lines changed

3 files changed

+43
-16
lines changed

doc/api/sqlite.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,14 +481,14 @@ console.log(db.limits.length);
481481
db.limits.sqlLength = 100000;
482482

483483
// Reset a limit to its compile-time maximum
484-
db.limits.sqlLength = null;
484+
db.limits.sqlLength = Infinity;
485485
```
486486

487487
Available properties: `length`, `sqlLength`, `column`, `exprDepth`,
488488
`compoundSelect`, `vdbeOp`, `functionArg`, `attach`, `likePatternLength`,
489489
`variableNumber`, `triggerDepth`.
490490

491-
Setting a property to `null` resets the limit to its compile-time maximum value.
491+
Setting a property to `Infinity` resets the limit to its compile-time maximum value.
492492

493493
### `database.open()`
494494

src/node_sqlite.cc

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
#include <array>
1616
#include <cinttypes>
17-
#include <climits>
17+
#include <cmath>
18+
#include <limits>
1819

1920
namespace node {
2021
namespace sqlite {
@@ -794,22 +795,28 @@ Intercepted DatabaseSyncLimits::LimitsSetter(
794795
return Intercepted::kYes;
795796
}
796797

798+
if (!value->IsNumber()) {
799+
THROW_ERR_INVALID_ARG_TYPE(
800+
isolate, "Limit value must be a non-negative integer or Infinity.");
801+
return Intercepted::kYes;
802+
}
803+
804+
const double num_value = value.As<Number>()->Value();
797805
int new_value;
798806

799-
if (value->IsNull()) {
800-
// null resets the limit to the compile-time maximum
801-
new_value = INT_MAX;
802-
} else if (value->IsInt32()) {
807+
if (std::isinf(num_value) && num_value > 0) {
808+
// Positive Infinity resets the limit to the compile-time maximum
809+
new_value = std::numeric_limits<int>::max();
810+
} else if (!value->IsInt32()) {
811+
THROW_ERR_INVALID_ARG_TYPE(
812+
isolate, "Limit value must be a non-negative integer or Infinity.");
813+
return Intercepted::kYes;
814+
} else {
803815
new_value = value.As<Int32>()->Value();
804-
// Validate non-negative
805816
if (new_value < 0) {
806817
THROW_ERR_OUT_OF_RANGE(isolate, "Limit value must be non-negative.");
807818
return Intercepted::kYes;
808819
}
809-
} else {
810-
THROW_ERR_INVALID_ARG_TYPE(isolate,
811-
"Limit value must be an integer or null.");
812-
return Intercepted::kYes;
813820
}
814821

815822
sqlite3_limit(

test/parallel/test-sqlite-limits.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,16 @@ suite('DatabaseSync limits', () => {
8080
t.assert.strictEqual(db.limits.column, 50);
8181
});
8282

83-
test('null resets limit to maximum', (t) => {
83+
test('Infinity resets limit to maximum', (t) => {
8484
const db = new DatabaseSync(':memory:');
8585
const originalLength = db.limits.length;
8686

8787
// Set to a lower value
8888
db.limits.length = 100;
8989
t.assert.strictEqual(db.limits.length, 100);
9090

91-
// Reset to maximum using null
92-
db.limits.length = null;
91+
// Reset to maximum using Infinity
92+
db.limits.length = Infinity;
9393
t.assert.strictEqual(db.limits.length, originalLength);
9494
});
9595

@@ -99,7 +99,7 @@ suite('DatabaseSync limits', () => {
9999
db.limits.length = 'invalid';
100100
}, {
101101
name: 'TypeError',
102-
message: /Limit value must be an integer or null/,
102+
message: /Limit value must be a non-negative integer or Infinity/,
103103
});
104104
});
105105

@@ -113,6 +113,26 @@ suite('DatabaseSync limits', () => {
113113
});
114114
});
115115

116+
test('throws on null value', (t) => {
117+
const db = new DatabaseSync(':memory:');
118+
t.assert.throws(() => {
119+
db.limits.length = null;
120+
}, {
121+
name: 'TypeError',
122+
message: /Limit value must be a non-negative integer or Infinity/,
123+
});
124+
});
125+
126+
test('throws on negative Infinity', (t) => {
127+
const db = new DatabaseSync(':memory:');
128+
t.assert.throws(() => {
129+
db.limits.length = -Infinity;
130+
}, {
131+
name: 'TypeError',
132+
message: /Limit value must be a non-negative integer or Infinity/,
133+
});
134+
});
135+
116136
test('throws on getter access after close', (t) => {
117137
const db = new DatabaseSync(':memory:');
118138
db.close();

0 commit comments

Comments
 (0)