git_patch_from_diff may return a NULL patch without an error#1412
git_patch_from_diff may return a NULL patch without an error#1412jdavid merged 2 commits intolibgit2:masterfrom
Conversation
|
Ah, the new mypy checks failed (they weren't set up to run automatically in my fork before I submitted the PR) because Some thoughts:
|
The second one is better in my opinion, the way it's already in this PR. |
|
Hi @jdavid! My latest changes are ready for review. I think pygit2 would benefit from eliminating this undefined behavior (until then, the test scenario can cause a segfault in my git client ;)) PS: I'm not sure why the "Wheels for linux-arm" check failed to start... |
|
Thanks @jorio and sorry for the delay. I have a question, there has been a string of mypy related changes, what do you think about it? For the record, in this PR mypy complained because of the union-attr rule, and to make mypy happy the test code is now more complicated that it would have been otherwise. |
|
Thank you @jdavid for the merge! Sorry it took me a while to get back to you about mypy. Here's my opinion: In this case, I think the warnings about If we have a unit test that should handle the failure case but doesn't, I'd prefer to just let the test fail, so that we're forced to review the failure case. In an ideal world, code coverage should give us the peace of mind that the test suite covers the failure cases - for example in this case, where |
|
For example I would write (in the tests): As: To me the fact that now patch may be |
Diff.__getitem__used to assume (incorrectly) that whengit_patch_from_diffreturns 0 (success), it also creates a validgit_patch.However, per the docs for git_patch_from_diff, "For an unchanged file (...) no git_patch will be created, the output will be set to NULL".
In practice,
git_patch_from_diffdoesn't always return a NULL patch if the file is unchanged. However, I found a scenario involving CRLF filters that consistently triggers this edge case (success code with NULL patch).This PR makes
Diff.__getitem__returnNonein this case. This prevents passing NULL towrap_patch, which would cause undefined behavior in release builds (because this assertion would be bypassed).