From: Jeremy Sowden <jeremy@azazel.net>
To: Netfilter Devel <netfilter-devel@vger.kernel.org>
Subject: [PATCH ulogd2 v2 v2 31/34] output: pgsql: remove variable-length arrays
Date: Tue, 29 Nov 2022 21:47:46 +0000 [thread overview]
Message-ID: <20221129214749.247878-32-jeremy@azazel.net> (raw)
In-Reply-To: <20221129214749.247878-1-jeremy@azazel.net>
VLA's are deprecated.
Group all the SQL macros at the top of the file and format them.
Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
---
output/pgsql/ulogd_output_PGSQL.c | 95 ++++++++++++++++++++-----------
1 file changed, 62 insertions(+), 33 deletions(-)
diff --git a/output/pgsql/ulogd_output_PGSQL.c b/output/pgsql/ulogd_output_PGSQL.c
index 70bfbee7f565..b125674b7364 100644
--- a/output/pgsql/ulogd_output_PGSQL.c
+++ b/output/pgsql/ulogd_output_PGSQL.c
@@ -79,20 +79,46 @@ static struct config_keyset pgsql_kset = {
#define schema_ce(x) ((x)->ces[DB_CE_NUM + 5])
#define connstr_ce(x) ((x)->ces[DB_CE_NUM + 6])
-#define PGSQL_HAVE_NAMESPACE_TEMPLATE \
- "SELECT nspname FROM pg_namespace n WHERE n.nspname='%s'"
+#define PGSQL_HAVE_NAMESPACE_TEMPLATE \
+ "SELECT nspname " \
+ "FROM pg_namespace n " \
+ "WHERE n.nspname = '%s'"
+
+#define PGSQL_GETCOLUMN_TEMPLATE \
+ "SELECT a.attname " \
+ "FROM pg_class c, pg_attribute a " \
+ "WHERE c.relname = '%s' " \
+ "AND a.attnum > 0 " \
+ "AND a.attrelid = c.oid " \
+ "ORDER BY a.attnum"
+
+#define PGSQL_GETCOLUMN_TEMPLATE_SCHEMA \
+ "SELECT a.attname " \
+ "FROM pg_attribute a, pg_class c " \
+ "LEFT JOIN pg_namespace n ON c.relnamespace = n.oid " \
+ "WHERE c.relname = '%s' " \
+ "AND n.nspname = '%s' " \
+ "AND a.attnum > 0 " \
+ "AND a.attrelid = c.oid " \
+ "AND a.attisdropped = FALSE " \
+ "ORDER BY a.attnum"
/* Determine if server support schemas */
static int pgsql_namespace(struct ulogd_pluginstance *upi)
{
struct pgsql_instance *pi = (struct pgsql_instance *) upi->private;
- char pgbuf[strlen(PGSQL_HAVE_NAMESPACE_TEMPLATE) +
- strlen(schema_ce(upi->config_kset).u.string) + 1];
PGresult *pgres;
+ char *pgbuf;
+ int rv = -1;
if (!pi->dbh)
return -1;
+ pgbuf = malloc(sizeof(PGSQL_HAVE_NAMESPACE_TEMPLATE) +
+ strlen(schema_ce(upi->config_kset).u.string));
+ if (!pgbuf)
+ return -1;
+
sprintf(pgbuf, PGSQL_HAVE_NAMESPACE_TEMPLATE,
schema_ce(upi->config_kset).u.string);
ulogd_log(ULOGD_DEBUG, "%s\n", pgbuf);
@@ -100,7 +126,7 @@ static int pgsql_namespace(struct ulogd_pluginstance *upi)
pgres = PQexec(pi->dbh, pgbuf);
if (!pgres) {
ulogd_log(ULOGD_DEBUG, "\n result false");
- return -1;
+ goto err_free_buf;
}
if (PQresultStatus(pgres) == PGRES_TUPLES_OK) {
@@ -108,51 +134,50 @@ static int pgsql_namespace(struct ulogd_pluginstance *upi)
ulogd_log(ULOGD_DEBUG, "using schema %s\n",
schema_ce(upi->config_kset).u.string);
pi->db_inst.schema = schema_ce(upi->config_kset).u.string;
- } else {
+ rv = 0;
+ } else
ulogd_log(ULOGD_ERROR, "schema %s not found: %s\n",
- schema_ce(upi->config_kset).u.string, PQerrorMessage(pi->dbh));
- PQclear(pgres);
- return -1;
- }
+ schema_ce(upi->config_kset).u.string,
+ PQerrorMessage(pi->dbh));
} else {
pi->db_inst.schema = NULL;
+ rv = 0;
}
PQclear(pgres);
- return 0;
-}
-
-#define PGSQL_GETCOLUMN_TEMPLATE \
- "SELECT a.attname FROM pg_class c, pg_attribute a WHERE c.relname ='%s' AND a.attnum>0 AND a.attrelid=c.oid ORDER BY a.attnum"
+err_free_buf:
+ free(pgbuf);
-#define PGSQL_GETCOLUMN_TEMPLATE_SCHEMA \
- "SELECT a.attname FROM pg_attribute a, pg_class c LEFT JOIN pg_namespace n ON c.relnamespace=n.oid WHERE c.relname ='%s' AND n.nspname='%s' AND a.attnum>0 AND a.attrelid=c.oid AND a.attisdropped=FALSE ORDER BY a.attnum"
+ return rv;
+}
/* find out which columns the table has */
static int get_columns_pgsql(struct ulogd_pluginstance *upi)
{
struct pgsql_instance *pi = (struct pgsql_instance *) upi->private;
- char pgbuf[strlen(PGSQL_GETCOLUMN_TEMPLATE_SCHEMA)
- + strlen(table_ce(upi->config_kset).u.string)
- + strlen(pi->db_inst.schema) + 2];
PGresult *pgres;
- int rv;
+ char *pgbuf;
+ int rv = -1;
if (!pi->dbh) {
ulogd_log(ULOGD_ERROR, "no database handle\n");
return -1;
}
- if (pi->db_inst.schema) {
- snprintf(pgbuf, sizeof(pgbuf)-1,
- PGSQL_GETCOLUMN_TEMPLATE_SCHEMA,
- table_ce(upi->config_kset).u.string,
- pi->db_inst.schema);
- } else {
- snprintf(pgbuf, sizeof(pgbuf)-1, PGSQL_GETCOLUMN_TEMPLATE,
- table_ce(upi->config_kset).u.string);
- }
+ pgbuf = malloc(sizeof(PGSQL_GETCOLUMN_TEMPLATE_SCHEMA) +
+ strlen(table_ce(upi->config_kset).u.string) +
+ strlen(pi->db_inst.schema));
+ if (!pgbuf)
+ return -1;
+
+ if (pi->db_inst.schema)
+ sprintf(pgbuf, PGSQL_GETCOLUMN_TEMPLATE_SCHEMA,
+ table_ce(upi->config_kset).u.string,
+ pi->db_inst.schema);
+ else
+ sprintf(pgbuf, PGSQL_GETCOLUMN_TEMPLATE,
+ table_ce(upi->config_kset).u.string);
ulogd_log(ULOGD_DEBUG, "%s\n", pgbuf);
@@ -160,14 +185,13 @@ static int get_columns_pgsql(struct ulogd_pluginstance *upi)
if (!pgres) {
ulogd_log(ULOGD_DEBUG, "result false (%s)",
PQerrorMessage(pi->dbh));
- return -1;
+ goto err_free_buf;
}
if (PQresultStatus(pgres) != PGRES_TUPLES_OK) {
ulogd_log(ULOGD_DEBUG, "pres_command_not_ok (%s)",
PQerrorMessage(pi->dbh));
- PQclear(pgres);
- return -1;
+ goto err_clear_res;
}
rv = ulogd_db_alloc_input_keys(upi, PQntuples(pgres), pgres);
@@ -176,7 +200,12 @@ static int get_columns_pgsql(struct ulogd_pluginstance *upi)
if (!rv && upi->input.keys[0].name[0] == '.')
upi->input.keys[0].flags |= ULOGD_KEYF_INACTIVE;
+err_clear_res:
PQclear(pgres);
+
+err_free_buf:
+ free(pgbuf);
+
return rv;
}
--
2.35.1
next prev parent reply other threads:[~2022-11-29 21:58 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-29 21:47 [PATCH ulogd2 v2 v2 00/34] Refactor of the DB output plug-ins Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 01/34] ulogd: fix parse-error check Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 02/34] filter: fix buffer sizes in filter plug-ins Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 03/34] output: JSON: remove incorrect config value check Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 04/34] db: fix back-log capacity checks Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 05/34] build: add checks to configure.ac Jeremy Sowden
2022-11-30 10:04 ` Jan Engelhardt
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 06/34] src: remove some trailing white space Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 07/34] src: remove zero-valued config-key fields Jeremy Sowden
2022-11-30 10:21 ` Jan Engelhardt
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 08/34] src: parenthesize config-entry macro arguments Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 09/34] src: define constructors and destructors consistently Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 10/34] src: remove `TIME_ERR` macro Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 11/34] src: remove superfluous casts Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 12/34] conffile: replace malloc+strcpy with strdup Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 13/34] output: remove zero-initialized `struct ulogd_plugin` members Jeremy Sowden
2022-11-30 10:26 ` Jan Engelhardt
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 14/34] output: de-duplicate allocation of input keys Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 15/34] db: reorganize source Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 16/34] db: use consistent integer return values to indicate errors Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 17/34] db: change return type of two functions to `void` Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 18/34] db: open-code `_loop_reconnect_db` Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 19/34] db: improve calculation of sql statement length Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 20/34] db: refactor configuration Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 21/34] db: refactor ring-buffer initialization Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 22/34] db: refactor ring-buffer Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 23/34] db: refactor backlog Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 24/34] db: use `struct db_stmt` objects more widely Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 25/34] db: synchronize access to ring-buffer Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 26/34] db: avoid cancelling ring-buffer thread Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 27/34] db, IP2BIN: defer formatting of raw strings Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 28/34] db: add prep & exec support Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 29/34] output: mysql: " Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 30/34] output: pgsql: remove a couple of struct members Jeremy Sowden
2022-11-29 21:47 ` Jeremy Sowden [this message]
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 32/34] output: pgsql: tidy up `open_db_pgsql` and fix memory leak Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 33/34] output: pgsql: add prep & exec support Jeremy Sowden
2022-11-29 21:47 ` [PATCH ulogd2 v2 v2 34/34] output: sqlite3: reimplement using the common DB API Jeremy Sowden
2022-11-30 10:27 ` [PATCH ulogd2 v2 v2 00/34] Refactor of the DB output plug-ins Pablo Neira Ayuso
2022-11-30 16:03 ` Jeremy Sowden
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221129214749.247878-32-jeremy@azazel.net \
--to=jeremy@azazel.net \
--cc=netfilter-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).