From 8ee71392a43191023708ae463d0f822c49f9c127 Mon Sep 17 00:00:00 2001 From: Michal Vasko Date: Tue, 30 Jul 2024 09:54:52 +0200 Subject: [PATCH] parser yang BUGFIX comments not allowed in unquoted strings --- src/parser_yang.c | 21 +++++++++++++++++---- tests/utests/schema/test_yang.c | 2 -- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/parser_yang.c b/src/parser_yang.c index 9fef72a3f..57b5f9b66 100644 --- a/src/parser_yang.c +++ b/src/parser_yang.c @@ -596,6 +596,7 @@ get_argument(struct lysp_yang_ctx *ctx, enum yang_arg arg, uint16_t *flags, char size_t buf_len = 0; uint8_t prefix = 0; ly_bool str_end = 0; + int comment; /* word buffer - dynamically allocated */ *word_b = NULL; @@ -627,18 +628,30 @@ get_argument(struct lysp_yang_ctx *ctx, enum yang_arg arg, uint16_t *flags, char str_end = 1; break; case '/': + comment = 0; if (ctx->in->current[1] == '/') { /* one-line comment */ - MOVE_INPUT(ctx, 2); - LY_CHECK_GOTO(ret = skip_comment(ctx, 1), error); + comment = 1; } else if (ctx->in->current[1] == '*') { /* block comment */ - MOVE_INPUT(ctx, 2); - LY_CHECK_GOTO(ret = skip_comment(ctx, 2), error); + comment = 2; } else { /* not a comment after all */ LY_CHECK_GOTO(ret = buf_store_char(ctx, arg, word_p, word_len, word_b, &buf_len, 0, &prefix), error); } + + if (comment) { + if (*word_len) { + /* invalid comment sequence (RFC 7950 sec. 6.1.3.) */ + LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Invalid comment sequence \"%.2s\" in an unquoted string.", ctx->in->current); + ret = LY_EVALID; + goto error; + } + + /* skip the comment */ + MOVE_INPUT(ctx, 2); + LY_CHECK_GOTO(ret = skip_comment(ctx, comment), error); + } break; case ' ': if (*word_len) { diff --git a/tests/utests/schema/test_yang.c b/tests/utests/schema/test_yang.c index 67f974702..034f95d80 100644 --- a/tests/utests/schema/test_yang.c +++ b/tests/utests/schema/test_yang.c @@ -284,8 +284,6 @@ test_arg(void **state) TEST_GET_ARGUMENT_SUCCESS("hello ", YCTX, Y_STR_ARG, "hello ", 5, " ", 1); - TEST_GET_ARGUMENT_SUCCESS("hello/*comment*/\n", YCTX, Y_STR_ARG, "hello/*comment*/\n", 5, "\n", 1); - TEST_GET_ARGUMENT_SUCCESS("\"hello\\n\\t\\\"\\\\\";", YCTX, Y_STR_ARG, "hello\n\t\"\\", 9, ";", 1); free(buf);