Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic when file ends with a block comment and no newline #123

Closed
belak opened this issue Oct 15, 2019 · 3 comments
Closed

Panic when file ends with a block comment and no newline #123

belak opened this issue Oct 15, 2019 · 3 comments

Comments

@belak
Copy link
Contributor

belak commented Oct 15, 2019

Describe the bug
When a file ends in a special quoted string and no newline, it will panic. A sample stack trace and test case has been included below.

% scc .
panic: runtime error: index out of range [649] with length 649

goroutine 98 [running]:
github.com/boyter/scc/processor.verifyIgnoreEscape(0xc000264000, 0xc000265200, 0xc000264900, 0xc000265b00, 0xc000266400, 0x7f2700237f00, 0x0, 0x0, 0x0, 0xc00019f5e0, ...)
	/private/tmp/scc-20191015-33901-1vdnpeq/scc-2.9.0/src/github.com/boyter/scc/processor/workers.go:352 +0xca
github.com/boyter/scc/processor.blankState(0xc0004a4000, 0x286, 0x288, 0x1, 0xc00051f4a0, 0x0, 0x0, 0xc0000ac2b8, 0x3, 0x8, ...)
	/private/tmp/scc-20191015-33901-1vdnpeq/scc-2.9.0/src/github.com/boyter/scc/processor/workers.go:317 +0x10f
github.com/boyter/scc/processor.CountStats(0xc0004a4000)
	/private/tmp/scc-20191015-33901-1vdnpeq/scc-2.9.0/src/github.com/boyter/scc/processor/workers.go:459 +0xe38
github.com/boyter/scc/processor.fileProcessorWorker.func1(0xc0000a6120, 0xc000236000, 0xc0000a6180, 0xc000236010)
	/private/tmp/scc-20191015-33901-1vdnpeq/scc-2.9.0/src/github.com/boyter/scc/processor/workers.go:743 +0x243
created by github.com/boyter/scc/processor.fileProcessorWorker
	/private/tmp/scc-20191015-33901-1vdnpeq/scc-2.9.0/src/github.com/boyter/scc/processor/workers.go:709 +0xb0

To Reproduce

In theory this should match the line numbers for if the file ends in a newline, but that doesn't seem to be counted the same and I really didn't want to figure that out. I've included a regression test.

func TestCountStatsIssue(t *testing.T) {
	ProcessConstants()
	fileJob := FileJob{
		Language: "Python",
	}

	fileJob.Content = []byte(`"""
hello there! how's it going?
"""`)

	CountStats(&fileJob)

	if fileJob.Lines != 2 {
		t.Errorf("Expected 2 lines got %d", fileJob.Lines)
	}

	if fileJob.Code != 1 {
		t.Errorf("Expected 1 lines got %d", fileJob.Code)
	}

	if fileJob.Comment != 1 {
		t.Errorf("Expected 1 lines got %d", fileJob.Comment)
	}

	if fileJob.Blank != 0 {
		t.Errorf("Expected 0 lines got %d", fileJob.Blank)
	}
}

Expected behavior
For scc to not panic

Desktop (please complete the following information):

  • OS: macOS
  • Version: 2.9.0 (79458d6)
@belak belak changed the title Index out of range when file ends with a block comment and no newline Panic when file ends with a block comment and no newline Oct 15, 2019
@boyter
Copy link
Owner

boyter commented Oct 15, 2019

Quality bug report and PR @belak
Already merged. Ill push out a 2.9.1 soon to ensure nobody gets this case.

@boyter
Copy link
Owner

boyter commented Oct 15, 2019

Release pushed out. Thanks so much for reporting this and providing PR.

@boyter boyter closed this as completed Oct 15, 2019
@belak
Copy link
Contributor Author

belak commented Oct 15, 2019

Yep, no problem! I ran across this project yesterday and decided to try it against our repos... which ran into this bug. Seemed like a simple fix. :) Thanks for the quick response!

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

No branches or pull requests

2 participants