Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/ly_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
8 changes: 5 additions & 3 deletions src/ly_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 4 additions & 14 deletions src/plugins_types/bits.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,30 +261,20 @@ 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;

/* 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;
Expand Down
13 changes: 8 additions & 5 deletions src/plugins_types/identityref.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 */
Expand Down
22 changes: 5 additions & 17 deletions src/plugins_types/instanceid_keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions src/plugins_types/ipv4_address.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 */
Expand All @@ -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 */
Expand Down
8 changes: 5 additions & 3 deletions src/plugins_types/ipv6_address.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 */
Expand All @@ -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 */
Expand Down
19 changes: 7 additions & 12 deletions src/plugins_types/union.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 */
Expand All @@ -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) {
Expand Down
Loading
Loading