gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
75.73k stars 7.53k forks source link

Panic when reading in invalid config.toml of module #9417

Closed deining closed 2 years ago

deining commented 2 years ago

What version of Hugo are you using (hugo version)?

$ hugo version
hugo v0.92.1-85E2E862+extended windows/amd64 BuildDate=2022-01-27T11:44:41Z VendorInfo=gohugoio

Does this issue reproduce with the latest release?

Yes.

How to reproduce

At your command prompt, issue the following commands:

hugo new site github-issue-9417
cd github-issue-9417/
echo -n "[params" >> config.toml
hugo serve

Now, hugo panics:

$ hugo serve
hugo: collected modules in 1335 ms
panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
github.com/pelletier/go-toml/v2.expect(...)
        /go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.0-beta.3.0.20210727221244-fa0796069526/parser.go:1033
github.com/pelletier/go-toml/v2.(*parser).parseStdTable(0xc00068a230, {0xc000532000, 0x2, 0x202})
        /go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.0-beta.3.0.20210727221244-fa0796069526/parser.go:198 +0x34a
github.com/pelletier/go-toml/v2.(*parser).parseTable(0x2030001, {0xc000532000, 0x2030001, 0x30001})
        /go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.0-beta.3.0.20210727221244-fa0796069526/parser.go:146 +0x39
github.com/pelletier/go-toml/v2.(*parser).parseExpression(0x2de37b0, {0xc000532000, 0x235f05a4511, 0xb5d7e0})
        /go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.0-beta.3.0.20210727221244-fa0796069526/parser.go:120 +0x9f
github.com/pelletier/go-toml/v2.(*parser).NextExpression(0xc00068a230)
        /go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.0-beta.3.0.20210727221244-fa0796069526/parser.go:61 +0x119
github.com/pelletier/go-toml/v2.(*decoder).nextExpr(...)
        /go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.0-beta.3.0.20210727221244-fa0796069526/unmarshaler.go:143
github.com/pelletier/go-toml/v2.(*decoder).fromParser(0xc000845d58, {0x26afd20, 0xc000006208, 0x3d0ab20})
        /go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.0-beta.3.0.20210727221244-fa0796069526/unmarshaler.go:191 +0x65
github.com/pelletier/go-toml/v2.(*decoder).FromParser(0xc000845d58, {0x25ed620, 0xc000006208})
        /go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.0-beta.3.0.20210727221244-fa0796069526/unmarshaler.go:177 +0xf8
github.com/pelletier/go-toml/v2.Unmarshal({0xc000532000, 0x2, 0x202}, {0x25ed620, 0xc000006208})
        /go/pkg/mod/github.com/pelletier/go-toml/v2@v2.0.0-beta.3.0.20210727221244-fa0796069526/unmarshaler.go:27 +0x112
github.com/gohugoio/hugo/parser/metadecoders.Decoder.UnmarshalTo({0x2e5c640, 0x0}, {0xc000532000, 0x0, 0xc000532000}, {0x28dbb68, 0x4}, {0x25ed620, 0xc000006208})
        /root/project/hugo/parser/metadecoders/decoder.go:159 +0x425
github.com/gohugoio/hugo/parser/metadecoders.Decoder.UnmarshalToMap({0x2e5c2c0, 0x0}, {0xc000532000, 0x2, 0x202}, {0x28dbb68, 0x4})
        /root/project/hugo/parser/metadecoders/decoder.go:67 +0xad
github.com/gohugoio/hugo/parser/metadecoders.Decoder.UnmarshalFileToMap({0x845f58, 0xc0}, {0x2e5c2c0, 0x3d6c440}, {0xc00003aa80, 0x24})
        /root/project/hugo/parser/metadecoders/decoder.go:84 +0xaa
github.com/gohugoio/hugo/config.loadConfigFromFile({0x2e5c2c0, 0x3d6c440}, {0xc00003aa80, 0xe9f5c5})
        /root/project/hugo/config/configLoader.go:86 +0x4a
github.com/gohugoio/hugo/config.FromFile({0x2e5c2c0, 0x3d6c440}, {0xc00003aa80, 0x24})
        /root/project/hugo/config/configLoader.go:61 +0x31
github.com/gohugoio/hugo/modules.(*collector).applyThemeConfig(0xc00069d5c0, 0xc0007fa2c0)
        /root/project/hugo/modules/collect.go:445 +0x388
github.com/gohugoio/hugo/modules.(*collector).add(0xc00069d5c0, 0xc0007fa160, {{0xc0000428e8, 0x14}, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...}, ...)
        /root/project/hugo/modules/collect.go:323 +0x985
github.com/gohugoio/hugo/modules.(*collector).addAndRecurse(0xc000262230, 0xc0007fa160, 0x0)
        /root/project/hugo/modules/collect.go:348 +0x1ec
