From 99b01040a77fa07e894a2ea14cadd266cf2149a6 Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Thu, 2 Jul 2020 13:26:15 +0200 Subject: [PATCH] * Fixed: [Double free when using libxml2 as regex engine #117](https://github.com/clicon/clixon/issues/117) * added libxml2 support in test_pattern.sh when libxml2 is configured --- CHANGELOG.md | 1 + configure | 2 ++ configure.ac | 1 + lib/src/clixon_validate.c | 4 +++- lib/src/clixon_yang.c | 13 +++++++++---- test/config.sh.in | 4 +++- test/test_pattern.sh | 14 ++++++++++---- 7 files changed, 29 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b95e0996..5bc464ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -87,6 +87,7 @@ Expected: July 2020 ### Corrected Bugs +* Fixed: [Double free when using libxml2 as regex engine #117](https://github.com/clicon/clixon/issues/117) * Fixed: Reading in a yang-spec file exactly the same size as the buffer (1024/2048/4096/...) could leave the buffer not terminated with a 0 byte * Fixed: The module `clixon-rfc5277` was always enabled, but should only be enabled when `CLICON_STREAM_DISCOVERY_RFC5277` is enabled. diff --git a/configure b/configure index f4161a7a..3cf981b2 100755 --- a/configure +++ b/configure @@ -637,6 +637,7 @@ CPP wwwuser wwwdir enable_optyangs +with_libxml2 with_restconf SH_SUFFIX CLIXON_DEFAULT_CONFIG @@ -3350,6 +3351,7 @@ test -z "$INSTALL_DATA" && INSTALL_DATA='${INSTALL} -m 644' # If yes, compile apps/restconf + wwwdir=/www-data wwwuser=www-data diff --git a/configure.ac b/configure.ac index 7a6b4b9f..eb6aa639 100644 --- a/configure.ac +++ b/configure.ac @@ -94,6 +94,7 @@ AC_SUBST(CLIXON_DEFAULT_CONFIG) AC_SUBST(LIBS) AC_SUBST(SH_SUFFIX) AC_SUBST(with_restconf) # If yes, compile apps/restconf +AC_SUBST(with_libxml2) AC_SUBST(enable_optyangs) AC_SUBST(wwwdir,/www-data) AC_SUBST(wwwuser,www-data) diff --git a/lib/src/clixon_validate.c b/lib/src/clixon_validate.c index 16466235..e57bce48 100644 --- a/lib/src/clixon_validate.c +++ b/lib/src/clixon_validate.c @@ -984,7 +984,9 @@ xml_yang_validate_add(clicon_handle h, goto fail; } } - if ((ys_cv_validate(h, cv, yt, &reason)) != 1){ + if ((ret = ys_cv_validate(h, cv, yt, &reason)) < 0) + goto done; + if (ret == 0){ if (netconf_bad_element_xml(xret, "application", yang_argument_get(yt), reason) < 0) goto done; goto fail; diff --git a/lib/src/clixon_yang.c b/lib/src/clixon_yang.c index fe24cfea..35ee3fc5 100644 --- a/lib/src/clixon_yang.c +++ b/lib/src/clixon_yang.c @@ -2827,17 +2827,22 @@ yang_type_cache_free(yang_type_cache *ycache) switch (ycache->yc_rxmode){ case REGEXP_POSIX: cligen_regex_posix_free(cv_void_get(cv)); + if ((p = cv_void_get(cv)) != NULL){ + free(p); + cv_void_set(cv, NULL); + } break; case REGEXP_LIBXML2: cligen_regex_libxml2_free(cv_void_get(cv)); + /* Note, already freed in libxml2 case */ + if ((p = cv_void_get(cv)) != NULL){ + cv_void_set(cv, NULL); + } break; default: break; } - if ((p = cv_void_get(cv)) != NULL){ - free(p); - cv_void_set(cv, NULL); - } + } cvec_free(ycache->yc_regexps); } diff --git a/test/config.sh.in b/test/config.sh.in index af2e19c2..76587c43 100755 --- a/test/config.sh.in +++ b/test/config.sh.in @@ -1,5 +1,7 @@ #!/usr/bin/env bash # Generated from autotools -WITH_RESTCONF=@with_restconf@ +WITH_RESTCONF=@with_restconf@ # evhtp, fcgi or "" +WITH_LIBXML2=@with_libxml2@ # yes or "" CXX=@CXX@ + diff --git a/test/test_pattern.sh b/test/test_pattern.sh index 1e723547..f374326c 100755 --- a/test/test_pattern.sh +++ b/test/test_pattern.sh @@ -7,7 +7,6 @@ # Test strings have been generated by: # https://www.browserling.com/tools/text-from-regex # This is an unit test, not a clixon system test -# See test_regexp.sh for unit regexp tests # # NOTE: no tests for ' quote in strings # NOTE, the following does not match in libxml2 (but in clixon): @@ -22,9 +21,15 @@ APPNAME=example cfg=$dir/pattern.xml fyang=$dir/pattern.yang -# Regexp mode: posix or libxml2 -: ${regex:=posix} +regexlist="posix" +if [ "${WITH_LIBXML2}" = yes ] ; then + regexlist="$regexlist libxml2" +fi +# Loop over supported regexps. Always run posix, run libxml2 if configured +for regex in $regexlist; do + new "pattern tests for regex:$regex" + cat < $cfg $cfg @@ -720,7 +725,6 @@ testrun "p$pnr" 0 '' #testrun "p$pnr" 0 '