Skip to content

value parsed incorrectly when a comment symbol (#) is inside a quoted string #393

Closed

Description

@nglgzz

Version

Commit: e2db55b

Describe the bug

Originally found this through yq (v4.53.3) which in turn uses this library. I cloned the repo and poked with the tests/debugger to confirm the bug.

When a # symbol is in the middle of a quoted string, everything after it gets dropped. The following for example:

[background]
primary-color      = '#e18477'
secondary-color    = '#000000'

Gets interpreted as:

[background]
primary-color      = '
secondary-color    = '

Same happens if the string is quoted with double quotes. Backticks work fine (and there is a test for this scenario that uses backticks).

To reproduce

[background]
primary-color      = '#e18477'
secondary-color    = '#000000'

Expected behavior

Nothing should be stripped after # if it appears inside a quoted string.

Additional context

I tried to take a quick look at the code and I suspect the issue is in the code block below. It seems to me that the logic that strips the comment doesn't take into account that the comment symbol could be inside a quoted string.

ini/parser.go

Lines 280 to 297 in e2db55b

if !p.options.IgnoreInlineComment {
var i int
if p.options.SpaceBeforeInlineComment {
i = strings.Index(line, " #")
if i == -1 {
i = strings.Index(line, " ;")
}
} else {
i = strings.IndexAny(line, "#;")
}
if i > -1 {
p.comment.WriteString(line[i:])
line = strings.TrimSpace(line[:i])
}
}

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

  1. unknwon commented on Nov 11, 2026

    @unknwon
    Member

    Fixed in v2 by #435, and will be in the next v2 pre-release after v2.0.0-beta.2. v2 is imported as unknwon.dev/ini/v2; see the migration guide for upgrading from v1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions