Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1892EC433F5 for ; Wed, 24 Nov 2021 22:45:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343998AbhKXWsz (ORCPT ); Wed, 24 Nov 2021 17:48:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49704 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343941AbhKXWsz (ORCPT ); Wed, 24 Nov 2021 17:48:55 -0500 Received: from kadath.azazel.net (unknown [IPv6:2001:8b0:135f:bcd1:e0cb:4eff:fedf:e608]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BDB03C061574 for ; Wed, 24 Nov 2021 14:45:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=azazel.net; s=20190108; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=YGQeURDdCz54z5j28KGKzNkdG9AOBtHDlzyefX1Os/I=; b=NEJjtz07ASxAdAlJdGsanpZJQp y48wSaU2CW5H0MRdeIHoxPjIz3DsWzn0nEAqN7w58n8hHPPBpZRcF+pvmiEtoG+sQh6n8acz28d+g IxotsItlrpigD1yqfkj7cDBTh8WKOQwuO0FSiV+T3ZOkxO9DZ4vqgFX3pVPmRtL/y9PoJcmQx1e+i vyJCb6fqOuVLSj8RzAhaTJfhC8wxPLLCn1qhCd50Ygf3gcpjBHjExty1II+GbmbZPgQHNxyo6ZwN0 O1Fas+99vRyrdsK3O4hjbHb0XgtcuAZfrbsG9rTPf95odkK4ErwouBE7ntgntMNhApTRDKc5n4uc9 xm1DgTOA==; Received: from ulthar.dreamlands.azazel.net ([2001:8b0:fb7d:d6d7:2e4d:54ff:fe4b:a9ae]) by kadath.azazel.net with esmtp (Exim 4.94.2) (envelope-from ) id 1mq0h9-00563U-Nn for netfilter-devel@vger.kernel.org; Wed, 24 Nov 2021 22:24:51 +0000 From: Jeremy Sowden To: Netfilter Devel Subject: [ulogd2 PATCH v3 23/30] db: improve mapping of input-keys to DB columns Date: Wed, 24 Nov 2021 22:24:27 +0000 Message-Id: <20211124222444.2597311-33-jeremy@azazel.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211124222444.2597311-1-jeremy@azazel.net> References: <20211124222444.2597311-1-jeremy@azazel.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 2001:8b0:fb7d:d6d7:2e4d:54ff:fe4b:a9ae X-SA-Exim-Mail-From: jeremy@azazel.net X-SA-Exim-Scanned: No (on kadath.azazel.net); SAEximRunCond expanded to false Precedence: bulk List-ID: X-Mailing-List: netfilter-devel@vger.kernel.org Hitherto, we copied the key-name to a buffer, iterated over it to replace the full-stops with underscores, using `strchr` from the start of the buffer on each iteration, then appended the buffer to the SQL statement. Apart from the inefficiency, `strncpy` was used to do the copies, which led gcc to complain: ../../util/db.c:118:25: warning: `strncpy` output may be truncated copying 31 bytes from a string of length 31 Furthermore, the buffer was one character too short and so there was the possibility of overruns. Instead, we now append the key-name directly to the statement using `sprintf`, and run `strchr` from the last underscore on each iteration. Signed-off-by: Jeremy Sowden --- util/db.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/util/db.c b/util/db.c index 2dbe0db2fbfe..339e39ef4797 100644 --- a/util/db.c +++ b/util/db.c @@ -96,8 +96,6 @@ static int sql_createstmt(struct ulogd_pluginstance *upi) (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" */ @@ -112,13 +110,18 @@ static int sql_createstmt(struct ulogd_pluginstance *upi) stmt_val += sprintf(stmt_val, "%s (", procedure); for (i = 0; i < upi->input.num_keys; i++) { + char *underscore; + if (upi->input.keys[i].flags & ULOGD_KEYF_INACTIVE) continue; - strncpy(buf, upi->input.keys[i].name, ULOGD_MAX_KEYLEN); - while ((underscore = strchr(buf, '.'))) + underscore = stmt_val; + + stmt_val += sprintf(stmt_val, "%s,", + upi->input.keys[i].name); + + while ((underscore = strchr(underscore, '.'))) *underscore = '_'; - stmt_val += sprintf(stmt_val, "%s,", buf); } *(stmt_val - 1) = ')'; -- 2.33.0