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 3A694A00C3; Fri, 17 Jun 2022 19:05:02 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id C3AF5410FC; Fri, 17 Jun 2022 19:05:01 +0200 (CEST) Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by mails.dpdk.org (Postfix) with ESMTP id 22EED40F19 for ; Fri, 17 Jun 2022 19:04: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=1655485500; x=1687021500; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=KEL49u1xRa5Q9uSpbSFtK9V2NSck9ujJR0mL9KR1XkU=; b=bA5XmdbXEf3qvS0NpyO+t5g1lbjlyhojQWWpw/LmZLr11MhfLt6BafGU gYGabLWdAkweN5u4D2XaNEp54eqaKh5RdgkWHzhceRsZgy1Qm26it9mPe cxlOs2KiirfCR/dB78TbSXOYU68GG3NnHdvbrJIb+4CTBk72RWEF5h7rO eT+qb35lfqkxKr+jvO5KJ4QjqyRYi7gvVfZYE/lXN5OGumxgjoFVRpSkC 2+1f7J8E5jEPZBu04u4ToWfLEgS8V0oP2OOKxEnqhi7VXcnkGgRiTYSVi wY+CdDwilYPiWKIqbjnF0gIxcmpZnnvY9pnsCI6VCW0N1EjKCJyB/gSIT w==; X-IronPort-AV: E=McAfee;i="6400,9594,10380"; a="259936557" X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="259936557" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jun 2022 04:27:29 -0700 X-IronPort-AV: E=Sophos;i="5.92,306,1650956400"; d="scan'208";a="590086382" Received: from bricha3-mobl.ger.corp.intel.com ([10.252.24.67]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-SHA; 17 Jun 2022 04:27:26 -0700 Date: Fri, 17 Jun 2022 12:27:23 +0100 From: Bruce Richardson To: Chengwen Feng Cc: thomas@monjalon.net, ferruh.yigit@xilinx.com, kevin.laatz@intel.com, andrew.rybchenko@oktetlabs.ru, jerinj@marvell.com, sachin.saxena@oss.nxp.com, hemant.agrawal@nxp.com, mb@smartsharesystems.com, dev@dpdk.org Subject: Re: [PATCH v2 1/5] telemetry: escape special char when tel string Message-ID: References: <20220615073915.14041-1-fengchengwen@huawei.com> <20220617094624.17578-1-fengchengwen@huawei.com> <20220617094624.17578-2-fengchengwen@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220617094624.17578-2-fengchengwen@huawei.com> 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 On Fri, Jun 17, 2022 at 05:46:20PM +0800, Chengwen Feng wrote: > This patch supports escape special characters (including: \",\\,/,\b, > /f,/n,/r,/t) when telemetry string. > This patch is used to support telemetry xxx-dump commands which the > string may include special characters. > > Signed-off-by: Chengwen Feng > --- > lib/telemetry/telemetry.c | 96 +++++++++++++++++++++++++++++++++++++-- > 1 file changed, 93 insertions(+), 3 deletions(-) > > diff --git a/lib/telemetry/telemetry.c b/lib/telemetry/telemetry.c > index c6fd03a5ab..0f762f633e 100644 > --- a/lib/telemetry/telemetry.c > +++ b/lib/telemetry/telemetry.c > @@ -215,6 +215,94 @@ container_to_json(const struct rte_tel_data *d, char *out_buf, size_t buf_len) > return used; > } > > +static bool > +json_is_special_char(char ch) > +{ > + static unsigned char is_spec[256] = { 0 }; > + static bool init_once; > + > + if (!init_once) { > + is_spec['\"'] = 1; > + is_spec['\\'] = 1; > + is_spec['/'] = 1; > + is_spec['\b'] = 1; > + is_spec['\f'] = 1; > + is_spec['\n'] = 1; > + is_spec['\r'] = 1; > + is_spec['\t'] = 1; > + init_once = true; > + } > + > + return (bool)is_spec[(unsigned char)ch]; > +} > + > +static size_t > +json_escape_special_char(char *buf, const char ch) > +{ > + size_t used = 0; > + > + switch (ch) { > + case '\"': > + buf[used++] = '\\'; > + buf[used++] = '\"'; > + break; > + case '\\': > + buf[used++] = '\\'; > + buf[used++] = '\\'; > + break; > + case '/': > + buf[used++] = '\\'; > + buf[used++] = '/'; > + break; > + case '\b': > + buf[used++] = '\\'; > + buf[used++] = 'b'; > + break; > + case '\f': > + buf[used++] = '\\'; > + buf[used++] = 'f'; > + break; > + case '\n': > + buf[used++] = '\\'; > + buf[used++] = 'n'; > + break; > + case '\r': > + buf[used++] = '\\'; > + buf[used++] = 'r'; > + break; > + case '\t': > + buf[used++] = '\\'; > + buf[used++] = 't'; > + break; > + default: > + break; > + } > + > + return used; > +} > + > +static size_t > +json_format_string(char *buf, size_t len, const char *str) > +{ > + size_t used = 0; > + > + while (*str) { > + if (unlikely(len < used + 2)) { > + TMTY_LOG(WARNING, "Insufficient buffer when json format string\n"); > + break; > + } > + > + if (json_is_special_char(*str)) > + used += json_escape_special_char(buf + used, *str); > + else > + buf[used++] = *str; > + > + str++; > + } > + > + return used; > +} > + > static void > output_json(const char *cmd, const struct rte_tel_data *d, int s) > { > @@ -232,9 +320,11 @@ 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); > + used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":\"", > + MAX_CMD_LEN, cmd); > + used += json_format_string(out_buf + used, > + sizeof(out_buf) - used - 3, d->data.str); > + used += snprintf(out_buf + used, sizeof(out_buf) - used, "\"}"); > break; > case RTE_TEL_DICT: > prefix_used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":", > -- I think it might be worthwhile to write a general json_str_printf function to do the snprintf and the escaping in one go. Then that might be more able to be used in other places where we output strings. /Bruce