From 6e8c61ddca0a03928223ca7c3fc47a3039daf649 Mon Sep 17 00:00:00 2001 From: Roshan Shyamsunder Date: Mon, 26 Jun 2023 15:16:35 -0700 Subject: [PATCH 1/2] fix: Include parsed comments between arguments only when ParseComments is true Comments between arguments should be added to the parsed result only if the ParseComments ParseOption is true. This check was previously missing and has been added as part of this fix. A unit test has also been added to validate this. --- parse.go | 6 ++--- parse_test.go | 23 +++++++++++++++++++ .../nginx.conf | 7 ++++++ 3 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 testdata/configs/comments-between-args-disable-parse/nginx.conf diff --git a/parse.go b/parse.go index cfaa06ba..92cd43c9 100644 --- a/parse.go +++ b/parse.go @@ -252,10 +252,10 @@ func (p *parser) parse(parsing *Config, tokens <-chan NgxToken, ctx blockCtx, co } } for t.IsQuoted || (t.Value != "{" && t.Value != ";" && t.Value != "}") { - if strings.HasPrefix(t.Value, "#") && !t.IsQuoted { - commentsInArgs = append(commentsInArgs, t.Value[1:]) - } else { + if !strings.HasPrefix(t.Value, "#") || t.IsQuoted { stmt.Args = append(stmt.Args, t.Value) + } else if p.options.ParseComments { + commentsInArgs = append(commentsInArgs, t.Value[1:]) } t, tokenOk = <-tokens if !tokenOk { diff --git a/parse_test.go b/parse_test.go index ae23ea23..ad7b691a 100644 --- a/parse_test.go +++ b/parse_test.go @@ -881,6 +881,29 @@ var parseFixtures = []parseFixture{ }, }, }}, + {"comments-between-args-disable-parse", "", ParseOptions{ParseComments: false}, Payload{ + Status: "ok", + Config: []Config{ + { + File: getTestConfigPath("comments-between-args-disable-parse", "nginx.conf"), + Status: "ok", + Parsed: Directives{ + { + Directive: "http", + Args: []string{}, + Line: 1, + Block: Directives{ + { + Directive: "log_format", + Args: []string{"\\#arg\\ 1", "#arg 2"}, + Line: 2, + }, + }, + }, + }, + }, + }, + }}, {"premature-eof", "", ParseOptions{}, Payload{ Status: "failed", Errors: []PayloadError{ diff --git a/testdata/configs/comments-between-args-disable-parse/nginx.conf b/testdata/configs/comments-between-args-disable-parse/nginx.conf new file mode 100644 index 00000000..b26f13d2 --- /dev/null +++ b/testdata/configs/comments-between-args-disable-parse/nginx.conf @@ -0,0 +1,7 @@ +http { #comment 1 + log_format #comment 2 + \#arg\ 1 #comment 3 + '#arg 2' #comment 4 + #comment 5 + ; +} \ No newline at end of file From 7f0c08a72307718ab6ab549c959ca1c194feec58 Mon Sep 17 00:00:00 2001 From: Roshan Shyamsunder Date: Tue, 27 Jun 2023 09:53:01 -0700 Subject: [PATCH 2/2] Disable unnecessary `exhauststruct` linter --- .golangci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.golangci.yml b/.golangci.yml index eda17d15..e5e1f8c7 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -24,11 +24,13 @@ linters: - golint - maligned - scopelint + - exhaustruct # deprecated - deadcode - varcheck - structcheck - nosnakecase + - exhaustivestruct # Run options