github.com/gohugoio/hugo/modules.(*collector).collect(0xc00069d5c0)
        /root/project/hugo/modules/collect.go:522 +0x387
github.com/gohugoio/hugo/modules.(*Client).collect(0xc0006ad180, 0x19)
        /root/project/hugo/modules/collect.go:96 +0x79
github.com/gohugoio/hugo/modules.(*Client).Collect(0xc0006ad180)
        /root/project/hugo/modules/collect.go:69 +0x9f
github.com/gohugoio/hugo/hugolib.configLoader.collectModules({{0x2e5db50, 0xc00007d090}, {{0x2e5c2c0, 0x3d6c440}, {0x2e625d0, 0xc00069d1a0}, {0x0, 0x0}, {0xc00003f840, 0x16}, ...}}, ...)
        /root/project/hugo/hugolib/config.go:420 +0x510
github.com/gohugoio/hugo/hugolib.LoadConfig({{0x2e5c2c0, 0x3d6c440}, {0x2e625d0, 0xc00069d1a0}, {0x0, 0x0}, {0xc00003f840, 0x16}, {0xc00003f840, 0x16}, ...}, ...)
        /root/project/hugo/hugolib/config.go:148 +0xbbe
github.com/gohugoio/hugo/commands.(*commandeer).loadConfig(0xc0004255e0)
        /root/project/hugo/commands/commandeer.go:309 +0x365
github.com/gohugoio/hugo/commands.newCommandeer(0x1, 0x1, 0x1, 0xc000425340, {0x2e0c840, 0xc000363f40}, 0xc00028fbc0, {0x0, 0x0, 0x0})
        /root/project/hugo/commands/commandeer.go:193 +0x310
github.com/gohugoio/hugo/commands.initializeConfig(0xd2, 0xee, 0xb3, 0x290eec0, {0x2e0c840, 0xc000363f40}, 0xc00061fc88)
        /root/project/hugo/commands/hugo.go:125 +0x31
github.com/gohugoio/hugo/commands.(*serverCmd).server(0xc000363f40, 0xc000176780, {0xc00061fcf0, 0x0, 0x0})
        /root/project/hugo/commands/server.go:242 +0x1ce
github.com/spf13/cobra.(*Command).execute(0xc000176780, {0x3d6ca08, 0x0, 0x0})
        /go/pkg/mod/github.com/spf13/cobra@v1.2.1/command.go:856 +0x60e
github.com/spf13/cobra.(*Command).ExecuteC(0xc00069f680)
        /go/pkg/mod/github.com/spf13/cobra@v1.2.1/command.go:974 +0x3bc
github.com/gohugoio/hugo/commands.Execute({0xc0000963b0, 0x1, 0x1})
        /root/project/hugo/commands/hugo.go:92 +0xb4
main.main()
        /root/project/hugo/main.go:23 +0x58
bep commented 2 years ago

This is an upstream issue. I will update the TOML library; if that doesn't work, please file an issue at https://github.com/pelletier/go-toml/issues

bep commented 2 years ago

See #9439

jmooring commented 2 years ago

With v0.92.0 I am unable to reproduce the panic as described.

git clone --single-branch -b hugo-github-issue-9417 https://github.com/jmooring/hugo-testing hugo-github-issue-9417
cd hugo-github-issue-9417
cd test-site
hugo server

Error: "/home/jmooring/temp/hugo-github-issue-9417/test-module/config.toml:1:3": unmarshal failed: toml: expected character U+005D

deining commented 2 years ago

With v0.92.0 I am unable to reproduce the panic as described.

Inside your file test-module/config.toml there is a newline at the end of the first line. Remove this newline so that you have one single line only, and you will be confronted with the panic. I updated my post so that this gets reflected.

jmooring commented 2 years ago

@deining I updated the example and can reproduce the problem. The fact that your invalid config.toml file exists in a module is irrelevant. The panic occurs when unmarshaling any TOML file where the last line[^1] contains an incomplete key (example: [params) and does not end with a newline.

[^1]: I use the term "line" loosely because, by POSIX standards, it isn't a line. See definition.

@bep I confirm that this is resolved with https://github.com/gohugoio/hugo/pull/9440. No panic, and the error is:

unmarshal failed: toml: expected character ] but the document ended here

deining commented 2 years ago

@deining I updated the example and can reproduce the problem.

Great.

The fact that your invalid config.toml file exists in a module is irrelevant.

You are right. I updated my initial post with instructions how to reproduce the problem quickly.

@bep I confirm that this is resolved with #9440. No panic, and the error is:

unmarshal failed: toml: expected character ] but the document ended here

Glad to see that the issue was fixed upstream already.

github-actions[bot] commented 2 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.