diff --git a/src/ly_common.c b/src/ly_common.c index 88aeba9f1..eb645b9ed 100644 --- a/src/ly_common.c +++ b/src/ly_common.c @@ -1803,25 +1803,29 @@ ly_strcat(char **dest, const char *format, ...) { va_list fp; char *addition = NULL; - size_t len; + size_t dest_len; + int add_len; va_start(fp, format); - len = vasprintf(&addition, format, fp); - len += (*dest ? strlen(*dest) : 0) + 1; + add_len = vasprintf(&addition, format, fp); + va_end(fp); + if (add_len == -1) { + return LY_EMEM; + } if (*dest) { - *dest = ly_realloc(*dest, len); + dest_len = strlen(*dest); + *dest = ly_realloc(*dest, dest_len + add_len + 1); if (!*dest) { - va_end(fp); + free(addition); return LY_EMEM; } - *dest = strcat(*dest, addition); + memcpy(*dest + dest_len, addition, add_len + 1); free(addition); } else { *dest = addition; } - va_end(fp); return LY_SUCCESS; } @@ -1992,8 +1996,20 @@ ly_append_str(char **str, uint32_t *size, uint32_t *used, const char *format, .. /* try to append the string */ p = vsnprintf(*str ? *str + *used : NULL, *size - *used, format, ap); + if (p < 0) { + va_end(ap); + LOGERR(NULL, LY_ESYS, "Failed to print a string (%s).", strerror(errno)); + return LY_ESYS; + } if ((unsigned)p >= *size - *used) { + /* the new size must fit into the size type */ + if ((unsigned)p >= UINT32_MAX - *size) { + va_end(ap); + LOGMEM(NULL); + return LY_EMEM; + } + /* realloc */ *str = ly_realloc(*str, *size + p + 1); LY_CHECK_ERR_RET(!*str, LOGMEM(NULL), LY_EMEM); diff --git a/src/ly_common.h b/src/ly_common.h index a8b7136d0..139ab3d6b 100644 --- a/src/ly_common.h +++ b/src/ly_common.h @@ -830,14 +830,16 @@ LY_ERR ly_val_get_quot(const struct ly_ctx *ctx, const char *value, char *quot); /** * @brief Append a string to a dynamic string variable. * - * @param[in,out] str String to use. + * The string is reallocated as needed so the caller never computes the length of the appended text. + * + * @param[in,out] str String to use, may be NULL (with @p size and @p used 0) to create a new string. * @param[in,out] size String size. - * @param[in,out] used String used size excluding terminating zero. If NULL, 0 is assumed. + * @param[in,out] used String used size excluding terminating zero. * @param[in] format Message format. * @param[in] ... Message format arguments. * @return LY_ERR value. */ -LY_ERR ly_append_str(char **str, uint32_t *size, uint32_t *used, const char *format, ...); +LY_ERR ly_append_str(char **str, uint32_t *size, uint32_t *used, const char *format, ...) _FORMAT_PRINTF(4, 5); /** * @brief mmap(2) wrapper to map input files into memory to unify parsing. diff --git a/src/plugins_types/bits.c b/src/plugins_types/bits.c index e5b6ff24c..6133cefe6 100644 --- a/src/plugins_types/bits.c +++ b/src/plugins_types/bits.c @@ -261,7 +261,7 @@ static LY_ERR bits_items2canon(struct lysc_type_bitenum_item **items, char **canonical) { char *ret; - uint32_t ret_len; + uint32_t ret_size, ret_len; LYA_COUNT_T u; *canonical = NULL; @@ -269,22 +269,12 @@ bits_items2canon(struct lysc_type_bitenum_item **items, char **canonical) /* init value */ ret = strdup(""); LY_CHECK_RET(!ret, LY_EMEM); + ret_size = 1; ret_len = 0; LYA_FOR(items, u) { - if (!ret_len) { - ret = ly_realloc(ret, strlen(items[u]->name) + 1); - LY_CHECK_RET(!ret, LY_EMEM); - strcpy(ret, items[u]->name); - - ret_len = strlen(ret); - } else { - ret = ly_realloc(ret, ret_len + 1 + strlen(items[u]->name) + 1); - LY_CHECK_RET(!ret, LY_EMEM); - sprintf(ret + ret_len, " %s", items[u]->name); - - ret_len += 1 + strlen(items[u]->name); - } + LY_CHECK_ERR_RET(ly_append_str(&ret, &ret_size, &ret_len, "%s%s", ret_len ? " " : "", items[u]->name), + free(ret), LY_EMEM); } *canonical = ret; diff --git a/src/plugins_types/identityref.c b/src/plugins_types/identityref.c index af2c2449f..84d62707a 100644 --- a/src/plugins_types/identityref.c +++ b/src/plugins_types/identityref.c @@ -148,7 +148,7 @@ identityref_check_base(const struct lysc_ident *ident, struct lysc_type_identity uint32_t value_size, struct ly_err_item **err) { LY_ERR ret; - uint32_t str_len; + uint32_t str_size, str_len; char *str; LYA_COUNT_T u; struct lysc_ident *base; @@ -164,12 +164,15 @@ identityref_check_base(const struct lysc_ident *ident, struct lysc_type_identity /* it does not, generate a nice error */ if (u == LYA_COUNT(type->bases)) { str = NULL; - str_len = 1; + str_size = 0; + str_len = 0; LYA_FOR(type->bases, u) { base = type->bases[u]; - str_len += (u ? 2 : 0) + 1 + strlen(base->module->name) + 1 + strlen(base->name) + 1; - str = ly_realloc(str, str_len); - sprintf(str + (u ? strlen(str) : 0), "%s\"%s:%s\"", u ? ", " : "", base->module->name, base->name); + if (ly_append_str(&str, &str_size, &str_len, "%s\"%s:%s\"", u ? ", " : "", base->module->name, + base->name)) { + free(str); + return ly_err_new_no_mem(err); + } } /* no match */ diff --git a/src/plugins_types/instanceid_keys.c b/src/plugins_types/instanceid_keys.c index ffecc2fcb..0b797d3e4 100644 --- a/src/plugins_types/instanceid_keys.c +++ b/src/plugins_types/instanceid_keys.c @@ -60,10 +60,9 @@ instanceid_keys_print_value(const struct lyd_value_instance_identifier_keys *val char **str_value, struct ly_err_item **err) { LY_ERR ret = LY_SUCCESS; - uint32_t cur_idx = 0, str_len = 0; + uint32_t cur_idx = 0, str_size = 0, str_len = 0; enum lyxp_token cur_tok; char *str_tok; - void *mem; const char *cur_exp_ptr; ly_bool is_nt; const struct lys_module *context_mod = NULL, *local_mod = NULL; @@ -89,27 +88,16 @@ instanceid_keys_print_value(const struct lyd_value_instance_identifier_keys *val val->ctx, val->format, val->prefix_data, format, prefix_data, &str_tok, err), cleanup); /* append the converted token */ - mem = realloc(*str_value, str_len + strlen(str_tok) + 1); - if (!mem) { - free(str_tok); - ret = ly_err_new_no_mem(err); - goto cleanup; - } - *str_value = mem; - str_len += sprintf(*str_value + str_len, "%s", str_tok); + ret = ly_append_str(str_value, &str_size, &str_len, "%s", str_tok); free(str_tok); + LY_CHECK_ERR_GOTO(ret, ret = ly_err_new_no_mem(err), cleanup); /* token processed */ ++cur_idx; } else { /* just copy the token */ - mem = realloc(*str_value, str_len + val->keys->tok_len[cur_idx] + 1); - if (!mem) { - ret = ly_err_new_no_mem(err); - goto cleanup; - } - *str_value = mem; - str_len += sprintf(*str_value + str_len, "%.*s", (int)val->keys->tok_len[cur_idx], cur_exp_ptr); + ret = ly_append_str(str_value, &str_size, &str_len, "%.*s", (int)val->keys->tok_len[cur_idx], cur_exp_ptr); + LY_CHECK_ERR_GOTO(ret, ret = ly_err_new_no_mem(err), cleanup); /* token processed */ ++cur_idx; diff --git a/src/plugins_types/ipv4_address.c b/src/plugins_types/ipv4_address.c index 03616759b..9d66d587e 100644 --- a/src/plugins_types/ipv4_address.c +++ b/src/plugins_types/ipv4_address.c @@ -251,7 +251,7 @@ lyplg_type_print_ipv4_address(const struct ly_ctx *ctx, const struct lyd_value * void *UNUSED(prefix_data), ly_bool *dynamic, uint64_t *value_size_bits) { struct lyd_value_ipv4_address *val; - uint32_t zone_len; + uint32_t zone_len, ret_size, ret_len; char *ret; LYD_VALUE_GET(value, val); @@ -285,7 +285,8 @@ lyplg_type_print_ipv4_address(const struct ly_ctx *ctx, const struct lyd_value * if (!value->_canonical) { /* '%' + zone */ zone_len = val->zone ? strlen(val->zone) + 1 : 0; - ret = malloc(INET_ADDRSTRLEN + zone_len); + ret_size = INET_ADDRSTRLEN + zone_len; + ret = malloc(ret_size); LY_CHECK_RET(!ret, NULL); /* get the address in string */ @@ -297,7 +298,8 @@ lyplg_type_print_ipv4_address(const struct ly_ctx *ctx, const struct lyd_value * /* add zone */ if (zone_len) { - sprintf(ret + strlen(ret), "%%%s", val->zone); + ret_len = strlen(ret); + snprintf(ret + ret_len, ret_size - ret_len, "%%%s", val->zone); } /* store it */ diff --git a/src/plugins_types/ipv6_address.c b/src/plugins_types/ipv6_address.c index b54c74a27..842e24b9f 100644 --- a/src/plugins_types/ipv6_address.c +++ b/src/plugins_types/ipv6_address.c @@ -253,7 +253,7 @@ lyplg_type_print_ipv6_address(const struct ly_ctx *ctx, const struct lyd_value * void *UNUSED(prefix_data), ly_bool *dynamic, uint64_t *value_size_bits) { struct lyd_value_ipv6_address *val; - uint32_t zone_len; + uint32_t zone_len, ret_size, ret_len; char *ret; LYD_VALUE_GET(value, val); @@ -287,7 +287,8 @@ lyplg_type_print_ipv6_address(const struct ly_ctx *ctx, const struct lyd_value * if (!value->_canonical) { /* '%' + zone */ zone_len = val->zone ? strlen(val->zone) + 1 : 0; - ret = malloc(INET6_ADDRSTRLEN + zone_len); + ret_size = INET6_ADDRSTRLEN + zone_len; + ret = malloc(ret_size); LY_CHECK_RET(!ret, NULL); /* get the address in string */ @@ -299,7 +300,8 @@ lyplg_type_print_ipv6_address(const struct ly_ctx *ctx, const struct lyd_value * /* add zone */ if (zone_len) { - sprintf(ret + strlen(ret), "%%%s", val->zone); + ret_len = strlen(ret); + snprintf(ret + ret_len, ret_size - ret_len, "%%%s", val->zone); } /* store it */ diff --git a/src/plugins_types/union.c b/src/plugins_types/union.c index b9835ecef..4906eb60d 100644 --- a/src/plugins_types/union.c +++ b/src/plugins_types/union.c @@ -433,12 +433,12 @@ union_find_type(const struct ly_ctx *ctx, struct lysc_type_union *type_u, struct uint32_t options, const struct lyd_node *ctx_node, const struct lyd_node *tree, uint32_t *type_idx, struct lys_glob_unres *unres, struct ly_err_item **err) { - LY_ERR rc = LY_SUCCESS; + LY_ERR rc = LY_SUCCESS, r; LYA_COUNT_T u; struct ly_err_item **errs = NULL, *e; uint32_t *prev_lo, temp_lo = 0; char *msg = NULL, *err_app_tag = NULL; - int msg_len = 0; + uint32_t msg_size = 0, msg_len = 0; ly_bool use_err_app_tag = 0; struct lyplg_type *type_plg; @@ -465,16 +465,12 @@ union_find_type(const struct ly_ctx *ctx, struct lysc_type_union *type_u, struct if (u == LYA_COUNT(type_u->types)) { /* create the full error */ if (subvalue->format == LY_VALUE_LYB) { - msg_len = asprintf(&msg, "Invalid LYB union value - no matching subtype found:\n"); + r = ly_append_str(&msg, &msg_size, &msg_len, "Invalid LYB union value - no matching subtype found:\n"); } else { - msg_len = asprintf(&msg, "Invalid union value \"%.*s\" - no matching subtype found:\n", + r = ly_append_str(&msg, &msg_size, &msg_len, "Invalid union value \"%.*s\" - no matching subtype found:\n", (int)subvalue->orig_size_bits / 8, (char *)subvalue->original); } - if (msg_len == -1) { - LY_CHECK_ERR_GOTO(!errs, rc = LY_EMEM, cleanup); - /* for further actions in function msg_len is just 0 */ - msg_len = 0; - } + LY_CHECK_ERR_GOTO(r, rc = LY_EMEM, cleanup); LYA_FOR(type_u->types, u) { if (!errs[u]) { /* no error for some reason */ @@ -494,9 +490,8 @@ union_find_type(const struct ly_ctx *ctx, struct lysc_type_union *type_u, struct type_plg = LYSC_GET_TYPE_PLG(type_u->types[u]->plugin_ref); - msg = ly_realloc(msg, msg_len + 4 + strlen(type_plg->id) + 2 + strlen(errs[u]->msg) + 2); - LY_CHECK_ERR_GOTO(!msg, rc = LY_EMEM, cleanup); - msg_len += sprintf(msg + msg_len, " %s: %s\n", type_plg->id, errs[u]->msg); + r = ly_append_str(&msg, &msg_size, &msg_len, " %s: %s\n", type_plg->id, errs[u]->msg); + LY_CHECK_ERR_GOTO(r, rc = LY_EMEM, cleanup); } if (!use_err_app_tag) { diff --git a/src/plugins_types/xpath1.0.c b/src/plugins_types/xpath1.0.c index 843f3926e..d5caebbdc 100644 --- a/src/plugins_types/xpath1.0.c +++ b/src/plugins_types/xpath1.0.c @@ -45,8 +45,7 @@ lyplg_type_xpath10_print_token(const char *token, uint16_t tok_len, ly_bool is_n const char *str_begin, *str_next, *prefix; ly_bool is_prefix, has_prefix = 0; char *str = NULL; - void *mem; - uint32_t len, str_len = 0, pref_len; + uint32_t len, str_size = 0, str_len = 0, pref_len; const struct lys_module *mod; str_begin = token; @@ -59,17 +58,13 @@ lyplg_type_xpath10_print_token(const char *token, uint16_t tok_len, ly_bool is_n assert(prefix); /* append the nametest and prefix */ - mem = realloc(str, str_len + strlen(prefix) + 1 + len + 1); - LY_CHECK_ERR_GOTO(!mem, ret = ly_err_new_no_mem(err), cleanup); - str = mem; - str_len += sprintf(str + str_len, "%s:%.*s", prefix, (int)len, str_begin); + LY_CHECK_ERR_GOTO(ly_append_str(&str, &str_size, &str_len, "%s:%.*s", prefix, (int)len, str_begin), + ret = ly_err_new_no_mem(err), cleanup); } else { /* just append the string, we may get the first expression node without a prefix but since this * is not strictly forbidden, allow it */ - mem = realloc(str, str_len + len + 1); - LY_CHECK_ERR_GOTO(!mem, ret = ly_err_new_no_mem(err), cleanup); - str = mem; - str_len += sprintf(str + str_len, "%.*s", (int)len, str_begin); + LY_CHECK_ERR_GOTO(ly_append_str(&str, &str_size, &str_len, "%.*s", (int)len, str_begin), + ret = ly_err_new_no_mem(err), cleanup); } } else { /* remember there was a prefix found */ @@ -98,10 +93,8 @@ lyplg_type_xpath10_print_token(const char *token, uint16_t tok_len, ly_bool is_n } /* append the prefix */ - mem = realloc(str, str_len + pref_len + 2); - LY_CHECK_ERR_GOTO(!mem, ret = ly_err_new_no_mem(err), cleanup); - str = mem; - str_len += sprintf(str + str_len, "%.*s:", (int)pref_len, prefix); + LY_CHECK_ERR_GOTO(ly_append_str(&str, &str_size, &str_len, "%.*s:", (int)pref_len, prefix), + ret = ly_err_new_no_mem(err), cleanup); } if (is_nametest) { @@ -133,6 +126,7 @@ lyplg_type_xpath10_print_token(const char *token, uint16_t tok_len, ly_bool is_n * @param[in] format Format to print in. * @param[in] prefix_data Format-specific prefix data. * @param[in,out] str_value Printed value, appended to. + * @param[in,out] str_size Allocated size of @p str_value, updated. * @param[in,out] str_len Length of @p str_value, updated. * @param[out] err Error structure on error. * @return LY_ERR value. @@ -140,11 +134,11 @@ lyplg_type_xpath10_print_token(const char *token, uint16_t tok_len, ly_bool is_n static LY_ERR xpath10_print_subexpr_r(uint16_t *cur_idx, enum lyxp_token end_tok, const struct lys_module *context_mod, const struct lyd_value_xpath10 *xp_val, LY_VALUE_FORMAT format, void *prefix_data, char **str_value, - uint32_t *str_len, struct ly_err_item **err) + uint32_t *str_size, uint32_t *str_len, struct ly_err_item **err) { + LY_ERR r; enum lyxp_token cur_tok, sub_end_tok; char *str_tok; - void *mem; const char *cur_exp_ptr; ly_bool is_nt; const struct lys_module *orig_context_mod = context_mod; @@ -160,30 +154,24 @@ xpath10_print_subexpr_r(uint16_t *cur_idx, enum lyxp_token end_tok, const struct xp_val->ctx, xp_val->format, xp_val->prefix_data, format, prefix_data, &str_tok, err)); /* append the converted token */ - mem = realloc(*str_value, *str_len + strlen(str_tok) + 1); - LY_CHECK_ERR_GOTO(!mem, free(str_tok), error_mem); - *str_value = mem; - *str_len += sprintf(*str_value + *str_len, "%s", str_tok); + r = ly_append_str(str_value, str_size, str_len, "%s", str_tok); free(str_tok); + LY_CHECK_GOTO(r, error_mem); /* token processed */ ++(*cur_idx); } else { if ((cur_tok == LYXP_TOKEN_OPER_LOG) || (cur_tok == LYXP_TOKEN_OPER_UNI) || (cur_tok == LYXP_TOKEN_OPER_MATH)) { /* copy the token with spaces around */ - mem = realloc(*str_value, *str_len + 1 + xp_val->exp->tok_len[*cur_idx] + 2); - LY_CHECK_GOTO(!mem, error_mem); - *str_value = mem; - *str_len += sprintf(*str_value + *str_len, " %.*s ", (int)xp_val->exp->tok_len[*cur_idx], cur_exp_ptr); + LY_CHECK_GOTO(ly_append_str(str_value, str_size, str_len, " %.*s ", (int)xp_val->exp->tok_len[*cur_idx], + cur_exp_ptr), error_mem); /* reset context mod */ context_mod = orig_context_mod; } else { /* just copy the token */ - mem = realloc(*str_value, *str_len + xp_val->exp->tok_len[*cur_idx] + 1); - LY_CHECK_GOTO(!mem, error_mem); - *str_value = mem; - *str_len += sprintf(*str_value + *str_len, "%.*s", (int)xp_val->exp->tok_len[*cur_idx], cur_exp_ptr); + LY_CHECK_GOTO(ly_append_str(str_value, str_size, str_len, "%.*s", (int)xp_val->exp->tok_len[*cur_idx], + cur_exp_ptr), error_mem); } /* token processed but keep it in cur_tok */ @@ -197,7 +185,7 @@ xpath10_print_subexpr_r(uint16_t *cur_idx, enum lyxp_token end_tok, const struct /* parse the subexpression separately, use the current context mod */ LY_CHECK_RET(xpath10_print_subexpr_r(cur_idx, sub_end_tok, context_mod, xp_val, format, prefix_data, - str_value, str_len, err)); + str_value, str_size, str_len, err)); } } } @@ -214,7 +202,7 @@ lyplg_type_print_xpath10_value(const struct lyd_value_xpath10 *xp_val, LY_VALUE_ { LY_ERR ret = LY_SUCCESS; uint16_t expr_idx = 0; - uint32_t str_len = 0; + uint32_t str_size = 0, str_len = 0; const struct lys_module *local_mod = NULL; struct ly_set *mods; @@ -243,7 +231,7 @@ lyplg_type_print_xpath10_value(const struct lyd_value_xpath10 *xp_val, LY_VALUE_ } /* recursively print the expression */ - ret = xpath10_print_subexpr_r(&expr_idx, 0, NULL, xp_val, format, prefix_data, str_value, &str_len, err); + ret = xpath10_print_subexpr_r(&expr_idx, 0, NULL, xp_val, format, prefix_data, str_value, &str_size, &str_len, err); if (local_mod) { mods->objs[0] = (void *)local_mod; diff --git a/tests/utests/types/identityref.c b/tests/utests/types/identityref.c index a62fbd4b4..83738332c 100644 --- a/tests/utests/types/identityref.c +++ b/tests/utests/types/identityref.c @@ -76,7 +76,9 @@ test_data_xml(void **state) " prefix d;" " import ident-base {prefix ib;}" " identity ident1 {base ib:ident-base;}" + " identity ident2 {base ident1;}" " leaf l1 {type identityref {base ib:ident-base;}}" + " leaf l2 {type identityref {base ib:ident-base; base ident1;}}" "}"; UTEST_ADD_MODULE(schema2, LYS_IN_YANG, NULL, NULL); @@ -109,6 +111,16 @@ test_data_xml(void **state) TEST_ERROR_XML("defs", "xmlns:x=\"urn:tests:unknown\"", "l1", "x:ident-base"); CHECK_LOG_CTX("Invalid identityref \"x:ident-base\" value - unable to map prefix to YANG schema.", "/defs:l1", 1); + + /* multiple bases */ + data = "ident2"; + CHECK_PARSE_LYD_PARAM(data, LYD_XML, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree); + CHECK_LYD_STRING_PARAM(tree, "{\"defs:l2\":\"ident2\"}", LYD_JSON, LYD_PRINT_SHRINK); + lyd_free_all(tree); + + TEST_ERROR_XML("defs", "xmlns:x=\"urn:tests:ident-base\"", "l2", "x:ident-base"); + CHECK_LOG_CTX("Invalid identityref \"x:ident-base\" value - identity not derived from all the bases " + "\"ident-base:ident-base\", \"defs:ident1\".", "/defs:l2", 1); } static void