Skip to content

Commit

Permalink
libyang BUGFIX type size problems
Browse files Browse the repository at this point in the history
  • Loading branch information
michalvasko committed Feb 20, 2024
1 parent 4294833 commit 21eaa39
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 48 deletions.
2 changes: 1 addition & 1 deletion compat/compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ getline(char **lineptr, size_t *n, FILE *stream)
written = 0;
while (fgets(chunk, sizeof(chunk), stream)) {
len = strlen(chunk);
if (written + len > *n) {
if ((size_t)(written + len) > *n) {
ptr = realloc(*lineptr, *n + sizeof(chunk));
if (!ptr) {
return -1;
Expand Down
2 changes: 1 addition & 1 deletion src/dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ lydict_clean(struct ly_dict *dict)
* before calling lydict_clean()
*/
dict_rec = (struct ly_dict_rec *)rec->val;
LOGWRN(NULL, "String \"%s\" not freed from the dictionary, refcount %d", dict_rec->value, dict_rec->refcount);
LOGWRN(NULL, "String \"%s\" not freed from the dictionary, refcount %" PRIu32 ".", dict_rec->value, dict_rec->refcount);
/* if record wasn't removed before free string allocated for that record */
#ifdef NDEBUG
free(dict_rec->value);
Expand Down
6 changes: 3 additions & 3 deletions src/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ lyjson_string(struct lyjson_ctx *jsonctx)

offset += i; /* add read escaped characters */
LY_CHECK_ERR_GOTO(ly_pututf8(&buf[len], value, &u),
LOGVAL(jsonctx->ctx, LYVE_SYNTAX, "Invalid character reference \"%.*s\" (0x%08x).",
LOGVAL(jsonctx->ctx, LYVE_SYNTAX, "Invalid character reference \"%.*s\" (0x%08" PRIx32 ").",
(int)(&in[offset] - c), c, value),
error);
len += u; /* update number of bytes in buffer */
Expand Down Expand Up @@ -271,7 +271,7 @@ lyjson_string(struct lyjson_ctx *jsonctx)
LOGVAL(jsonctx->ctx, LY_VCODE_INCHAR, in[offset]), error);

LY_CHECK_ERR_GOTO(!is_jsonstrchar(value),
LOGVAL(jsonctx->ctx, LYVE_SYNTAX, "Invalid character in JSON string \"%.*s\" (0x%08x).",
LOGVAL(jsonctx->ctx, LYVE_SYNTAX, "Invalid character in JSON string \"%.*s\" (0x%08" PRIx32 ").",
(int)(&in[offset] - start + u), start, value),
error);

Expand Down Expand Up @@ -354,7 +354,7 @@ lyjson_number_is_zero(const char *in, const char *end)
}
}

return lyjson_count_in_row(in, end, '0', 0) == end - in;
return lyjson_count_in_row(in, end, '0', 0) == (uint32_t)(end - in);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/parser_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -761,8 +761,8 @@ lydjson_metadata(struct lyd_json_ctx *lydctx, const struct lysc_node *snode, str
LY_CHECK_GOTO((status != LYJSON_OBJECT) && (status != LYJSON_NULL), representation_error);

if (!node || (node->schema != prev->schema)) {
LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance #%u of %s:%s to be coupled with metadata.",
instance, prev->schema->module->name, prev->schema->name);
LOGVAL(lydctx->jsonctx->ctx, LYVE_REFERENCE, "Missing JSON data instance #%" PRIu32
" of %s:%s to be coupled with metadata.", instance, prev->schema->module->name, prev->schema->name);
rc = LY_EVALID;
goto cleanup;
}
Expand Down
2 changes: 1 addition & 1 deletion src/parser_xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ lydxml_get_hints_opaq(const char *name, size_t name_len, const char *value, size
if ((unsigned)(ptr - value) == value_len) {
/* number value */
*hints |= LYD_VALHINT_DECNUM;
if ((num < INT32_MIN) || (num > UINT32_MAX)) {
if ((num < INT32_MIN) || (num > (long)UINT32_MAX)) {
/* large number */
*hints |= LYD_VALHINT_NUM64;
}
Expand Down
14 changes: 7 additions & 7 deletions src/path.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ ly_path_check_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_no
/* check prefix based on the options */
name = strnstr(exp->expr + exp->tok_pos[*tok_idx], ":", exp->tok_len[*tok_idx]);
if ((prefix == LY_PATH_PREFIX_MANDATORY) && !name) {
LOGVAL(ctx, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", exp->tok_len[*tok_idx],
LOGVAL(ctx, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", (int)exp->tok_len[*tok_idx],
exp->expr + exp->tok_pos[*tok_idx]);
goto token_error;
} else if ((prefix == LY_PATH_PREFIX_STRICT_INHERIT) && name) {
LOGVAL(ctx, LYVE_XPATH, "Redundant prefix for \"%.*s\" in path.", exp->tok_len[*tok_idx],
LOGVAL(ctx, LYVE_XPATH, "Redundant prefix for \"%.*s\" in path.", (int)exp->tok_len[*tok_idx],
exp->expr + exp->tok_pos[*tok_idx]);
goto token_error;
}
Expand Down Expand Up @@ -207,7 +207,7 @@ ly_path_check_predicate(const struct ly_ctx *ctx, const struct lysc_node *cur_no
if ((exp->tok_len[*tok_idx] != ly_strlen_const("current")) ||
strncmp(exp->expr + exp->tok_pos[*tok_idx], "current", ly_strlen_const("current"))) {
LOGVAL(ctx, LYVE_XPATH, "Invalid function \"%.*s\" invocation in path.",
exp->tok_len[*tok_idx], exp->expr + exp->tok_pos[*tok_idx]);
(int)exp->tok_len[*tok_idx], exp->expr + exp->tok_pos[*tok_idx]);
goto token_error;
}
++(*tok_idx);
Expand Down Expand Up @@ -285,7 +285,7 @@ ly_path_parse_deref(const struct ly_ctx *ctx, const struct lysc_node *ctx_node,
LY_CHECK_RET(lyxp_next_token(ctx, exp, tok_idx, LYXP_TOKEN_FUNCNAME), LY_EVALID);
if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
LOGVAL(ctx, LYVE_XPATH, "Unexpected XPath function \"%.*s\" in path, expected \"deref(...)\"",
exp->tok_len[*tok_idx], exp->expr + exp->tok_pos[*tok_idx]);
(int)exp->tok_len[*tok_idx], exp->expr + exp->tok_pos[*tok_idx]);
return LY_EVALID;
}

Expand Down Expand Up @@ -390,15 +390,15 @@ ly_path_parse(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const
cur_len = exp->tok_len[tok_idx];
if (prefix == LY_PATH_PREFIX_MANDATORY) {
if (!strnstr(cur_node, ":", cur_len)) {
LOGVAL(ctx, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", cur_len, cur_node);
LOGVAL(ctx, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", (int)cur_len, cur_node);
ret = LY_EVALID;
goto error;
}
} else if ((prefix == LY_PATH_PREFIX_FIRST) || (prefix == LY_PATH_PREFIX_STRICT_INHERIT)) {
if (!prev_prefix && is_abs) {
/* the first node must have a prefix */
if (!strnstr(cur_node, ":", cur_len)) {
LOGVAL(ctx, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", cur_len, cur_node);
LOGVAL(ctx, LYVE_XPATH, "Prefix missing for \"%.*s\" in path.", (int)cur_len, cur_node);
ret = LY_EVALID;
goto error;
}
Expand All @@ -410,7 +410,7 @@ ly_path_parse(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const
ptr = strnstr(cur_node, ":", cur_len);
if (ptr) {
if (!strncmp(prev_prefix, cur_node, ptr - cur_node) && (prev_prefix[ptr - cur_node] == ':')) {
LOGVAL(ctx, LYVE_XPATH, "Duplicate prefix for \"%.*s\" in path.", cur_len, cur_node);
LOGVAL(ctx, LYVE_XPATH, "Duplicate prefix for \"%.*s\" in path.", (int)cur_len, cur_node);
ret = LY_EVALID;
goto error;
}
Expand Down
7 changes: 4 additions & 3 deletions src/plugins_types/xpath1.0.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ lyplg_type_xpath10_print_token(const char *token, uint16_t tok_len, ly_bool is_n
mem = realloc(str, str_len + strlen(prefix) + 1 + len + 1);
LY_CHECK_ERR_GOTO(!mem, ret = ly_err_new(err, LY_EMEM, LYVE_DATA, NULL, NULL, "No memory."), cleanup);
str = mem;
str_len += sprintf(str + str_len, "%s:%.*s", prefix, len, str_begin);
str_len += sprintf(str + str_len, "%s:%.*s", prefix, (int)len, str_begin);
} 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(err, LY_EMEM, LYVE_DATA, NULL, NULL, "No memory."), cleanup);
str = mem;
str_len += sprintf(str + str_len, "%.*s", len, str_begin);
str_len += sprintf(str + str_len, "%.*s", (int)len, str_begin);
}
} else {
/* remember there was a prefix found */
Expand All @@ -79,7 +79,8 @@ lyplg_type_xpath10_print_token(const char *token, uint16_t tok_len, ly_bool is_n
/* resolve the module in the original format */
mod = lyplg_type_identity_module(resolve_ctx, NULL, str_begin, len, resolve_format, resolve_prefix_data);
if (!mod && is_nametest) {
ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to resolve prefix \"%.*s\".", len, str_begin);
ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to resolve prefix \"%.*s\".",
(int)len, str_begin);
goto cleanup;
}

Expand Down
2 changes: 1 addition & 1 deletion src/printer_lyb.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ lyb_write_string(const char *str, size_t str_len, uint8_t len_size, struct ly_ou
error = str_len > UINT32_MAX;
break;
case sizeof(uint64_t):
error = str_len > UINT64_MAX;
error = 0;
break;
default:
error = 1;
Expand Down
11 changes: 6 additions & 5 deletions src/schema_compile_amend.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ lys_nodeid_mod_check(struct lysc_ctx *ctx, const char *str, ly_bool abs, struct
/* descendant schema nodeid */
if (e->tokens[0] != LYXP_TOKEN_NAMETEST) {
LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".",
nodeid_type, str, e->tok_len[0], e->expr + e->tok_pos[0]);
nodeid_type, str, (int)e->tok_len[0], e->expr + e->tok_pos[0]);
ret = LY_EVALID;
goto cleanup;
}
Expand All @@ -234,7 +234,7 @@ lys_nodeid_mod_check(struct lysc_ctx *ctx, const char *str, ly_bool abs, struct
for ( ; i < e->used; i += 2) {
if (e->tokens[i] != LYXP_TOKEN_OPER_PATH) {
LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - \"/\" expected instead of \"%.*s\".",
nodeid_type, str, e->tok_len[i], e->expr + e->tok_pos[i]);
nodeid_type, str, (int)e->tok_len[i], e->expr + e->tok_pos[i]);
ret = LY_EVALID;
goto cleanup;
} else if (e->used == i + 1) {
Expand All @@ -244,7 +244,7 @@ lys_nodeid_mod_check(struct lysc_ctx *ctx, const char *str, ly_bool abs, struct
goto cleanup;
} else if (e->tokens[i + 1] != LYXP_TOKEN_NAMETEST) {
LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s value \"%s\" - name test expected instead of \"%.*s\".",
nodeid_type, str, e->tok_len[i + 1], e->expr + e->tok_pos[i + 1]);
nodeid_type, str, (int)e->tok_len[i + 1], e->expr + e->tok_pos[i + 1]);
ret = LY_EVALID;
goto cleanup;
}
Expand Down Expand Up @@ -1235,7 +1235,8 @@ lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysp_deviate_add *d, struct l

if (target->flags & LYS_SET_MIN) {
LOGVAL(ctx->ctx, LYVE_REFERENCE,
"Invalid deviation adding \"min-elements\" property which already exists (with value \"%u\").", *num);
"Invalid deviation adding \"min-elements\" property which already exists (with value \"%" PRIu32 "\").",
*num);
ret = LY_EVALID;
goto cleanup;
}
Expand All @@ -1259,7 +1260,7 @@ lys_apply_deviate_add(struct lysc_ctx *ctx, struct lysp_deviate_add *d, struct l
if (target->flags & LYS_SET_MAX) {
if (*num) {
LOGVAL(ctx->ctx, LYVE_REFERENCE,
"Invalid deviation adding \"max-elements\" property which already exists (with value \"%u\").",
"Invalid deviation adding \"max-elements\" property which already exists (with value \"%" PRIu32 "\").",
*num);
} else {
LOGVAL(ctx->ctx, LYVE_REFERENCE,
Expand Down
17 changes: 9 additions & 8 deletions src/schema_compile_node.c
Original file line number Diff line number Diff line change
Expand Up @@ -1494,7 +1494,7 @@ lys_compile_type_enums(struct lysc_ctx *ctx, const struct lysp_type_enum *enums_
LY_ARRAY_FOR(*bitenums, v) {
if (cur_val == (*bitenums)[v].value) {
LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
"Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
"Invalid enumeration - value %" PRId32 " collide in items \"%s\" and \"%s\".",
cur_val, enums_p[u].name, (*bitenums)[v].name);
return LY_EVALID;
}
Expand Down Expand Up @@ -1528,7 +1528,7 @@ lys_compile_type_enums(struct lysc_ctx *ctx, const struct lysp_type_enum *enums_
LY_ARRAY_FOR(*bitenums, v) {
if (cur_pos == (*bitenums)[v].position) {
LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
"Invalid bits - position %u collide in items \"%s\" and \"%s\".",
"Invalid bits - position %" PRIu32 " collide in items \"%s\" and \"%s\".",
cur_pos, enums_p[u].name, (*bitenums)[v].name);
return LY_EVALID;
}
Expand Down Expand Up @@ -1562,15 +1562,15 @@ lys_compile_type_enums(struct lysc_ctx *ctx, const struct lysp_type_enum *enums_
if (basetype == LY_TYPE_ENUM) {
if (cur_val != base_enums[match].value) {
LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
"Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
enums_p[u].name, base_enums[match].value, cur_val);
"Invalid enumeration - value of the item \"%s\" has changed from %" PRId32 " to %" PRId32
" in the derived type.", enums_p[u].name, base_enums[match].value, cur_val);
return LY_EVALID;
}
} else {
if (cur_pos != base_enums[match].position) {
LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
"Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
enums_p[u].name, base_enums[match].position, cur_pos);
"Invalid bits - position of the item \"%s\" has changed from %" PRIu32 " to %" PRIu32
" in the derived type.", enums_p[u].name, base_enums[match].position, cur_pos);
return LY_EVALID;
}
}
Expand Down Expand Up @@ -3063,7 +3063,7 @@ lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct
}

if (llist->min > llist->max) {
LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list min-elements %" PRIu32 " is bigger than max-elements %" PRIu32 ".",
llist->min, llist->max);
return LY_EVALID;
}
Expand Down Expand Up @@ -3504,7 +3504,8 @@ lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc

/* checks */
if (list->min > list->max) {
LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List min-elements %u is bigger than max-elements %u.", list->min, list->max);
LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List min-elements %" PRIu32 " is bigger than max-elements %" PRIu32 ".",
list->min, list->max);
return LY_EVALID;
}

Expand Down
9 changes: 5 additions & 4 deletions src/tree_schema_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ LY_ERR
lysp_check_stringchar(struct lysp_ctx *ctx, uint32_t c)
{
if (!is_yangutf8char(c)) {
LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, c);
LOGVAL_PARSER(ctx, LY_VCODE_INCHAR, (char)c);
return LY_EVALID;
}
return LY_SUCCESS;
Expand All @@ -1014,11 +1014,12 @@ lysp_check_identifierchar(struct lysp_ctx *ctx, uint32_t c, ly_bool first, uint8
if (!is_yangidentstartchar(c)) {
if ((c < UCHAR_MAX) && isprint(c)) {
if (ctx) {
LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04x).", (char)c, c);
LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character '%c' (0x%04" PRIx32 ").",
(char)c, c);
}
} else {
if (ctx) {
LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character 0x%04x.", c);
LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier first character 0x%04" PRIx32 ".", c);
}
}
return LY_EVALID;
Expand All @@ -1034,7 +1035,7 @@ lysp_check_identifierchar(struct lysp_ctx *ctx, uint32_t c, ly_bool first, uint8
(*prefix) = 1;
} else if (!is_yangidentchar(c)) {
if (ctx) {
LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04x).", (char)c, c);
LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Invalid identifier character '%c' (0x%04" PRIx32 ").", (char)c, c);
}
return LY_EVALID;
}
Expand Down
2 changes: 1 addition & 1 deletion src/xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ lyxml_parse_value(struct lyxml_ctx *xmlctx, char endchar, char **value, size_t *
}
++offset;
if (ly_pututf8(&buf[len], n, &u)) {
LOGVAL(ctx, LYVE_SYNTAX, "Invalid character reference \"%.12s\" (0x%08x).", p, n);
LOGVAL(ctx, LYVE_SYNTAX, "Invalid character reference \"%.12s\" (0x%08" PRIx32 ").", p, n);
goto error;
}
len += u;
Expand Down
Loading

0 comments on commit 21eaa39

Please sign in to comment.