Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 1 Oct 2025 09:39:10 GMT
From:      Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= <des@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: b5e2bd5ef381 - main - libucl: Fix bugs in C-style comment parser
Message-ID:  <202510010939.5919dABY017245@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=b5e2bd5ef38181ce4a445ec19f1fa5cb6c8ea692

commit b5e2bd5ef38181ce4a445ec19f1fa5cb6c8ea692
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2025-10-01 09:28:30 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2025-10-01 09:28:56 +0000

    libucl: Fix bugs in C-style comment parser
    
    When an asterisk is encountered inside a C-style comment, we first check
    if there is at least one more character left in the buffer, and if that
    character is a slash, which would terminate the comment.  If that is not
    the case, the next two characters are consumed without being inspected.
    If one of those is a double quote, or the initial asterisk of an
    asterisk-slash pair, we end up misparsing the comment.
    
    MFC after:      3 days
    Reviewed by:    kevans, bofh
    Differential Revision:  https://reviews.freebsd.org/D52808
---
 contrib/libucl/src/ucl_parser.c | 77 ++++++++++++++++++++---------------------
 1 file changed, 38 insertions(+), 39 deletions(-)

diff --git a/contrib/libucl/src/ucl_parser.c b/contrib/libucl/src/ucl_parser.c
index 6be16d12169c..728cd6381056 100644
--- a/contrib/libucl/src/ucl_parser.c
+++ b/contrib/libucl/src/ucl_parser.c
@@ -164,51 +164,50 @@ start:
 			}
 		}
 	}
-	else if (chunk->remain >= 2 && *p == '/') {
-		if (p[1] == '*') {
-			beg = p;
-			ucl_chunk_skipc (chunk, p);
-			comments_nested ++;
-			ucl_chunk_skipc (chunk, p);
-
-			while (p < chunk->end) {
-				if (*p == '"' && *(p - 1) != '\\') {
-					quoted = !quoted;
-				}
-
-				if (!quoted) {
-					if (*p == '*') {
-						ucl_chunk_skipc (chunk, p);
-						if (chunk->remain > 0 && *p == '/') {
-							comments_nested --;
-							if (comments_nested == 0) {
-								if (parser->flags & UCL_PARSER_SAVE_COMMENTS) {
-									ucl_save_comment (parser, beg, p - beg + 1);
-									beg = NULL;
-								}
-
-								ucl_chunk_skipc (chunk, p);
-								goto start;
-							}
-						}
-						ucl_chunk_skipc (chunk, p);
-					}
-					else if (p[0] == '/' && chunk->remain >= 2 && p[1] == '*') {
-						comments_nested ++;
-						ucl_chunk_skipc (chunk, p);
-						ucl_chunk_skipc (chunk, p);
-						continue;
+	else if (chunk->remain >= 2 && *p == '/' && p[1] == '*') {
+		beg = p;
+		comments_nested ++;
+		ucl_chunk_skipc (chunk, p);
+		ucl_chunk_skipc (chunk, p);
+		while (p < chunk->end) {
+			if (*p == '"' && *(p - 1) != '\\') {
+				/* begin or end double-quoted string */
+				quoted = !quoted;
+				ucl_chunk_skipc (chunk, p);
+			}
+			else if (quoted) {
+				/* quoted character */
+				ucl_chunk_skipc (chunk, p);
+			}
+			else if (chunk->remain >= 2 && *p == '*' && p[1] == '/') {
+				/* end of comment */
+				ucl_chunk_skipc (chunk, p);
+				ucl_chunk_skipc (chunk, p);
+				comments_nested --;
+				if (comments_nested == 0) {
+					if (parser->flags & UCL_PARSER_SAVE_COMMENTS) {
+						ucl_save_comment (parser, beg, p - beg + 1);
+						beg = NULL;
 					}
+					goto start;
 				}
-
+			}
+			else if (chunk->remain >= 2 && *p == '/' && p[1] == '*') {
+				/* start of nested comment */
+				comments_nested ++;
+				ucl_chunk_skipc (chunk, p);
 				ucl_chunk_skipc (chunk, p);
 			}
-			if (comments_nested != 0) {
-				ucl_set_err (parser, UCL_ENESTED,
-						"unfinished multiline comment", &parser->err);
-				return false;
+			else {
+				/* anything else */
+				ucl_chunk_skipc (chunk, p);
 			}
 		}
+		if (comments_nested != 0) {
+			ucl_set_err (parser, UCL_ENESTED,
+			    "unfinished multiline comment", &parser->err);
+			return false;
+		}
 	}
 
 	if (beg && p > beg && (parser->flags & UCL_PARSER_SAVE_COMMENTS)) {



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202510010939.5919dABY017245>