From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id BD615428D4; Wed, 5 Apr 2023 17:45:08 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id C28BA42D0C; Wed, 5 Apr 2023 17:44:41 +0200 (CEST) Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by mails.dpdk.org (Postfix) with ESMTP id 7DE3A42D0C for ; Wed, 5 Apr 2023 17:44:40 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1680709480; x=1712245480; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=3AkT5t0n+8KM11Lio4V7yr97BYKxeiFJwttdaTNR43I=; b=NRAG5+9wghQj09hpgNe4TmlAXT9o9BGCjv36KM+l8BmaL0V3ak5HTf+G cc6DZ6F7qukGyIm9/S8EQAgpHLZbQpD9YsAzv7BmiZHcF3Y23dGtL2BGR iH+D/CES1xUpMbiQJpiiWipqmO297b3aQN1UTR1xmT0Vl72+P+fYLidpc flchVcwdTKqGpR5lIVj5lq2xjfGZt+AeAtgl5cpPoopUHhsDKF1KDbp29 bxbmtWoSWM4y5IuCtSxtE78iGs9uvL5The1/iw2oZYJ5xvKZjsR7JvoRC yHHcZRURTxKM3zrht/b+3sjhvHq0aSCXVzM06aEBNsOSOozXhbhv7L3w+ A==; X-IronPort-AV: E=McAfee;i="6600,9927,10671"; a="339980405" X-IronPort-AV: E=Sophos;i="5.98,321,1673942400"; d="scan'208";a="339980405" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Apr 2023 08:44:40 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10671"; a="686790315" X-IronPort-AV: E=Sophos;i="5.98,321,1673942400"; d="scan'208";a="686790315" Received: from silpixa00401385.ir.intel.com ([10.237.214.40]) by orsmga002.jf.intel.com with ESMTP; 05 Apr 2023 08:44:38 -0700 From: Bruce Richardson To: dev@dpdk.org Cc: ciara.power@intel.com, roretzla@linux.microsoft.com, Bruce Richardson Subject: [PATCH v2 5/5] telemetry: remove VLA in json string format function Date: Wed, 5 Apr 2023 16:44:14 +0100 Message-Id: <20230405154414.183915-6-bruce.richardson@intel.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230405154414.183915-1-bruce.richardson@intel.com> References: <20230310181836.162336-1-bruce.richardson@intel.com> <20230405154414.183915-1-bruce.richardson@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Since variable length arrays (VLAs) are potentially unsecure and unsupported by some compilers, rework the code to remove their use. As with previous changes to remove VLAs in the telemetry code, this function uses two methods to avoid modifying the buffer when adding to it fails: * if there are only a few characters in the buffer, save them off to restore on failure, then use the buffer as-is, * otherwise use malloc rather than a VLA to allocate a temporary buffer and copy from that on success only. Signed-off-by: Bruce Richardson --- app/test/test_telemetry_json.c | 2 +- lib/telemetry/telemetry_json.h | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/test/test_telemetry_json.c b/app/test/test_telemetry_json.c index e81e3a8a98..5617eac540 100644 --- a/app/test/test_telemetry_json.c +++ b/app/test/test_telemetry_json.c @@ -129,7 +129,7 @@ test_string_char_escaping(void) { static const char str[] = "A string across\ntwo lines and \"with quotes\"!"; const char *expected = "\"A string across\\ntwo lines and \\\"with quotes\\\"!\""; - char buf[sizeof(str) + 10]; + char buf[sizeof(str) + 10] = ""; int used = 0; used = rte_tel_json_str(buf, sizeof(buf), used, str); diff --git a/lib/telemetry/telemetry_json.h b/lib/telemetry/telemetry_json.h index 4d725d938b..fceff91842 100644 --- a/lib/telemetry/telemetry_json.h +++ b/lib/telemetry/telemetry_json.h @@ -130,13 +130,28 @@ __json_format_str_to_buf(char *buf, const int len, static inline int __json_format_str(char *buf, const int len, const char *prefix, const char *str, const char *suffix) { - char tmp[len]; int ret; + char saved[4] = ""; + char *tmp; + + if (strnlen(buf, sizeof(saved)) < sizeof(saved)) { + /* we have only a few bytes in buffer, so save them off to restore on error*/ + strcpy(saved, buf); + ret = __json_format_str_to_buf(buf, len, prefix, str, suffix); + if (ret == 0) + strcpy(buf, saved); /* restore */ + return ret; + } + + tmp = malloc(len); + if (tmp == NULL) + return 0; ret = __json_format_str_to_buf(tmp, len, prefix, str, suffix); if (ret > 0) - strcpy(buf, tmp); + strcpy(buf, saved); + free(tmp); return ret; } -- 2.37.2