Skip to content

Commit

Permalink
Removing usage of heap operations, new and delete
Browse files Browse the repository at this point in the history
  • Loading branch information
cstiborg committed Aug 26, 2024
1 parent 6606faa commit ac5ee7b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
6 changes: 5 additions & 1 deletion include/soci/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class SOCI_DECL session
SOCI_NOT_COPYABLE(session)

void reset_after_move();
schema_table_name * alloc_schema_table_name(const std::string& tableName);
schema_table_name& alloc_schema_table_name(const std::string& tableName);

std::ostringstream query_stream_;
std::unique_ptr<details::query_transformation_function> query_transformation_;
Expand All @@ -237,6 +237,10 @@ class SOCI_DECL session
std::size_t poolPosition_;
connection_pool * pool_;

// Storing schema_table_names in a forward list as these are required
// as persistent input to prepare_temp_type object during their life-
// span. The prepare_temp_type uses the addresses of the content of the
// schema_table_name_ thus, a container which doesn't move data is used.
std::forward_list<schema_table_name> schema_table_name_;
};

Expand Down
20 changes: 8 additions & 12 deletions src/backends/postgresql/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,15 @@ std::string quote(PGconn * conn, std::string& s)
std::string retv;
retv.resize(2 * s.length() + 3);
retv[0] = '\'';
size_t len_esc = PQescapeStringConn(conn, retv.data() + 1, s.c_str(), s.length(), &error_code);
size_t len_esc = PQescapeStringConn(conn, const_cast<char *>(retv.data()) + 1, s.c_str(), s.length(), &error_code);
if (error_code > 0)
{
len_esc = 0;
}
retv[len_esc] = '\0';
std::string returnValue("'");
returnValue += retv;
returnValue += "'";
retv[len_esc + 1] = '\'';
retv.resize(len_esc + 2);

delete[] retv;

return returnValue;
return retv;
}

// helper function to collect schemas from search_path
Expand All @@ -66,7 +62,7 @@ std::vector<std::string> get_schema_names(postgresql_session_backend & session,
bool quoted = false;
std::string schema;
while (!search_path_content.empty())
{
{
switch (search_path_content[0])
{
case '"':
Expand Down Expand Up @@ -119,7 +115,7 @@ std::vector<std::string> get_schema_names(postgresql_session_backend & session,
{
std::string user = PQgetvalue(current_user_result, 0, 0);

// Assure no bad characters
// Ensure no bad characters
schema_names.push_back(quote(conn, user));
}
}
Expand All @@ -130,11 +126,11 @@ std::vector<std::string> get_schema_names(postgresql_session_backend & session,
}

// helper function to create a comma separated list of strings
std::string create_list_of_strings(const std::vector<std::string>& list)
std::string create_list_of_strings(const std::vector<std::string>& strings)
{
std::ostringstream oss;
bool first = true;
for ( const auto& s: list )
for ( const auto& s: strings )
{
if ( first )
first = false;
Expand Down
8 changes: 4 additions & 4 deletions src/core/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ void session::reset_after_move()
pool_ = nullptr;
}

schema_table_name* session::alloc_schema_table_name(std::string & tableName)
schema_table_name& session::alloc_schema_table_name(const std::string & tableName)
{
std::size_t dot_pos = tableName.find( '.' );
schema_table_name stn;
Expand All @@ -241,7 +241,7 @@ schema_table_name* session::alloc_schema_table_name(std::string & tableName)
// These will remain in memory until the session object is deleted.
schema_table_name_.push_front(stn);

return &schema_table_name_.front();
return schema_table_name_.front();
}

session::~session()
Expand Down Expand Up @@ -585,9 +585,9 @@ details::prepare_temp_type session::prepare_column_descriptions(std::string & ta
}
else
{
schema_table_name * stn = alloc_schema_table_name(table_name);
schema_table_name& stn = alloc_schema_table_name(table_name);

return prepare << column_description_query, use(stn->schema, stn->ind, "s"), use(stn->table, "t");
return prepare << column_description_query, use(stn.schema, stn.ind, "s"), use(stn.table, "t");
}
}

Expand Down

0 comments on commit ac5ee7b

Please sign in to comment.