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 1065FA0540; Fri, 9 Sep 2022 11:36:05 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id AEB1E42905; Fri, 9 Sep 2022 11:35:50 +0200 (CEST) Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by mails.dpdk.org (Postfix) with ESMTP id 4F0B141155 for ; Fri, 9 Sep 2022 11:35:47 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662716147; x=1694252147; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Wc4msroVjF7qakmFzGILlAml5a7kYcygtN+xKqYE3pE=; b=ibKfyboAkmbzR/JMv8UKQgQxAlG40NzAJdRKKUagrKhiYiBK3ejYt6N7 QhvYEASX7+tBBreTKtCP0qt0UzEEiUax3QYYLuPmL/yuc7Pc5a7nMYxfr 9dBYUeKUdyQonEI/fPLU/55XxL0k9bqmVafA6izd6LVCmpZXMUvgikRaX fmFXXol2SWeNN/Z7hiRiYwo41DF8haJQCm9vtpLfwyWk4qccEZuLCyx7B XiuE30uadrQwDtOPXbG1WEGwKCY12AodAd1/RHb1ZnHkP1i9l1Bv4nD/o famCKa6tMHVLR+c4OuwW1t+sBaa06CYgA0lhiJ0SpRVpVUwUYE5lTeQ73 A==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="297437126" X-IronPort-AV: E=Sophos;i="5.93,302,1654585200"; d="scan'208";a="297437126" 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:46 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.93,302,1654585200"; d="scan'208";a="740996375" Received: from silpixa00401385.ir.intel.com ([10.237.214.161]) by orsmga004.jf.intel.com with ESMTP; 09 Sep 2022 02:35:44 -0700 From: Bruce Richardson To: dev@dpdk.org Cc: Bruce Richardson , Ciara Power , =?UTF-8?q?Morten=20Br=C3=B8rup?= Subject: [PATCH v3 05/13] telemetry: add escaping of strings in arrays Date: Fri, 9 Sep 2022 10:35:15 +0100 Message-Id: <20220909093523.471727-6-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 array 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 | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/telemetry/telemetry_json.h b/lib/telemetry/telemetry_json.h index 13df5d07e3..c4442a0bf0 100644 --- a/lib/telemetry/telemetry_json.h +++ b/lib/telemetry/telemetry_json.h @@ -52,17 +52,22 @@ static const char control_chars[0x20] = { /** * @internal - * Does the same as __json_snprintf(buf, len, "\"%s\"", str) - * except that it does proper escaping as necessary. + * 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. * Drops any invalid characters we don't support */ static inline int -__json_format_str(char *buf, const int len, const char *str) +__json_format_str(char *buf, const int len, const char *prefix, const char *str, const char *suffix) { char tmp[len]; int tmpidx = 0; - tmp[tmpidx++] = '"'; + while (*prefix != '\0' && tmpidx < len) + tmp[tmpidx++] = *prefix++; + if (tmpidx >= len) + return 0; + while (*str != '\0') { if (*str < (int)RTE_DIM(control_chars)) { int idx = *str; /* compilers don't like char type as index */ @@ -75,7 +80,7 @@ __json_format_str(char *buf, const int len, const char *str) tmp[tmpidx++] = *str; } else tmp[tmpidx++] = *str; - /* we always need space for closing quote and null character. + /* we always need space for (at minimum) closing quote and null character. * Ensuring at least two free characters also means we can always take an * escaped character like "\n" without overflowing */ @@ -83,7 +88,12 @@ __json_format_str(char *buf, const int len, const char *str) return 0; str++; } - tmp[tmpidx++] = '"'; + + while (*suffix != '\0' && tmpidx < len) + tmp[tmpidx++] = *suffix++; + if (tmpidx >= len) + return 0; + tmp[tmpidx] = '\0'; strcpy(buf, tmp); @@ -108,7 +118,7 @@ rte_tel_json_empty_obj(char *buf, const int len, const int used) static inline int rte_tel_json_str(char *buf, const int len, const int used, const char *str) { - return used + __json_format_str(buf + used, len - used, str); + return used + __json_format_str(buf + used, len - used, "\"", str, "\""); } /* Appends a string into the JSON array in the provided buffer. */ @@ -118,9 +128,9 @@ rte_tel_json_add_array_string(char *buf, const int len, const int used, { int ret, end = used - 1; /* strip off final delimiter */ if (used <= 2) /* assume empty, since minimum is '[]' */ - return __json_snprintf(buf, len, "[\"%s\"]", str); + return __json_format_str(buf, len, "[\"", str, "\"]"); - ret = __json_snprintf(buf + end, len - end, ",\"%s\"]", str); + ret = __json_format_str(buf + end, len - end, ",\"", str, "\"]"); return ret == 0 ? used : end + ret; } -- 2.34.1