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 B839FA0540; Fri, 9 Sep 2022 11:36:22 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 16F0F42B79; Fri, 9 Sep 2022 11:35:53 +0200 (CEST) Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by mails.dpdk.org (Postfix) with ESMTP id 5924742829 for ; Fri, 9 Sep 2022 11:35:50 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662716150; x=1694252150; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=j54IeXZePIv63X4WGZqjquKQa6jTxa9vk57lowo70H0=; b=eaG2QyJhqCk8aAk/Pjif+8dv6PNPzO6PfvVjKCOvadCW5qRqbdJl74Yh /24YQiEHyTK2UzOIqoz91ZMhcVmJew7w98mNMfFmTg6vJQ3s8ErC6CmZI G4IDjnfRB6WC+JNniPPIcTNX8sZ4mNrKpVKwHDHQkaHA0TjF88wO3f+L5 spg/+JBpcIZfz6Ocfi1XPSAQJU9rYZc58SwxxRBSrYD6s5A6lk3Kcw+vM NHjZWj5OSH7IAfr8ABl68tOcW6qpU/MWcFxOgEFA2FCk1FdReRMr49gc5 8x3Qry2WEmTWFn0QjvHpi9TF/7n+AQZy8UeH+3xBSlvwskmzhr9BkDGGJ g==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="297437131" X-IronPort-AV: E=Sophos;i="5.93,302,1654585200"; d="scan'208";a="297437131" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Sep 2022 02:35:48 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,302,1654585200"; d="scan'208";a="740996388" Received: from silpixa00401385.ir.intel.com ([10.237.214.161]) by orsmga004.jf.intel.com with ESMTP; 09 Sep 2022 02:35:46 -0700 From: Bruce Richardson To: dev@dpdk.org Cc: Bruce Richardson , Ciara Power , =?UTF-8?q?Morten=20Br=C3=B8rup?= Subject: [PATCH v3 07/13] telemetry: add escaping of strings in dicts Date: Fri, 9 Sep 2022 10:35:17 +0100 Message-Id: <20220909093523.471727-8-bruce.richardson@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220909093523.471727-1-bruce.richardson@intel.com> References: <20220623164245.561371-1-bruce.richardson@intel.com> <20220909093523.471727-1-bruce.richardson@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 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 When strings are added to an dict variable, we need to properly escape the invalid json characters in the strings. Signed-off-by: Bruce Richardson Acked-by: Ciara Power Acked-by: Morten Brørup --- lib/telemetry/telemetry_json.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/telemetry/telemetry_json.h b/lib/telemetry/telemetry_json.h index c4442a0bf0..e3fae7c30d 100644 --- a/lib/telemetry/telemetry_json.h +++ b/lib/telemetry/telemetry_json.h @@ -54,7 +54,7 @@ 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 not needing escaping. + * time constants, or values not needing escaping. * Drops any invalid characters we don't support */ static inline int @@ -219,12 +219,16 @@ static inline int rte_tel_json_add_obj_str(char *buf, const int len, const int used, const char *name, const char *val) { + char tmp_name[RTE_TEL_MAX_STRING_LEN + 5]; int ret, end = used - 1; + + /* names are limited to certain characters so need no escaping */ + snprintf(tmp_name, sizeof(tmp_name), "{\"%s\":\"", name); if (used <= 2) /* assume empty, since minimum is '{}' */ - return __json_snprintf(buf, len, "{\"%s\":\"%s\"}", name, val); + return __json_format_str(buf, len, tmp_name, val, "\"}"); - ret = __json_snprintf(buf + end, len - end, ",\"%s\":\"%s\"}", - name, val); + tmp_name[0] = ','; /* replace '{' with ',' at start */ + ret = __json_format_str(buf + end, len - end, tmp_name, val, "\"}"); return ret == 0 ? used : end + ret; } -- 2.34.1