diff --git a/apps/backend/backend_plugin.c b/apps/backend/backend_plugin.c index 450fd720..29038b63 100644 --- a/apps/backend/backend_plugin.c +++ b/apps/backend/backend_plugin.c @@ -115,6 +115,7 @@ clixon_plugin_statedata(clicon_handle h, cxobj *x = NULL; clixon_plugin *cp = NULL; plgstatedata_t *fn; /* Plugin statedata fn */ + cbuf *cberr = NULL; while ((cp = clixon_plugin_each(h, cp)) != NULL) { if ((fn = cp->cp_api.ca_statedata) == NULL) @@ -133,21 +134,11 @@ clixon_plugin_statedata(clicon_handle h, if (ret > 0 && (ret = xml_yang_validate_add(h, x, &xerr)) < 0) goto done; if (ret == 0){ - cbuf *cberr = NULL; /* XXX Cumbersome, try to fold into one cb */ - cbuf *cberr2 = NULL; - if (netconf_err2cb(xpath_first(xerr, "rpc-error"), &cberr) < 0) + cprintf(cberr, "Internal error: state callback returned invalid XML: "); + if (netconf_err2cb(xpath_first(xerr, "rpc-error"), cberr) < 0) goto done; - if ((cberr2 = cbuf_new()) == NULL){ - clicon_err(OE_UNIX, errno, "cbuf_new"); + if (netconf_operation_failed_xml(xret, "application", cbuf_get(cberr))< 0) goto done; - } - cprintf(cberr2, "Internal error: state callback returned invalid XML: %s", cbuf_get(cberr)); - if (netconf_operation_failed_xml(xret, "application", cbuf_get(cberr2))< 0) - goto done; - if (cberr) - cbuf_free(cberr); - if (cberr2) - cbuf_free(cberr2); goto fail; } if ((ret = netconf_trymerge(x, yspec, xret)) < 0) @@ -161,6 +152,8 @@ clixon_plugin_statedata(clicon_handle h, } retval = 1; done: + if (cberr) + cbuf_free(cberr); if (x) xml_free(x); if (xerr) diff --git a/lib/clixon/clixon_netconf_lib.h b/lib/clixon/clixon_netconf_lib.h index 463671c2..06e5c83b 100644 --- a/lib/clixon/clixon_netconf_lib.h +++ b/lib/clixon/clixon_netconf_lib.h @@ -90,7 +90,7 @@ int netconf_trymerge(cxobj *x, yang_stmt *yspec, cxobj **xret); int netconf_module_features(clicon_handle h); int netconf_module_load(clicon_handle h); char *netconf_db_find(cxobj *xn, char *name); -int netconf_err2cb(cxobj *xerr, cbuf **cberr); +int netconf_err2cb(cxobj *xerr, cbuf *cberr); const netconf_content netconf_content_str2int(char *str); const char *netconf_content_int2str(netconf_content nr); int netconf_hello_server(clicon_handle h, cbuf *cb, uint32_t session_id); diff --git a/lib/src/clixon_netconf_lib.c b/lib/src/clixon_netconf_lib.c index 2f9efaec..78d18692 100644 --- a/lib/src/clixon_netconf_lib.c +++ b/lib/src/clixon_netconf_lib.c @@ -1338,39 +1338,36 @@ netconf_db_find(cxobj *xn, } /*! Generate netconf error msg to cbuf to use in string printout or logs - * @param[in] xerr Netconf error message on the level: - * @param[out] cberr Translation from netconf err to cbuf. Free with cbuf_free. + * @param[in] xerr Netconf error message on the level: + * @param[in,out] cberr Translation from netconf err to cbuf. * @retval 0 OK, with cberr set * @retval -1 Error * @code - * cbuf *cb = NULL; - * if (netconf_err2cb(xerr, &cb) < 0) + * cbuf *cb = NULL; + * if ((cb = cbuf_new()) ==NULL){ + * err; + * if (netconf_err2cb(xerr, cb) < 0) * err; * printf("%s", cbuf_get(cb)); + * cbuf_free(cb); * @endcode * @see clicon_rpc_generate_error */ int netconf_err2cb(cxobj *xerr, - cbuf **cberr) + cbuf *cberr) { int retval = -1; - cbuf *cb = NULL; cxobj *x; - if ((cb = cbuf_new()) ==NULL){ - clicon_err(OE_XML, errno, "cbuf_new"); - goto done; - } if ((x=xpath_first(xerr, "error-type"))!=NULL) - cprintf(cb, "%s ", xml_body(x)); + cprintf(cberr, "%s ", xml_body(x)); if ((x=xpath_first(xerr, "error-tag"))!=NULL) - cprintf(cb, "%s ", xml_body(x)); + cprintf(cberr, "%s ", xml_body(x)); if ((x=xpath_first(xerr, "error-message"))!=NULL) - cprintf(cb, "%s ", xml_body(x)); + cprintf(cberr, "%s ", xml_body(x)); if ((x=xpath_first(xerr, "error-info"))!=NULL) - clicon_xml2cbuf(cb, xml_child_i(x,0), 0, 0, -1); - *cberr = cb; + clicon_xml2cbuf(cberr, xml_child_i(x,0), 0, 0, -1); retval = 0; done: return retval; diff --git a/lib/src/clixon_proto_client.c b/lib/src/clixon_proto_client.c index 146cc52a..a1de8f4e 100644 --- a/lib/src/clixon_proto_client.c +++ b/lib/src/clixon_proto_client.c @@ -236,7 +236,11 @@ clicon_rpc_generate_error(const char *prefix, int retval = -1; cbuf *cb = NULL; - if (netconf_err2cb(xerr, &cb) < 0) + if ((cb = cbuf_new()) ==NULL){ + clicon_err(OE_XML, errno, "cbuf_new"); + goto done; + } + if (netconf_err2cb(xerr, cb) < 0) goto done; if (prefix) clicon_log(LOG_ERR, "%s: %s", prefix, cbuf_get(cb)); diff --git a/util/clixon_util_xml.c b/util/clixon_util_xml.c index 45a1b0e7..30b4d9e2 100644 --- a/util/clixon_util_xml.c +++ b/util/clixon_util_xml.c @@ -234,7 +234,11 @@ main(int argc, if (ret > 0 && (ret = xml_yang_validate_add(h, xc, &xerr)) < 0) goto done; if (ret == 0){ - if (netconf_err2cb(xerr, &cbret) < 0) + if ((cbret = cbuf_new()) ==NULL){ + clicon_err(OE_XML, errno, "cbuf_new"); + goto done; + } + if (netconf_err2cb(xerr, cbret) < 0) goto done; fprintf(stderr, "xml validation error: %s\n", cbuf_get(cbret)); goto done;