From 18503c0f7cc540d8c3bb72c8aace01cbb1bb18c8 Mon Sep 17 00:00:00 2001 From: Victor Seva Date: Wed, 13 May 2020 14:39:46 +0200 Subject: [PATCH] TT#81182 jansson fix > janssonmod_set(): result has json error at line 1: end of file expected > > https://github.com/kamailio/kamailio/issues/2327 Change-Id: Ieaa3511b3abf447740357d4dd690b363043a73a5 --- debian/patches/series | 3 + ...rong-undo-ed-version-of-STR_ZTOV-mac.patch | 22 ++++ ...macro-to-set-ending-zero-with-backup.patch | 124 ++++++++++++++++++ ...core-macros-for-ending-string-value-.patch | 118 +++++++++++++++++ 4 files changed, 267 insertions(+) create mode 100644 debian/patches/upstream/core-str-fixed-wrong-undo-ed-version-of-STR_ZTOV-mac.patch create mode 100644 debian/patches/upstream/core-str-helper-macro-to-set-ending-zero-with-backup.patch create mode 100644 debian/patches/upstream/jansson-use-the-core-macros-for-ending-string-value-.patch diff --git a/debian/patches/series b/debian/patches/series index 1197aa26f..404be63e4 100644 --- a/debian/patches/series +++ b/debian/patches/series @@ -63,6 +63,9 @@ upstream/pv-add-KEMI-functions-pvx.xavp_get_keys-and-pvx.xavp.patch upstream/pv-fixes-for-KEMI-pvx.xavp_getd.patch upstream/cfgt-don-t-process-non-sip-messages.patch upstream/cfgt-fix-implicit-declaration-of-strcasestr.patch +upstream/core-str-helper-macro-to-set-ending-zero-with-backup.patch +upstream/core-str-fixed-wrong-undo-ed-version-of-STR_ZTOV-mac.patch +upstream/jansson-use-the-core-macros-for-ending-string-value-.patch ## backport from kamailio trunk (5.3) # ### relevant for upstream diff --git a/debian/patches/upstream/core-str-fixed-wrong-undo-ed-version-of-STR_ZTOV-mac.patch b/debian/patches/upstream/core-str-fixed-wrong-undo-ed-version-of-STR_ZTOV-mac.patch new file mode 100644 index 000000000..c78158f1b --- /dev/null +++ b/debian/patches/upstream/core-str-fixed-wrong-undo-ed-version-of-STR_ZTOV-mac.patch @@ -0,0 +1,22 @@ +From: Daniel-Constantin Mierla +Date: Mon, 27 Apr 2020 15:24:23 +0200 +Subject: [PATCH] core: str - fixed wrong (undo'ed) version of STR_ZTOV() + macro + +--- + src/core/str.h | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/src/core/str.h b/src/core/str.h +index 83334b8..01b12ec 100644 +--- a/src/core/str.h ++++ b/src/core/str.h +@@ -137,7 +137,7 @@ typedef struct _str str; + if (v != '\0') { \ + c = v; \ + } \ +- } ++ } while(0) + + /** @} */ + diff --git a/debian/patches/upstream/core-str-helper-macro-to-set-ending-zero-with-backup.patch b/debian/patches/upstream/core-str-helper-macro-to-set-ending-zero-with-backup.patch new file mode 100644 index 000000000..859cf7736 --- /dev/null +++ b/debian/patches/upstream/core-str-helper-macro-to-set-ending-zero-with-backup.patch @@ -0,0 +1,124 @@ +From: Daniel-Constantin Mierla +Date: Mon, 27 Apr 2020 14:53:33 +0200 +Subject: [PATCH] core: str - helper macro to set ending zero with backup and + restore from backup + +--- + src/core/str.h | 57 ++++++++++++++++++++++++++++++++++++++++----------------- + 1 file changed, 40 insertions(+), 17 deletions(-) + +diff --git a/src/core/str.h b/src/core/str.h +index de226c7..83334b8 100644 +--- a/src/core/str.h ++++ b/src/core/str.h +@@ -13,8 +13,8 @@ + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * +- * You should have received a copy of the GNU General Public License +- * along with this program; if not, write to the Free Software ++ * You should have received a copy of the GNU General Public License ++ * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +@@ -23,23 +23,23 @@ + + #include + +-/** @defgroup str_string Counted-Length Strings ++/** @defgroup str_string Counted-Length Strings + * @{ +- * ++ * + * Implementation of counted-length strings. In SER and its modules, strings + * are often stored in the ::str structure. In addition to the pointer + * pointing to the first character of the string, the structure also contains + * the length of the string. +- * ++ * + * @section motivation Motivation + * Storing the length of the string together with the pointer to the string + * has two advantages. First, it makes many string operations faster because + * it is not necessary to count the number of characters at runtime. Second, + * the pointer can point to arbitrary substrings within a SIP message (which + * itself is stored as one long string spanning the whole message) without the +- * need to make a zero-terminated copy of it. ++ * need to make a zero-terminated copy of it. + * +- * @section drawbacks Drawbacks ++ * @section drawbacks Drawbacks + * Note well that the fact that string stored + * using this data structure are not zero terminated makes them a little + * incovenient to use with many standard libc string functions, because these +@@ -49,7 +49,7 @@ + * characters is considered to be dangerous. + */ + +-/** @file ++/** @file + * This header field defines the ::str data structure that is used across + * SER sources to store counted-length strings. The file also defines several + * convenience macros. +@@ -91,13 +91,13 @@ typedef struct _str str; + #define STR_NULL {0, 0} + + /** Formats ::str string for use in printf-like functions. +- * This is a macro that prepares a ::str string for use in functions which +- * use printf-like formatting strings. This macro is necessary because +- * ::str strings do not have to be zero-terminated and thus it is necessary +- * to provide printf-like functions with the number of characters in the +- * string manually. Here is an example how to use the macro: +- * \code printf("%.*s\n", STR_FMT(var));\endcode Note well that the correct +- * sequence in the formatting string is %.*, see the man page of printf for ++ * This is a macro that prepares a ::str string for use in functions which ++ * use printf-like formatting strings. This macro is necessary because ++ * ::str strings do not have to be zero-terminated and thus it is necessary ++ * to provide printf-like functions with the number of characters in the ++ * string manually. Here is an example how to use the macro: ++ * \code printf("%.*s\n", STR_FMT(var));\endcode Note well that the correct ++ * sequence in the formatting string is %.*, see the man page of printf for + * more details. + */ + #define STR_FMT(_pstr_) \ +@@ -106,8 +106,8 @@ typedef struct _str str; + + + /** Compares two ::str strings. +- * This macro implements comparison of two strings represented using ::str +- * structures. First it compares the lengths of both string and if and only ++ * This macro implements comparison of two strings represented using ::str ++ * structures. First it compares the lengths of both string and if and only + * if they are same then both strings are compared using memcmp. + * @param x is first string to be compared + * @param y is second string to be compared +@@ -116,6 +116,29 @@ typedef struct _str str; + #define STR_EQ(x,y) (((x).len == (y).len) && \ + (memcmp((x).s, (y).s, (x).len) == 0)) + ++/** ++ * If c != '\0', backup c in v and set c = '\0' ++ * - useful to terminate str->s with '\0' (if not already '\0'), keeping original ++ * value in v, to be able to restore with STR_ZTOV(...) ++ */ ++#define STR_VTOZ(c,v) do { \ ++ v = '\0'; \ ++ if(c!='\0') { \ ++ v = c; \ ++ c = '\0'; \ ++ } \ ++ } while(0) ++ ++/** ++ * If v != '\0', set c = v ++ * - restore original value after using STR_VTOZ(...) ++ */ ++#define STR_ZTOV(c,v) do { \ ++ if (v != '\0') { \ ++ c = v; \ ++ } \ ++ } ++ + /** @} */ + + /** Appends a sufffix diff --git a/debian/patches/upstream/jansson-use-the-core-macros-for-ending-string-value-.patch b/debian/patches/upstream/jansson-use-the-core-macros-for-ending-string-value-.patch new file mode 100644 index 000000000..ff5232636 --- /dev/null +++ b/debian/patches/upstream/jansson-use-the-core-macros-for-ending-string-value-.patch @@ -0,0 +1,118 @@ +From: Victor Seva +Date: Thu, 14 May 2020 10:41:06 +0200 +Subject: [PATCH] jansson: use the core macros for ending string value with + '\0' and to restore + +--- + src/modules/jansson/jansson_funcs.c | 35 +++++++++++++++++++---------------- + 1 file changed, 19 insertions(+), 16 deletions(-) + +diff --git a/src/modules/jansson/jansson_funcs.c b/src/modules/jansson/jansson_funcs.c +index 7eceae4..a5f8b1f 100644 +--- a/src/modules/jansson/jansson_funcs.c ++++ b/src/modules/jansson/jansson_funcs.c +@@ -26,6 +26,7 @@ + + #include "../../core/mod_fix.h" + #include "../../core/lvalue.h" ++#include "../../core/str.h" + + #include "jansson_path.h" + #include "jansson_funcs.h" +@@ -33,13 +34,13 @@ + + int janssonmod_get_helper(sip_msg_t* msg, str *path_s, str *src_s, pv_spec_t *dst_pv) + { +- ++ char c; + pv_value_t dst_val; + json_t* json = NULL; + json_error_t parsing_error; +- ++ STR_VTOZ(src_s->s[src_s->len], c); + json = json_loads(src_s->s, JSON_REJECT_DUPLICATES, &parsing_error); +- ++ STR_ZTOV(src_s->s[src_s->len], c); + if(!json) { + ERR("failed to parse json: %.*s\n", src_s->len, src_s->s); + ERR("json error at line %d, col %d: %s\n", +@@ -98,7 +99,7 @@ int janssonmod_set(unsigned int append, struct sip_msg* msg, char* type_in, + str type_s; + str value_s; + str path_s; +- ++ char c; + pv_spec_t* result_pv; + pv_value_t result_val; + +@@ -126,14 +127,11 @@ int janssonmod_set(unsigned int append, struct sip_msg* msg, char* type_in, + result_val.rs.len = strlen("{}"); + } + +-/* +- ALERT("type is: %.*s\n", type_s.len, type_s.s); +- ALERT("path is: %.*s\n", path_s.len, path_s.s); +- ALERT("value is: %.*s\n", value_s.len, value_s.s); +- ALERT("result is: %.*s\n", result_val.rs.len, result_val.rs.s); +-*/ + +- char* result = result_val.rs.s; ++ LM_DBG("type is: %.*s\n", type_s.len, type_s.s); ++ LM_DBG("path is: %.*s\n", path_s.len, path_s.s); ++ LM_DBG("value is: %.*s\n", value_s.len, value_s.s); ++ LM_DBG("result is: %.*s\n", result_val.rs.len, result_val.rs.s); + + json_t* result_json = NULL; + json_t* value = NULL; +@@ -143,14 +141,18 @@ int janssonmod_set(unsigned int append, struct sip_msg* msg, char* type_in, + + /* check the type */ + if(STR_EQ_STATIC(type_s, "object") || STR_EQ_STATIC(type_s, "obj")){ ++ STR_VTOZ(value_s.s[value_s.len], c); + value = json_loads(value_s.s, JSON_REJECT_DUPLICATES, &parsing_error); ++ STR_ZTOV(value_s.s[value_s.len], c); + if(value && !json_is_object(value)) { + ERR("value to add is not an object - \"%s\"\n", path_s.s); + goto fail; + } + + }else if(STR_EQ_STATIC(type_s, "array")) { ++ STR_VTOZ(value_s.s[value_s.len], c); + value = json_loads(value_s.s, JSON_REJECT_DUPLICATES, &parsing_error); ++ STR_ZTOV(value_s.s[value_s.len], c); + if(value && !json_is_array(value)) { + ERR("value to add is not an array - \"%s\"\n", path_s.s); + goto fail; +@@ -211,9 +213,9 @@ int janssonmod_set(unsigned int append, struct sip_msg* msg, char* type_in, + } + + char* path = path_s.s; +- +- result_json = json_loads(result, JSON_REJECT_DUPLICATES, &parsing_error); +- ++ STR_VTOZ(result_val.rs.s[result_val.rs.len], c); ++ result_json = json_loads(result_val.rs.s, JSON_REJECT_DUPLICATES, &parsing_error); ++ STR_ZTOV(result_val.rs.s[result_val.rs.len], c); + if(!result_json) { + ERR("result has json error at line %d: %s\n", + parsing_error.line, parsing_error.text); +@@ -240,6 +242,7 @@ fail: + + int janssonmod_array_size(struct sip_msg* msg, char* path_in, char* src_in, char* dst) + { ++ char c; + str src_s; + str path_s; + pv_spec_t *dst_pv; +@@ -259,9 +262,9 @@ int janssonmod_array_size(struct sip_msg* msg, char* path_in, char* src_in, char + + json_t* json = NULL; + json_error_t parsing_error; +- ++ STR_VTOZ(src_s.s[src_s.len], c); + json = json_loads(src_s.s, JSON_REJECT_DUPLICATES, &parsing_error); +- ++ STR_ZTOV(src_s.s[src_s.len], c); + if(!json) { + ERR("json error at line %d: %s\n", + parsing_error.line, parsing_error.text);