common CHANGE ly_value_prefix_next returns LY_ERR
Passed the result of the ly_getutf8 function in the case of error.
diff --git a/src/common.c b/src/common.c
index a434235..9e485c8 100644
--- a/src/common.c
+++ b/src/common.c
@@ -101,22 +101,22 @@
return LY_SUCCESS;
}
-uint32_t
-ly_value_prefix_next(const char *str_begin, const char *str_end, ly_bool *is_prefix, const char **str_next)
+LY_ERR
+ly_value_prefix_next(const char *str_begin, const char *str_end, uint32_t *len, ly_bool *is_prefix, const char **str_next)
{
const char *stop, *prefix;
- size_t bytes;
+ size_t bytes_read;
uint32_t c;
ly_bool prefix_found;
- uint32_t ret;
+ LY_ERR ret = LY_SUCCESS;
- assert(is_prefix && str_next);
+ assert(len && is_prefix && str_next);
#define IS_AT_END(PTR, STR_END) (STR_END ? PTR == STR_END : !(*PTR))
*str_next = NULL;
*is_prefix = 0;
- ret = 0;
+ *len = 0;
if (!str_begin || !(*str_begin) || (str_begin == str_end)) {
return ret;
@@ -128,21 +128,21 @@
do {
/* look for the beginning of the YANG value */
- for (ly_getutf8(&stop, &c, &bytes);
- !is_xmlqnamestartchar(c) && !IS_AT_END(stop, str_end);
- ly_getutf8(&stop, &c, &bytes)) {}
+ do {
+ LY_CHECK_RET(ly_getutf8(&stop, &c, &bytes_read));
+ } while (!is_xmlqnamestartchar(c) && !IS_AT_END(stop, str_end));
if (IS_AT_END(stop, str_end)) {
break;
}
/* maybe the prefix was found */
- prefix = stop - bytes;
+ prefix = stop - bytes_read;
/* look for the the end of the prefix */
- for (ly_getutf8(&stop, &c, &bytes);
- is_xmlqnamechar(c) && !IS_AT_END(stop, str_end);
- ly_getutf8(&stop, &c, &bytes)) {}
+ do {
+ LY_CHECK_RET(ly_getutf8(&stop, &c, &bytes_read));
+ } while (is_xmlqnamechar(c) && !IS_AT_END(stop, str_end));
prefix_found = c == ':' ? 1 : 0;
@@ -153,14 +153,14 @@
/* prefix found at the beginning of the input string */
*is_prefix = 1;
*str_next = IS_AT_END(stop, str_end) ? NULL : stop;
- ret = (stop - bytes) - str_begin;
+ *len = (stop - bytes_read) - str_begin;
} else if ((str_begin != prefix) && (prefix_found)) {
/* there is a some string before prefix */
*str_next = prefix;
- ret = prefix - str_begin;
+ *len = prefix - str_begin;
} else {
/* no prefix found */
- ret = stop - str_begin;
+ *len = stop - str_begin;
}
#undef IS_AT_END
diff --git a/src/common.h b/src/common.h
index 7a08f97..cb2591b 100644
--- a/src/common.h
+++ b/src/common.h
@@ -432,13 +432,14 @@
*
* @param[in] str_begin Begin of the input string.
* @param[in] str_end Length of the @p str_begin. If set to NULL then the @p str_begin must be NULL-terminated string.
+ * @param[out] len Number of bytes (length) of the found prefix/substring starting at @p str_begin.
* @param[out] is_prefix Type of substring found. Set to True for prefix, otherwise False.
* @param[out] str_next Remaining string starting after prefix/substring and ending with @p str_end.
* If the @p is_prefix is set to True then the colon character is skipped.
* If no string remains, it is set to NULL.
- * @return Number of bytes (length) of the found prefix/substring starting at @p str_begin.
+ * @return LY_ERR value.
*/
-uint32_t ly_value_prefix_next(const char *str_begin, const char *str_end, ly_bool *is_prefix, const char **str_next);
+LY_ERR ly_value_prefix_next(const char *str_begin, const char *str_end, uint32_t *len, ly_bool *is_prefix, const char **str_next);
/**
* @brief Wrapper around strlen() to handle NULL strings.
diff --git a/src/plugins_types/xpath1.0.c b/src/plugins_types/xpath1.0.c
index e1600cb..a743dd0 100644
--- a/src/plugins_types/xpath1.0.c
+++ b/src/plugins_types/xpath1.0.c
@@ -79,7 +79,8 @@
str_begin = token;
- while ((len = ly_value_prefix_next(str_begin, token + tok_len, &is_prefix, &str_next))) {
+ while (!(ret = ly_value_prefix_next(str_begin, token + tok_len, &len, &is_prefix, &str_next)) && len) {
+
if (is_prefix) {
/* resolve the module in the original format */
mod = lyplg_type_identity_module(resolve_ctx, NULL, str_begin, len, resolve_format, resolve_prefix_data);
diff --git a/src/tree_data_helpers.c b/src/tree_data_helpers.c
index 22c1899..89afee3 100644
--- a/src/tree_data_helpers.c
+++ b/src/tree_data_helpers.c
@@ -535,7 +535,7 @@
/* add all used prefixes */
value_end = value + value_len;
for (value_iter = value; value_iter; value_iter = value_next) {
- substr_len = ly_value_prefix_next(value_iter, value_end, &is_prefix, &value_next);
+ LY_CHECK_GOTO(ret = ly_value_prefix_next(value_iter, value_end, &substr_len, &is_prefix, &value_next), cleanup);
if (is_prefix) {
/* we have a possible prefix. Do we already have the prefix? */
mod = ly_resolve_prefix(ctx, value_iter, substr_len, *format_p, *prefix_data_p);
@@ -571,7 +571,7 @@
/* add all used prefixes */
value_end = value + value_len;
for (value_iter = value; value_iter; value_iter = value_next) {
- substr_len = ly_value_prefix_next(value_iter, value_end, &is_prefix, &value_next);
+ LY_CHECK_GOTO(ret = ly_value_prefix_next(value_iter, value_end, &substr_len, &is_prefix, &value_next), cleanup);
if (is_prefix) {
/* we have a possible prefix. Do we already have the prefix? */
ns = lyxml_ns_get(ns_list, value_iter, substr_len);
diff --git a/src/xml.c b/src/xml.c
index e4bc8bd..b39b3a4 100644
--- a/src/xml.c
+++ b/src/xml.c
@@ -1115,8 +1115,12 @@
for (value1_iter = value1, value2_iter = value2;
value1_iter && value2_iter;
value1_iter = value1_next, value2_iter = value2_next) {
- value1_len = ly_value_prefix_next(value1_iter, NULL, &is_prefix1, &value1_next);
- value2_len = ly_value_prefix_next(value2_iter, NULL, &is_prefix2, &value2_next);
+ if ((ret = ly_value_prefix_next(value1_iter, NULL, &value1_len, &is_prefix1, &value1_next))) {
+ break;
+ }
+ if ((ret = ly_value_prefix_next(value2_iter, NULL, &value2_len, &is_prefix2, &value2_next))) {
+ break;
+ }
if (is_prefix1 != is_prefix2) {
ret = LY_ENOT;