gnunet

Main GNUnet Logic
Log | Files | Refs | Submodules | README | LICENSE

commit 0efa2d4d007cc24fcf087d8f61e2b879a89cf0a5
parent 55553e01b8e19d250756538a3d94e48f16ecbb7b
Author: Christian Grothoff <christian@grothoff.org>
Date:   Wed, 12 Oct 2022 14:46:44 +0200

-avoid grow-by-one in statement preparations

Diffstat:
Msrc/pq/pq.h | 10++++++++++
Msrc/pq/pq_prepare.c | 30+++++++++++++++++-------------
Msrc/pq/test_pq.c | 6++----
3 files changed, 29 insertions(+), 17 deletions(-)

diff --git a/src/pq/pq.h b/src/pq/pq.h @@ -50,6 +50,16 @@ struct GNUNET_PQ_Context struct GNUNET_PQ_PreparedStatement *ps; /** + * Length of the @e ps array. + */ + unsigned int ps_len; + + /** + * Last used offset in the @e ps array. + */ + unsigned int ps_off; + + /** * Configuration to use to connect to the DB. */ char *config_str; diff --git a/src/pq/pq_prepare.c b/src/pq/pq_prepare.c @@ -91,26 +91,30 @@ GNUNET_PQ_prepare_statements (struct GNUNET_PQ_Context *db, if (db->ps != ps) { /* add 'ps' to list db->ps of prepared statements to run on reconnect! */ - unsigned int olen = 0; /* length of existing 'db->ps' array */ unsigned int nlen = 0; /* length of 'ps' array */ + unsigned int xlen; struct GNUNET_PQ_PreparedStatement *rps; /* combined array */ - if (NULL != db->ps) - while (NULL != db->ps[olen].name) - olen++; while (NULL != ps[nlen].name) nlen++; - rps = GNUNET_new_array (olen + nlen + 1, - struct GNUNET_PQ_PreparedStatement); - if (NULL != db->ps) - memcpy (rps, - db->ps, - olen * sizeof (struct GNUNET_PQ_PreparedStatement)); - memcpy (&rps[olen], + xlen = nlen + db->ps_off; + if (xlen > db->ps_len) + { + xlen = 2 * xlen + 1; + rps = GNUNET_new_array (xlen, + struct GNUNET_PQ_PreparedStatement); + if (NULL != db->ps) + memcpy (rps, + db->ps, + db->ps_off * sizeof (struct GNUNET_PQ_PreparedStatement)); + GNUNET_free (db->ps); + db->ps_len = xlen; + db->ps = rps; + } + memcpy (&db->ps[db->ps_off], ps, nlen * sizeof (struct GNUNET_PQ_PreparedStatement)); - GNUNET_free (db->ps); - db->ps = rps; + db->ps_off += nlen; } return GNUNET_PQ_prepare_once (db, diff --git a/src/pq/test_pq.c b/src/pq/test_pq.c @@ -70,8 +70,7 @@ postgres_prepare (struct GNUNET_PQ_Context *db) ",unn" ") VALUES " "($1, $2, $3, $4, $5, $6," - "$7, $8, $9, $10);", - 10), + "$7, $8, $9, $10);"), GNUNET_PQ_make_prepare ("test_select", "SELECT" " pub" @@ -86,8 +85,7 @@ postgres_prepare (struct GNUNET_PQ_Context *db) ",unn" " FROM test_pq" " ORDER BY abs_time DESC " - " LIMIT 1;", - 0), + " LIMIT 1;"), GNUNET_PQ_PREPARED_STATEMENT_END };