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

patch_parse.c: Handle CRLF in parse_header_start #5027

Merged
merged 1 commit into from
Apr 16, 2019
Merged

patch_parse.c: Handle CRLF in parse_header_start #5027

merged 1 commit into from
Apr 16, 2019

Conversation

ddevault
Copy link
Contributor

@ddevault ddevault commented Mar 23, 2019

This becomes a segfault later (in check_prefix) when parsing patches where a file was added. CRLF seems to be handled fine otherwise. Added test which demonstrates the segfault.

Supporting CRLF is important because patches are often emailed - and emails use CRLF.

@ethomson
Copy link
Member

Why does this segfault later? It's not obvious to me how this would prevent that, so I'm concerned that there's two issues here (the CRLF issue something causing else causing a segfault). Can you provide reproduction steps?

@ddevault
Copy link
Contributor Author

ddevault commented Mar 23, 2019

I added a test which reproduces the segfault - roll back the change to patch_parse.c and run the test suite to reproduce. The issue is that if this branch is taken, it sets the header_old_path to NULL, assuming that it just parsed some bunk data and hoping the --- line will clear things up (per the comments nearby) - in this case, it doesn't, because the --- is /dev/null - no prefix. The resulting segfault happens here:

libgit2/src/patch_parse.c

Lines 931 to 940 in 5e88f13

prefixed_old = (!added && patch->old_path) ? patch->old_path :
patch->header_old_path;
prefixed_new = (!deleted && patch->new_path) ? patch->new_path :
patch->header_new_path;
if (check_prefix(
&patch->old_prefix, &old_prefixlen, patch, prefixed_old) < 0 ||
check_prefix(
&patch->new_prefix, &new_prefixlen, patch, prefixed_new) < 0)
return -1;

Because the file is added (and the left side would be /dev/null and not have a prefix), this chooses to use header_old_path - which was erroneously set to NULL earlier.

@eaigner
Copy link
Contributor

eaigner commented Mar 24, 2019

@ethomson @ddevault it seems the underlying issue is something else.

git__isspace should consume any of SPACE, \t, \n, \f, \r or \v which makes me skeptical that the issue is related to \r at all.

I fixed a SPACE issue here #5030

@ddevault
Copy link
Contributor Author

Hm, that doesn't seem related. There are no spaces in the paths in the test case I added, and when I add my test case to your patch it still segfaults.

@eaigner
Copy link
Contributor

eaigner commented Mar 24, 2019

Yeah, I added the \r at the end to test and it segfaults

@pks-t
Copy link
Member

pks-t commented Mar 29, 2019

Yeah, I added the \r at the end to test and it segfaults

You mean when testing against your #5030? For what it's worth, it does work just fine with #5035 (at least there's no segfault there, the test still fails due to mis-parsing).

@eaigner
Copy link
Contributor

eaigner commented Mar 30, 2019

It would segfault before my patch. Works with my patch. But I'm fine with either solution. I don't care about it being properly parsed, just that it doesn't crash my app.

Copy link
Member

@pks-t pks-t left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ddevault, could you please rebase the PR to fix the conflict? There's two minor stylistic issues -- I don't care too much about the stylistic issue in the tests, but the other one should probably be fixed before merging this.

src/patch_parse.c Outdated Show resolved Hide resolved
tests/diff/parse.c Outdated Show resolved Hide resolved
@pks-t
Copy link
Member

pks-t commented Apr 5, 2019 via email

@ddevault
Copy link
Contributor Author

ddevault commented Apr 6, 2019

Conflicts & style issues addressed.

@pks-t pks-t merged commit ed959ca into libgit2:master Apr 16, 2019
@pks-t
Copy link
Member

pks-t commented Apr 16, 2019

Thanks a lot!

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

Successfully merging this pull request may close these issues.

None yet

4 participants