Fix memory management issues in Pgsql_Params leave sources of moves in a valid state.

This commit is contained in:
eelke 2026-10-18 13:38:41 +02:00
parent ee404e8121
commit 8dea806cd9
4 changed files with 132 additions and 48 deletions

View file

@ -1,6 +1,7 @@
#ifndef PGSQL_PARAMS_H
#define PGSQL_PARAMS_H
#include <memory>
#include <vector>
#include <QString>
#include <libpq-fe.h>
@ -56,27 +57,26 @@ namespace Pgsql {
const int* formats() const { return m_paramFormats.data(); }
private:
using t_paramData = std::vector<std::unique_ptr<char[]>>;
using t_paramValues = std::vector<const char *>;
void deleteValues()
{
for (auto e : m_paramValues)
delete[] e;
}
/** \brief Deep copies the values of src, the other lists must already have been updated */
void appendValues(const Params &src);
/* Assumes other lists already have been copied */
void appendValues(const t_paramValues &r);
/** \brief Takes ownership of data and makes it visible through values() */
void pushValue(std::unique_ptr<char[]> data);
std::vector<Oid> m_paramTypes;
t_paramValues m_paramValues;
t_paramData m_paramData; ///< owns the text buffers, nullptr for NULL values
t_paramValues m_paramValues; ///< points into m_paramData, handed to libpq
std::vector<int> m_paramLengths; ///< postgresql ignores lengths for text parameters but we will it anyway for efficient copying
std::vector<int> m_paramFormats;
/** \brief Add a parameter to the list.
*
* The class takes ownership of data and will try to delete[] it.
* The class takes ownership of data.
*/
Param addText(const char *data, Oid oid=varchar_oid);
Param addText(std::unique_ptr<char[]> data, Oid oid=varchar_oid);
};
} // end namespace Pgsql