Fix memory management for postgres in edge cases (#89)

This commit is contained in:
Marco Craveiro
2025-11-14 11:34:22 +00:00
committed by GitHub
parent 5131953c35
commit e07be25a5d
2 changed files with 15 additions and 3 deletions

View File

@@ -33,8 +33,11 @@ Result<Nothing> Connection::end_write() {
}
const auto res = PQgetResult(conn_.get());
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
return error(PQerrorMessage(conn_.get()));
const auto err = error(PQerrorMessage(conn_.get()));
PQclear(res);
return err;
}
PQclear(res);
return Nothing{};
}
@@ -68,6 +71,7 @@ Result<Nothing> Connection::insert_impl(
const auto& d = _data[i];
if (d.size() != current_row.size()) {
execute("ROLLBACK;");
execute("DEALLOCATE " + name + ";");
return error("Error in entry " + std::to_string(i) + ": Expected " +
std::to_string(current_row.size()) + " entries, got " +
@@ -88,13 +92,15 @@ Result<Nothing> Connection::insert_impl(
);
const auto status = PQresultStatus(res);
if (status != PGRES_COMMAND_OK) {
PQclear(res);
const auto err = error(std::string("Executing INSERT failed: ") +
PQresultErrorMessage(res));
execute("ROLLBACK;");
execute("DEALLOCATE " + name + ";");
return err;
}
PQclear(res);
}
return execute("DEALLOCATE " + name + ";");
@@ -170,6 +176,9 @@ Result<Nothing> Connection::write_impl(
static_cast<int>(buffer.size()));
if (success != 1) {
PQputCopyEnd(conn_.get(), NULL);
while (auto res = PQgetResult(conn_.get()))
PQclear(res);
return error("Error occurred while writing data to postgres.");
}
}

View File

@@ -9,7 +9,7 @@ namespace sqlgen::postgres {
Result<Ref<PGresult>> exec(const Ref<PGconn>& _conn,
const std::string& _sql) noexcept {
const auto res = PQexec(_conn.get(), _sql.c_str());
auto res = PQexec(_conn.get(), _sql.c_str());
const auto status = PQresultStatus(res);
@@ -18,6 +18,9 @@ Result<Ref<PGresult>> exec(const Ref<PGconn>& _conn,
const auto err =
error("Executing '" + _sql + "' failed: " + PQresultErrorMessage(res));
PQclear(res);
while ((res = PQgetResult(_conn.get())) != nullptr)
PQclear(res);
return err;
}