Skip to content

Commit 84c019f

Browse files
ly_path_parse OPTIMIZE fail faster on invalid path
`ly_path_parse()` is called from various functions like `lyplg_type_store_node_instanceid()` and `lyplg_type_lypath_new()` with the `LY_PATH_BEGIN_ABSOLUTE` flag set. This means that the path is expected to start with a '/' character. If it doesn't, this could mean that the value is not a valid path. This can happen while finding a union type, where it is not optimal to call `lyxp_expr_parse()`, which is much heavier to compute.
1 parent 516181c commit 84c019f

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/path.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ ly_path_parse(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const
335335
(prefix == LY_PATH_PREFIX_FIRST) || (prefix == LY_PATH_PREFIX_STRICT_INHERIT));
336336
assert((pred == LY_PATH_PRED_KEYS) || (pred == LY_PATH_PRED_SIMPLE) || (pred == LY_PATH_PRED_LEAFREF));
337337

338+
/* path_len must be specified or begin cannot be absolute */
339+
assert(path_len || (begin != LY_PATH_BEGIN_ABSOLUTE));
340+
338341
if (ctx_node) {
339342
LOG_LOCSET(ctx_node, NULL);
340343
}
@@ -343,6 +346,14 @@ ly_path_parse(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const
343346
path_len = strlen(str_path);
344347
}
345348

349+
/* check if path begins with '/' if expected to and fail early if not */
350+
if ((begin == LY_PATH_BEGIN_ABSOLUTE) && (str_path[0] != '/')) {
351+
LOGVAL(ctx, LYVE_XPATH, "Unexpected XPath token \"%c\" (\"%.*s\"), expected \"Operator(Path)\".",
352+
str_path[0], (int)path_len, str_path);
353+
ret = LY_EVALID;
354+
goto error;
355+
}
356+
346357
/* parse as a generic XPath expression, reparse is performed manually */
347358
LY_CHECK_GOTO(ret = lyxp_expr_parse(ctx, str_path, path_len, 0, &exp), error);
348359
tok_idx = 0;

0 commit comments

Comments
 (0)