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

Closed
opened 2025-11-02 06:52:45 -06:00 by GiteaMirror · 7 comments
Owner

Originally created by @davidsvantesson on GitHub (Nov 17, 2020).

  • 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.

Originally created by @davidsvantesson on GitHub (Nov 17, 2020). - Gitea version (or commit ref): 1.12.6 - Git version: - Operating system: Ubuntu - Database (use `[x]`): - [ ] PostgreSQL - [x] 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.
GiteaMirror added the issue/regressiontype/bug labels 2025-11-02 06:52:45 -06:00
Author
Owner

@lunny commented on GitHub (Nov 17, 2020):

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

@lunny commented on GitHub (Nov 17, 2020): So we should add some new unit tests for git diff.
Author
Owner

@johanvdw commented on GitHub (Nov 18, 2020):

Also affects 1.13.rc2

@johanvdw commented on GitHub (Nov 18, 2020): Also affects 1.13.rc2
Author
Owner

@mrsdizzie commented on GitHub (Nov 19, 2020):

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

@mrsdizzie commented on GitHub (Nov 19, 2020): 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
Author
Owner

@johanvdw commented on GitHub (Nov 19, 2020):

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 commented on GitHub (Nov 19, 2020): 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
Author
Owner

@johanvdw commented on GitHub (Nov 19, 2020):

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

@johanvdw commented on GitHub (Nov 19, 2020): The file contains random chars, so feel free to reuse for tests if needed.
Author
Owner

@mrsdizzie commented on GitHub (Nov 20, 2020):

Thanks for example that helps:

This happens in this case because:

24b3b2140a/services/gitdiff/gitdiff.go (L743-L749)

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:

24b3b2140a/services/gitdiff/gitdiff.go (L782-L792)

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:

24b3b2140a/services/gitdiff/gitdiff.go (L775-L778)

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

@mrsdizzie commented on GitHub (Nov 20, 2020): Thanks for example that helps: This happens in this case because: https://github.com/go-gitea/gitea/blob/24b3b2140a094a108707c2994946b5a99eda016f/services/gitdiff/gitdiff.go#L743-L749 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: https://github.com/go-gitea/gitea/blob/24b3b2140a094a108707c2994946b5a99eda016f/services/gitdiff/gitdiff.go#L782-L792 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: https://github.com/go-gitea/gitea/blob/24b3b2140a094a108707c2994946b5a99eda016f/services/gitdiff/gitdiff.go#L775-L778 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
Author
Owner

@zeripath commented on GitHub (Nov 21, 2020):

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 commented on GitHub (Nov 21, 2020): 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.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/gitea#6330