Skip to content

Commit 9385d06

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 9385d06

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/path.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,14 @@ ly_path_parse(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const
343343
path_len = strlen(str_path);
344344
}
345345

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

0 commit comments

Comments
 (0)