From patchwork Thu Jun 23 16:42:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bruce Richardson X-Patchwork-Id: 113354 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 25EA4A0093; Thu, 23 Jun 2022 18:43:06 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 50202427F1; Thu, 23 Jun 2022 18:43:01 +0200 (CEST) Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by mails.dpdk.org (Postfix) with ESMTP id 19D4E4067B for ; Thu, 23 Jun 2022 18:42:58 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1656002579; x=1687538579; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=hJ0cmGTh9OsXMyI5rf+157EMkILCdCaBZf9lZ5yBuAM=; b=Ei6rl00SEPrrvASrYJFJH/E2Q6oQuocmyhnbAS+GduSZHfmHFZVNjO/k wIcOwH67Cz2dJPqyb9fBDE+OoE0r4iOFPH+/tkBg5KIumGY5s8w1G2lkx 6qcxNPWdLsFqkS8KOqelwxGD8wh48TJQPVwZruaawQ712Nyr3FZ5ZPSBX mD1WBYqC04VmexGMeqO27yrSTrW4WExK7E6smOQCM/+GRiOkINzZToMmu eznBpDEEfdFx1iVjKLHNi5WPIoxndvKPr2iwz8sIi1DvC6EhwgJ/DLCiP QWwnqChjDE+rVzO921emwUtzxZL0RzL5aVPjtO+XIh+UoXxXXq/OmLHHh Q==; X-IronPort-AV: E=McAfee;i="6400,9594,10386"; a="260589072" X-IronPort-AV: E=Sophos;i="5.92,216,1650956400"; d="scan'208";a="260589072" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jun 2022 09:42:57 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.92,216,1650956400"; d="scan'208";a="915267926" Received: from silpixa00401385.ir.intel.com (HELO silpixa00401385.ger.corp.intel.com.) ([10.237.223.125]) by fmsmga005.fm.intel.com with ESMTP; 23 Jun 2022 09:42:56 -0700 From: Bruce Richardson To: dev@dpdk.org Cc: ciara.power@intel.com, fengchengwen@huawei.com, mb@smartsharesystems.com, Bruce Richardson Subject: [RFC PATCH 1/6] test/telemetry_json: print success or failure per subtest Date: Thu, 23 Jun 2022 17:42:40 +0100 Message-Id: <20220623164245.561371-2-bruce.richardson@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220623164245.561371-1-bruce.richardson@intel.com> References: <20220623164245.561371-1-bruce.richardson@intel.com> MIME-Version: 1.0 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 rather than just printing out success or failure at the end of the test only, print out "OK" or "ERROR" for each individual test case within the overall test. As part of this, ensure each case returns 0 on success and any other value on failure. Signed-off-by: Bruce Richardson --- app/test/test_telemetry_json.c | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/app/test/test_telemetry_json.c b/app/test/test_telemetry_json.c index 790181d316..748b7cfe5a 100644 --- a/app/test/test_telemetry_json.c +++ b/app/test/test_telemetry_json.c @@ -102,8 +102,10 @@ test_large_array_element(void) used = rte_tel_json_add_array_string(buf, sizeof(buf), used, str); printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected); + if (used != 0) + return -1; - return strlen(buf) != 0; + return strncmp(expected, buf, sizeof(buf)); } static int @@ -117,20 +119,33 @@ test_large_obj_element(void) used = rte_tel_json_add_obj_u64(buf, sizeof(buf), used, str, 0); printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected); + if (used != 0) + return -1; - return strlen(buf) != 0; + return strncmp(expected, buf, sizeof(buf)); } +typedef int (*test_fn)(void); + static int test_telemetry_json(void) { - if (test_basic_array() < 0 || - test_basic_obj() < 0 || - test_overflow_array() < 0 || - test_overflow_obj() < 0 || - test_large_array_element() < 0 || - test_large_obj_element() < 0) - return -1; + unsigned int i; + test_fn fns[] = { + test_basic_array, + test_basic_obj, + test_overflow_array, + test_overflow_obj, + test_large_array_element, + test_large_obj_element, + }; + for (i = 0; i < RTE_DIM(fns); i++) + if (fns[i]() == 0) + printf("OK\n"); + else { + printf("ERROR\n"); + return -1; + } return 0; } From patchwork Thu Jun 23 16:42:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bruce Richardson X-Patchwork-Id: 113355 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 1B5A8A0093; Thu, 23 Jun 2022 18:43:12 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 45E784280E; Thu, 23 Jun 2022 18:43:03 +0200 (CEST) Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by mails.dpdk.org (Postfix) with ESMTP id 902FA40A82 for ; Thu, 23 Jun 2022 18:42:59 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1656002580; x=1687538580; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ndzsh55Qhi4bo4uHF+vIIOHfFT4xTmROSJfyCvq6obA=; b=cyCQm5/PS2b6SIuJAWX5fy3+vMS5S0UFyhfioW1wcxvq9kkQjzxWRG/i qXJEuY5z81XszL+njOI7bnpza1eF8F3XwRGVaeQccvUMmlHrDyDWsxJz7 uMaC4RDxjVwvfHGoGSHLZXi4N7OvOLUtasfQLYqv8YU70CdqsHNAkFkdV dsIcWj5mJ7Dl9CGMSUvHLrDVeuImZkIKBwcmfHHDd7iV+yqfYHc3rZEpb m9tHCPWT84dH/3vQh67ZGHv6y6XednPN65Dthi2mFs63D3wxm3i1ass9/ bvm/tf0vp+LquVdCzEtzYaw0XvBCGmWp/cyaRoNM/HflGBWnSpgXosJ46 g==; X-IronPort-AV: E=McAfee;i="6400,9594,10386"; a="260589080" X-IronPort-AV: E=Sophos;i="5.92,216,1650956400"; d="scan'208";a="260589080" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jun 2022 09:42:59 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.92,216,1650956400"; d="scan'208";a="915267929" Received: from silpixa00401385.ir.intel.com (HELO silpixa00401385.ger.corp.intel.com.) ([10.237.223.125]) by fmsmga005.fm.intel.com with ESMTP; 23 Jun 2022 09:42:57 -0700 From: Bruce Richardson To: dev@dpdk.org Cc: ciara.power@intel.com, fengchengwen@huawei.com, mb@smartsharesystems.com, Bruce Richardson Subject: [RFC PATCH 2/6] telemetry: fix escaping of invalid json characters Date: Thu, 23 Jun 2022 17:42:41 +0100 Message-Id: <20220623164245.561371-3-bruce.richardson@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220623164245.561371-1-bruce.richardson@intel.com> References: <20220623164245.561371-1-bruce.richardson@intel.com> MIME-Version: 1.0 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 For string values returned from telemetry, escape any values that cannot normally appear in a json string. According to the json spec[1], the characters than need to be handled are control chars (char value < 0x20) and '"' and '\' characters. To handle this, we replace the snprintf call with a separate string copying and encapsulation routine which checks each character as it copies it to the final array. [1] https://www.rfc-editor.org/rfc/rfc8259.txt Signed-off-by: Bruce Richardson --- lib/telemetry/telemetry_json.h | 48 +++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/lib/telemetry/telemetry_json.h b/lib/telemetry/telemetry_json.h index db70690274..13df5d07e3 100644 --- a/lib/telemetry/telemetry_json.h +++ b/lib/telemetry/telemetry_json.h @@ -44,6 +44,52 @@ __json_snprintf(char *buf, const int len, const char *format, ...) return 0; /* nothing written or modified */ } +static const char control_chars[0x20] = { + ['\n'] = 'n', + ['\r'] = 'r', + ['\t'] = 't', +}; + +/** + * @internal + * Does the same as __json_snprintf(buf, len, "\"%s\"", str) + * except that it does proper escaping as necessary. + * Drops any invalid characters we don't support + */ +static inline int +__json_format_str(char *buf, const int len, const char *str) +{ + char tmp[len]; + int tmpidx = 0; + + tmp[tmpidx++] = '"'; + while (*str != '\0') { + if (*str < (int)RTE_DIM(control_chars)) { + int idx = *str; /* compilers don't like char type as index */ + if (control_chars[idx] != 0) { + tmp[tmpidx++] = '\\'; + tmp[tmpidx++] = control_chars[idx]; + } + } else if (*str == '"' || *str == '\\') { + tmp[tmpidx++] = '\\'; + tmp[tmpidx++] = *str; + } else + tmp[tmpidx++] = *str; + /* we always need space for closing quote and null character. + * Ensuring at least two free characters also means we can always take an + * escaped character like "\n" without overflowing + */ + if (tmpidx > len - 2) + return 0; + str++; + } + tmp[tmpidx++] = '"'; + tmp[tmpidx] = '\0'; + + strcpy(buf, tmp); + return tmpidx; +} + /* Copies an empty array into the provided buffer. */ static inline int rte_tel_json_empty_array(char *buf, const int len, const int used) @@ -62,7 +108,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_snprintf(buf + used, len - used, "\"%s\"", str); + return used + __json_format_str(buf + used, len - used, str); } /* Appends a string into the JSON array in the provided buffer. */ From patchwork Thu Jun 23 16:42:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bruce Richardson X-Patchwork-Id: 113356 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 6356DA0093; Thu, 23 Jun 2022 18:43:19 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 3691E42824; Thu, 23 Jun 2022 18:43:04 +0200 (CEST) Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by mails.dpdk.org (Postfix) with ESMTP id 0270A427ED for ; Thu, 23 Jun 2022 18:43:00 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1656002581; x=1687538581; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=TeOUnYCpYeTAYv70jBTxhM9hwZjqFuPDJh+53UbReAY=; b=TSfZRRh7gqDzG6VkmkCu26uGoJRoL6mMRa6URHRqlfnqFW5+qzAk1xcw YkvTr8hKGYBRBWmJmRittJyUFvLvkny2CKW4rvxeZVboooAUUP+N93TlX cADIYtwZGSy40DF/L1RosYOpZ3rACKJO7ZudeuBvNCcP7l2K8FM5lYyu1 tCGs+9e/SAllMUzePDmLhesqyzOK0fbbFGBlZdWqjoisafTgh33vGuaEc UeencYZPkCnHVDhdBGG0zp5CDhMRa3L74Xn0Jwe60MK6+8SUS+bJ8tz+D x23PVMLrXk0wZSYd4qZ60UpGP5sYBClN6Lf7q9gC26JCWBpLmgvLvfPtZ g==; X-IronPort-AV: E=McAfee;i="6400,9594,10386"; a="260589088" X-IronPort-AV: E=Sophos;i="5.92,216,1650956400"; d="scan'208";a="260589088" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jun 2022 09:43:00 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.92,216,1650956400"; d="scan'208";a="915267934" Received: from silpixa00401385.ir.intel.com (HELO silpixa00401385.ger.corp.intel.com.) ([10.237.223.125]) by fmsmga005.fm.intel.com with ESMTP; 23 Jun 2022 09:42:59 -0700 From: Bruce Richardson To: dev@dpdk.org Cc: ciara.power@intel.com, fengchengwen@huawei.com, mb@smartsharesystems.com, Bruce Richardson Subject: [RFC PATCH 3/6] telemetry: use json string function for string outputs Date: Thu, 23 Jun 2022 17:42:42 +0100 Message-Id: <20220623164245.561371-4-bruce.richardson@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220623164245.561371-1-bruce.richardson@intel.com> References: <20220623164245.561371-1-bruce.richardson@intel.com> MIME-Version: 1.0 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 returning just a string in response to a query, use the proper json string printing function to escape characters rather than just snprintf. Signed-off-by: Bruce Richardson --- lib/telemetry/telemetry.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/telemetry/telemetry.c b/lib/telemetry/telemetry.c index c6fd03a5ab..7188b1905c 100644 --- a/lib/telemetry/telemetry.c +++ b/lib/telemetry/telemetry.c @@ -232,9 +232,14 @@ output_json(const char *cmd, const struct rte_tel_data *d, int s) MAX_CMD_LEN, cmd ? cmd : "none"); break; case RTE_TEL_STRING: - used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":\"%.*s\"}", - MAX_CMD_LEN, cmd, - RTE_TEL_MAX_SINGLE_STRING_LEN, d->data.str); + prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":", + MAX_CMD_LEN, cmd); + cb_data_buf = &out_buf[prefix_used]; + buf_len = sizeof(out_buf) - prefix_used - 1; /* space for '}' */ + + used = rte_tel_json_str(cb_data_buf, buf_len, 0, d->data.str); + used += prefix_used; + used += strlcat(out_buf + used, "}", sizeof(out_buf) - used); break; case RTE_TEL_DICT: prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":", From patchwork Thu Jun 23 16:42:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bruce Richardson X-Patchwork-Id: 113357 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 092D9A0093; Thu, 23 Jun 2022 18:43:27 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 259EC427EE; Thu, 23 Jun 2022 18:43:07 +0200 (CEST) Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by mails.dpdk.org (Postfix) with ESMTP id 0DA774280C for ; Thu, 23 Jun 2022 18:43:02 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1656002583; x=1687538583; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=3R7YDQkLSJb0lMuB2SKKfDQinNEFHdqXX2l2iCPysLk=; b=Oz6hIN7nA9nSoUuqakZbhCSR0lmPtSmtivYUGtUE/19+h8Wetk0y5aLX S3b6/ypqX8nCo45aLcsHt4m+/ilAnU3YbNn3o3iiuYGAadA2ugWyNf7gh 8rfjJwiGoc7XCFiAy0oNhhoFv1f8eWKegkI7DjunZ6EBQqUHZf8K0HBfd C4FkRLfB87NCismdfJb8HMsUE0xmtcZoFfm9EFJhOorUOJKsWb+fyPTws mVMAx8paEqg30oG9vC2K/hHOdq1m0mSucWW0F8MJcX7hd/cy+gsduVnjw WxKLRI/Yvg0b3Ng2Uup95P8Wzb2MSKZHmfgsWzcfb5SxKgMr0doHbPQ8Q w==; X-IronPort-AV: E=McAfee;i="6400,9594,10386"; a="260589095" X-IronPort-AV: E=Sophos;i="5.92,216,1650956400"; d="scan'208";a="260589095" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jun 2022 09:43:02 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.92,216,1650956400"; d="scan'208";a="915267944" Received: from silpixa00401385.ir.intel.com (HELO silpixa00401385.ger.corp.intel.com.) ([10.237.223.125]) by fmsmga005.fm.intel.com with ESMTP; 23 Jun 2022 09:43:00 -0700 From: Bruce Richardson To: dev@dpdk.org Cc: ciara.power@intel.com, fengchengwen@huawei.com, mb@smartsharesystems.com, Bruce Richardson Subject: [RFC PATCH 4/6] test/telemetry_json: add test for string character escaping Date: Thu, 23 Jun 2022 17:42:43 +0100 Message-Id: <20220623164245.561371-5-bruce.richardson@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220623164245.561371-1-bruce.richardson@intel.com> References: <20220623164245.561371-1-bruce.richardson@intel.com> MIME-Version: 1.0 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 Add unit test to validate that when creating a string response in json, that characters such as \n or quotes are properly escaped. Signed-off-by: Bruce Richardson --- app/test/test_telemetry_json.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/app/test/test_telemetry_json.c b/app/test/test_telemetry_json.c index 748b7cfe5a..955c2e5b1b 100644 --- a/app/test/test_telemetry_json.c +++ b/app/test/test_telemetry_json.c @@ -125,6 +125,22 @@ test_large_obj_element(void) return strncmp(expected, buf, sizeof(buf)); } +static int +test_string_char_escaping(void) +{ + static const char str[] = "A string across\ntwo lines and \"with quotes\"!"; + const char *expected = "\"A string across\\ntwo lines and \\\"with quotes\\\"!\""; + char buf[sizeof(str) + 10]; + int used = 0; + + used = rte_tel_json_str(buf, sizeof(buf), used, str); + printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected); + if (used != (int)strlen(expected)) + return -1; + + return strncmp(expected, buf, sizeof(buf)); +} + typedef int (*test_fn)(void); static int @@ -138,6 +154,7 @@ test_telemetry_json(void) test_overflow_obj, test_large_array_element, test_large_obj_element, + test_string_char_escaping, }; for (i = 0; i < RTE_DIM(fns); i++) if (fns[i]() == 0) From patchwork Thu Jun 23 16:42:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bruce Richardson X-Patchwork-Id: 113358 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 47883A0093; Thu, 23 Jun 2022 18:43:34 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 25B1342905; Thu, 23 Jun 2022 18:43:08 +0200 (CEST) Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by mails.dpdk.org (Postfix) with ESMTP id 1EF464281C for ; Thu, 23 Jun 2022 18:43:03 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1656002584; x=1687538584; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=p8/jQNoGnup04KD0QRg89eDZA6GFDXqWTb4/qfascmA=; b=Uwgxpx9Fw7yydFm/Wta31VFMj18h/RwoioK0N6lMgCeyZ8BDgwfWBVE7 hgSqRuMqlEw3hwaJ2cSKbSAc2UAzYS4CWqaxIwe74YVum5gDqmJIPn9cD 7jh6RxZ845U6dYLnpSFjlkpPsEl24yygQx74aW7X8E70HTk5bOauJYwub HkgHaNYI8LqbfseKr+Rq6nbjyCZ2pKkbvj/TFl7shdcjJN3asRDvlaxGE wUABdDJ7Fo0+ZL5u2jF4t2q6umRMFkggnQ/Ev2hfmfq5SEeHavkEifJu9 4g3y+nmGe7auh4LE9rRpSmI6c08ptlyhxKIu5Im+tjDMeadGRcuriK3ys A==; X-IronPort-AV: E=McAfee;i="6400,9594,10386"; a="260589102" X-IronPort-AV: E=Sophos;i="5.92,216,1650956400"; d="scan'208";a="260589102" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jun 2022 09:43:03 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.92,216,1650956400"; d="scan'208";a="915267953" Received: from silpixa00401385.ir.intel.com (HELO silpixa00401385.ger.corp.intel.com.) ([10.237.223.125]) by fmsmga005.fm.intel.com with ESMTP; 23 Jun 2022 09:43:02 -0700 From: Bruce Richardson To: dev@dpdk.org Cc: ciara.power@intel.com, fengchengwen@huawei.com, mb@smartsharesystems.com, Bruce Richardson Subject: [RFC PATCH 5/6] telemetry: add escaping of strings in arrays Date: Thu, 23 Jun 2022 17:42:44 +0100 Message-Id: <20220623164245.561371-6-bruce.richardson@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220623164245.561371-1-bruce.richardson@intel.com> References: <20220623164245.561371-1-bruce.richardson@intel.com> MIME-Version: 1.0 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 --- 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; } From patchwork Thu Jun 23 16:42:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bruce Richardson X-Patchwork-Id: 113359 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id B28B4A0093; Thu, 23 Jun 2022 18:43:41 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 1EFE342B70; Thu, 23 Jun 2022 18:43:09 +0200 (CEST) Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by mails.dpdk.org (Postfix) with ESMTP id 751484282D for ; Thu, 23 Jun 2022 18:43:05 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1656002585; x=1687538585; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=+qyS8o8pNJCdFShqPIAOC4g6Y3w7aNO9gF8yuDVnr24=; b=Axl79FWBbX0NPH1/3hT/cOgevOpf4AmMUVu+4gCvZfg4GOGiU+FZPz43 qhEThHj0NqilTEfSZOhD69SpFyGk8dWJHN/m15rLgXcTCd5cFSTQV9X94 3QR8A0bFwf4KaUr+7DBSf6hIeaz0Z+BP8jADfrgvn3BAxW015YfljHm02 u0u+zas8DcacRhIj1trWkEIeAB3LpjR2TUBbDG9Wh0SNeWpGi+NiNfNmL BfvDZaY+r7hwQpQEv4IcN0evhFueEuY+jqq0zL+8mNJv4EAr3UJ9jWGat FIL4GZeJh5wOXmrZg1vu7cErMmFYCzyvAyItSy/viqac2ka26l+q56pvV g==; X-IronPort-AV: E=McAfee;i="6400,9594,10386"; a="260589107" X-IronPort-AV: E=Sophos;i="5.92,216,1650956400"; d="scan'208";a="260589107" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jun 2022 09:43:05 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.92,216,1650956400"; d="scan'208";a="915267958" Received: from silpixa00401385.ir.intel.com (HELO silpixa00401385.ger.corp.intel.com.) ([10.237.223.125]) by fmsmga005.fm.intel.com with ESMTP; 23 Jun 2022 09:43:03 -0700 From: Bruce Richardson To: dev@dpdk.org Cc: ciara.power@intel.com, fengchengwen@huawei.com, mb@smartsharesystems.com, Bruce Richardson Subject: [RFC PATCH 6/6] test/telemetry-json: add test case for escaping strings in arrays Date: Thu, 23 Jun 2022 17:42:45 +0100 Message-Id: <20220623164245.561371-7-bruce.richardson@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220623164245.561371-1-bruce.richardson@intel.com> References: <20220623164245.561371-1-bruce.richardson@intel.com> MIME-Version: 1.0 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 Add test-case to validate that when adding strings to arrays, the strings are properly escaped to remove any invalid characters. Signed-off-by: Bruce Richardson --- app/test/test_telemetry_json.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/app/test/test_telemetry_json.c b/app/test/test_telemetry_json.c index 955c2e5b1b..642ae9d6e1 100644 --- a/app/test/test_telemetry_json.c +++ b/app/test/test_telemetry_json.c @@ -141,6 +141,29 @@ test_string_char_escaping(void) return strncmp(expected, buf, sizeof(buf)); } +static int +test_array_char_escaping(void) +{ + /* "meaning of life", with tab between first two words, \n at end, and "life" in quotes, + * followed by "all the fish" in quotes */ + const char *expected = "[\"meaning\\tof \\\"life\\\"\\n\",\"\\\"all the fish\\\"\"]"; + char buf[1024]; + int used = 0; + + printf("%s: ", __func__); + used = rte_tel_json_empty_array(buf, sizeof(buf), used); + if (used != 2 || strcmp(buf, "[]")) + return -1; + + used = rte_tel_json_add_array_string(buf, sizeof(buf), used, "meaning\tof \"life\"\n"); + used = rte_tel_json_add_array_string(buf, sizeof(buf), used, "\"all the fish\""); + + printf("buf = '%s', expected = '%s'\n", buf, expected); + if (used != (int)strlen(expected)) + return -1; + return strncmp(expected, buf, sizeof(buf)); +} + typedef int (*test_fn)(void); static int @@ -155,6 +178,7 @@ test_telemetry_json(void) test_large_array_element, test_large_obj_element, test_string_char_escaping, + test_array_char_escaping, }; for (i = 0; i < RTE_DIM(fns); i++) if (fns[i]() == 0)