From 46d07c1372e78010071883c17afa172f1beb300b Mon Sep 17 00:00:00 2001 From: Alan Yaniger Date: Sun, 25 Jul 2021 09:04:26 +0300 Subject: [PATCH] split up long function, use safe string handling functions --- apps/restconf/restconf_methods.c | 690 ++++++++++++++++++------------- apps/restconf/restconf_methods.h | 1 + 2 files changed, 409 insertions(+), 282 deletions(-) diff --git a/apps/restconf/restconf_methods.c b/apps/restconf/restconf_methods.c index 3d9630bb..a13ba562 100644 --- a/apps/restconf/restconf_methods.c +++ b/apps/restconf/restconf_methods.c @@ -581,6 +581,367 @@ api_data_write(clicon_handle h, } /* api_data_write */ #ifdef YANG_PATCH + +char * init_str() +{ + char* s; + s = malloc(TEMP_STR_MALLOC_SIZE); + memset(s, 0, TEMP_STR_MALLOC_SIZE); + return s; +} + +int cpy_str(char *dest, char *src, size_t size) +{ + if (src == NULL) { + return 0; + } + if (dest == NULL) { + init_str(dest); + } + if (size <= 0 || size > TEMP_STR_MALLOC_SIZE) { + return 0; + } + size_t i; + for (i = 0; i < size - 1 && src[i]; i++) { + dest[i] = src[i]; + } + dest[i] = '\0'; + return i; +} + +int cat_str(char *dest, char *src, size_t size) +{ + if (src == NULL) { + return 0; + } + if (dest == NULL) { + init_str(dest); + } + if (size <= 0 || size > TEMP_STR_MALLOC_SIZE) { + return 0; + } + size_t i; + int old_len = strlen(dest); + for (i = 0; i < size - 1 && src[i]; i++) { + dest[i + old_len] = src[i]; + } + dest[i + old_len] = '\0'; + return i; +} + +void free_mem(void *str) +{ + if (str != NULL) + free(str); +} + +int get_xval( + cvec* nsc, + cxobj* xn, + char* val, + cxobj **vec, + size_t veclen, + const char* key + ) +{ + char* tmp_val = NULL; + int ret = xpath_vec(xn, nsc, "%s", &vec, &veclen, key); + if (ret < 0) { + return ret; + } + for (int j = 0; j < veclen; j++) { + cxobj *xn = vec[j]; + tmp_val = xml_body(xn); + } + cpy_str(val, tmp_val, TEMP_STR_MALLOC_SIZE); + return 0; +} + +int do_replace ( + clicon_handle h, + void *req, + int pi, + cvec *qvec, + int pretty, + restconf_media media_out, + ietf_ds_t ds, + char* simple_patch_request_uri, + char* target_val, + int value_vec_len, + cxobj** value_vec, + cxobj * value_vec_tmp, + cxobj *x_simple_patch, + char *patch_header + ) +{ + char *delete_req_uri = init_str(); + if (delete_req_uri == NULL) + return 1; + + if (cpy_str(delete_req_uri, simple_patch_request_uri, TEMP_STR_MALLOC_SIZE) <= 0) + return 1; + + if (cat_str(delete_req_uri, target_val, TEMP_STR_MALLOC_SIZE) <= 0) + return 1; + + // Delete the object with the old values + int ret = api_data_delete(h, req, delete_req_uri, pi, pretty, YANG_DATA_JSON, ds ); + free_mem((void *)delete_req_uri); + if (ret != 0) + return ret; + + // Now insert the object with the new values + char *json_simple_patch = init_str(); + if (json_simple_patch == NULL) + return 1; // goto done; + + for (int k = 0; k < value_vec_len; k++) { + if (value_vec[k] != NULL) { + value_vec_tmp = xml_dup(value_vec[k]); + xml_addsub(x_simple_patch, value_vec_tmp); + } + } + cbuf* cb = cbuf_new(); + xml2json_cbuf(cb, x_simple_patch, 1); + + // Some ugly text processing to get the JSON to match what api_data_post() expects + char *json_simple_patch_tmp = cbuf_get(cb); + int brace_count = 0; + for (int l = 0; l < strlen(json_simple_patch_tmp); l++) { + char c = json_simple_patch_tmp[l]; + if (c == '{') { + brace_count++; + if (brace_count == 2) { + json_simple_patch[strlen(json_simple_patch)] = '['; + } + } + json_simple_patch[strlen(json_simple_patch)] = c; + } + + /* strip /... from end */ + char *post_req_uri = init_str(); + if (post_req_uri == NULL) + return 1; + + int idx = strlen(target_val); + for (int l = strlen(target_val); l>= 0; l--) { + if (target_val[l] == '/') { + idx = l; + break; + } + } + cpy_str(post_req_uri, target_val, idx); + cat_str(simple_patch_request_uri, post_req_uri, TEMP_STR_MALLOC_SIZE); + free_mem((void *)post_req_uri); + for (int l = strlen(json_simple_patch); l>= 0; l--) { + char c = json_simple_patch[l]; + if (c == '}') { + json_simple_patch[l] = ']'; + json_simple_patch[l + 1] = '}'; + break; + } + } + + // Send the POST request + ret = api_data_post(h, req, simple_patch_request_uri, pi, qvec, json_simple_patch, pretty, YANG_DATA_JSON, media_out, ds ); + + free_mem((void *)value_vec_tmp); + free_mem((void *)x_simple_patch); + free_mem((void *)patch_header); + return ret; +} + +int do_create ( + clicon_handle h, + void *req, + int pi, + cvec *qvec, + int pretty, + restconf_media media_out, + ietf_ds_t ds, + char* simple_patch_request_uri, + int value_vec_len, + cxobj** value_vec, + cxobj * value_vec_tmp, + cxobj *x_simple_patch, + char *patch_header + ) +{ + for (int k = 0; k < value_vec_len; k++) { + if (value_vec[k] != NULL) { + value_vec_tmp = xml_dup(value_vec[k]); + xml_addsub(x_simple_patch, value_vec_tmp); + } + } + + // Send the POST request + cbuf* cb = cbuf_new(); + xml2json_cbuf(cb, x_simple_patch, 1); + char *json_simple_patch = cbuf_get(cb); + int ret = api_data_post(h, req, simple_patch_request_uri, pi, qvec, json_simple_patch, pretty, YANG_DATA_JSON, media_out, ds ); + free_mem((void *)value_vec_tmp); + free_mem((void *)x_simple_patch); + free_mem((void *)patch_header); + return ret; +} + +int do_insert ( + clicon_handle h, + void *req, + int pi, + int pretty, + restconf_media media_out, + ietf_ds_t ds, + char* simple_patch_request_uri, + int value_vec_len, + cxobj** value_vec, + cxobj * value_vec_tmp, + cxobj *x_simple_patch, + char *patch_header, + char* where_val, + char* api_path, + char *point_val + ) +{ + char *json_simple_patch = init_str(); + if (json_simple_patch == NULL) + return 1; + + // Loop through the XML, and get each value + for (int k = 0; k < value_vec_len; k++) { + if (value_vec[k] != NULL) { + value_vec_tmp = xml_dup(value_vec[k]); + xml_addsub(x_simple_patch, value_vec_tmp); + } + } + cbuf* cb = cbuf_new(); + xml2json_cbuf(cb, x_simple_patch, 1); + + // Some ugly text processing to get the JSON to match what api_data_post() expects + char *json_simple_patch_tmp = cbuf_get(cb); + int brace_count = 0; + for (int l = 0; l < strlen(json_simple_patch_tmp); l++) { + char c = json_simple_patch_tmp[l]; + if (c == '{') { + brace_count++; + if (brace_count == 2) { + json_simple_patch[strlen(json_simple_patch)] = '['; + } + } + json_simple_patch[strlen(json_simple_patch)] = c; + } + for (int l = strlen(json_simple_patch); l>= 0; l--) { + char c = json_simple_patch[l]; + if (c == '}') { + json_simple_patch[l] = ']'; + json_simple_patch[l + 1] = '}'; + break; + } + } + + // Set the insert attributes + cvec* qvec_tmp = NULL; + qvec_tmp = cvec_new(0); + if (qvec_tmp == NULL) + return 1; + cg_var *cv; + if ((cv = cvec_add(qvec_tmp, CGV_STRING)) == NULL){ + return 1; + } + cv_name_set(cv, "insert"); + cv_string_set(cv, where_val); + char *point_str = init_str(); + if (point_str == NULL) + return 1; + cpy_str(point_str, api_path, TEMP_STR_MALLOC_SIZE); + cat_str(point_str, point_val, TEMP_STR_MALLOC_SIZE); + if ((cv = cvec_add(qvec_tmp, CGV_STRING)) == NULL){ + return 1; + } + cv_name_set(cv, "point"); + cv_string_set(cv, point_str); + + // Send the POST request + int ret = api_data_post(h, req, simple_patch_request_uri, pi, qvec_tmp, json_simple_patch, pretty, YANG_DATA_JSON, media_out, ds ); + if (cb != NULL) + cbuf_free(cb); + free_mem((void *)value_vec_tmp); + free_mem((void *)point_str); + free_mem((void *)json_simple_patch); + free_mem((void *)patch_header); + free_mem((void *)x_simple_patch); + return ret; +} + +int do_merge ( + clicon_handle h, + void *req, + cvec *pcvec, + int pi, + cvec *qvec, + int pretty, + restconf_media media_out, + ietf_ds_t ds, + char* simple_patch_request_uri, + int value_vec_len, + cxobj** value_vec, + cxobj * value_vec_tmp, + cxobj *x_simple_patch, + cxobj *key_xn, + int plain_patch_val, + char *patch_header + ) +{ + int ret = -1; + if (key_xn != NULL) + xml_addsub(x_simple_patch, key_xn); + + char *json_simple_patch = init_str(); + if (json_simple_patch == NULL) + return 1; + + // Loop through the XML, create JSON from each one, and submit a simple patch + for (int k = 0; k < value_vec_len; k++) { + if (value_vec[k] != NULL) { + value_vec_tmp = xml_dup(value_vec[k]); + xml_addsub(x_simple_patch, value_vec_tmp); + } + cbuf* cb = cbuf_new(); + xml2json_cbuf(cb, x_simple_patch, 1); + + // Some ugly text processing to get the JSON to match what api_data_write() expects for a simple patch + char *json_simple_patch_tmp = cbuf_get(cb); + memset(json_simple_patch, 0, TEMP_STR_MALLOC_SIZE); + int brace_count = 0; + for (int l = 0; l < strlen(json_simple_patch_tmp); l++) { + char c = json_simple_patch_tmp[l]; + if (c == '{') { + brace_count++; + if (brace_count == 2) { + json_simple_patch[strlen(json_simple_patch)] = '['; + } + } + json_simple_patch[strlen(json_simple_patch)] = c; + } + for (int l = strlen(json_simple_patch); l>= 0; l--) { + char c = json_simple_patch[l]; + if (c == '}') { + json_simple_patch[l] = ']'; + json_simple_patch[l + 1] = '}'; + break; + } + } + free_mem(value_vec_tmp); + // Send the simple patch request + ret = api_data_write(h, req, simple_patch_request_uri, pcvec, pi, qvec, json_simple_patch, pretty, YANG_DATA_JSON, media_out, plain_patch_val, ds ); + cbuf_free(cb); + } + free_mem(json_simple_patch); + free_mem(patch_header); + free_mem(x_simple_patch); + return ret; +} + /*! YANG PATCH method * @param[in] h Clixon handle * @param[in] req Generic Www handle @@ -627,7 +988,6 @@ api_data_yang_patch(clicon_handle h, cvec *nsc = NULL; yang_bind yb; char *xpath = NULL; - const int temp_str_malloc_size = 5000; char *path_orig_1 = NULL; clicon_debug(1, "%s api_path:\"%s\"", __FUNCTION__, api_path0); @@ -697,84 +1057,58 @@ api_data_yang_patch(clicon_handle h, continue; } } - path_orig_1 = malloc(temp_str_malloc_size); + path_orig_1 = init_str(); if (path_orig_1 == NULL) { goto done; } else { - strcpy(path_orig_1, restconf_uripath(h)); + cpy_str(path_orig_1, restconf_uripath(h), TEMP_STR_MALLOC_SIZE); } // Loop through the edits for (int i = 0; i < veclen; i++) { + cxobj **tmp_vec = NULL; + size_t tmp_veclen = 0; + cxobj *xn = vec[i]; - // Get target - char *target_val = NULL; - cxobj **target_vec = NULL; - size_t target_veclen; - ret = xpath_vec(xn, nsc, "target", &target_vec, &target_veclen); + char *target_val = init_str(); + ret = get_xval(nsc, xn, target_val, tmp_vec, tmp_veclen, "target"); if (ret < 0) { goto done; } - for (int j = 0; j < target_veclen; j++) { - cxobj *target_xn = target_vec[j]; - target_val = xml_body(target_xn); - } - // Get operation - char *op_val = NULL; - cxobj **operation_vec = NULL; - size_t operation_veclen; - ret = xpath_vec(xn, nsc, "operation", &operation_vec, &operation_veclen); + char *op_val = init_str(); + ret = get_xval(nsc, xn, op_val, tmp_vec, tmp_veclen, "operation"); if (ret < 0) { goto done; } - for (int j = 0; j < operation_veclen; j++) { - cxobj *operation_xn = operation_vec[j]; - op_val = xml_body(operation_xn); - } - // Get "point" and "where" for insert operations - char *point_val = NULL; - cxobj **point_vec = NULL; - size_t point_veclen; - if (strcmp(op_val, "insert") == 0) { - ret = xpath_vec(xn, nsc, "point", &point_vec, &point_veclen); + char *point_val = init_str(); + char *where_val = init_str(); + if (strcmp(op_val, "insert") == 0) { // TODO - test + point_val = init_str(); + ret = get_xval(nsc, xn, point_val, tmp_vec, tmp_veclen, "point"); if (ret < 0) { goto done; } - for (int j = 0; j < point_veclen; j++) { - cxobj *point_xn = point_vec[j]; - point_val = xml_body(point_xn); - } - } - char *where_val = NULL; - cxobj **where_vec = NULL; - size_t where_veclen; - if (strcmp(op_val, "insert") == 0) { - ret = xpath_vec(xn, nsc, "where", &where_vec, &where_veclen); + where_val = init_str(); + ret = get_xval(nsc, xn, where_val, tmp_vec, tmp_veclen, "where"); if (ret < 0) { goto done; } - for (int j = 0; j < where_veclen; j++) { - cxobj *where_xn = where_vec[j]; - where_val = xml_body(where_xn); - } } // Construct request URI - char* simple_patch_request_uri = NULL; - simple_patch_request_uri = malloc(temp_str_malloc_size); - strcpy(simple_patch_request_uri, path_orig_1); + char* simple_patch_request_uri = init_str(); + cpy_str(simple_patch_request_uri, path_orig_1, TEMP_STR_MALLOC_SIZE); int plain_patch_val = 0; - char* api_path_target = NULL; - api_path_target = malloc(temp_str_malloc_size); - strcpy(api_path_target, api_path); + char* api_path_target = init_str(); + cpy_str(api_path_target, api_path, TEMP_STR_MALLOC_SIZE); if (strcmp(op_val, "merge") == 0) { plain_patch_val = 1; - strcat(api_path_target, target_val); - strcat(simple_patch_request_uri, target_val); + cat_str(api_path_target, target_val, TEMP_STR_MALLOC_SIZE); + cat_str(simple_patch_request_uri, target_val, TEMP_STR_MALLOC_SIZE); } if (xerr) @@ -809,266 +1143,63 @@ api_data_yang_patch(clicon_handle h, key_xn = key_vec[0]; } - // Get values (for "delete", there are no values) - cxobj **values_vec = NULL; - size_t values_veclen; - xpath_vec(xn, nsc, "value", &values_vec, &values_veclen); + // Get values (for "delete" and "remove", there are no values) + xpath_vec(xn, nsc, "value", &tmp_vec, &tmp_veclen); key_node_id = NULL; // Loop through the values - for (int j = 0; j < values_veclen; j++) { - cxobj *values_xn = values_vec[j]; + for (int j = 0; j < tmp_veclen; j++) { + cxobj *values_xn = tmp_vec[j]; cxobj** values_child_vec = xml_childvec_get(values_xn); if (key_node_id == NULL) key_node_id = xml_name(*values_child_vec); - char *patch_header = NULL; - patch_header = malloc(temp_str_malloc_size); + char *patch_header = init_str(); if (patch_header == NULL) { goto done; } - strcpy(patch_header, modname); - strcat(patch_header, ":"); - strcat(patch_header, key_node_id); + cpy_str(patch_header, modname, TEMP_STR_MALLOC_SIZE); + cat_str(patch_header, ":", TEMP_STR_MALLOC_SIZE); + cat_str(patch_header, key_node_id, TEMP_STR_MALLOC_SIZE); cxobj *x_simple_patch = xml_new(patch_header, NULL, CX_ELMNT); if (x_simple_patch == NULL) goto done; int value_vec_len = xml_child_nr(*values_child_vec); cxobj** value_vec = xml_childvec_get(*values_child_vec); cxobj * value_vec_tmp = NULL; - // For "replace", delete the item and then POST it // TODO - in an ordered list, insert it into its original position if (strcmp(op_val,"replace") == 0) { - char *delete_req_uri = malloc(temp_str_malloc_size); - if (delete_req_uri == NULL) - break; - - strcpy(delete_req_uri, simple_patch_request_uri); - strcat(delete_req_uri, target_val); - - // Delete the object with the old values - ret = api_data_delete(h, req, delete_req_uri, pi, pretty, YANG_DATA_JSON, ds ); - free(delete_req_uri); - - // Now insert the object with the new values - char *json_simple_patch = malloc(temp_str_malloc_size); - if (json_simple_patch == NULL) + ret = do_replace(h, req, pi, qvec, pretty, media_out, ds, simple_patch_request_uri, target_val, value_vec_len, value_vec, value_vec_tmp, x_simple_patch, patch_header); + if (ret != 0) { goto done; - memset(json_simple_patch, 0, temp_str_malloc_size); - - for (int k = 0; k < value_vec_len; k++) { - if (value_vec[k] != NULL) { - value_vec_tmp = xml_dup(value_vec[k]); - xml_addsub(x_simple_patch, value_vec_tmp); - } } - cbuf* cb = cbuf_new(); - xml2json_cbuf(cb, x_simple_patch, 1); - - // Some ugly text processing to get the JSON to match what api_data_post() expects - char *json_simple_patch_tmp = cbuf_get(cb); - int brace_count = 0; - for (int l = 0; l < strlen(json_simple_patch_tmp); l++) { - char c = json_simple_patch_tmp[l]; - if (c == '{') { - brace_count++; - if (brace_count == 2) { - json_simple_patch[strlen(json_simple_patch)] = '['; - } - } - json_simple_patch[strlen(json_simple_patch)] = c; - } - /* strip /... from end */ - char *post_req_uri = malloc(temp_str_malloc_size); - if (post_req_uri == NULL) - break; - memset(post_req_uri, 0, temp_str_malloc_size); - if (post_req_uri == NULL) - break; - int idx = strlen(target_val); - for (int l = strlen(target_val); l>= 0; l--) { - if (target_val[l] == '/') { - idx = l; - break; - } - } - strncpy(post_req_uri, target_val, idx); - strcat(simple_patch_request_uri, post_req_uri); - free(post_req_uri); - for (int l = strlen(json_simple_patch); l>= 0; l--) { - char c = json_simple_patch[l]; - if (c == '}') { - json_simple_patch[l] = ']'; - json_simple_patch[l + 1] = '}'; - break; - } - } - - // Send the POST request - ret = api_data_post(h, req, simple_patch_request_uri, pi, qvec, json_simple_patch, pretty, YANG_DATA_JSON, media_out, ds ); - if (value_vec_tmp != NULL) - free(value_vec_tmp); - free(x_simple_patch); - free(patch_header); // NULL check was already done before - if (ret != 0) - goto done; - break; } - // For "create", put all the data values into a single POST request if (strcmp(op_val,"create") == 0) { - for (int k = 0; k < value_vec_len; k++) { - if (value_vec[k] != NULL) { - value_vec_tmp = xml_dup(value_vec[k]); - xml_addsub(x_simple_patch, value_vec_tmp); - } - } - - // Send the POST request - cbuf* cb = cbuf_new(); - xml2json_cbuf(cb, x_simple_patch, 1); - char *json_simple_patch = cbuf_get(cb); - ret = api_data_post(h, req, simple_patch_request_uri, pi, qvec, json_simple_patch, pretty, YANG_DATA_JSON, media_out, ds ); - if (value_vec_tmp != NULL) - free(value_vec_tmp); - free(x_simple_patch); - free(patch_header); // NULL check was already done before - if (ret != 0) + ret = do_create(h, req, pi, qvec, pretty, media_out, ds, simple_patch_request_uri, value_vec_len, value_vec, value_vec_tmp, x_simple_patch, patch_header); + if (ret != 0) { goto done; - break; + } } - // For "insert", make a api_data_post request + // For "insert", make a api_data_post request // TODO - test if (strcmp(op_val, "insert") == 0) { - char *json_simple_patch = malloc(temp_str_malloc_size); - if (json_simple_patch == NULL) - goto done; - memset(json_simple_patch, 0, temp_str_malloc_size); - - // Loop through the XML, and get each value - for (int k = 0; k < value_vec_len; k++) { - if (value_vec[k] != NULL) { - value_vec_tmp = xml_dup(value_vec[k]); - xml_addsub(x_simple_patch, value_vec_tmp); - } - } - cbuf* cb = cbuf_new(); - xml2json_cbuf(cb, x_simple_patch, 1); - - // Some ugly text processing to get the JSON to match what api_data_post() expects - char *json_simple_patch_tmp = cbuf_get(cb); - int brace_count = 0; - for (int l = 0; l < strlen(json_simple_patch_tmp); l++) { - char c = json_simple_patch_tmp[l]; - if (c == '{') { - brace_count++; - if (brace_count == 2) { - json_simple_patch[strlen(json_simple_patch)] = '['; - } - } - json_simple_patch[strlen(json_simple_patch)] = c; - } - for (int l = strlen(json_simple_patch); l>= 0; l--) { - char c = json_simple_patch[l]; - if (c == '}') { - json_simple_patch[l] = ']'; - json_simple_patch[l + 1] = '}'; - break; - } - } - - // Set the insert attributes - cvec* qvec_tmp = NULL; - qvec_tmp = cvec_new(0); - if (qvec_tmp == NULL) - goto done; - cg_var *cv; - if ((cv = cvec_add(qvec_tmp, CGV_STRING)) == NULL){ + ret = do_insert(h, req, pi, pretty, media_out, ds, simple_patch_request_uri, value_vec_len, value_vec, value_vec_tmp, x_simple_patch, patch_header, where_val, api_path, point_val); + if (ret != 0) { goto done; } - cv_name_set(cv, "insert"); - cv_string_set(cv, where_val); - char *point_str = malloc(temp_str_malloc_size); - if (point_str == NULL) - goto done; - memset(point_str, 0, temp_str_malloc_size); - strcpy(point_str, api_path); - strcat(point_str, point_val); - if ((cv = cvec_add(qvec_tmp, CGV_STRING)) == NULL){ - goto done; - } - cv_name_set(cv, "point"); - cv_string_set(cv, point_str); - - // Send the POST request - ret = api_data_post(h, req, simple_patch_request_uri, pi, qvec_tmp, json_simple_patch, pretty, YANG_DATA_JSON, media_out, ds ); - if (cb != NULL) - cbuf_free(cb); - if (value_vec_tmp != NULL) - free(value_vec_tmp); - free(point_str); // NULL check was already done above - free(json_simple_patch); // NULL check was already done above - free(patch_header); // NULL check was already done before - if (x_simple_patch != NULL) - free(x_simple_patch); - break; } - // For merge", make single simple patch requests for each value if (strcmp(op_val,"merge") == 0) { - if (key_xn != NULL) - xml_addsub(x_simple_patch, key_xn); - - char *json_simple_patch = malloc(temp_str_malloc_size); - if (json_simple_patch == NULL) + ret = do_merge(h, req, pcvec, pi, qvec, pretty, media_out, ds, simple_patch_request_uri, value_vec_len, value_vec, value_vec_tmp, x_simple_patch, key_xn, plain_patch_val, patch_header); + if (ret != 0) { goto done; - - // Loop through the XML, create JSON from each one, and submit a simple patch - for (int k = 0; k < value_vec_len; k++) { - if (value_vec[k] != NULL) { - value_vec_tmp = xml_dup(value_vec[k]); - xml_addsub(x_simple_patch, value_vec_tmp); - } - cbuf* cb = cbuf_new(); - xml2json_cbuf(cb, x_simple_patch, 1); - - // Some ugly text processing to get the JSON to match what api_data_write() expects for a simple patch - char *json_simple_patch_tmp = cbuf_get(cb); - memset(json_simple_patch, 0, temp_str_malloc_size); - int brace_count = 0; - for (int l = 0; l < strlen(json_simple_patch_tmp); l++) { - char c = json_simple_patch_tmp[l]; - if (c == '{') { - brace_count++; - if (brace_count == 2) { - json_simple_patch[strlen(json_simple_patch)] = '['; - } - } - json_simple_patch[strlen(json_simple_patch)] = c; - } - for (int l = strlen(json_simple_patch); l>= 0; l--) { - char c = json_simple_patch[l]; - if (c == '}') { - json_simple_patch[l] = ']'; - json_simple_patch[l + 1] = '}'; - break; - } - } - if (value_vec_tmp != NULL) - free(value_vec_tmp); - // Send the simple patch request - ret = api_data_write(h, req, simple_patch_request_uri, pcvec, pi, qvec, json_simple_patch, pretty, YANG_DATA_JSON, media_out, plain_patch_val, ds ); - cbuf_free(cb); } - free(json_simple_patch); // NULL check was already done above - free(patch_header); // NULL check was already done before - if (x_simple_patch != NULL) - free(x_simple_patch); } } if ((strcmp(op_val, "delete") == 0) || (strcmp(op_val, "remove") == 0)) { - strcat(simple_patch_request_uri, target_val); + cat_str(simple_patch_request_uri, target_val, TEMP_STR_MALLOC_SIZE); if (strcmp(op_val, "delete") == 0) { // TODO - send error } else { @@ -1076,20 +1207,15 @@ api_data_yang_patch(clicon_handle h, } api_data_delete(h, req, simple_patch_request_uri, pi, pretty, YANG_DATA_JSON, ds); } - if (simple_patch_request_uri) - free(simple_patch_request_uri); - if (api_path_target) - free(api_path_target); + free_mem((void *)simple_patch_request_uri); + free_mem((void *)api_path_target); } ok: retval = 0; done: - if (path_orig_1 != NULL) - free(path_orig_1); - if (vec) - free(vec); - if (xpath) - free(xpath); + free_mem((void *)path_orig_1); + free_mem((void *)vec); + free_mem((void *)xpath); if (nsc) xml_nsctx_free(nsc); if (xret) @@ -1194,7 +1320,7 @@ api_data_patch(clicon_handle h, ietf_ds_t ds) { restconf_media media_in; - int ret; + int ret = -1; media_in = restconf_content_type(h); switch (media_in){ diff --git a/apps/restconf/restconf_methods.h b/apps/restconf/restconf_methods.h index 975b63eb..95943ba6 100644 --- a/apps/restconf/restconf_methods.h +++ b/apps/restconf/restconf_methods.h @@ -39,6 +39,7 @@ #ifndef _RESTCONF_METHODS_H_ #define _RESTCONF_METHODS_H_ +#define TEMP_STR_MALLOC_SIZE 5000 /* * Prototypes */