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 1A957A00C4; Mon, 25 Jul 2022 18:36:40 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id C7D9B4282E; Mon, 25 Jul 2022 18:36:23 +0200 (CEST) Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by mails.dpdk.org (Postfix) with ESMTP id 6BB9542B81 for ; Mon, 25 Jul 2022 18:36:22 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1658766982; x=1690302982; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=fdnJUlYPRuTz9Il2dzBYqFZuqYYM4NfKV7ENacocaaY=; b=JrN9TSE3IeNu5hDEy3kDybRWw0UhcdrouJE5uWkuAEXEpsomXKLttZR9 OL8SeQCkjIsYR2YfBTlA8CMVmEnfX2l+skdh76Z6vPZ2dxNhIPLam1uhM r4O/71NxppnDclhXvwTvLNzVs0UJukbueSG4imECD8w//nPTmQaMXAY+m 45OByHcuayBWQfvJNxX6rsRkvtaFNe+mqDK71Pc35W7i9flGrcigTXGkf jAk/zFEth7+dtjENw2csnIj5p2eLPDdhLLqyWLG7BNMRyNGkW02j6Y8q9 Gjm+jxn4Tk97kpLXsYihGp0ftgn/QcywCJIv/HBzxwJCGQ1Z/KPy8EzbT A==; X-IronPort-AV: E=McAfee;i="6400,9594,10419"; a="288499111" X-IronPort-AV: E=Sophos;i="5.93,193,1654585200"; d="scan'208";a="288499111" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jul 2022 09:36:22 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,193,1654585200"; d="scan'208";a="575122148" Received: from silpixa00401385.ir.intel.com (HELO silpixa00401385.ger.corp.intel.com.) ([10.237.223.47]) by orsmga006.jf.intel.com with ESMTP; 25 Jul 2022 09:36:20 -0700 From: Bruce Richardson To: dev@dpdk.org Cc: Bruce Richardson , Ciara Power Subject: [PATCH v2 07/13] telemetry: add escaping of strings in dicts Date: Mon, 25 Jul 2022 17:35:36 +0100 Message-Id: <20220725163543.875775-8-bruce.richardson@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220725163543.875775-1-bruce.richardson@intel.com> References: <20220623164245.561371-1-bruce.richardson@intel.com> <20220725163543.875775-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 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 --- 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