]> git.ipfire.org Git - thirdparty/ulogd2.git/commitdiff
output: PGSQL: improve mapping of DB columns to input-keys
authorJeremy Sowden <jeremy@azazel.net>
Tue, 30 Nov 2021 10:55:44 +0000 (10:55 +0000)
committerPablo Neira Ayuso <pablo@netfilter.org>
Mon, 6 Dec 2021 21:31:30 +0000 (22:31 +0100)
Currently, we copy the column-name to a buffer, iterate over it to
replace the underscores with full-stops, using `strchr` from the start
of the buffer on each iteration, then copy the buffer to the input-key's
`name` member.

Apart from the inefficiency, `strncpy` is used to do the copies, which
leads gcc to complain:

  ulogd_output_PGSQL.c:204:17: warning: `strncpy` output may be truncated copying 31 bytes from a string of length 31

Furthermore, the buffer is not initialized, which means that there is
also a possible buffer overrun if the column-name is too long, since
`strncpy` will not append a NUL.

Instead, copy the column-name directly to the input-key using
`snprintf`, and run `strchr` from the last underscore on each iteration.

Signed-off-by: Jeremy Sowden <jeremy@azazel.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
output/pgsql/ulogd_output_PGSQL.c

index f5a2823a7e1d46bf2df0b59170b3289d79f7227f..71d94031ac4e73db747984bd810079bf8638f865 100644 (file)
@@ -190,18 +190,18 @@ static int get_columns_pgsql(struct ulogd_pluginstance *upi)
        }
 
        for (i = 0; i < PQntuples(pi->pgres); i++) {
-               char buf[ULOGD_MAX_KEYLEN+1];
                char *underscore;
 
+               snprintf(upi->input.keys[i].name,
+                        sizeof(upi->input.keys[i].name),
+                        "%s", PQgetvalue(pi->pgres, i, 0));
+
                /* replace all underscores with dots */
-               strncpy(buf, PQgetvalue(pi->pgres, i, 0), ULOGD_MAX_KEYLEN);
-               while ((underscore = strchr(buf, '_')))
+               for (underscore = upi->input.keys[i].name;
+                    (underscore = strchr(underscore, '_')); )
                        *underscore = '.';
 
-               DEBUGP("field '%s' found: ", buf);
-
-               /* add it to list of input keys */
-               strncpy(upi->input.keys[i].name, buf, ULOGD_MAX_KEYLEN);
+               DEBUGP("field '%s' found\n", upi->input.keys[i].name);
        }
 
        /* ID (starting by '.') is a sequence */