Skip to content

Commit a0bb20a

Browse files
committed
Added initial check for buffer space [skip ci]
1 parent 5ccec0f commit a0bb20a

2 files changed

Lines changed: 27 additions & 12 deletions

File tree

include/pgvector/pqxx.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ template<> struct string_traits<pgvector::Vector> {
4747
}
4848

4949
static std::string_view to_buf(std::span<char> buf, const pgvector::Vector& value, ctx c = {}) {
50+
// confirm caller provided estimated buffer space
51+
if (buf.size() < size_buffer(value)) {
52+
throw conversion_overrun{"Not enough space in buffer for vector"};
53+
}
54+
5055
const std::vector<float>& values = value.values();
5156

5257
// important! size_buffer cannot throw an exception on overflow
@@ -115,6 +120,11 @@ template<> struct string_traits<pgvector::HalfVector> {
115120
}
116121

117122
static std::string_view to_buf(std::span<char> buf, const pgvector::HalfVector& value, ctx c = {}) {
123+
// confirm caller provided estimated buffer space
124+
if (buf.size() < size_buffer(value)) {
125+
throw conversion_overrun{"Not enough space in buffer for halfvec"};
126+
}
127+
118128
const std::vector<pgvector::Half>& values = value.values();
119129

120130
// important! size_buffer cannot throw an exception on overflow
@@ -213,6 +223,11 @@ template<> struct string_traits<pgvector::SparseVector> {
213223
}
214224

215225
static std::string_view to_buf(std::span<char> buf, const pgvector::SparseVector& value, ctx c = {}) {
226+
// confirm caller provided estimated buffer space
227+
if (buf.size() < size_buffer(value)) {
228+
throw conversion_overrun{"Not enough space in buffer for sparsevec"};
229+
}
230+
216231
int dimensions = value.dimensions();
217232
const std::vector<int>& indices = value.indices();
218233
const std::vector<float>& values = value.values();

test/pqxx_test.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -281,63 +281,63 @@ void test_sparsevec_from_string() {
281281
}
282282

283283
void test_vector_to_buf() {
284-
char buf[10];
284+
char buf[60];
285285
assert_equal(pqxx::to_buf(std::span<char>{buf}, pgvector::Vector{{1, 2, 3}}), "[1,2,3]");
286286

287287
assert_exception<pqxx::conversion_overrun>([] {
288288
return pqxx::to_buf(std::span<char>{}, pgvector::Vector{{1, 2, 3}});
289-
});
289+
}, "Not enough space in buffer for vector");
290290
}
291291

292292
void test_vector_into_buf() {
293-
char buf[10];
293+
char buf[60];
294294
size_t size = pqxx::into_buf(std::span<char>{buf}, pgvector::Vector{{1, 2, 3}});
295295
assert_equal(size, 7u);
296296
assert_equal(std::string_view{buf, size}, "[1,2,3]");
297297

298298
assert_exception<pqxx::conversion_overrun>([] {
299299
return pqxx::into_buf(std::span<char>{}, pgvector::Vector{{1, 2, 3}});
300-
});
300+
}, "Not enough space in buffer for vector");
301301
}
302302

303303
void test_halfvec_to_buf() {
304-
char buf[10];
304+
char buf[60];
305305
assert_equal(pqxx::to_buf(std::span<char>{buf}, pgvector::HalfVector{{1, 2, 3}}), "[1,2,3]");
306306

307307
assert_exception<pqxx::conversion_overrun>([] {
308308
return pqxx::to_buf(std::span<char>{}, pgvector::HalfVector{{1, 2, 3}});
309-
});
309+
}, "Not enough space in buffer for halfvec");
310310
}
311311

312312
void test_halfvec_into_buf() {
313-
char buf[10];
313+
char buf[60];
314314
size_t size = pqxx::into_buf(std::span<char>{buf}, pgvector::HalfVector{{1, 2, 3}});
315315
assert_equal(size, 7u);
316316
assert_equal(std::string_view{buf, size}, "[1,2,3]");
317317

318318
assert_exception<pqxx::conversion_overrun>([] {
319319
return pqxx::into_buf(std::span<char>{}, pgvector::HalfVector{{1, 2, 3}});
320-
});
320+
}, "Not enough space in buffer for halfvec");
321321
}
322322

323323
void test_sparsevec_to_buf() {
324-
char buf[40];
324+
char buf[120];
325325
assert_equal(pqxx::to_buf(std::span<char>{buf}, pgvector::SparseVector{{1, 2, 3}}), "{1:1,2:2,3:3}/3");
326326

327327
assert_exception<pqxx::conversion_overrun>([] {
328328
return pqxx::to_buf(std::span<char>{}, pgvector::SparseVector{{1, 2, 3}});
329-
});
329+
}, "Not enough space in buffer for sparsevec");
330330
}
331331

332332
void test_sparsevec_into_buf() {
333-
char buf[40];
333+
char buf[120];
334334
size_t size = pqxx::into_buf(std::span<char>{buf}, pgvector::SparseVector{{1, 2, 3}});
335335
assert_equal(size, 15u);
336336
assert_equal(std::string_view{buf, size}, "{1:1,2:2,3:3}/3");
337337

338338
assert_exception<pqxx::conversion_overrun>([] {
339339
return pqxx::into_buf(std::span<char>{}, pgvector::SparseVector{{1, 2, 3}});
340-
});
340+
}, "Not enough space in buffer for sparsevec");
341341
}
342342

343343
void test_vector_size_buffer() {

0 commit comments

Comments
 (0)