[PATCH ulogd2 v2 v2 31/34] output: pgsql: remove variable-length arrays

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



VLA's are deprecated.

Group all the SQL macros at the top of the file and format them.

Signed-off-by: Jeremy Sowden <jeremy@xxxxxxxxxx>
---
 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




[Index of Archives]     [Netfitler Users]     [Berkeley Packet Filter]     [LARTC]     [Bugtraq]     [Yosemite Forum]

  Powered by Linux