summaryrefslogtreecommitdiffstats
path: root/output/dbi
diff options
context:
space:
mode:
authorJeremy Sowden <jeremy@azazel.net>2021-11-30 10:55:40 +0000
committerPablo Neira Ayuso <pablo@netfilter.org>2021-11-30 23:08:26 +0100
commit0a9fa5603a435afed955f5458a17331b7f2f18ca (patch)
tree2195e3c97b456e3ad151388a75f5a6f3557f1af3 /output/dbi
parent97fd6fa529330024264e5fa1ee5481a494c47137 (diff)
output: DBI: improve mapping of DB columns to input-keys
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, iterate over it a second time to lower-case all letters, and finally copy the buffer to the input-key's `name` member. In addition to being inefficient, `strncpy` is used to do the copies, which leads gcc to complain: ulogd_output_DBI.c:160: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 then iterate over it once to replace underscores and lower-case letters. Signed-off-by: Jeremy Sowden <jeremy@azazel.net> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'output/dbi')
-rw-r--r--output/dbi/ulogd_output_DBI.c34
1 files changed, 13 insertions, 21 deletions
diff --git a/output/dbi/ulogd_output_DBI.c b/output/dbi/ulogd_output_DBI.c
index b4a5bac..fff9abc 100644
--- a/output/dbi/ulogd_output_DBI.c
+++ b/output/dbi/ulogd_output_DBI.c
@@ -91,15 +91,6 @@ static struct config_keyset dbi_kset = {
#define dbtype_ce(x) (x->ces[DB_CE_NUM+6])
-/* lower-cases s in place */
-static void str_tolower(char *s)
-{
- while(*s) {
- *s = tolower(*s);
- s++;
- }
-}
-
/* find out which columns the table has */
static int get_columns_dbi(struct ulogd_pluginstance *upi)
{
@@ -140,24 +131,25 @@ static int get_columns_dbi(struct ulogd_pluginstance *upi)
}
for (ui=1; ui<=upi->input.num_keys; ui++) {
- char buf[ULOGD_MAX_KEYLEN+1];
- char *underscore;
- const char* field_name = dbi_result_get_field_name(pi->result, ui);
+ const char *field_name = dbi_result_get_field_name(pi->result, ui);
+ char *cp;
if (!field_name)
break;
- /* replace all underscores with dots */
- strncpy(buf, field_name, ULOGD_MAX_KEYLEN);
- while ((underscore = strchr(buf, '_')))
- *underscore = '.';
-
- str_tolower(buf);
+ snprintf(upi->input.keys[ui - 1].name,
+ sizeof(upi->input.keys[ui - 1].name),
+ "%s", field_name);
- DEBUGP("field '%s' found: ", buf);
+ /* down-case and replace all underscores with dots */
+ for (cp = upi->input.keys[ui - 1].name; *cp; cp++) {
+ if (*cp == '_')
+ *cp = '.';
+ else
+ *cp = tolower(*cp);
+ }
- /* add it to list of input keys */
- strncpy(upi->input.keys[ui-1].name, buf, ULOGD_MAX_KEYLEN);
+ DEBUGP("field '%s' found: ", upi->input.keys[ui - 1].name);
}
/* ID is a sequence */