From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id C4B23428D4;
	Wed,  5 Apr 2023 18:04:54 +0200 (CEST)
Received: from mails.dpdk.org (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id 23B2042D17;
	Wed,  5 Apr 2023 18:04:47 +0200 (CEST)
Received: from mga04.intel.com (mga04.intel.com [192.55.52.120])
 by mails.dpdk.org (Postfix) with ESMTP id 0310D42C76
 for <dev@dpdk.org>; Wed,  5 Apr 2023 18:04:44 +0200 (CEST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple;
 d=intel.com; i=@intel.com; q=dns/txt; s=Intel;
 t=1680710685; x=1712246685;
 h=from:to:cc:subject:date:message-id:in-reply-to:
 references:mime-version:content-transfer-encoding;
 bh=UPZTfqHBNQu8Jg6cu1IrchoURK1D6gcL/1fIHkBd+8E=;
 b=hDKWtXuWZixeCTG1tJI045Sfpko4cPUMYT4PL0wEPwWzF/FcCvVpGG0y
 4bALnw1w5qQMn/KhwSMKkTkdH2t58npkn4y+DiBUeU7lIfrF5nqdcE0pF
 cs+N8OyxrK4hsnMa8fI+UjXqsPuy+1xulipPkRshTgQVjiHi7a3QKxc71
 RQBbAjZM6G+7GcHZOvrS5CkBaxw4vJdDHt6RGdhhvCv6rzigfa1KyuWSb
 d+VWFqpBYWvX8MXaoR+Iz5dPzjb+F6odkqn1DQJ16+v4IV8ZH3vBxboTZ
 MohccySA5WsGM/r95uO4O1j9feiMA3gyRXy0NBxxlm56s67G4AabC350K g==;
X-IronPort-AV: E=McAfee;i="6600,9927,10671"; a="341218605"
X-IronPort-AV: E=Sophos;i="5.98,321,1673942400"; d="scan'208";a="341218605"
Received: from fmsmga001.fm.intel.com ([10.253.24.23])
 by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384;
 05 Apr 2023 09:04:20 -0700
X-ExtLoop1: 1
X-IronPort-AV: E=McAfee;i="6600,9927,10671"; a="830405816"
X-IronPort-AV: E=Sophos;i="5.98,321,1673942400"; d="scan'208";a="830405816"
Received: from silpixa00401385.ir.intel.com ([10.237.214.40])
 by fmsmga001.fm.intel.com with ESMTP; 05 Apr 2023 09:04:19 -0700
From: Bruce Richardson <bruce.richardson@intel.com>
To: dev@dpdk.org
Cc: ciara.power@intel.com, roretzla@linux.microsoft.com,
 Bruce Richardson <bruce.richardson@intel.com>
Subject: [PATCH v3 3/5] telemetry: split out body of json string format fn
Date: Wed,  5 Apr 2023 17:03:24 +0100
Message-Id: <20230405160326.186921-4-bruce.richardson@intel.com>
X-Mailer: git-send-email 2.37.2
In-Reply-To: <20230405160326.186921-1-bruce.richardson@intel.com>
References: <20230310181836.162336-1-bruce.richardson@intel.com>
 <20230405160326.186921-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 <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org

To enable further rework to (efficiently) avoid using variable-length
arrays, we first separate out the body of the __json_format_str
function. This means that the actual VLA buffer is in the wrapper
function, and means we can reuse the actual writing code in multiple
code paths without duplication.

Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
 lib/telemetry/telemetry_json.h | 32 ++++++++++++++++++++++++--------
 1 file changed, 24 insertions(+), 8 deletions(-)

diff --git a/lib/telemetry/telemetry_json.h b/lib/telemetry/telemetry_json.h
index 1bddd124f9..aada523a27 100644
--- a/lib/telemetry/telemetry_json.h
+++ b/lib/telemetry/telemetry_json.h
@@ -80,15 +80,13 @@ static const char control_chars[0x20] = {
 
 /**
  * @internal
- * This function acts the same as __json_snprintf(buf, len, "%s%s%s", prefix, str, suffix)
- * except that it does proper escaping of "str" as necessary. Prefix and suffix should be compile-
- * time constants, or values not needing escaping.
- * Drops any invalid characters we don't support
+ * Function that does the actual printing, used by __json_format_str. Modifies buffer
+ * directly, but returns 0 on overflow. Otherwise returns number of chars written to buffer.
  */
 static inline int
-__json_format_str(char *buf, const int len, const char *prefix, const char *str, const char *suffix)
+__json_format_str_to_buf(char *tmp, const int len,
+		const char *prefix, const char *str, const char *suffix)
 {
-	char tmp[len];
 	int tmpidx = 0;
 
 	while (*prefix != '\0' && tmpidx < len)
@@ -123,11 +121,29 @@ __json_format_str(char *buf, const int len, const char *prefix, const char *str,
 		return 0;
 
 	tmp[tmpidx] = '\0';
-
-	strcpy(buf, tmp);
 	return tmpidx;
 }
 
+/**
+ * @internal
+ * This function acts the same as __json_snprintf(buf, len, "%s%s%s", prefix, str, suffix)
+ * except that it does proper escaping of "str" as necessary. Prefix and suffix should be compile-
+ * time constants, or values not needing escaping.
+ * Drops any invalid characters we don't support
+ */
+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;
+
+	ret = __json_format_str_to_buf(tmp, len, prefix, str, suffix);
+	if (ret > 0)
+		strcpy(buf, tmp);
+
+	return ret;
+}
+
 /* Copies an empty array into the provided buffer. */
 static inline int
 rte_tel_json_empty_array(char *buf, const int len, const int used)
-- 
2.37.2