From 170d06cb63ada7e2cf786426d3b5d99f1559963b Mon Sep 17 00:00:00 2001 From: Eric Kinzie Date: Thu, 16 Jan 2020 19:00:47 +0000 Subject: [PATCH 1/2] fix check for root data resource in URI For a vector v of length n, element v[n] is not guaranteed to be NULL as vector_grow does not clear newly allocated memory. Get the length of the vector instead of checking for a NULL element. This (a) fixes a segfault when the unitialized pointer value is non-null and (b) correctly detects the root node so that a POST to /restconf/data creates a new top-level node. Signed-off-by: Eric Kinzie --- src/restconf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/restconf.c b/src/restconf.c index 0eae42d..5267b2e 100644 --- a/src/restconf.c +++ b/src/restconf.c @@ -38,7 +38,7 @@ static int api_root(struct CgiContext *cgi) { static int data_root(struct CgiContext *cgi, char **pathvec) { int retval = 1; - if (pathvec[1] == NULL) { + if (vector_size(pathvec) == 1) { // root if (is_OPTIONS(cgi->method)) { content_type_json(); From 4bb83554fe99802c00aa17d41f35199199d02272 Mon Sep 17 00:00:00 2001 From: Eric Kinzie Date: Thu, 16 Jan 2020 19:38:28 +0000 Subject: [PATCH 2/2] fix formatting of key in Location response header MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Initialize the key_out string with a NULL at the beginning so that strcat() and strlen() are not confused by whatever uninitialized non-zero data happens to be there. This avoids appending = when no key should be output (and it contains garbage). It also fixes the formatting of keys when strcat() has appended the first key after some uninitialized data. example from tavern-ci output: ERROR tavern.response.base:base.py:51 Value mismatch in headers: Key mismatch: (expected["location"] = 'https://192.168.1.40/cgi-bin/restconf/data/restconf-example:course/instructors' (type = .InnerFormattedString'>), actual["location"] = 'https://192.168.1.40/cgi-bin/restconf/data/restconf-example:course/instructors=*Ò¶ÿÿ' (type = )) Signed-off-by: Eric Kinzie --- src/restconf-method.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/restconf-method.c b/src/restconf-method.c index 8e207c8..5768b9c 100644 --- a/src/restconf-method.c +++ b/src/restconf-method.c @@ -528,6 +528,8 @@ int data_post(struct CgiContext *cgi, char **pathvec, int root) { retval = restconf_operation_failed_internal(); goto done; } + + key_out[0] = 0; root_key_copy = str_dup(root_key); struct json_object *created = NULL; if ((created = json_get_object_from_map(top_level, root_key_copy))) { @@ -700,6 +702,7 @@ int data_put(struct CgiContext *cgi, char **pathvec, int root) { delete_uci = uci; + key_out[0] = 0; if (yang_is_list(json_get_string(top_level, YANG_TYPE))) { struct json_object *keys = NULL; if ((keys = json_get_array(top_level, YANG_KEYS))) {