diff options
author | Jeremy Sowden <jeremy@azazel.net> | 2021-11-30 10:55:52 +0000 |
---|---|---|
committer | Pablo Neira Ayuso <pablo@netfilter.org> | 2022-01-03 16:33:31 +0100 |
commit | 2bb3c1b74c40fdd4de709ffc469619d5e404db73 (patch) | |
tree | 6373aeb5694baf13adfacf294e3a21f12fb45408 /util | |
parent | dd80349ac72244049ab8f57023ab5d362e3843f1 (diff) |
db: improve formatting of insert statement
`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>
Diffstat (limited to 'util')
-rw-r--r-- | util/db.c | 19 |
1 files changed, 9 insertions, 10 deletions
@@ -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) = ')'; |