[v3,3/5] telemetry: split out body of json string format fn
Checks
Commit Message
To enable further rework to (efficiently) avoid using variable-length
arrays, we first separate out the body of the __json_format_str
function. This means that the actual VLA buffer is in the wrapper
function, and means we can reuse the actual writing code in multiple
code paths without duplication.
Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
lib/telemetry/telemetry_json.h | 32 ++++++++++++++++++++++++--------
1 file changed, 24 insertions(+), 8 deletions(-)
Comments
On Wed, Apr 05, 2023 at 05:03:24PM +0100, Bruce Richardson wrote:
> To enable further rework to (efficiently) avoid using variable-length
> arrays, we first separate out the body of the __json_format_str
> function. This means that the actual VLA buffer is in the wrapper
> function, and means we can reuse the actual writing code in multiple
> code paths without duplication.
>
> Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
> ---
Acked-by: Tyler Retzlaff <roretzla@linux.microsoft.com>
@@ -80,15 +80,13 @@ 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, or values not needing escaping.
- * Drops any invalid characters we don't support
+ * Function that does the actual printing, used by __json_format_str. Modifies buffer
+ * directly, but returns 0 on overflow. Otherwise returns number of chars written to buffer.
*/
static inline int
-__json_format_str(char *buf, const int len, const char *prefix, const char *str, const char *suffix)
+__json_format_str_to_buf(char *tmp, const int len,
+ const char *prefix, const char *str, const char *suffix)
{
- char tmp[len];
int tmpidx = 0;
while (*prefix != '\0' && tmpidx < len)
@@ -123,11 +121,29 @@ __json_format_str(char *buf, const int len, const char *prefix, const char *str,
return 0;
tmp[tmpidx] = '\0';
-
- strcpy(buf, tmp);
return tmpidx;
}
+/**
+ * @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, or values not needing escaping.
+ * Drops any invalid characters we don't support
+ */
+static inline int
+__json_format_str(char *buf, const int len, const char *prefix, const char *str, const char *suffix)
+{
+ char tmp[len];
+ int ret;
+
+ ret = __json_format_str_to_buf(tmp, len, prefix, str, suffix);
+ if (ret > 0)
+ strcpy(buf, tmp);
+
+ return ret;
+}
+
/* Copies an empty array into the provided buffer. */
static inline int
rte_tel_json_empty_array(char *buf, const int len, const int used)