Skip to content

Commit 5cdf935

Browse files
committed
Improved index check for from_string [skip ci]
1 parent 957cec2 commit 5cdf935

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

include/pgvector/pqxx.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ template<> struct string_traits<pgvector::SparseVector> {
200200
int index = pqxx::from_string<int>(substr.substr(0, ne), c);
201201
float value = pqxx::from_string<float>(substr.substr(ne + 1), c);
202202

203-
if (index < 1) {
203+
if (index < 1 || index > dimensions) {
204204
throw conversion_error{"Index out of bounds"};
205205
}
206206

test/pqxx_test.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ void test_sparsevec_from_string() {
264264
auto _ = pqxx::from_string<pgvector::SparseVector>("{-2147483648:1}/1");
265265
}, "Index out of bounds");
266266

267+
assert_exception<pqxx::conversion_error>([] {
268+
auto _ = pqxx::from_string<pgvector::SparseVector>("{2:1}/1");
269+
}, "Index out of bounds");
270+
267271
assert_exception<pqxx::conversion_error>([] {
268272
auto _ = pqxx::from_string<pgvector::SparseVector>("{1:4e38}/1");
269273
}, float_error("Could not convert '4e38' to float: Value out of range."));

0 commit comments

Comments
 (0)