Skip to content

Commit 9ef7aa9

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 64c0b0f commit 9ef7aa9

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/path.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,16 @@ ly_path_parse(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const
339339
LOG_LOCSET(ctx_node, NULL);
340340
}
341341

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

0 commit comments

Comments
 (0)