]> git.ipfire.org Git - thirdparty/ulogd2.git/commitdiff
output: SQLITE3: improve formatting of insert statement
authorJeremy Sowden <jeremy@azazel.net>
Tue, 30 Nov 2021 10:55:48 +0000 (10:55 +0000)
committerPablo Neira Ayuso <pablo@netfilter.org>
Mon, 3 Jan 2022 14:00:49 +0000 (15:00 +0100)
`sqlite3_createstmt` contains a variable `stmt_pos` 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_pos, ...);
  stmt_pos = priv->stmt + strlen(priv->stmt);

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

Pablo mangled this original patch to add this chunk at the end of this
patch (originally submitted as a conversion to use strcpy).

+       for (i = 0; i < cols - 1; i++)
+               stmt_pos += sprintf(stmt_pos, "?,");

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

index 41aeeec2785467a85ade236c7d4b1c30fb701ec6..692ff2bd3e294423fca109fd93639fc0d3924b4a 100644 (file)
@@ -226,9 +226,9 @@ sqlite3_createstmt(struct ulogd_pluginstance *pi)
                ulogd_log(ULOGD_ERROR, "SQLITE3: out of memory\n");
                return -1;
        }
+       stmt_pos = priv->stmt;
 
-       sprintf(priv->stmt, "insert into %s (", table_ce(pi));
-       stmt_pos = priv->stmt + strlen(priv->stmt);
+       stmt_pos += sprintf(stmt_pos, "insert into %s (", table_ce(pi));
 
        tailq_for_each(f, priv->fields, link) {
                strncpy(buf, f->name, ULOGD_MAX_KEYLEN);
@@ -236,21 +236,17 @@ sqlite3_createstmt(struct ulogd_pluginstance *pi)
                while ((underscore = strchr(buf, '.')))
                        *underscore = '_';
 
-               sprintf(stmt_pos, "%s,", buf);
-               stmt_pos = priv->stmt + strlen(priv->stmt);
+               stmt_pos += sprintf(stmt_pos, "%s,", buf);
 
                cols++;
        }
 
        *(stmt_pos - 1) = ')';
 
-       sprintf(stmt_pos, " values (");
-       stmt_pos = priv->stmt + strlen(priv->stmt);
+       stmt_pos += sprintf(stmt_pos, " values (");
 
-       for (i = 0; i < cols - 1; i++) {
-               sprintf(stmt_pos,"?,");
-               stmt_pos += 2;
-       }
+       for (i = 0; i < cols - 1; i++)
+               stmt_pos += sprintf(stmt_pos, "?,");
 
        sprintf(stmt_pos, "?)");
        ulogd_log(ULOGD_DEBUG, "%s: stmt='%s'\n", pi->id, priv->stmt);