From: Jeremy Harris Date: Sat, 9 Feb 2019 16:56:59 +0000 (+0000) Subject: Fix json extract operator for unfound case X-Git-Url: https://git.exim.org/users/heiko/exim.git/commitdiff_plain/b2734f7b45111f9b7de790c7b334a2ece47675b5 Fix json extract operator for unfound case (cherry picked from commit e73798976812e652320f096870359ef35ed069ff) --- diff --git a/doc/doc-docbook/spec.xfpt b/doc/doc-docbook/spec.xfpt index 4d4bab7c2..20b08f693 100644 --- a/doc/doc-docbook/spec.xfpt +++ b/doc/doc-docbook/spec.xfpt @@ -9537,6 +9537,8 @@ The expanded <&'string1'&> must be of the form: The braces, commas and colons, and the quoting of the member name are required; the spaces are optional. Matching of the key against the member names is done case-sensitively. +If a returned value is a JSON string, it retains its leading and +trailing quotes. . XXX should be a UTF-8 compare The results of matching are handled as above. @@ -9584,6 +9586,8 @@ apart from leading and trailing white space, which is ignored. Field selection and result handling is as above; there is no choice of field separator. +If a returned value is a JSON string, it retains its leading and +trailing quotes. .wen diff --git a/src/src/expand.c b/src/src/expand.c index b2a921701..2c290251d 100644 --- a/src/src/expand.c +++ b/src/src/expand.c @@ -3901,7 +3901,8 @@ return NULL; /* Pull off the leading array or object element, returning a copy in an allocated string. Update the list pointer. -The element may itself be an abject or array. +The element may itself be an object or array. +Return NULL when the list is empty. */ uschar * @@ -3923,6 +3924,7 @@ for (item = s; case '}': object_depth--; break; } *list = *s ? s+1 : s; +if (item == s) return NULL; item = string_copyn(item, s - item); DEBUG(D_expand) debug_printf_indent(" json ele: '%s'\n", item); return US item; @@ -5790,10 +5792,11 @@ while (*s != 0) } while (field_number > 0 && (item = json_nextinlist(&list))) field_number--; - s = item; - lookup_value = s; - while (*s) s++; - while (--s >= lookup_value && isspace(*s)) *s = '\0'; + if ((lookup_value = s = item)) + { + while (*s) s++; + while (--s >= lookup_value && isspace(*s)) *s = '\0'; + } } else { diff --git a/test/scripts/0000-Basic/0002 b/test/scripts/0000-Basic/0002 index e192a5593..7a9b38dba 100644 --- a/test/scripts/0000-Basic/0002 +++ b/test/scripts/0000-Basic/0002 @@ -902,6 +902,9 @@ expect: {"1":116, "2":943, "3":234} ${extract json{IDs}{ \{"id": \{"a":101, "b":102\}, "IDs": \{"1":116, "2":943, "3":234\}\} }} expect: {"1":116, "2":943, "3":234} +<${extract json{nonexistent}{ \{"id": \{"a":101, "b":102\}, "IDs": \{"1":116, "2":943, "3":234\}\} }}> +expect: <> + **** # Test "escape" with print_topbitchars exim -be -DPTBC=print_topbitchars diff --git a/test/stdout/0002 b/test/stdout/0002 index 949c35eb2..c2f5f2f3c 100644 --- a/test/stdout/0002 +++ b/test/stdout/0002 @@ -845,6 +845,9 @@ xyz > {"1":116, "2":943, "3":234} > expect: {"1":116, "2":943, "3":234} > +> <> +> expect: <> +> > > escape: B7·F2ò >