From 467917e2da4d211230eb2cda2978e8eb9fe29c8c Mon Sep 17 00:00:00 2001 From: "Yann E. MORIN" Date: Sat, 13 Nov 2021 14:28:22 +0100 Subject: [PATCH] package/pkg-utils: introduce helper to properly json-escape a string In quite a few places, we need to generate string that are proper JSON values or keys. However, JSON is very strict on what constitute a string, and most JSON parsers (like jq or python3's json module) are very picky when parsing a string; any deviation from the spec is immediately sanctioned by a hard error (jq aborts, python3's json module raise an exception). Introduce a macro that properly prepares a Makefile value into a valid JSON string: - backslash '\' must be escaped; - double-quotes need to be escaped of course, as they are the string delimiter in JSON; - anything in the range [0x00..0x1F] must be escaped; in practice, we only ever need to escape \n, \t, and ESC (we could add more in the future if need be); - finally, we also escape the space, \x20, so that we can call $(strip) on a JSON blurb (like we do for example do build a comma-separated list, or when we sanitise the JSON) without losing multiple spaces where they make sense. It would have been nice if we had been able to split the macro on multiple lines, but spaces creep in from everywhere in that case, and getting rid of them is getting quite nasty... We could introduce intermediate macros, but meh... Signed-off-by: Yann E. MORIN Signed-off-by: Thomas Petazzoni --- package/pkg-utils.mk | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/package/pkg-utils.mk b/package/pkg-utils.mk index 94e806963b..15e3e14e1b 100644 --- a/package/pkg-utils.mk +++ b/package/pkg-utils.mk @@ -176,6 +176,17 @@ clean-json = $(strip \ ))))) \ ) +# mk-json-str -- escape and double-quote a string to make it a valid json string +# - escape \ +# - escape " +# - escape \n +# - escape \t +# - escape ESC +# - escape SPACE (so that we can $(strip) a JSON blurb without squashing multiple spaces) +# This unfortunately has to be on a single line... +mk-json-str = "$(subst $(space),\u0020,$(subst $(escape),\u001b,$(subst $(tab),\t,$(subst $(sep),\n,$(subst ",\",$(subst \,\\,$(1)))))))" +# )))))" # Syntax colouring + ifeq ($(BR2_PER_PACKAGE_DIRECTORIES),y) # rsync the contents of per-package directories # $1: space-separated list of packages to rsync from