Skip to content

Commit 22d2a1b

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 22d2a1b

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/path.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,13 @@ 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, "XPath \"%.*s\" was expected to be absolute.", (int)path_len, str_path);
349+
ret = LY_EVALID;
350+
goto error;
351+
}
352+
346353
/* parse as a generic XPath expression, reparse is performed manually */
347354
LY_CHECK_GOTO(ret = lyxp_expr_parse(ctx, str_path, path_len, 0, &exp), error);
348355
tok_idx = 0;

tests/utests/types/instanceid.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ test_data_xml(void **state)
200200

201201
TEST_ERROR_XML2("<cont xmlns=\"urn:tests:mod\"/>",
202202
"defs", "xmlns:m=\"urn:tests:mod\"", "l1", "[1]", LY_EVALID);
203-
CHECK_LOG_CTX("Invalid instance-identifier \"[1]\" value - syntax error: Unexpected XPath token \"[\" (\"[1]\"), expected \"Operator(Path)\".",
203+
CHECK_LOG_CTX("Invalid instance-identifier \"[1]\" value - syntax error: XPath \"[1]\" was expected to be absolute.",
204204
"/defs:l1", 1);
205205

206206
TEST_ERROR_XML2("<cont xmlns=\"urn:tests:mod\"><l2/></cont>",

0 commit comments

Comments
 (0)