From dcaeb581a1548f7013bc5ccd88e60b4c91abc944 Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Thu, 5 Aug 2021 09:59:20 +0200 Subject: [PATCH] * Fixed: Yang patterns: \n and other non-printable characters were broken * Example: Clixon interpereted them two characters: `\\ n` instead of ascii 10 --- CHANGELOG.md | 2 + lib/clixon/clixon_string.h | 2 + lib/src/clixon_string.c | 77 +++++++++++++++++++++++++++++++++ lib/src/clixon_yang_parse_lib.c | 12 +++++ test/test_pattern.sh | 14 ++++++ 5 files changed, 107 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 420059b2..7f09de7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,8 @@ Users may have to change how they access the system ### Corrected Bugs +* Fixed: Yang patterns: \n and other non-printable characters were broken + * Example: Clixon interpereted them two characters: `\\ n` instead of ascii 10 * Fixed: The auto-cli identityref did not expand identities in grouping/usecases properly. * Fixed: [OpenConfig BGP afi-safi and when condition issues #249](https://github.com/clicon/clixon/issues/249) * YANG when was not properly implemented for default values diff --git a/lib/clixon/clixon_string.h b/lib/clixon/clixon_string.h index fea7fd8a..689623e3 100644 --- a/lib/clixon/clixon_string.h +++ b/lib/clixon/clixon_string.h @@ -100,6 +100,8 @@ int xml_chardata_encode(char **escp, const char *fmt, ...); #endif int xml_chardata_cbuf_append(cbuf *cb, char *str); int uri_percent_decode(char *enc, char **str); +int nonprint_encode(char *orig, char **encp); + const char *clicon_int2str(const map_str2int *mstab, int i); int clicon_str2int(const map_str2int *mstab, char *str); int clicon_str2int_search(const map_str2int *mstab, char *str, int upper); diff --git a/lib/src/clixon_string.c b/lib/src/clixon_string.c index d6004a32..0e73b28a 100644 --- a/lib/src/clixon_string.c +++ b/lib/src/clixon_string.c @@ -646,6 +646,83 @@ uri_str2cvec(char *string, goto done; } +/*! Translate \n and others from \\n (two chars) to \n (one char) + * + * This is needed in yang regex it seems. + * It was triggered by eg draft-wwlh-netconf-list-pagination-00 module example-social tagline + * leaf tagline { + * type string { + * length "1..80"; + * pattern '.*[\n].*' { + * modifier invert-match; + * @param[in] orig Original string eg with \\n + * @param[out] enc Encoded string with \n, malloced. + * + * @see https://www.regular-expressions.info/nonprint.html + */ +int +nonprint_encode(char *orig, + char **encp) +{ + int retval = -1; + char *enc = NULL; + int i; + int j; + int esc = 0; + char c; + char c1; + + if (orig == NULL){ + clicon_err(OE_UNIX, EINVAL, "orig is NULL"); + goto done; + } + /* Encoded string is equal or shorter */ + if ((enc = malloc(strlen(orig)+1)) == NULL){ + clicon_err(OE_UNIX, errno, "strdup"); + goto done; + } + j = 0; + for (i=0; i