Skip to content

Commit 957cec2

Browse files
committed
Added cast to to_buf for SparseVector [skip ci]
1 parent 4e28ff3 commit 957cec2

2 files changed

Lines changed: 4 additions & 3 deletions

File tree

include/pgvector/pqxx.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@ template<> struct string_traits<pgvector::SparseVector> {
246246
if (i != 0) {
247247
here += pqxx::into_buf(buf.subspan(here), ",", c);
248248
}
249-
here += pqxx::into_buf(buf.subspan(here), indices[i] + 1, c);
249+
// cast to prevent undefined behavior on overflow and require less buffer space
250+
here += pqxx::into_buf(buf.subspan(here), static_cast<unsigned int>(indices[i]) + 1, c);
250251
here += pqxx::into_buf(buf.subspan(here), ":", c);
251252
here += pqxx::into_buf(buf.subspan(here), values[i], c);
252253
}
@@ -270,7 +271,7 @@ template<> struct string_traits<pgvector::SparseVector> {
270271
size += pqxx::size_buffer("{");
271272
for (size_t i = 0; i < nnz; i++) {
272273
size += pqxx::size_buffer(",");
273-
size += pqxx::size_buffer(indices[i] + 1);
274+
size += pqxx::size_buffer(static_cast<unsigned int>(indices[i]) + 1);
274275
size += pqxx::size_buffer(":");
275276
size += pqxx::size_buffer(values[i]);
276277
}

test/pqxx_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ void test_halfvec_size_buffer() {
353353
}
354354

355355
void test_sparsevec_size_buffer() {
356-
assert_equal(pqxx::size_buffer(pgvector::SparseVector{{1, 2, 3}}), 106u);
356+
assert_equal(pqxx::size_buffer(pgvector::SparseVector{{1, 2, 3}}), 103u);
357357
}
358358

359359
void test_pqxx() {

0 commit comments

Comments
 (0)