Skip to content

Commit 278e51f

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 278e51f

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/path.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,8 @@ ly_path_parse(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const
328328
struct lyxp_expr *exp = NULL;
329329
uint32_t tok_idx, cur_len;
330330
const char *cur_node, *prev_prefix = NULL, *ptr;
331+
char path_begin_str[2] = {0};
332+
331333
ly_bool is_abs;
332334

333335
assert((begin == LY_PATH_BEGIN_ABSOLUTE) || (begin == LY_PATH_BEGIN_EITHER));
@@ -339,6 +341,14 @@ ly_path_parse(const struct ly_ctx *ctx, const struct lysc_node *ctx_node, const
339341
LOG_LOCSET(ctx_node, NULL);
340342
}
341343

344+
/* check if path begins with '/' if expected to and fail early if not */
345+
if ((begin == LY_PATH_BEGIN_ABSOLUTE) && str_path && (str_path[0] != '/')) {
346+
path_begin_str[0] = str_path[0];
347+
LOGVAL(ctx, LY_VCODE_XP_INTOK2, path_begin_str, str_path, "Operator(Path)");
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;

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: Unexpected XPath token \"[\" (\"[1]</l1>\"), expected \"Operator(Path)\".",
204204
"/defs:l1", 1);
205205

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

0 commit comments

Comments
 (0)