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

Error creating pull requests or diffs with very long diffs containing very long lines #13602

Closed
1 of 6 tasks
davidsvantesson opened this issue Nov 17, 2020 · 7 comments · Fixed by #13662
Closed
1 of 6 tasks
Labels
issue/regression Issue needs no code to be fixed, only a description on how to fix it yourself type/bug
Milestone

Comments

@davidsvantesson
Copy link
Contributor

  • Gitea version (or commit ref): 1.12.6
  • Git version:
  • Operating system: Ubuntu
  • Database (use [x]):
    • PostgreSQL
    • MySQL
    • MSSQL
    • SQLite
  • Can you reproduce the bug at https://try.gitea.io:
    • Yes (provide example URL)
    • No
  • Log gist:
    2020/11/17 10:12:48 ...ters/repo/compare.go:449:PrepareCompareDiff() [E] GetDiffRange: ParsePatch: Unexpected line in hunk: <line containing /// characters>

Description

When creating a PR with a file containing a "//" this will cause a 500 error page. Issue was introduced with #13157 (see services/gitdiff/gitdiff.go line 671).
Suggested solution: Even if this is considered not valid, it should only affect the specific file (For example with a text "Gitea can't generate a diff for this file"). It should not cause a 500 error page making it impossible to create PR.

This is similar to #13597 but seem to be related to LFS, so I decided to create a new issue.

@lunny
Copy link
Member

lunny commented Nov 17, 2020

So we should add some new unit tests for git diff.

@johanvdw
Copy link
Contributor

Also affects 1.13.rc2

@mrsdizzie mrsdizzie added type/bug issue/regression Issue needs no code to be fixed, only a description on how to fix it yourself labels Nov 18, 2020
@mrsdizzie mrsdizzie added this to the 1.13.0 milestone Nov 18, 2020
@mrsdizzie
Copy link
Member

Whats a working example of this? I don't seem to be able to recreate it with the description"create a file containing a "//"

If anybody could post one or link to a current example of a 500 on PR at try.gitea.io

@johanvdw
Copy link
Contributor

Here is an example:
https://try.gitea.io/johanvdw/problem500/compare/master...problem

does not contain // though, but shows the same behaviour (it is reworked from a file that showed this error).

seems files have to be large and have to contain long lines for this to happen

@johanvdw
Copy link
Contributor

The file contains random chars, so feel free to reuse for tests if needed.

@mrsdizzie
Copy link
Member

Thanks for example that helps:

This happens in this case because:

case '+':
curFileLinesCount++
curFile.Addition++
if curFileLinesCount >= maxLines {
curFile.IsIncomplete = true
continue
}

Once the current file number is above the maxLines value, it does a continue for some reason. this means it never hits this check below the switch:

if isFragment {
curFile.IsIncomplete = true
for isFragment {
lineBytes, isFragment, err = input.ReadLine()
if err != nil {
// Now by the definition of ReadLine this cannot be io.EOF
err = fmt.Errorf("Unable to ReadLine: %v", err)
return
}
}
}

So esentially anytime you have a line which is longer than 4096 char (which will make isFragment true) and curFileLinesCount >= maxLines you will hit this error

Its going to read the first part of the line that starts with +, hit that if statement in side the +case, continue back to the top of the loop, continue reading from the middle of the previous line where it left off by calling input.ReadLine() again and then fail here:

default:
// This is unexpected
err = fmt.Errorf("Unexpected line in hunk: %s", string(lineBytes))
return

Because lineBytes[0] is just going to be whatever value was next in the large line. I'm not sure why it is like this, @zeripath should know hopefully

@zeripath
Copy link
Contributor

It's simply a bug - I've failed to munch the end of the fragment when there is a maxLinesCount because I've just forgotten to do it.

zeripath added a commit to zeripath/gitea that referenced this issue Nov 21, 2020
The code for parsing diff hunks has a bug whereby a very long line
in a very long diff would not be completely read leading to an unexpected
character.

This PR ensures that the line is completely cleared

Fix go-gitea#13602

Signed-off-by: Andrew Thornton <[email protected]>
@zeripath zeripath changed the title Error 500 when creating pull request with files containing // Error creating pull requests or diffs with very long diffs containing very long lines Nov 21, 2020
6543 pushed a commit to 6543-forks/gitea that referenced this issue Nov 21, 2020
  The code for parsing diff hunks has a bug whereby a very long line in a very long diff would not be completely read leading to an unexpected character.

  This PR ensures that the line is completely cleared

* Also allow git max line length <4096

* Add test case

Fix go-gitea#13602

Signed-off-by: Andrew Thornton <[email protected]>
6543 pushed a commit to 6543-forks/gitea that referenced this issue Nov 21, 2020
  The code for parsing diff hunks has a bug whereby a very long line in a very long diff would not be completely read leading to an unexpected character.

  This PR ensures that the line is completely cleared

* Also allow git max line length <4096

* Add test case

Fix go-gitea#13602

Signed-off-by: Andrew Thornton <[email protected]>
techknowlogick added a commit that referenced this issue Nov 21, 2020
* Handle incomplete diff files properly

The code for parsing diff hunks has a bug whereby a very long line
in a very long diff would not be completely read leading to an unexpected
character.

This PR ensures that the line is completely cleared

Fix #13602

Signed-off-by: Andrew Thornton <[email protected]>

* Also allow git max line length <4096

Signed-off-by: Andrew Thornton <[email protected]>

* Add test case

Signed-off-by: Andrew Thornton <[email protected]>

Co-authored-by: techknowlogick <[email protected]>
lafriks pushed a commit that referenced this issue Nov 22, 2020
The code for parsing diff hunks has a bug whereby a very long line in a very long diff would not be completely read leading to an unexpected character.

  This PR ensures that the line is completely cleared

* Also allow git max line length <4096

* Add test case

Fix #13602

Signed-off-by: Andrew Thornton <[email protected]>

Co-authored-by: Andrew Thornton <[email protected]>
zeripath added a commit that referenced this issue Nov 22, 2020
The code for parsing diff hunks has a bug whereby a very long line in a very long diff would not be completely read leading to an unexpected character.

  This PR ensures that the line is completely cleared

* Also allow git max line length <4096

* Add test case

Fix #13602

Signed-off-by: Andrew Thornton <[email protected]>

Co-authored-by: Andrew Thornton <[email protected]>
@go-gitea go-gitea locked and limited conversation to collaborators Jan 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
issue/regression Issue needs no code to be fixed, only a description on how to fix it yourself type/bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants