Skip to content
Open
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
6 changes: 3 additions & 3 deletions src/parser_cbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ lydcbor_item_to_string(const struct ly_ctx *ctx, const cbor_item_t *item, char *
}
*str_val = malloc(len + 1);
LY_CHECK_ERR_RET(!*str_val, LOGMEM(ctx), LY_EMEM);
sprintf(*str_val, "%" PRIu64, val);
snprintf(*str_val, len + 1, "%" PRIu64, val);
*str_len = len;
break;
}
Expand All @@ -468,7 +468,7 @@ lydcbor_item_to_string(const struct ly_ctx *ctx, const cbor_item_t *item, char *
}
*str_val = malloc(len + 1);
LY_CHECK_ERR_RET(!*str_val, LOGMEM(ctx), LY_EMEM);
sprintf(*str_val, "-%" PRIu64, val + 1);
snprintf(*str_val, len + 1, "-%" PRIu64, val + 1);
*str_len = len;
break;
}
Expand Down Expand Up @@ -526,7 +526,7 @@ lydcbor_item_to_string(const struct ly_ctx *ctx, const cbor_item_t *item, char *
}
*str_val = malloc(len + 1);
LY_CHECK_ERR_RET(!*str_val, LOGMEM(ctx), LY_EMEM);
sprintf(*str_val, "%g", val);
snprintf(*str_val, len + 1, "%g", val);
*str_len = len;
}
break;
Expand Down
5 changes: 2 additions & 3 deletions src/parser_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ lys_parser_fill_filepath(struct ly_ctx *ctx, struct ly_in *in, const char **file
}
#else
/* get URI if there is /proc */
sprintf(proc_path, "/proc/self/fd/%d", in->method.fd);
snprintf(proc_path, sizeof proc_path, "/proc/self/fd/%d", in->method.fd);
if ((len = readlink(proc_path, path, PATH_MAX - 1)) > 0) {
LY_CHECK_RET(lysdict_insert(ctx, path, len, filepath));
}
Expand Down Expand Up @@ -1355,9 +1355,8 @@ lysp_stmt_type_pattern_modifier(struct lysp_ctx *ctx, const struct lysp_stmt *st
}

/* replace the value in the dictionary */
buf = malloc(strlen(*pat) + 1);
buf = strdup(*pat);
LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
strcpy(buf, *pat);
lysdict_remove(PARSER_CTX(ctx), *pat);

assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
Expand Down
43 changes: 10 additions & 33 deletions src/parser_lyb.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ lyb_check_mod_features(struct lyd_lyb_ctx *lybctx, const char *mod_name, const s
LY_ERR rc = LY_SUCCESS;
struct lylyb_parse_ctx *pctx = lybctx->parse_ctx;
char *feature_name = NULL, **features = NULL, *en_feats_printer = NULL, *en_feats_parser = NULL;
uint32_t i, feature_count = 0, ctx_feature_count, len;
uint32_t i, feature_count = 0, ctx_feature_count, size = 0, used = 0;

/* read feature count */
LY_CHECK_GOTO(rc = lyb_read_count(&feature_count, pctx), cleanup);
Expand Down Expand Up @@ -461,47 +461,23 @@ lyb_check_mod_features(struct lyd_lyb_ctx *lybctx, const char *mod_name, const s
}

/* feature mismatch, prepare error message, unpack enabled features for printer */
len = 0;
for (i = 0; i < feature_count; i++) {
if ((uint64_t)len + strlen(features[i]) + 2 > UINT32_MAX - 1) {
LOGERR(pctx->ctx, LY_EINVAL, "Invalid enabled features of module \"%s\", too long.", mod_name);
rc = LY_EDENIED;
goto cleanup;
}
len += strlen(features[i]) + 2;
}
en_feats_printer = calloc(1, len + 1);
LY_CHECK_ERR_GOTO(!en_feats_printer, LOGMEM(pctx->ctx), cleanup);
for (i = 0; i < feature_count; i++) {
strcat(en_feats_printer, features[i]);
if (i + 1 < feature_count) {
strcat(en_feats_printer, ", ");
}
LY_CHECK_GOTO(rc = ly_append_str(&en_feats_printer, &size, &used, "%s%s", i ? ", " : "", features[i]),
cleanup);
}

/* unpack enabled features for parser */
len = 0;
size = 0;
used = 0;
ctx_feature_count = LYA_COUNT(mod->compiled->features);
for (i = 0; i < ctx_feature_count; i++) {
if ((uint64_t)len + strlen(mod->compiled->features[i]) + 2 > UINT32_MAX - 1) {
LOGINT(pctx->ctx);
rc = LY_EINT;
goto cleanup;
}
len += strlen(mod->compiled->features[i]) + 2;
}
en_feats_parser = calloc(1, len + 1);
LY_CHECK_ERR_GOTO(!en_feats_parser, LOGMEM(pctx->ctx), cleanup);
for (i = 0; i < ctx_feature_count; i++) {
strcat(en_feats_parser, mod->compiled->features[i]);
if (i + 1 < ctx_feature_count) {
strcat(en_feats_parser, ", ");
}
LY_CHECK_GOTO(rc = ly_append_str(&en_feats_parser, &size, &used, "%s%s", i ? ", " : "",
mod->compiled->features[i]), cleanup);
}

LOGERR(pctx->ctx, LY_EDENIED,
"Invalid context for LYB data parsing, module \"%s\" enabled feature mismatch (printer: {%s}, parser: {%s}).",
mod_name, en_feats_printer, en_feats_parser);
mod_name, en_feats_printer ? en_feats_printer : "", en_feats_parser ? en_feats_parser : "");
rc = LY_EDENIED;

cleanup:
Expand Down Expand Up @@ -546,7 +522,8 @@ lyb_parse_module(struct lyd_lyb_ctx *lybctx, const struct lys_module **mod)
/* module revision */
LY_CHECK_GOTO(rc = lyb_read(&rev, 2 * 8, pctx), cleanup);
if (rev) {
sprintf(mod_rev, "%04u-%02u-%02u", ((rev & LYB_REV_YEAR_MASK) >> LYB_REV_YEAR_SHIFT) + LYB_REV_YEAR_OFFSET,
snprintf(mod_rev, sizeof mod_rev, "%04u-%02u-%02u",
((rev & LYB_REV_YEAR_MASK) >> LYB_REV_YEAR_SHIFT) + LYB_REV_YEAR_OFFSET,
(rev & LYB_REV_MONTH_MASK) >> LYB_REV_MONTH_SHIFT, rev & LYB_REV_DAY_MASK);
}

Expand Down
85 changes: 35 additions & 50 deletions src/tree_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdint.h>
Expand Down Expand Up @@ -54,6 +55,8 @@
#include "xpath.h"

static int lyd_insert_has_keys(const struct lyd_node *list);
static LY_ERR lyd_path_str_append(char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static,
const char *format, ...) _FORMAT_PRINTF(5, 6);

static LY_ERR lyd_compare_siblings_(const struct lyd_node *node1, const struct lyd_node *node2, uint32_t options,
ly_bool parental_schemas_checked);
Expand Down Expand Up @@ -2936,42 +2939,53 @@ lyd_merge_module(struct lyd_node **target, const struct lyd_node *source, const
}

static LY_ERR
lyd_path_str_enlarge(char **buffer, size_t *buflen, size_t reqlen, ly_bool is_static)
lyd_path_str_append(char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static, const char *format, ...)
{
/* ending \0 */
++reqlen;
va_list ap;
int len;

if (reqlen > *buflen) {
va_start(ap, format);
len = vsnprintf(NULL, 0, format, ap);
va_end(ap);
if (len < 0) {
LOGERR(NULL, LY_ESYS, "Failed to print a path (%s).", strerror(errno));
return LY_ESYS;
}

/* grow the buffer if it is not static, +1 for the terminating zero */
if (*bufused + len + 1 > *buflen) {
if (is_static) {
return LY_EINCOMPLETE;
}

*buffer = ly_realloc(*buffer, reqlen * sizeof **buffer);
*buffer = ly_realloc(*buffer, (*bufused + len + 1) * sizeof **buffer);
if (!*buffer) {
return LY_EMEM;
}

*buflen = reqlen;
*buflen = *bufused + len + 1;
}

va_start(ap, format);
vsnprintf(*buffer + *bufused, *buflen - *bufused, format, ap);
va_end(ap);
*bufused += len;

return LY_SUCCESS;
}

LY_ERR
lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static)
{
const struct lyd_node *key;
size_t len;
const char *val;
char quot;

for (key = lyd_child(node); key && key->schema && (key->schema->flags & LYS_KEY); key = key->next) {
val = lyd_get_value(key);
len = 1 + strlen(key->schema->name) + 2 + strlen(val) + 2;
LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));

LY_CHECK_RET(ly_val_get_quot(LYD_CTX(node), val, &quot));
*bufused += sprintf(*buffer + *bufused, "[%s=%c%s%c]", key->schema->name, quot, val, quot);
LY_CHECK_RET(lyd_path_str_append(buffer, buflen, bufused, is_static, "[%s=%c%s%c]", key->schema->name, quot,
val, quot));
}

return LY_SUCCESS;
Expand All @@ -2990,16 +3004,12 @@ lyd_path_list_predicate(const struct lyd_node *node, char **buffer, size_t *bufl
static LY_ERR
lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static)
{
size_t len;
const char *val;
char quot;

val = lyd_get_value(node);
len = 4 + strlen(val) + 2; /* "[.='" + val + "']" */
LY_CHECK_RET(lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static));

LY_CHECK_RET(ly_val_get_quot(LYD_CTX(node), val, &quot));
*bufused += sprintf(*buffer + *bufused, "[.=%c%s%c]", quot, val, quot);
LY_CHECK_RET(lyd_path_str_append(buffer, buflen, bufused, is_static, "[.=%c%s%c]", quot, val, quot));

return LY_SUCCESS;
}
Expand All @@ -3017,35 +3027,18 @@ lyd_path_leaflist_predicate(const struct lyd_node *node, char **buffer, size_t *
static LY_ERR
lyd_path_position_predicate(const struct lyd_node *node, char **buffer, size_t *buflen, size_t *bufused, ly_bool is_static)
{
size_t len;
uint32_t pos;
char *val = NULL;
LY_ERR rc;

pos = lyd_list_pos(node);
if (asprintf(&val, "%" PRIu32, pos) == -1) {
return LY_EMEM;
}

len = 1 + strlen(val) + 1;
rc = lyd_path_str_enlarge(buffer, buflen, *bufused + len, is_static);
if (rc != LY_SUCCESS) {
goto cleanup;
}

*bufused += sprintf(*buffer + *bufused, "[%s]", val);

cleanup:
free(val);
return rc;
return lyd_path_str_append(buffer, buflen, bufused, is_static, "[%" PRIu32 "]", pos);
}

LIBYANG_API_DEF char *
lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size_t buflen)
{
ly_bool is_static = 0;
uint32_t i, depth;
size_t bufused = 0, len;
size_t bufused = 0;
const struct lyd_node *iter, *parent;
const struct lys_module *mod, *prev_mod;
LY_ERR rc = LY_SUCCESS;
Expand Down Expand Up @@ -3079,17 +3072,13 @@ lyd_path(const struct lyd_node *node, LYD_PATH_TYPE pathtype, char *buffer, size
mod = NULL;
}

/* realloc string */
len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
strlen(((struct lyd_node_opaq *)iter)->name.name));
rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, is_static);
/* print next node */
rc = lyd_path_str_append(&buffer, &buflen, &bufused, is_static, "/%s%s%s", mod ? mod->name : "",
mod ? ":" : "", LYD_NAME(iter));
if (rc) {
break;
}

/* print next node */
bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));

/* do not always print the last (first) predicate */
if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
switch (iter->schema->nodetype) {
Expand Down Expand Up @@ -3136,7 +3125,7 @@ char *
lyd_path_set(const struct ly_set *dnodes, LYD_PATH_TYPE pathtype)
{
uint32_t depth;
size_t bufused = 0, buflen = 0, len;
size_t bufused = 0, buflen = 0;
char *buffer = NULL;
const struct lyd_node *iter, *parent;
const struct lys_module *mod, *prev_mod;
Expand All @@ -3162,16 +3151,12 @@ lyd_path_set(const struct ly_set *dnodes, LYD_PATH_TYPE pathtype)
mod = NULL;
}

/* realloc string */
len = 1 + (mod ? strlen(mod->name) + 1 : 0) + (iter->schema ? strlen(iter->schema->name) :
strlen(((struct lyd_node_opaq *)iter)->name.name));
if ((rc = lyd_path_str_enlarge(&buffer, &buflen, bufused + len, 0))) {
/* print next node */
if ((rc = lyd_path_str_append(&buffer, &buflen, &bufused, 0, "/%s%s%s", mod ? mod->name : "",
mod ? ":" : "", LYD_NAME(iter)))) {
break;
}

/* print next node */
bufused += sprintf(buffer + bufused, "/%s%s%s", mod ? mod->name : "", mod ? ":" : "", LYD_NAME(iter));

/* do not always print the last (first) predicate */
if (iter->schema && ((depth > 1) || (pathtype == LYD_PATH_STD))) {
switch (iter->schema->nodetype) {
Expand Down
Loading
Loading