]> git.ipfire.org Git - thirdparty/ulogd2.git/commitdiff
db: improve formatting of insert statement
authorJeremy Sowden <jeremy@azazel.net>
Tue, 30 Nov 2021 10:55:52 +0000 (10:55 +0000)
committerPablo Neira Ayuso <pablo@netfilter.org>
Mon, 3 Jan 2022 15:33:31 +0000 (16:33 +0100)
`sql_createstmt` contains a variable `stmt_val` which points to the end
of the SQL already written, where the next chunk should be appended.
Currently, this is assigned after every write:

  sprintf(stmt_val, ...);
  stmt_val = mi->stmt + strlen(mi->stmt);

However, since `sprintf` returns the number of bytes written, increment
`stmt_val` by the return-value of `sprintf` in order to avoid the
repeated `strlen` calls.

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

index f0711146867fb4603fc124d77dc86f4f87b735cc..2dbe0db2fbfe454ea8b24bbad6782f5e02e7867d 100644 (file)
--- a/util/db.c
+++ b/util/db.c
@@ -67,7 +67,6 @@ static int sql_createstmt(struct ulogd_pluginstance *upi)
        unsigned int i;
        char *table = table_ce(upi->config_kset).u.string;
        char *procedure = procedure_ce(upi->config_kset).u.string;
-       char *stmt_val = NULL;
 
        if (mi->stmt)
                free(mi->stmt);
@@ -96,20 +95,21 @@ static int sql_createstmt(struct ulogd_pluginstance *upi)
        if (strncasecmp(procedure,"INSERT", strlen("INSERT")) == 0 &&
            (procedure[strlen("INSERT")] == '\0' ||
                        procedure[strlen("INSERT")] == ' ')) {
+               char *stmt_val = mi->stmt;
                char buf[ULOGD_MAX_KEYLEN];
                char *underscore;
 
                if(procedure[6] == '\0') {
                        /* procedure == "INSERT" */
                        if (mi->schema)
-                               sprintf(mi->stmt, "insert into %s.%s (", mi->schema, table);
+                               stmt_val += sprintf(stmt_val,
+                                                   "insert into %s.%s (",
+                                                   mi->schema, table);
                        else
-                               sprintf(mi->stmt, "insert into %s (", table);
-               }
-               else
-                       sprintf(mi->stmt, "%s (", procedure);
-
-               stmt_val = mi->stmt + strlen(mi->stmt);
+                               stmt_val += sprintf(stmt_val,
+                                                   "insert into %s (", table);
+               } else
+                       stmt_val += sprintf(stmt_val, "%s (", procedure);
 
                for (i = 0; i < upi->input.num_keys; i++) {
                        if (upi->input.keys[i].flags & ULOGD_KEYF_INACTIVE)
@@ -118,8 +118,7 @@ static int sql_createstmt(struct ulogd_pluginstance *upi)
                        strncpy(buf, upi->input.keys[i].name, ULOGD_MAX_KEYLEN);        
                        while ((underscore = strchr(buf, '.')))
                                *underscore = '_';
-                       sprintf(stmt_val, "%s,", buf);
-                       stmt_val = mi->stmt + strlen(mi->stmt);
+                       stmt_val += sprintf(stmt_val, "%s,", buf);
                }
                *(stmt_val - 1) = ')';