Skip to content

Fix indented comment parsed as a key, dropping the next key/value pair (#155) - #157

Merged
zonyitoo merged 1 commit into
zonyitoo:masterfrom
youdie006:fix/155-indented-comment-parsed-as-key
Aug 26, 2026
Merged

zonyitoo merged 1 commit into
zonyitoo:masterfrom
youdie006:fix/155-indented-comment-parsed-as-key

Conversation

@youdie006

Copy link
Copy Markdown
Contributor

Fixes #155.

Problem

With default features, an indented comment line silently drops the following key/value pair:

Ini::load_from_str("[s]\na=1\n  ; c\nb=2\n")

Section s ends up with a bogus property "; c\nb" = "2", and get_from(Some("s"), "b") returns None -- no error is raised.

This is a regression: rust-ini 0.21.1 returned Err("3:4 doesn't support inline comment") for this exact input, so the current behaviour is a silent data loss introduced by the leading-whitespace key support (PRs #139 / #145 / #152).

Root cause

In Parser::parse(), the ' ' | '\t' arm consumes the leading indentation and then dispatches on the next character. With no arm for a comment marker, an indented ; / # fell through to the generic key-parsing branch, whose parse_str_until(&[Some('='), Some(':')], ...) scans past the comment marker and across the newline to the = on the next line -- producing a bogus key and swallowing the following pair. The top-level comment arm never sees indented comments.

Fix

Handle an indented ; / # as a whole-line comment (via the existing parse_comment), matching Python's configparser and virtually every INI consumer. The reporter accepted either b = "2" or a parse error; this picks the configparser-consistent whole-line-comment behavior, which also restores the pre-regression intent without reintroducing the hard error. A tab-indented # behaves identically.

Tests

Added indented_comment_not_parsed_as_key, covering both ; c and # c indented comments and asserting a still parses and b is recovered. Verified red/green (reverting the fix makes the new test fail with b == None); full suite is 69 tests + 3 doc-tests, all green.


This PR was prepared with AI assistance (Claude Code); the fix, tests, and red/green verification were reviewed by me.

With default features an indented comment line silently drops the following
key/value pair: load_from_str("[s]\na=1\n  ; c\nb=2\n") leaves section s with a
bogus property "; c\nb"="2" and get_from("s","b") returns None with no error.
This is a regression - 0.21.1 returned an inline-comment error - introduced by
the leading-whitespace key support (zonyitoo#139/zonyitoo#145/zonyitoo#152).

In Parser::parse the whitespace arm consumes indentation then dispatches on the
next char; with no arm for a comment marker, an indented ;/# fell through to key
parsing, whose parse_str_until scans past the marker and across the newline to
the next line's =, producing a bogus key and swallowing the following pair. Add
an arm handling an indented ;/# as a whole-line comment via parse_comment,
matching configparser.

Fixes zonyitoo#155
@zonyitoo
zonyitoo merged commit a0aa25c into zonyitoo:master Aug 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Indented comment parses as a key name and swallows the next key/value pair

2 participants