Skip to content

Commit 2306077

Browse files
Pierre-Luc Gagnéclaude
andcommitted
fix: guard .error() calls on std::expected to avoid UB in prepared_statement tests
Calling .error() on a std::expected that holds a value is undefined behavior. The uninitialized string caused std::bad_alloc when operator+ read garbage size. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent a82d0d0 commit 2306077

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

tests/integration/mysql/test_query_builder.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,11 +1656,11 @@ suite<"prepared_statement Integration"> prepared_statement_integration_suite = [
16561656
expect(db->execute(create_table(trade{})).has_value());
16571657

16581658
auto stmt = db->prepare("INSERT INTO trade (code, type, executed_at, recorded_at) VALUES (?, ?, NOW(), NOW())");
1659-
expect(fatal(stmt.has_value())) << "prepare should succeed: " + stmt.error();
1659+
expect(fatal(stmt.has_value())) << "prepare should succeed: " + (stmt.has_value() ? "" : stmt.error());
16601660
expect(stmt->param_count() == 2u);
16611661

16621662
auto result = stmt->execute(std::string{"AAPL"}, std::string{"Stock"});
1663-
expect(fatal(result.has_value())) << "execute should succeed: " + result.error();
1663+
expect(fatal(result.has_value())) << "execute should succeed: " + (result.has_value() ? "" : result.error());
16641664
expect(*result == 1u) << "should affect 1 row";
16651665

16661666
result = stmt->execute(std::string{"GOOGL"}, std::string{"Stock"});
@@ -1698,7 +1698,7 @@ suite<"prepared_statement Integration"> prepared_statement_integration_suite = [
16981698

16991699
using row_t = std::tuple<uint32_t, std::string>;
17001700
auto rows = sel->query<row_t>(std::string{"Stock"});
1701-
expect(fatal(rows.has_value())) << "query should succeed: " + rows.error();
1701+
expect(fatal(rows.has_value())) << "query should succeed: " + (rows.has_value() ? "" : rows.error());
17021702
expect(rows->size() == 3u) << "should return 3 rows, got " + std::to_string(rows->size());
17031703

17041704
// Verify ordered by code: AAPL, GOOGL, MSFT

0 commit comments

Comments
 (0)