Skip to content

Commit 6c2a64a

Browse files
committed
Added dimension checks [skip ci]
1 parent 597e3f8 commit 6c2a64a

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

include/pgvector/pqxx.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ template <> struct string_traits<pgvector::Vector> {
4949
static std::string_view to_buf(std::span<char> buf, const pgvector::Vector& value, ctx c = {}) {
5050
auto values = static_cast<std::vector<float>>(value);
5151

52+
// important! size_buffer cannot throw an exception on overflow
53+
// so perform this check before writing any data
54+
if (values.size() > 16000) {
55+
throw conversion_overrun{"vector cannot have more than 16000 dimensions"};
56+
}
57+
5258
size_t here = 0;
5359
buf[here++] = '[';
5460

@@ -103,6 +109,12 @@ template <> struct string_traits<pgvector::HalfVector> {
103109
static std::string_view to_buf(std::span<char> buf, const pgvector::HalfVector& value, ctx c = {}) {
104110
auto values = static_cast<std::vector<float>>(value);
105111

112+
// important! size_buffer cannot throw an exception on overflow
113+
// so perform this check before writing any data
114+
if (values.size() > 16000) {
115+
throw conversion_overrun{"halfvec cannot have more than 16000 dimensions"};
116+
}
117+
106118
size_t here = 0;
107119
buf[here++] = '[';
108120

0 commit comments

Comments
 (0)