confighelper: fix inline comment parsing

Require that inline comments be separated from configuration
data by whitespace.  Unescape comment specifiers that follow
the correct escape sequence.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2023-10-06 12:25:26 -04:00
parent 52ee49eed3
commit 4329241949
No known key found for this signature in database
GPG Key ID: 5A1EB336DFB4C71B
1 changed files with 6 additions and 7 deletions

View File

@ -952,13 +952,12 @@ class FileSourceWrapper(ConfigSourceWrapper):
# ignore lines that contain only whitespace/comments # ignore lines that contain only whitespace/comments
continue continue
line = line.expandtabs(tabsize=4) line = line.expandtabs(tabsize=4)
# Remove inline comments # Search for and remove inline comments
for prefix in "#;": cmt_match = re.search(r" +[#;]", line)
icmt = line.find(prefix) if cmt_match is not None:
if icmt > 0 and line[icmt-1] != "\\": line = line[:cmt_match.start()]
# inline comment, remove it # Unescape prefix chars that are preceded by whitespace
line = line[:icmt] line = re.sub(r" \\(#|;)", r" \1", line)
break
line_indent = len(line) - len(line.lstrip()) line_indent = len(line) - len(line.lstrip())
if opt_indent != -1 and line_indent > opt_indent: if opt_indent != -1 and line_indent > opt_indent:
# Multi-line value, append to buffer and resume parsing # Multi-line value, append to buffer and resume parsing