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 6EE31428D4; Wed, 5 Apr 2023 17:44:55 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 50D7642D1A; Wed, 5 Apr 2023 17:44:38 +0200 (CEST) Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by mails.dpdk.org (Postfix) with ESMTP id 642D342BAC for ; Wed, 5 Apr 2023 17:44:36 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1680709476; x=1712245476; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=aMqGXe4qQ2TIKAIHehX1IKjUBXVjPvqniY4tJ8Tfm9w=; b=kCHHYYWYJkkHufeidY8n3eHUrUZrswFXQ+e7RNAH0wpmQR9kjy+gs3GH wRFKHZcDfM01pHVFBbd0slMgTv2+M5ejykKhnUWGI0DMaiKQVoAO1mJHe Jr/oVMSZwsnAkU5tKDEjx7vHcrtdEbKNifwPcgHio3GvmyLY3wE/fovIg uS4lnqCTI8hf+s5RVpR7/TT5QgnvYvGBSdazfT1d8B0K/bwVhmhHL9UDk ZC891pnl71qCPLQQrPMriGgJjIHpLivm2ymd2MBw6QV7G++TuZ7IwNC65 SmxsP1H8A0qfnmInmva27fZfWn/P6OP6G+kzX0B1J4aXNa4sFN9auJEU0 A==; X-IronPort-AV: E=McAfee;i="6600,9927,10671"; a="339980381" X-IronPort-AV: E=Sophos;i="5.98,321,1673942400"; d="scan'208";a="339980381" 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:35 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10671"; a="686790287" X-IronPort-AV: E=Sophos;i="5.98,321,1673942400"; d="scan'208";a="686790287" Received: from silpixa00401385.ir.intel.com ([10.237.214.40]) by orsmga002.jf.intel.com with ESMTP; 05 Apr 2023 08:44:33 -0700 From: Bruce Richardson To: dev@dpdk.org Cc: ciara.power@intel.com, roretzla@linux.microsoft.com, Bruce Richardson Subject: [PATCH v2 3/5] telemetry: split out body of json string format fn Date: Wed, 5 Apr 2023 16:44:12 +0100 Message-Id: <20230405154414.183915-4-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 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 --- 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 cb18453449..4c1941ad15 100644 --- a/lib/telemetry/telemetry_json.h +++ b/lib/telemetry/telemetry_json.h @@ -76,15 +76,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) @@ -119,11 +117,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