1 Star 0 Fork 68

李辉松 / dpdk

forked from src-openEuler / dpdk 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
0184-telemetry-fix-escaping-of-invalid-json-characters.patch 4.10 KB
一键复制 编辑 原始数据 按行查看 历史
From bdedf8a96e0782e98291e219ed7b74cb7b04fc9c Mon Sep 17 00:00:00 2001
From: Bruce Richardson <bruce.richardson@intel.com>
Date: Fri, 21 Oct 2022 15:37:00 +0800
Subject: [PATCH 184/189] telemetry: fix escaping of invalid json characters
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
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
Bugzilla ID: 1037
Fixes: 6dd571fd07c3 ("telemetry: introduce new functionality")
Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
Acked-by: Ciara Power <ciara.power@intel.com>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
Acked-by: Chengwen Feng <fengchengwen@huawei.com>
---
lib/telemetry/telemetry.c | 11 +++++---
lib/telemetry/telemetry_json.h | 48 +++++++++++++++++++++++++++++++++-
2 files changed, 55 insertions(+), 4 deletions(-)
diff --git a/lib/telemetry/telemetry.c b/lib/telemetry/telemetry.c
index e5ccfe47f7..d4a7838ded 100644
--- a/lib/telemetry/telemetry.c
+++ b/lib/telemetry/telemetry.c
@@ -233,9 +233,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\":",
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. */
--
2.23.0
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/li-huisong/dpdk.git
git@gitee.com:li-huisong/dpdk.git
li-huisong
dpdk
dpdk
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891