Skip to content

Header-only file without final newline causes panic #686

Description

@birjj

What happened?

If you create a file that only consists of a single header, and don't leave a final newline at the end of the file, mark will panic with

panic: runtime error: slice bounds out of range [8:7]

goroutine 1 [running]:
github.com/kovetskiy/mark/metadata.ExtractMeta({0xc0003eb0a8, 0x7, 0x8}, {0xc00001009b, 0x3}, 0x1, 0x0, {0xc00058e960, 0x16}, {0xc00072b7a0, ...}, ...)
	/go/src/github.com/kovetskiy/mark/metadata/metadata.go:192 +0x106d
github.com/kovetskiy/mark/util.processFile({0xc00058e960, 0x16}, 0xc000408b60, 0xc0005cc288, {0x0, 0x0}, {0xc0000400fe, 0x3f}, 0xc00066b997)
	/go/src/github.com/kovetskiy/mark/util/cli.go:119 +0x22c
github.com/kovetskiy/mark/util.RunMark({0xc0005cc288?, 0x1b86658?}, 0xc0005cc288)
	/go/src/github.com/kovetskiy/mark/util/cli.go:87 +0x6c5
github.com/urfave/cli/v3.(*Command).run(0xc0005cc288, {0x1b86658, 0xc0004d5c20}, {0xc0000b00a0, 0x1, 0x1})
	/go/pkg/mod/github.com/urfave/cli/v3@v3.4.1/command_run.go:344 +0x2773
github.com/urfave/cli/v3.(*Command).Run(...)
	/go/pkg/mod/github.com/urfave/cli/v3@v3.4.1/command_run.go:94
main.main()
	github.com/kovetskiy/mark/main.go:37 +0x18a

What did you expect to happen?

mark should not panic

How can we reproduce the behavior you experienced?

Steps to reproduce the behavior:

  1. Create a file containing exactly # Blogs. The file must not be saved with a final newline.
  2. Attempt to run mark on the file.

Minimal markdown (remember to check that the saved file does not have a final newline):

# Blogs

Further Information (please complete the following information)

  • Mark Version (mark --version): v15.0.0 (Docker image)
  • Mark Parameters: [e.g. --drop-h1 --title-from-h1]
  • Confluence Hosting: [e.g. Cloud, Server or Datacenter]
  • Confluence Version: [e.g. v7.13]
  • Environment specific Information: Running in GitHub Actions

Logs or other output

panic: runtime error: slice bounds out of range [8:7]

goroutine 1 [running]:
github.com/kovetskiy/mark/metadata.ExtractMeta({0xc0003eb0a8, 0x7, 0x8}, {0xc00001009b, 0x3}, 0x1, 0x0, {0xc00058e960, 0x16}, {0xc00072b7a0, ...}, ...)
	/go/src/github.com/kovetskiy/mark/metadata/metadata.go:192 +0x106d
github.com/kovetskiy/mark/util.processFile({0xc00058e960, 0x16}, 0xc000408b60, 0xc0005cc288, {0x0, 0x0}, {0xc0000400fe, 0x3f}, 0xc00066b997)
	/go/src/github.com/kovetskiy/mark/util/cli.go:119 +0x22c
github.com/kovetskiy/mark/util.RunMark({0xc0005cc288?, 0x1b86658?}, 0xc0005cc288)
	/go/src/github.com/kovetskiy/mark/util/cli.go:87 +0x6c5
github.com/urfave/cli/v3.(*Command).run(0xc0005cc288, {0x1b86658, 0xc0004d5c20}, {0xc0000b00a0, 0x1, 0x1})
	/go/pkg/mod/github.com/urfave/cli/v3@v3.4.1/command_run.go:344 +0x2773
github.com/urfave/cli/v3.(*Command).Run(...)
	/go/pkg/mod/github.com/urfave/cli/v3@v3.4.1/command_run.go:94
main.main()
	github.com/kovetskiy/mark/main.go:37 +0x18a

Additional context

I believe this is caused by

offset += len(line) + 1

Since mark adds 1 to the length of the line, which is presumably done to increment past the \n that the scanner strips, the final offset on a single-line-no-newline file ends up being 1 beyond the length of the file.

Note that the current implementation also has another issue: if the file uses Windows-style newlines (\r\n) then + 1 does not increment to the beginning of the next line as intended.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions